ab8500-gpadc.c 19.7 KB
Newer Older
A
Arun Murthy 已提交
1 2 3 4 5
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * License Terms: GNU General Public License v2
 * Author: Arun R Murthy <arun.murthy@stericsson.com>
6
 * Author: Daniel Willerud <daniel.willerud@stericsson.com>
7
 * Author: Johan Palsson <johan.palsson@stericsson.com>
A
Arun Murthy 已提交
8 9 10 11 12 13 14
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
15
#include <linux/pm_runtime.h>
A
Arun Murthy 已提交
16 17 18 19 20
#include <linux/platform_device.h>
#include <linux/completion.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/slab.h>
21
#include <linux/list.h>
A
Arun Murthy 已提交
22
#include <linux/mfd/abx500.h>
23 24
#include <linux/mfd/abx500/ab8500.h>
#include <linux/mfd/abx500/ab8500-gpadc.h>
A
Arun Murthy 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

/*
 * GPADC register offsets
 * Bank : 0x0A
 */
#define AB8500_GPADC_CTRL1_REG		0x00
#define AB8500_GPADC_CTRL2_REG		0x01
#define AB8500_GPADC_CTRL3_REG		0x02
#define AB8500_GPADC_AUTO_TIMER_REG	0x03
#define AB8500_GPADC_STAT_REG		0x04
#define AB8500_GPADC_MANDATAL_REG	0x05
#define AB8500_GPADC_MANDATAH_REG	0x06
#define AB8500_GPADC_AUTODATAL_REG	0x07
#define AB8500_GPADC_AUTODATAH_REG	0x08
#define AB8500_GPADC_MUX_CTRL_REG	0x09

41 42 43 44 45 46 47 48 49 50 51 52
/*
 * OTP register offsets
 * Bank : 0x15
 */
#define AB8500_GPADC_CAL_1		0x0F
#define AB8500_GPADC_CAL_2		0x10
#define AB8500_GPADC_CAL_3		0x11
#define AB8500_GPADC_CAL_4		0x12
#define AB8500_GPADC_CAL_5		0x13
#define AB8500_GPADC_CAL_6		0x14
#define AB8500_GPADC_CAL_7		0x15

A
Arun Murthy 已提交
53 54 55 56 57 58 59
/* gpadc constants */
#define EN_VINTCORE12			0x04
#define EN_VTVOUT			0x02
#define EN_GPADC			0x01
#define DIS_GPADC			0x00
#define SW_AVG_16			0x60
#define ADC_SW_CONV			0x04
60
#define EN_ICHAR			0x80
61
#define BTEMP_PULL_UP			0x08
A
Arun Murthy 已提交
62 63 64 65
#define EN_BUF				0x40
#define DIS_ZERO			0x00
#define GPADC_BUSY			0x01

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/* GPADC constants from AB8500 spec, UM0836 */
#define ADC_RESOLUTION			1024
#define ADC_CH_BTEMP_MIN		0
#define ADC_CH_BTEMP_MAX		1350
#define ADC_CH_DIETEMP_MIN		0
#define ADC_CH_DIETEMP_MAX		1350
#define ADC_CH_CHG_V_MIN		0
#define ADC_CH_CHG_V_MAX		20030
#define ADC_CH_ACCDET2_MIN		0
#define ADC_CH_ACCDET2_MAX		2500
#define ADC_CH_VBAT_MIN			2300
#define ADC_CH_VBAT_MAX			4800
#define ADC_CH_CHG_I_MIN		0
#define ADC_CH_CHG_I_MAX		1500
#define ADC_CH_BKBAT_MIN		0
#define ADC_CH_BKBAT_MAX		3200

/* This is used to not lose precision when dividing to get gain and offset */
#define CALIB_SCALE			1000

86 87 88
/* Time in ms before disabling regulator */
#define GPADC_AUDOSUSPEND_DELAY		1

89 90
#define CONVERSION_TIME			500 /* ms */

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
enum cal_channels {
	ADC_INPUT_VMAIN = 0,
	ADC_INPUT_BTEMP,
	ADC_INPUT_VBAT,
	NBR_CAL_INPUTS,
};

/**
 * struct adc_cal_data - Table for storing gain and offset for the calibrated
 * ADC channels
 * @gain:		Gain of the ADC channel
 * @offset:		Offset of the ADC channel
 */
struct adc_cal_data {
	u64 gain;
	u64 offset;
};

A
Arun Murthy 已提交
109
/**
110
 * struct ab8500_gpadc - AB8500 GPADC device information
A
Arun Murthy 已提交
111
 * @dev:			pointer to the struct device
112 113
 * @node:			a list of AB8500 GPADCs, hence prepared for
				reentrance
114
 * @parent:			pointer to the struct ab8500
A
Arun Murthy 已提交
115 116 117 118 119
 * @ab8500_gpadc_complete:	pointer to the struct completion, to indicate
 *				the completion of gpadc conversion
 * @ab8500_gpadc_lock:		structure of type mutex
 * @regu:			pointer to the struct regulator
 * @irq:			interrupt number that is used by gpadc
120
 * @cal_data			array of ADC calibration data structs
A
Arun Murthy 已提交
121
 */
122
struct ab8500_gpadc {
A
Arun Murthy 已提交
123
	struct device *dev;
124
	struct list_head node;
125
	struct ab8500 *parent;
A
Arun Murthy 已提交
126 127 128 129
	struct completion ab8500_gpadc_complete;
	struct mutex ab8500_gpadc_lock;
	struct regulator *regu;
	int irq;
130
	struct adc_cal_data cal_data[NBR_CAL_INPUTS];
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
};

static LIST_HEAD(ab8500_gpadc_list);

/**
 * ab8500_gpadc_get() - returns a reference to the primary AB8500 GPADC
 * (i.e. the first GPADC in the instance list)
 */
struct ab8500_gpadc *ab8500_gpadc_get(char *name)
{
	struct ab8500_gpadc *gpadc;

	list_for_each_entry(gpadc, &ab8500_gpadc_list, node) {
		if (!strcmp(name, dev_name(gpadc->dev)))
		    return gpadc;
	}

	return ERR_PTR(-ENOENT);
}
EXPORT_SYMBOL(ab8500_gpadc_get);
A
Arun Murthy 已提交
151

152 153 154 155
/**
 * ab8500_gpadc_ad_to_voltage() - Convert a raw ADC value to a voltage
 */
int ab8500_gpadc_ad_to_voltage(struct ab8500_gpadc *gpadc, u8 channel,
156 157 158 159
	int ad_value)
{
	int res;

160
	switch (channel) {
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	case MAIN_CHARGER_V:
		/* For some reason we don't have calibrated data */
		if (!gpadc->cal_data[ADC_INPUT_VMAIN].gain) {
			res = ADC_CH_CHG_V_MIN + (ADC_CH_CHG_V_MAX -
				ADC_CH_CHG_V_MIN) * ad_value /
				ADC_RESOLUTION;
			break;
		}
		/* Here we can use the calibrated data */
		res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VMAIN].gain +
			gpadc->cal_data[ADC_INPUT_VMAIN].offset) / CALIB_SCALE;
		break;

	case BAT_CTRL:
	case BTEMP_BALL:
	case ACC_DETECT1:
	case ADC_AUX1:
	case ADC_AUX2:
		/* For some reason we don't have calibrated data */
		if (!gpadc->cal_data[ADC_INPUT_BTEMP].gain) {
			res = ADC_CH_BTEMP_MIN + (ADC_CH_BTEMP_MAX -
				ADC_CH_BTEMP_MIN) * ad_value /
				ADC_RESOLUTION;
			break;
		}
		/* Here we can use the calibrated data */
		res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_BTEMP].gain +
			gpadc->cal_data[ADC_INPUT_BTEMP].offset) / CALIB_SCALE;
		break;

	case MAIN_BAT_V:
		/* For some reason we don't have calibrated data */
		if (!gpadc->cal_data[ADC_INPUT_VBAT].gain) {
			res = ADC_CH_VBAT_MIN + (ADC_CH_VBAT_MAX -
				ADC_CH_VBAT_MIN) * ad_value /
				ADC_RESOLUTION;
			break;
		}
		/* Here we can use the calibrated data */
		res = (int) (ad_value * gpadc->cal_data[ADC_INPUT_VBAT].gain +
			gpadc->cal_data[ADC_INPUT_VBAT].offset) / CALIB_SCALE;
		break;

	case DIE_TEMP:
		res = ADC_CH_DIETEMP_MIN +
			(ADC_CH_DIETEMP_MAX - ADC_CH_DIETEMP_MIN) * ad_value /
			ADC_RESOLUTION;
		break;

	case ACC_DETECT2:
		res = ADC_CH_ACCDET2_MIN +
			(ADC_CH_ACCDET2_MAX - ADC_CH_ACCDET2_MIN) * ad_value /
			ADC_RESOLUTION;
		break;

	case VBUS_V:
		res = ADC_CH_CHG_V_MIN +
			(ADC_CH_CHG_V_MAX - ADC_CH_CHG_V_MIN) * ad_value /
			ADC_RESOLUTION;
		break;

	case MAIN_CHARGER_C:
	case USB_CHARGER_C:
		res = ADC_CH_CHG_I_MIN +
			(ADC_CH_CHG_I_MAX - ADC_CH_CHG_I_MIN) * ad_value /
			ADC_RESOLUTION;
		break;

	case BK_BAT_V:
		res = ADC_CH_BKBAT_MIN +
			(ADC_CH_BKBAT_MAX - ADC_CH_BKBAT_MIN) * ad_value /
			ADC_RESOLUTION;
		break;

	default:
		dev_err(gpadc->dev,
			"unknown channel, not possible to convert\n");
		res = -EINVAL;
		break;

	}
	return res;
}
244
EXPORT_SYMBOL(ab8500_gpadc_ad_to_voltage);
245

A
Arun Murthy 已提交
246 247
/**
 * ab8500_gpadc_convert() - gpadc conversion
248
 * @channel:	analog channel to be converted to digital data
A
Arun Murthy 已提交
249 250
 *
 * This function converts the selected analog i/p to digital
251
 * data.
A
Arun Murthy 已提交
252
 */
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
int ab8500_gpadc_convert(struct ab8500_gpadc *gpadc, u8 channel)
{
	int ad_value;
	int voltage;

	ad_value = ab8500_gpadc_read_raw(gpadc, channel);
	if (ad_value < 0) {
		dev_err(gpadc->dev, "GPADC raw value failed ch: %d\n", channel);
		return ad_value;
	}

	voltage = ab8500_gpadc_ad_to_voltage(gpadc, channel, ad_value);

	if (voltage < 0)
		dev_err(gpadc->dev, "GPADC to voltage conversion failed ch:"
			" %d AD: 0x%x\n", channel, ad_value);

	return voltage;
}
EXPORT_SYMBOL(ab8500_gpadc_convert);

/**
 * ab8500_gpadc_read_raw() - gpadc read
 * @channel:	analog channel to be read
 *
 * This function obtains the raw ADC value, this then needs
 * to be converted by calling ab8500_gpadc_ad_to_voltage()
 */
int ab8500_gpadc_read_raw(struct ab8500_gpadc *gpadc, u8 channel)
A
Arun Murthy 已提交
282 283 284 285 286
{
	int ret;
	int looplimit = 0;
	u8 val, low_data, high_data;

287
	if (!gpadc)
A
Arun Murthy 已提交
288 289
		return -ENODEV;

290
	mutex_lock(&gpadc->ab8500_gpadc_lock);
291

A
Arun Murthy 已提交
292
	/* Enable VTVout LDO this is required for GPADC */
293
	pm_runtime_get_sync(gpadc->dev);
A
Arun Murthy 已提交
294 295 296

	/* Check if ADC is not busy, lock and proceed */
	do {
297 298
		ret = abx500_get_register_interruptible(gpadc->dev,
			AB8500_GPADC, AB8500_GPADC_STAT_REG, &val);
A
Arun Murthy 已提交
299 300 301 302 303 304 305
		if (ret < 0)
			goto out;
		if (!(val & GPADC_BUSY))
			break;
		msleep(10);
	} while (++looplimit < 10);
	if (looplimit >= 10 && (val & GPADC_BUSY)) {
306
		dev_err(gpadc->dev, "gpadc_conversion: GPADC busy");
A
Arun Murthy 已提交
307 308 309 310 311
		ret = -EINVAL;
		goto out;
	}

	/* Enable GPADC */
312 313
	ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
		AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_GPADC, EN_GPADC);
A
Arun Murthy 已提交
314
	if (ret < 0) {
315
		dev_err(gpadc->dev, "gpadc_conversion: enable gpadc failed\n");
A
Arun Murthy 已提交
316 317
		goto out;
	}
318

319
	/* Select the channel source and set average samples to 16 */
320
	ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
321
		AB8500_GPADC_CTRL2_REG, (channel | SW_AVG_16));
A
Arun Murthy 已提交
322
	if (ret < 0) {
323
		dev_err(gpadc->dev,
A
Arun Murthy 已提交
324 325 326
			"gpadc_conversion: set avg samples failed\n");
		goto out;
	}
327

328 329
	/*
	 * Enable ADC, buffering, select rising edge and enable ADC path
330 331
	 * charging current sense if it needed, ABB 3.0 needs some special
	 * treatment too.
332
	 */
333
	switch (channel) {
334 335 336 337 338 339 340
	case MAIN_CHARGER_C:
	case USB_CHARGER_C:
		ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
			AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
			EN_BUF | EN_ICHAR,
			EN_BUF | EN_ICHAR);
		break;
341
	case BTEMP_BALL:
342
		if (!is_ab8500_2p0_or_earlier(gpadc->parent)) {
343 344 345 346
			/* Turn on btemp pull-up on ABB 3.0 */
			ret = abx500_mask_and_set_register_interruptible(
				gpadc->dev,
				AB8500_GPADC, AB8500_GPADC_CTRL1_REG,
347 348
				EN_BUF | BTEMP_PULL_UP,
				EN_BUF | BTEMP_PULL_UP);
349 350 351

		 /*
		  * Delay might be needed for ABB8500 cut 3.0, if not, remove
M
Masanari Iida 已提交
352
		  * when hardware will be available
353
		  */
354
			usleep_range(1000, 1000);
355 356 357
			break;
		}
		/* Intentional fallthrough */
358 359 360 361 362
	default:
		ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
			AB8500_GPADC, AB8500_GPADC_CTRL1_REG, EN_BUF, EN_BUF);
		break;
	}
A
Arun Murthy 已提交
363
	if (ret < 0) {
364
		dev_err(gpadc->dev,
A
Arun Murthy 已提交
365 366 367
			"gpadc_conversion: select falling edge failed\n");
		goto out;
	}
368

369 370
	ret = abx500_mask_and_set_register_interruptible(gpadc->dev,
		AB8500_GPADC, AB8500_GPADC_CTRL1_REG, ADC_SW_CONV, ADC_SW_CONV);
A
Arun Murthy 已提交
371
	if (ret < 0) {
372
		dev_err(gpadc->dev,
A
Arun Murthy 已提交
373 374 375 376
			"gpadc_conversion: start s/w conversion failed\n");
		goto out;
	}
	/* wait for completion of conversion */
377 378
	if (!wait_for_completion_timeout(&gpadc->ab8500_gpadc_complete,
					 msecs_to_jiffies(CONVERSION_TIME))) {
379
		dev_err(gpadc->dev,
L
Lucas De Marchi 已提交
380
			"timeout: didn't receive GPADC conversion interrupt\n");
A
Arun Murthy 已提交
381 382 383 384 385
		ret = -EINVAL;
		goto out;
	}

	/* Read the converted RAW data */
386
	ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
A
Arun Murthy 已提交
387 388
		AB8500_GPADC_MANDATAL_REG, &low_data);
	if (ret < 0) {
389
		dev_err(gpadc->dev, "gpadc_conversion: read low data failed\n");
A
Arun Murthy 已提交
390 391 392
		goto out;
	}

393
	ret = abx500_get_register_interruptible(gpadc->dev, AB8500_GPADC,
A
Arun Murthy 已提交
394 395
		AB8500_GPADC_MANDATAH_REG, &high_data);
	if (ret < 0) {
396 397
		dev_err(gpadc->dev,
			"gpadc_conversion: read high data failed\n");
A
Arun Murthy 已提交
398 399 400 401
		goto out;
	}

	/* Disable GPADC */
402
	ret = abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
A
Arun Murthy 已提交
403 404
		AB8500_GPADC_CTRL1_REG, DIS_GPADC);
	if (ret < 0) {
405
		dev_err(gpadc->dev, "gpadc_conversion: disable gpadc failed\n");
A
Arun Murthy 已提交
406 407
		goto out;
	}
408 409 410 411

	pm_runtime_mark_last_busy(gpadc->dev);
	pm_runtime_put_autosuspend(gpadc->dev);

412
	mutex_unlock(&gpadc->ab8500_gpadc_lock);
413 414

	return (high_data << 8) | low_data;
A
Arun Murthy 已提交
415 416 417 418 419 420 421 422

out:
	/*
	 * It has shown to be needed to turn off the GPADC if an error occurs,
	 * otherwise we might have problem when waiting for the busy bit in the
	 * GPADC status register to go low. In V1.1 there wait_for_completion
	 * seems to timeout when waiting for an interrupt.. Not seen in V2.0
	 */
423
	(void) abx500_set_register_interruptible(gpadc->dev, AB8500_GPADC,
A
Arun Murthy 已提交
424
		AB8500_GPADC_CTRL1_REG, DIS_GPADC);
425 426 427

	pm_runtime_put(gpadc->dev);

428 429
	mutex_unlock(&gpadc->ab8500_gpadc_lock);
	dev_err(gpadc->dev,
430
		"gpadc_conversion: Failed to AD convert channel %d\n", channel);
A
Arun Murthy 已提交
431 432
	return ret;
}
433
EXPORT_SYMBOL(ab8500_gpadc_read_raw);
A
Arun Murthy 已提交
434 435 436 437 438 439 440 441 442 443 444

/**
 * ab8500_bm_gpswadcconvend_handler() - isr for s/w gpadc conversion completion
 * @irq:	irq number
 * @data:	pointer to the data passed during request irq
 *
 * This is a interrupt service routine for s/w gpadc conversion completion.
 * Notifies the gpadc completion is completed and the converted raw value
 * can be read from the registers.
 * Returns IRQ status(IRQ_HANDLED)
 */
445
static irqreturn_t ab8500_bm_gpswadcconvend_handler(int irq, void *_gpadc)
A
Arun Murthy 已提交
446
{
447
	struct ab8500_gpadc *gpadc = _gpadc;
A
Arun Murthy 已提交
448 449 450 451 452 453

	complete(&gpadc->ab8500_gpadc_complete);

	return IRQ_HANDLED;
}

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
static int otp_cal_regs[] = {
	AB8500_GPADC_CAL_1,
	AB8500_GPADC_CAL_2,
	AB8500_GPADC_CAL_3,
	AB8500_GPADC_CAL_4,
	AB8500_GPADC_CAL_5,
	AB8500_GPADC_CAL_6,
	AB8500_GPADC_CAL_7,
};

static void ab8500_gpadc_read_calibration_data(struct ab8500_gpadc *gpadc)
{
	int i;
	int ret[ARRAY_SIZE(otp_cal_regs)];
	u8 gpadc_cal[ARRAY_SIZE(otp_cal_regs)];

	int vmain_high, vmain_low;
	int btemp_high, btemp_low;
	int vbat_high, vbat_low;

	/* First we read all OTP registers and store the error code */
	for (i = 0; i < ARRAY_SIZE(otp_cal_regs); i++) {
		ret[i] = abx500_get_register_interruptible(gpadc->dev,
			AB8500_OTP_EMUL, otp_cal_regs[i],  &gpadc_cal[i]);
		if (ret[i] < 0)
			dev_err(gpadc->dev, "%s: read otp reg 0x%02x failed\n",
				__func__, otp_cal_regs[i]);
	}

	/*
	 * The ADC calibration data is stored in OTP registers.
	 * The layout of the calibration data is outlined below and a more
	 * detailed description can be found in UM0836
	 *
	 * vm_h/l = vmain_high/low
	 * bt_h/l = btemp_high/low
	 * vb_h/l = vbat_high/low
	 *
	 * Data bits:
	 * | 7	   | 6	   | 5	   | 4	   | 3	   | 2	   | 1	   | 0
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * |						   | vm_h9 | vm_h8
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * |		   | vm_h7 | vm_h6 | vm_h5 | vm_h4 | vm_h3 | vm_h2
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * | vm_h1 | vm_h0 | vm_l4 | vm_l3 | vm_l2 | vm_l1 | vm_l0 | bt_h9
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * | bt_h8 | bt_h7 | bt_h6 | bt_h5 | bt_h4 | bt_h3 | bt_h2 | bt_h1
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * | bt_h0 | bt_l4 | bt_l3 | bt_l2 | bt_l1 | bt_l0 | vb_h9 | vb_h8
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * | vb_h7 | vb_h6 | vb_h5 | vb_h4 | vb_h3 | vb_h2 | vb_h1 | vb_h0
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 * | vb_l5 | vb_l4 | vb_l3 | vb_l2 | vb_l1 | vb_l0 |
	 * |.......|.......|.......|.......|.......|.......|.......|.......
	 *
	 *
	 * Ideal output ADC codes corresponding to injected input voltages
	 * during manufacturing is:
	 *
	 * vmain_high: Vin = 19500mV / ADC ideal code = 997
	 * vmain_low:  Vin = 315mV   / ADC ideal code = 16
	 * btemp_high: Vin = 1300mV  / ADC ideal code = 985
	 * btemp_low:  Vin = 21mV    / ADC ideal code = 16
	 * vbat_high:  Vin = 4700mV  / ADC ideal code = 982
	 * vbat_low:   Vin = 2380mV  / ADC ideal code = 33
	 */

	/* Calculate gain and offset for VMAIN if all reads succeeded */
	if (!(ret[0] < 0 || ret[1] < 0 || ret[2] < 0)) {
		vmain_high = (((gpadc_cal[0] & 0x03) << 8) |
			((gpadc_cal[1] & 0x3F) << 2) |
			((gpadc_cal[2] & 0xC0) >> 6));

		vmain_low = ((gpadc_cal[2] & 0x3E) >> 1);

		gpadc->cal_data[ADC_INPUT_VMAIN].gain = CALIB_SCALE *
			(19500 - 315) /	(vmain_high - vmain_low);

		gpadc->cal_data[ADC_INPUT_VMAIN].offset = CALIB_SCALE * 19500 -
			(CALIB_SCALE * (19500 - 315) /
			 (vmain_high - vmain_low)) * vmain_high;
	} else {
		gpadc->cal_data[ADC_INPUT_VMAIN].gain = 0;
	}

	/* Calculate gain and offset for BTEMP if all reads succeeded */
	if (!(ret[2] < 0 || ret[3] < 0 || ret[4] < 0)) {
		btemp_high = (((gpadc_cal[2] & 0x01) << 9) |
			(gpadc_cal[3] << 1) |
			((gpadc_cal[4] & 0x80) >> 7));

		btemp_low = ((gpadc_cal[4] & 0x7C) >> 2);

		gpadc->cal_data[ADC_INPUT_BTEMP].gain =
			CALIB_SCALE * (1300 - 21) / (btemp_high - btemp_low);

		gpadc->cal_data[ADC_INPUT_BTEMP].offset = CALIB_SCALE * 1300 -
			(CALIB_SCALE * (1300 - 21) /
			(btemp_high - btemp_low)) * btemp_high;
	} else {
		gpadc->cal_data[ADC_INPUT_BTEMP].gain = 0;
	}

	/* Calculate gain and offset for VBAT if all reads succeeded */
	if (!(ret[4] < 0 || ret[5] < 0 || ret[6] < 0)) {
		vbat_high = (((gpadc_cal[4] & 0x03) << 8) | gpadc_cal[5]);
		vbat_low = ((gpadc_cal[6] & 0xFC) >> 2);

		gpadc->cal_data[ADC_INPUT_VBAT].gain = CALIB_SCALE *
			(4700 - 2380) /	(vbat_high - vbat_low);

		gpadc->cal_data[ADC_INPUT_VBAT].offset = CALIB_SCALE * 4700 -
			(CALIB_SCALE * (4700 - 2380) /
			(vbat_high - vbat_low)) * vbat_high;
	} else {
		gpadc->cal_data[ADC_INPUT_VBAT].gain = 0;
	}

	dev_dbg(gpadc->dev, "VMAIN gain %llu offset %llu\n",
		gpadc->cal_data[ADC_INPUT_VMAIN].gain,
		gpadc->cal_data[ADC_INPUT_VMAIN].offset);

	dev_dbg(gpadc->dev, "BTEMP gain %llu offset %llu\n",
		gpadc->cal_data[ADC_INPUT_BTEMP].gain,
		gpadc->cal_data[ADC_INPUT_BTEMP].offset);

	dev_dbg(gpadc->dev, "VBAT gain %llu offset %llu\n",
		gpadc->cal_data[ADC_INPUT_VBAT].gain,
		gpadc->cal_data[ADC_INPUT_VBAT].offset);
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
static int ab8500_gpadc_runtime_suspend(struct device *dev)
{
	struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);

	regulator_disable(gpadc->regu);
	return 0;
}

static int ab8500_gpadc_runtime_resume(struct device *dev)
{
	struct ab8500_gpadc *gpadc = dev_get_drvdata(dev);

	regulator_enable(gpadc->regu);
	return 0;
}

static int ab8500_gpadc_runtime_idle(struct device *dev)
{
	pm_runtime_suspend(dev);
	return 0;
}

B
Bill Pemberton 已提交
608
static int ab8500_gpadc_probe(struct platform_device *pdev)
A
Arun Murthy 已提交
609 610 611 612 613 614 615 616 617 618 619 620
{
	int ret = 0;
	struct ab8500_gpadc *gpadc;

	gpadc = kzalloc(sizeof(struct ab8500_gpadc), GFP_KERNEL);
	if (!gpadc) {
		dev_err(&pdev->dev, "Error: No memory\n");
		return -ENOMEM;
	}

	gpadc->irq = platform_get_irq_byname(pdev, "SW_CONV_END");
	if (gpadc->irq < 0) {
621
		dev_err(&pdev->dev, "failed to get platform irq-%d\n",
622
			gpadc->irq);
A
Arun Murthy 已提交
623 624 625 626 627
		ret = gpadc->irq;
		goto fail;
	}

	gpadc->dev = &pdev->dev;
628
	gpadc->parent = dev_get_drvdata(pdev->dev.parent);
629
	mutex_init(&gpadc->ab8500_gpadc_lock);
A
Arun Murthy 已提交
630 631 632 633 634 635 636

	/* Initialize completion used to notify completion of conversion */
	init_completion(&gpadc->ab8500_gpadc_complete);

	/* Register interrupt  - SwAdcComplete */
	ret = request_threaded_irq(gpadc->irq, NULL,
		ab8500_bm_gpswadcconvend_handler,
637 638
		IRQF_ONESHOT | IRQF_NO_SUSPEND | IRQF_SHARED,
				"ab8500-gpadc", gpadc);
A
Arun Murthy 已提交
639 640 641 642 643 644 645 646 647 648 649
	if (ret < 0) {
		dev_err(gpadc->dev, "Failed to register interrupt, irq: %d\n",
			gpadc->irq);
		goto fail;
	}

	/* VTVout LDO used to power up ab8500-GPADC */
	gpadc->regu = regulator_get(&pdev->dev, "vddadc");
	if (IS_ERR(gpadc->regu)) {
		ret = PTR_ERR(gpadc->regu);
		dev_err(gpadc->dev, "failed to get vtvout LDO\n");
650
		goto fail_irq;
A
Arun Murthy 已提交
651
	}
652 653 654 655 656 657 658 659 660 661

	platform_set_drvdata(pdev, gpadc);

	regulator_enable(gpadc->regu);

	pm_runtime_set_autosuspend_delay(gpadc->dev, GPADC_AUDOSUSPEND_DELAY);
	pm_runtime_use_autosuspend(gpadc->dev);
	pm_runtime_set_active(gpadc->dev);
	pm_runtime_enable(gpadc->dev);

662
	ab8500_gpadc_read_calibration_data(gpadc);
663
	list_add_tail(&gpadc->node, &ab8500_gpadc_list);
A
Arun Murthy 已提交
664 665
	dev_dbg(gpadc->dev, "probe success\n");
	return 0;
666 667
fail_irq:
	free_irq(gpadc->irq, gpadc);
A
Arun Murthy 已提交
668 669 670 671 672 673
fail:
	kfree(gpadc);
	gpadc = NULL;
	return ret;
}

B
Bill Pemberton 已提交
674
static int ab8500_gpadc_remove(struct platform_device *pdev)
A
Arun Murthy 已提交
675 676 677
{
	struct ab8500_gpadc *gpadc = platform_get_drvdata(pdev);

678 679
	/* remove this gpadc entry from the list */
	list_del(&gpadc->node);
A
Arun Murthy 已提交
680
	/* remove interrupt  - completion of Sw ADC conversion */
681
	free_irq(gpadc->irq, gpadc);
682 683 684 685 686 687 688 689 690 691

	pm_runtime_get_sync(gpadc->dev);
	pm_runtime_disable(gpadc->dev);

	regulator_disable(gpadc->regu);

	pm_runtime_set_suspended(gpadc->dev);

	pm_runtime_put_noidle(gpadc->dev);

A
Arun Murthy 已提交
692 693 694 695 696
	kfree(gpadc);
	gpadc = NULL;
	return 0;
}

697 698 699 700 701 702
static const struct dev_pm_ops ab8500_gpadc_pm_ops = {
	SET_RUNTIME_PM_OPS(ab8500_gpadc_runtime_suspend,
			   ab8500_gpadc_runtime_resume,
			   ab8500_gpadc_runtime_idle)
};

A
Arun Murthy 已提交
703 704
static struct platform_driver ab8500_gpadc_driver = {
	.probe = ab8500_gpadc_probe,
B
Bill Pemberton 已提交
705
	.remove = ab8500_gpadc_remove,
A
Arun Murthy 已提交
706 707 708
	.driver = {
		.name = "ab8500-gpadc",
		.owner = THIS_MODULE,
709
		.pm = &ab8500_gpadc_pm_ops,
A
Arun Murthy 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
	},
};

static int __init ab8500_gpadc_init(void)
{
	return platform_driver_register(&ab8500_gpadc_driver);
}

static void __exit ab8500_gpadc_exit(void)
{
	platform_driver_unregister(&ab8500_gpadc_driver);
}

subsys_initcall_sync(ab8500_gpadc_init);
module_exit(ab8500_gpadc_exit);

MODULE_LICENSE("GPL v2");
727
MODULE_AUTHOR("Arun R Murthy, Daniel Willerud, Johan Palsson");
A
Arun Murthy 已提交
728 729
MODULE_ALIAS("platform:ab8500_gpadc");
MODULE_DESCRIPTION("AB8500 GPADC driver");