twl4030-power.c 25.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *
 * Handle TWL4030 Power initialization
 *
 * Copyright (C) 2008 Nokia Corporation
 * Copyright (C) 2006 Texas Instruments, Inc
 *
 * Written by 	Kalle Jokiniemi
 *		Peter De Schrijver <peter.de-schrijver@nokia.com>
 * Several fixes by Amit Kucheria <amit.kucheria@verdurent.com>
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License. See the file "COPYING" in the main directory of this
 * archive for more details.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/pm.h>
28
#include <linux/i2c/twl.h>
29
#include <linux/platform_device.h>
30
#include <linux/of.h>
31
#include <linux/of_device.h>
32 33 34 35 36

#include <asm/mach-types.h>

static u8 twl4030_start_script_address = 0x2b;

37 38 39 40 41 42 43 44 45
/* Register bits for P1, P2 and P3_SW_EVENTS */
#define PWR_STOPON_PRWON	BIT(6)
#define PWR_STOPON_SYSEN	BIT(5)
#define PWR_ENABLE_WARMRESET	BIT(4)
#define PWR_LVL_WAKEUP		BIT(3)
#define PWR_DEVACT		BIT(2)
#define PWR_DEVSLP		BIT(1)
#define PWR_DEVOFF		BIT(0)

46 47 48 49 50 51 52 53 54
/* Register bits for CFG_P1_TRANSITION (also for P2 and P3) */
#define STARTON_SWBUG		BIT(7)	/* Start on watchdog */
#define STARTON_VBUS		BIT(5)	/* Start on VBUS */
#define STARTON_VBAT		BIT(4)	/* Start on battery insert */
#define STARTON_RTC		BIT(3)	/* Start on RTC */
#define STARTON_USB		BIT(2)	/* Start on USB host */
#define STARTON_CHG		BIT(1)	/* Start on charger */
#define STARTON_PWON		BIT(0)	/* Start on PWRON button */

55
#define SEQ_OFFSYNC		(1 << 0)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#define PHY_TO_OFF_PM_MASTER(p)		(p - 0x36)
#define PHY_TO_OFF_PM_RECEIVER(p)	(p - 0x5b)

/* resource - hfclk */
#define R_HFCLKOUT_DEV_GRP 	PHY_TO_OFF_PM_RECEIVER(0xe6)

/* PM events */
#define R_P1_SW_EVENTS		PHY_TO_OFF_PM_MASTER(0x46)
#define R_P2_SW_EVENTS		PHY_TO_OFF_PM_MASTER(0x47)
#define R_P3_SW_EVENTS		PHY_TO_OFF_PM_MASTER(0x48)
#define R_CFG_P1_TRANSITION	PHY_TO_OFF_PM_MASTER(0x36)
#define R_CFG_P2_TRANSITION	PHY_TO_OFF_PM_MASTER(0x37)
#define R_CFG_P3_TRANSITION	PHY_TO_OFF_PM_MASTER(0x38)

#define END_OF_SCRIPT		0x3f

#define R_SEQ_ADD_A2S		PHY_TO_OFF_PM_MASTER(0x55)
#define R_SEQ_ADD_S2A12		PHY_TO_OFF_PM_MASTER(0x56)
#define	R_SEQ_ADD_S2A3		PHY_TO_OFF_PM_MASTER(0x57)
#define	R_SEQ_ADD_WARM		PHY_TO_OFF_PM_MASTER(0x58)
#define R_MEMORY_ADDRESS	PHY_TO_OFF_PM_MASTER(0x59)
#define R_MEMORY_DATA		PHY_TO_OFF_PM_MASTER(0x5a)

80 81 82 83 84 85
/* resource configuration registers
   <RESOURCE>_DEV_GRP   at address 'n+0'
   <RESOURCE>_TYPE      at address 'n+1'
   <RESOURCE>_REMAP     at address 'n+2'
   <RESOURCE>_DEDICATED at address 'n+3'
*/
86
#define DEV_GRP_OFFSET		0
87
#define TYPE_OFFSET		1
88 89
#define REMAP_OFFSET		2
#define DEDICATED_OFFSET	3
90

91
/* Bit positions in the registers */
92 93

/* <RESOURCE>_DEV_GRP */
94 95
#define DEV_GRP_SHIFT		5
#define DEV_GRP_MASK		(7 << DEV_GRP_SHIFT)
96 97

/* <RESOURCE>_TYPE */
98 99 100 101 102
#define TYPE_SHIFT		0
#define TYPE_MASK		(7 << TYPE_SHIFT)
#define TYPE2_SHIFT		3
#define TYPE2_MASK		(3 << TYPE2_SHIFT)

103 104 105 106 107 108
/* <RESOURCE>_REMAP */
#define SLEEP_STATE_SHIFT	0
#define SLEEP_STATE_MASK	(0xf << SLEEP_STATE_SHIFT)
#define OFF_STATE_SHIFT		4
#define OFF_STATE_MASK		(0xf << OFF_STATE_SHIFT)

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
static u8 res_config_addrs[] = {
	[RES_VAUX1]	= 0x17,
	[RES_VAUX2]	= 0x1b,
	[RES_VAUX3]	= 0x1f,
	[RES_VAUX4]	= 0x23,
	[RES_VMMC1]	= 0x27,
	[RES_VMMC2]	= 0x2b,
	[RES_VPLL1]	= 0x2f,
	[RES_VPLL2]	= 0x33,
	[RES_VSIM]	= 0x37,
	[RES_VDAC]	= 0x3b,
	[RES_VINTANA1]	= 0x3f,
	[RES_VINTANA2]	= 0x43,
	[RES_VINTDIG]	= 0x47,
	[RES_VIO]	= 0x4b,
	[RES_VDD1]	= 0x55,
	[RES_VDD2]	= 0x63,
	[RES_VUSB_1V5]	= 0x71,
	[RES_VUSB_1V8]	= 0x74,
	[RES_VUSB_3V1]	= 0x77,
	[RES_VUSBCP]	= 0x7a,
	[RES_REGEN]	= 0x7f,
	[RES_NRES_PWRON] = 0x82,
	[RES_CLKEN]	= 0x85,
	[RES_SYSEN]	= 0x88,
	[RES_HFCLKOUT]	= 0x8b,
	[RES_32KCLKOUT]	= 0x8e,
	[RES_RESET]	= 0x91,
137
	[RES_MAIN_REF]	= 0x94,
138 139
};

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
/*
 * Usable values for .remap_sleep and .remap_off
 * Based on table "5.3.3 Resource Operating modes"
 */
enum {
	TWL_REMAP_OFF = 0,
	TWL_REMAP_SLEEP = 8,
	TWL_REMAP_ACTIVE = 9,
};

/*
 * Macros to configure the PM register states for various resources.
 * Note that we can make MSG_SINGULAR etc private to this driver once
 * omap3 has been made DT only.
 */
#define TWL_DFLT_DELAY		2	/* typically 2 32 KiHz cycles */
156
#define TWL_DEV_GRP_P123	(DEV_GRP_P1 | DEV_GRP_P2 | DEV_GRP_P3)
157 158 159 160 161 162 163 164 165
#define TWL_RESOURCE_SET(res, state)					\
	{ MSG_SINGULAR(DEV_GRP_NULL, (res), (state)), TWL_DFLT_DELAY }
#define TWL_RESOURCE_ON(res)	TWL_RESOURCE_SET(res, RES_STATE_ACTIVE)
#define TWL_RESOURCE_OFF(res)	TWL_RESOURCE_SET(res, RES_STATE_OFF)
#define TWL_RESOURCE_RESET(res)	TWL_RESOURCE_SET(res, RES_STATE_WRST)
/*
 * It seems that type1 and type2 is just the resource init order
 * number for the type1 and type2 group.
 */
166 167
#define TWL_RESOURCE_SET_ACTIVE(res, state)			       	\
	{ MSG_SINGULAR(DEV_GRP_NULL, (res), RES_STATE_ACTIVE), (state) }
168 169 170
#define TWL_RESOURCE_GROUP_RESET(group, type1, type2)			\
	{ MSG_BROADCAST(DEV_GRP_NULL, (group), (type1), (type2),	\
		RES_STATE_WRST), TWL_DFLT_DELAY }
171 172 173 174 175 176
#define TWL_RESOURCE_GROUP_SLEEP(group, type, type2)			\
	{ MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2),		\
		RES_STATE_SLEEP), TWL_DFLT_DELAY }
#define TWL_RESOURCE_GROUP_ACTIVE(group, type, type2)			\
	{ MSG_BROADCAST(DEV_GRP_NULL, (group), (type), (type2),		\
		RES_STATE_ACTIVE), TWL_DFLT_DELAY }
177 178 179 180 181
#define TWL_REMAP_SLEEP(res, devgrp, typ, typ2)				\
	{ .resource = (res), .devgroup = (devgrp),			\
	  .type = (typ), .type2 = (typ2),				\
	  .remap_off = TWL_REMAP_OFF,					\
	  .remap_sleep = TWL_REMAP_SLEEP, }
182 183 184 185
#define TWL_REMAP_OFF(res, devgrp, typ, typ2)				\
	{ .resource = (res), .devgroup = (devgrp),			\
	  .type = (typ), .type2 = (typ2),				\
	  .remap_off = TWL_REMAP_OFF, .remap_sleep = TWL_REMAP_OFF, }
186

B
Bill Pemberton 已提交
187
static int twl4030_write_script_byte(u8 address, u8 byte)
188 189 190
{
	int err;

191
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_MEMORY_ADDRESS);
192 193
	if (err)
		goto out;
194
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, byte, R_MEMORY_DATA);
195 196 197 198
out:
	return err;
}

B
Bill Pemberton 已提交
199
static int twl4030_write_script_ins(u8 address, u16 pmb_message,
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
					   u8 delay, u8 next)
{
	int err;

	address *= 4;
	err = twl4030_write_script_byte(address++, pmb_message >> 8);
	if (err)
		goto out;
	err = twl4030_write_script_byte(address++, pmb_message & 0xff);
	if (err)
		goto out;
	err = twl4030_write_script_byte(address++, delay);
	if (err)
		goto out;
	err = twl4030_write_script_byte(address++, next);
out:
	return err;
}

B
Bill Pemberton 已提交
219
static int twl4030_write_script(u8 address, struct twl4030_ins *script,
220 221
				       int len)
{
222
	int err = -EINVAL;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

	for (; len; len--, address++, script++) {
		if (len == 1) {
			err = twl4030_write_script_ins(address,
						script->pmb_message,
						script->delay,
						END_OF_SCRIPT);
			if (err)
				break;
		} else {
			err = twl4030_write_script_ins(address,
						script->pmb_message,
						script->delay,
						address + 1);
			if (err)
				break;
		}
	}
	return err;
}

B
Bill Pemberton 已提交
244
static int twl4030_config_wakeup3_sequence(u8 address)
245 246 247 248 249
{
	int err;
	u8 data;

	/* Set SLEEP to ACTIVE SEQ address for P3 */
250
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A3);
251 252 253 254
	if (err)
		goto out;

	/* P3 LVL_WAKEUP should be on LEVEL */
255
	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P3_SW_EVENTS);
256 257
	if (err)
		goto out;
258
	data |= PWR_LVL_WAKEUP;
259
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P3_SW_EVENTS);
260 261 262 263 264 265
out:
	if (err)
		pr_err("TWL4030 wakeup sequence for P3 config error\n");
	return err;
}

266 267 268
static int
twl4030_config_wakeup12_sequence(const struct twl4030_power_data *pdata,
				 u8 address)
269 270 271 272 273
{
	int err = 0;
	u8 data;

	/* Set SLEEP to ACTIVE SEQ address for P1 and P2 */
274
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_S2A12);
275 276 277 278
	if (err)
		goto out;

	/* P1/P2 LVL_WAKEUP should be on LEVEL */
279
	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P1_SW_EVENTS);
280 281 282
	if (err)
		goto out;

283
	data |= PWR_LVL_WAKEUP;
284
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P1_SW_EVENTS);
285 286 287
	if (err)
		goto out;

288
	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data, R_P2_SW_EVENTS);
289 290 291
	if (err)
		goto out;

292
	data |= PWR_LVL_WAKEUP;
293
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data, R_P2_SW_EVENTS);
294 295 296
	if (err)
		goto out;

297 298
	if (pdata->ac_charger_quirk || machine_is_omap_3430sdp() ||
	    machine_is_omap_ldp()) {
299
		/* Disabling AC charger effect on sleep-active transitions */
300 301
		err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &data,
				      R_CFG_P1_TRANSITION);
302 303
		if (err)
			goto out;
304
		data &= ~STARTON_CHG;
305 306
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, data,
				       R_CFG_P1_TRANSITION);
307 308 309 310 311 312 313 314 315 316 317
		if (err)
			goto out;
	}

out:
	if (err)
		pr_err("TWL4030 wakeup sequence for P1 and P2" \
			"config error\n");
	return err;
}

B
Bill Pemberton 已提交
318
static int twl4030_config_sleep_sequence(u8 address)
319 320 321 322
{
	int err;

	/* Set ACTIVE to SLEEP SEQ address in T2 memory*/
323
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_A2S);
324 325 326 327 328 329 330

	if (err)
		pr_err("TWL4030 sleep sequence config error\n");

	return err;
}

B
Bill Pemberton 已提交
331
static int twl4030_config_warmreset_sequence(u8 address)
332 333 334 335 336
{
	int err;
	u8 rd_data;

	/* Set WARM RESET SEQ address for P1 */
337
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, address, R_SEQ_ADD_WARM);
338 339 340 341
	if (err)
		goto out;

	/* P1/P2/P3 enable WARMRESET */
342
	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P1_SW_EVENTS);
343 344 345
	if (err)
		goto out;

346
	rd_data |= PWR_ENABLE_WARMRESET;
347
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P1_SW_EVENTS);
348 349 350
	if (err)
		goto out;

351
	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P2_SW_EVENTS);
352 353 354
	if (err)
		goto out;

355
	rd_data |= PWR_ENABLE_WARMRESET;
356
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P2_SW_EVENTS);
357 358 359
	if (err)
		goto out;

360
	err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &rd_data, R_P3_SW_EVENTS);
361 362 363
	if (err)
		goto out;

364
	rd_data |= PWR_ENABLE_WARMRESET;
365
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, rd_data, R_P3_SW_EVENTS);
366 367 368 369 370 371
out:
	if (err)
		pr_err("TWL4030 warmreset seq config error\n");
	return err;
}

B
Bill Pemberton 已提交
372
static int twl4030_configure_resource(struct twl4030_resconfig *rconfig)
373 374 375 376 377
{
	int rconfig_addr;
	int err;
	u8 type;
	u8 grp;
378
	u8 remap;
379 380 381 382 383 384 385 386 387 388

	if (rconfig->resource > TOTAL_RESOURCES) {
		pr_err("TWL4030 Resource %d does not exist\n",
			rconfig->resource);
		return -EINVAL;
	}

	rconfig_addr = res_config_addrs[rconfig->resource];

	/* Set resource group */
389
	err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &grp,
B
Balaji T K 已提交
390
			      rconfig_addr + DEV_GRP_OFFSET);
391 392 393 394 395 396
	if (err) {
		pr_err("TWL4030 Resource %d group could not be read\n",
			rconfig->resource);
		return err;
	}

397
	if (rconfig->devgroup != TWL4030_RESCONFIG_UNDEF) {
398 399
		grp &= ~DEV_GRP_MASK;
		grp |= rconfig->devgroup << DEV_GRP_SHIFT;
400
		err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
B
Balaji T K 已提交
401
				       grp, rconfig_addr + DEV_GRP_OFFSET);
402 403 404 405 406 407 408
		if (err < 0) {
			pr_err("TWL4030 failed to program devgroup\n");
			return err;
		}
	}

	/* Set resource types */
409
	err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &type,
410 411 412 413 414 415 416
				rconfig_addr + TYPE_OFFSET);
	if (err < 0) {
		pr_err("TWL4030 Resource %d type could not be read\n",
			rconfig->resource);
		return err;
	}

417
	if (rconfig->type != TWL4030_RESCONFIG_UNDEF) {
418 419 420 421
		type &= ~TYPE_MASK;
		type |= rconfig->type << TYPE_SHIFT;
	}

422
	if (rconfig->type2 != TWL4030_RESCONFIG_UNDEF) {
423 424 425 426
		type &= ~TYPE2_MASK;
		type |= rconfig->type2 << TYPE2_SHIFT;
	}

427
	err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
428 429 430 431 432 433
				type, rconfig_addr + TYPE_OFFSET);
	if (err < 0) {
		pr_err("TWL4030 failed to program resource type\n");
		return err;
	}

434
	/* Set remap states */
435
	err = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &remap,
B
Balaji T K 已提交
436
			      rconfig_addr + REMAP_OFFSET);
437 438 439 440 441 442
	if (err < 0) {
		pr_err("TWL4030 Resource %d remap could not be read\n",
			rconfig->resource);
		return err;
	}

443
	if (rconfig->remap_off != TWL4030_RESCONFIG_UNDEF) {
444 445 446 447
		remap &= ~OFF_STATE_MASK;
		remap |= rconfig->remap_off << OFF_STATE_SHIFT;
	}

448
	if (rconfig->remap_sleep != TWL4030_RESCONFIG_UNDEF) {
449
		remap &= ~SLEEP_STATE_MASK;
450
		remap |= rconfig->remap_sleep << SLEEP_STATE_SHIFT;
451 452
	}

453
	err = twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
B
Balaji T K 已提交
454 455
			       remap,
			       rconfig_addr + REMAP_OFFSET);
456 457 458 459 460
	if (err < 0) {
		pr_err("TWL4030 failed to program remap\n");
		return err;
	}

461 462 463
	return 0;
}

464 465 466
static int load_twl4030_script(const struct twl4030_power_data *pdata,
			       struct twl4030_script *tscript,
			       u8 address)
467 468
{
	int err;
469
	static int order;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

	/* Make sure the script isn't going beyond last valid address (0x3f) */
	if ((address + tscript->size) > END_OF_SCRIPT) {
		pr_err("TWL4030 scripts too big error\n");
		return -EINVAL;
	}

	err = twl4030_write_script(address, tscript->script, tscript->size);
	if (err)
		goto out;

	if (tscript->flags & TWL4030_WRST_SCRIPT) {
		err = twl4030_config_warmreset_sequence(address);
		if (err)
			goto out;
	}
	if (tscript->flags & TWL4030_WAKEUP12_SCRIPT) {
487 488 489 490 491 492
		/* Reset any existing sleep script to avoid hangs on reboot */
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
				       R_SEQ_ADD_A2S);
		if (err)
			goto out;

493
		err = twl4030_config_wakeup12_sequence(pdata, address);
494 495
		if (err)
			goto out;
496
		order = 1;
497 498 499 500 501 502
	}
	if (tscript->flags & TWL4030_WAKEUP3_SCRIPT) {
		err = twl4030_config_wakeup3_sequence(address);
		if (err)
			goto out;
	}
503
	if (tscript->flags & TWL4030_SLEEP_SCRIPT) {
504
		if (!order)
505 506 507
			pr_warning("TWL4030: Bad order of scripts (sleep "\
					"script before wakeup) Leads to boot"\
					"failure on some boards\n");
508
		err = twl4030_config_sleep_sequence(address);
509
	}
510 511 512 513
out:
	return err;
}

514 515 516 517
int twl4030_remove_script(u8 flags)
{
	int err = 0;

518 519
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
			       TWL4030_PM_MASTER_PROTECT_KEY);
520 521 522 523 524
	if (err) {
		pr_err("twl4030: unable to unlock PROTECT_KEY\n");
		return err;
	}

525 526
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
			       TWL4030_PM_MASTER_PROTECT_KEY);
527 528 529 530 531 532
	if (err) {
		pr_err("twl4030: unable to unlock PROTECT_KEY\n");
		return err;
	}

	if (flags & TWL4030_WRST_SCRIPT) {
533 534
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
				       R_SEQ_ADD_WARM);
535 536 537 538
		if (err)
			return err;
	}
	if (flags & TWL4030_WAKEUP12_SCRIPT) {
539 540
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
				       R_SEQ_ADD_S2A12);
541
		if (err)
542 543 544
			return err;
	}
	if (flags & TWL4030_WAKEUP3_SCRIPT) {
545 546
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
				       R_SEQ_ADD_S2A3);
547 548 549 550
		if (err)
			return err;
	}
	if (flags & TWL4030_SLEEP_SCRIPT) {
551 552
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, END_OF_SCRIPT,
				       R_SEQ_ADD_A2S);
553 554 555 556
		if (err)
			return err;
	}

557 558
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
			       TWL4030_PM_MASTER_PROTECT_KEY);
559 560 561 562 563 564
	if (err)
		pr_err("TWL4030 Unable to relock registers\n");

	return err;
}

565 566
static int
twl4030_power_configure_scripts(const struct twl4030_power_data *pdata)
567 568 569 570 571 572
{
	int err;
	int i;
	u8 address = twl4030_start_script_address;

	for (i = 0; i < pdata->num; i++) {
573
		err = load_twl4030_script(pdata, pdata->scripts[i], address);
574 575 576 577 578 579 580 581
		if (err)
			return err;
		address += pdata->scripts[i]->size;
	}

	return 0;
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
static void twl4030_patch_rconfig(struct twl4030_resconfig *common,
				  struct twl4030_resconfig *board)
{
	while (common->resource) {
		struct twl4030_resconfig *b = board;

		while (b->resource) {
			if (b->resource == common->resource) {
				*common = *b;
				break;
			}
			b++;
		}
		common++;
	}
}

599 600
static int
twl4030_power_configure_resources(const struct twl4030_power_data *pdata)
601 602
{
	struct twl4030_resconfig *resconfig = pdata->resource_config;
603
	struct twl4030_resconfig *boardconf = pdata->board_config;
604 605 606
	int err;

	if (resconfig) {
607 608 609
		if (boardconf)
			twl4030_patch_rconfig(resconfig, boardconf);

610 611 612 613 614 615 616 617 618 619 620
		while (resconfig->resource) {
			err = twl4030_configure_resource(resconfig);
			if (err)
				return err;
			resconfig++;
		}
	}

	return 0;
}

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
static int twl4030_starton_mask_and_set(u8 bitmask, u8 bitvalues)
{
	u8 regs[3] = { TWL4030_PM_MASTER_CFG_P1_TRANSITION,
		       TWL4030_PM_MASTER_CFG_P2_TRANSITION,
		       TWL4030_PM_MASTER_CFG_P3_TRANSITION, };
	u8 val;
	int i, err;

	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
			       TWL4030_PM_MASTER_PROTECT_KEY);
	if (err)
		goto relock;
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
			       TWL4030_PM_MASTER_KEY_CFG2,
			       TWL4030_PM_MASTER_PROTECT_KEY);
	if (err)
		goto relock;

	for (i = 0; i < sizeof(regs); i++) {
		err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER,
				      &val, regs[i]);
		if (err)
			break;
		val = (~bitmask & val) | (bitmask & bitvalues);
		err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
				       val, regs[i]);
		if (err)
			break;
	}

	if (err)
		pr_err("TWL4030 Register access failed: %i\n", err);

relock:
	return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
				TWL4030_PM_MASTER_PROTECT_KEY);
}

659 660 661 662 663 664 665 666 667
/*
 * In master mode, start the power off sequence.
 * After a successful execution, TWL shuts down the power to the SoC
 * and all peripherals connected to it.
 */
void twl4030_power_off(void)
{
	int err;

668 669 670 671 672
	/* Disable start on charger or VBUS as it can break poweroff */
	err = twl4030_starton_mask_and_set(STARTON_VBUS | STARTON_CHG, 0);
	if (err)
		pr_err("TWL4030 Unable to configure start-up\n");

673
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, PWR_DEVOFF,
674 675 676 677 678
			       TWL4030_PM_MASTER_P1_SW_EVENTS);
	if (err)
		pr_err("TWL4030 Unable to power off\n");
}

679
static bool twl4030_power_use_poweroff(const struct twl4030_power_data *pdata,
680 681 682 683 684
					struct device_node *node)
{
	if (pdata && pdata->use_poweroff)
		return true;

685 686 687
	if (of_property_read_bool(node, "ti,system-power-controller"))
		return true;

688 689 690 691 692 693
	if (of_property_read_bool(node, "ti,use_poweroff"))
		return true;

	return false;
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
#ifdef CONFIG_OF

/* Generic warm reset configuration for omap3 */

static struct twl4030_ins omap3_wrst_seq[] = {
	TWL_RESOURCE_OFF(RES_NRES_PWRON),
	TWL_RESOURCE_OFF(RES_RESET),
	TWL_RESOURCE_RESET(RES_MAIN_REF),
	TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R2),
	TWL_RESOURCE_RESET(RES_VUSB_3V1),
	TWL_RESOURCE_GROUP_RESET(RES_GRP_ALL, RES_TYPE_R0, RES_TYPE2_R1),
	TWL_RESOURCE_GROUP_RESET(RES_GRP_RC, RES_TYPE_ALL, RES_TYPE2_R0),
	TWL_RESOURCE_ON(RES_RESET),
	TWL_RESOURCE_ON(RES_NRES_PWRON),
};

static struct twl4030_script omap3_wrst_script = {
	.script	= omap3_wrst_seq,
	.size	= ARRAY_SIZE(omap3_wrst_seq),
	.flags	= TWL4030_WRST_SCRIPT,
};

static struct twl4030_script *omap3_reset_scripts[] = {
	&omap3_wrst_script,
};

static struct twl4030_resconfig omap3_rconfig[] = {
	TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, -1, -1),
	TWL_REMAP_SLEEP(RES_VDD1, DEV_GRP_P1, -1, -1),
	TWL_REMAP_SLEEP(RES_VDD2, DEV_GRP_P1, -1, -1),
	{ 0, 0 },
};

static struct twl4030_power_data omap3_reset = {
	.scripts		= omap3_reset_scripts,
	.num			= ARRAY_SIZE(omap3_reset_scripts),
	.resource_config	= omap3_rconfig,
};

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
/* Recommended generic default idle configuration for off-idle */

/* Broadcast message to put res to sleep */
static struct twl4030_ins omap3_idle_sleep_on_seq[] = {
	TWL_RESOURCE_GROUP_SLEEP(RES_GRP_ALL, RES_TYPE_ALL, 0),
};

static struct twl4030_script omap3_idle_sleep_on_script = {
	.script	= omap3_idle_sleep_on_seq,
	.size	= ARRAY_SIZE(omap3_idle_sleep_on_seq),
	.flags	= TWL4030_SLEEP_SCRIPT,
};

/* Broadcast message to put res to active */
static struct twl4030_ins omap3_idle_wakeup_p12_seq[] = {
	TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
};

static struct twl4030_script omap3_idle_wakeup_p12_script = {
	.script	= omap3_idle_wakeup_p12_seq,
	.size	= ARRAY_SIZE(omap3_idle_wakeup_p12_seq),
	.flags	= TWL4030_WAKEUP12_SCRIPT,
};

/* Broadcast message to put res to active */
static struct twl4030_ins omap3_idle_wakeup_p3_seq[] = {
	TWL_RESOURCE_SET_ACTIVE(RES_CLKEN, 0x37),
	TWL_RESOURCE_GROUP_ACTIVE(RES_GRP_ALL, RES_TYPE_ALL, 0),
};

static struct twl4030_script omap3_idle_wakeup_p3_script = {
	.script	= omap3_idle_wakeup_p3_seq,
	.size	= ARRAY_SIZE(omap3_idle_wakeup_p3_seq),
	.flags	= TWL4030_WAKEUP3_SCRIPT,
};

static struct twl4030_script *omap3_idle_scripts[] = {
	&omap3_idle_wakeup_p12_script,
	&omap3_idle_wakeup_p3_script,
	&omap3_wrst_script,
	&omap3_idle_sleep_on_script,
};

/*
 * Recommended configuration based on "Recommended Sleep
 * Sequences for the Zoom Platform":
 * http://omappedia.com/wiki/File:Recommended_Sleep_Sequences_Zoom.pdf
 * Note that the type1 and type2 seem to be just the init order number
 * for type1 and type2 groups as specified in the document mentioned
 * above.
 */
static struct twl4030_resconfig omap3_idle_rconfig[] = {
785 786 787 788 789 790
	TWL_REMAP_SLEEP(RES_VAUX1, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VAUX2, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VAUX3, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VAUX4, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VMMC1, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VMMC2, TWL4030_RESCONFIG_UNDEF, 0, 0),
791 792
	TWL_REMAP_OFF(RES_VPLL1, DEV_GRP_P1, 3, 1),
	TWL_REMAP_SLEEP(RES_VPLL2, DEV_GRP_P1, 0, 0),
793 794
	TWL_REMAP_SLEEP(RES_VSIM, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VDAC, TWL4030_RESCONFIG_UNDEF, 0, 0),
795 796 797 798 799 800
	TWL_REMAP_SLEEP(RES_VINTANA1, TWL_DEV_GRP_P123, 1, 2),
	TWL_REMAP_SLEEP(RES_VINTANA2, TWL_DEV_GRP_P123, 0, 2),
	TWL_REMAP_SLEEP(RES_VINTDIG, TWL_DEV_GRP_P123, 1, 2),
	TWL_REMAP_SLEEP(RES_VIO, TWL_DEV_GRP_P123, 2, 2),
	TWL_REMAP_OFF(RES_VDD1, DEV_GRP_P1, 4, 1),
	TWL_REMAP_OFF(RES_VDD2, DEV_GRP_P1, 3, 1),
801 802
	TWL_REMAP_SLEEP(RES_VUSB_1V5, TWL4030_RESCONFIG_UNDEF, 0, 0),
	TWL_REMAP_SLEEP(RES_VUSB_1V8, TWL4030_RESCONFIG_UNDEF, 0, 0),
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	TWL_REMAP_SLEEP(RES_VUSB_3V1, TWL_DEV_GRP_P123, 0, 0),
	/* Resource #20 USB charge pump skipped */
	TWL_REMAP_SLEEP(RES_REGEN, TWL_DEV_GRP_P123, 2, 1),
	TWL_REMAP_SLEEP(RES_NRES_PWRON, TWL_DEV_GRP_P123, 0, 1),
	TWL_REMAP_SLEEP(RES_CLKEN, TWL_DEV_GRP_P123, 3, 2),
	TWL_REMAP_SLEEP(RES_SYSEN, TWL_DEV_GRP_P123, 6, 1),
	TWL_REMAP_SLEEP(RES_HFCLKOUT, DEV_GRP_P3, 0, 2),
	TWL_REMAP_SLEEP(RES_32KCLKOUT, TWL_DEV_GRP_P123, 0, 0),
	TWL_REMAP_SLEEP(RES_RESET, TWL_DEV_GRP_P123, 6, 0),
	TWL_REMAP_SLEEP(RES_MAIN_REF, TWL_DEV_GRP_P123, 0, 0),
	{ /* Terminator */ },
};

static struct twl4030_power_data omap3_idle = {
	.scripts		= omap3_idle_scripts,
	.num			= ARRAY_SIZE(omap3_idle_scripts),
	.resource_config	= omap3_idle_rconfig,
};

822 823 824 825 826 827 828 829 830 831 832 833 834
/* Disable 32 KiHz oscillator during idle */
static struct twl4030_resconfig osc_off_rconfig[] = {
	TWL_REMAP_OFF(RES_CLKEN, DEV_GRP_P1 | DEV_GRP_P3, 3, 2),
	{ /* Terminator */ },
};

static struct twl4030_power_data osc_off_idle = {
	.scripts		= omap3_idle_scripts,
	.num			= ARRAY_SIZE(omap3_idle_scripts),
	.resource_config	= omap3_idle_rconfig,
	.board_config		= osc_off_rconfig,
};

835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
static struct twl4030_power_data omap3_idle_ac_quirk = {
	.scripts		= omap3_idle_scripts,
	.num			= ARRAY_SIZE(omap3_idle_scripts),
	.resource_config	= omap3_idle_rconfig,
	.ac_charger_quirk	= true,
};

static struct twl4030_power_data omap3_idle_ac_quirk_osc_off = {
	.scripts		= omap3_idle_scripts,
	.num			= ARRAY_SIZE(omap3_idle_scripts),
	.resource_config	= omap3_idle_rconfig,
	.board_config		= osc_off_rconfig,
	.ac_charger_quirk	= true,
};

850
static const struct of_device_id twl4030_power_of_match[] = {
851 852 853
	{
		.compatible = "ti,twl4030-power",
	},
854 855 856 857
	{
		.compatible = "ti,twl4030-power-reset",
		.data = &omap3_reset,
	},
858 859 860 861
	{
		.compatible = "ti,twl4030-power-idle",
		.data = &omap3_idle,
	},
862 863 864 865
	{
		.compatible = "ti,twl4030-power-idle-osc-off",
		.data = &osc_off_idle,
	},
866 867 868 869 870 871 872 873 874 875 876 877
	{
		.compatible = "ti,twl4030-power-omap3-sdp",
		.data = &omap3_idle_ac_quirk,
	},
	{
		.compatible = "ti,twl4030-power-omap3-ldp",
		.data = &omap3_idle_ac_quirk_osc_off,
	},
	{
		.compatible = "ti,twl4030-power-omap3-evm",
		.data = &omap3_idle_ac_quirk,
	},
878 879 880 881 882
	{ },
};
MODULE_DEVICE_TABLE(of, twl4030_power_of_match);
#endif	/* CONFIG_OF */

883
static int twl4030_power_probe(struct platform_device *pdev)
884
{
885
	const struct twl4030_power_data *pdata = dev_get_platdata(&pdev->dev);
886
	struct device_node *node = pdev->dev.of_node;
887
	const struct of_device_id *match;
888
	int err = 0;
889
	int err2 = 0;
890
	u8 val;
891

892 893 894 895 896
	if (!pdata && !node) {
		dev_err(&pdev->dev, "Platform data is missing\n");
		return -EINVAL;
	}

897 898
	err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
			       TWL4030_PM_MASTER_PROTECT_KEY);
899 900
	err |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
			       TWL4030_PM_MASTER_KEY_CFG2,
901
			       TWL4030_PM_MASTER_PROTECT_KEY);
902 903 904 905 906

	if (err) {
		pr_err("TWL4030 Unable to unlock registers\n");
		return err;
	}
907

908 909 910 911 912
	match = of_match_device(of_match_ptr(twl4030_power_of_match),
				&pdev->dev);
	if (match && match->data)
		pdata = match->data;

913 914
	if (pdata) {
		err = twl4030_power_configure_scripts(pdata);
915 916
		if (err) {
			pr_err("TWL4030 failed to load scripts\n");
917
			goto relock;
918
		}
919
		err = twl4030_power_configure_resources(pdata);
920 921
		if (err) {
			pr_err("TWL4030 failed to configure resource\n");
922
			goto relock;
923
		}
924
	}
925

926
	/* Board has to be wired properly to use this feature */
927
	if (twl4030_power_use_poweroff(pdata, node) && !pm_power_off) {
928
		/* Default for SEQ_OFFSYNC is set, lets ensure this */
929
		err = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
930 931 932 933 934 935
				      TWL4030_PM_MASTER_CFG_P123_TRANSITION);
		if (err) {
			pr_warning("TWL4030 Unable to read registers\n");

		} else if (!(val & SEQ_OFFSYNC)) {
			val |= SEQ_OFFSYNC;
936
			err = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
937 938 939 940 941 942 943 944 945 946 947
					TWL4030_PM_MASTER_CFG_P123_TRANSITION);
			if (err) {
				pr_err("TWL4030 Unable to setup SEQ_OFFSYNC\n");
				goto relock;
			}
		}

		pm_power_off = twl4030_power_off;
	}

relock:
948
	err2 = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
949
			       TWL4030_PM_MASTER_PROTECT_KEY);
950
	if (err2) {
951
		pr_err("TWL4030 Unable to relock registers\n");
952 953 954
		return err2;
	}

955 956 957 958 959 960
	return err;
}

static int twl4030_power_remove(struct platform_device *pdev)
{
	return 0;
961
}
962 963 964 965

static struct platform_driver twl4030_power_driver = {
	.driver = {
		.name	= "twl4030_power",
966
		.of_match_table = of_match_ptr(twl4030_power_of_match),
967 968 969 970 971 972 973 974 975 976 977 978
	},
	.probe		= twl4030_power_probe,
	.remove		= twl4030_power_remove,
};

module_platform_driver(twl4030_power_driver);

MODULE_AUTHOR("Nokia Corporation");
MODULE_AUTHOR("Texas Instruments, Inc.");
MODULE_DESCRIPTION("Power management for TWL4030");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:twl4030_power");