wm8994-core.c 17.9 KB
Newer Older
M
Mark Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * wm8994-core.c  --  Device access for Wolfson WM8994
 *
 * Copyright 2009 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
 *  This program is free software; you can redistribute  it and/or modify it
 *  under  the terms of  the GNU General  Public License as published by the
 *  Free Software Foundation;  either version 2 of the  License, or (at your
 *  option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
17
#include <linux/slab.h>
M
Mark Brown 已提交
18
#include <linux/i2c.h>
19
#include <linux/err.h>
M
Mark Brown 已提交
20 21
#include <linux/delay.h>
#include <linux/mfd/core.h>
M
Mark Brown 已提交
22 23 24
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
25
#include <linux/pm_runtime.h>
26
#include <linux/regmap.h>
M
Mark Brown 已提交
27 28 29 30 31 32 33
#include <linux/regulator/consumer.h>
#include <linux/regulator/machine.h>

#include <linux/mfd/wm8994/core.h>
#include <linux/mfd/wm8994/pdata.h>
#include <linux/mfd/wm8994/registers.h>

34
#include "wm8994.h"
M
Mark Brown 已提交
35 36 37 38 39 40 41 42 43

/**
 * wm8994_reg_read: Read a single WM8994 register.
 *
 * @wm8994: Device to read from.
 * @reg: Register to read.
 */
int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg)
{
44
	unsigned int val;
M
Mark Brown 已提交
45 46
	int ret;

47
	ret = regmap_read(wm8994->regmap, reg, &val);
M
Mark Brown 已提交
48 49 50 51

	if (ret < 0)
		return ret;
	else
52
		return val;
M
Mark Brown 已提交
53 54 55 56 57 58 59 60 61
}
EXPORT_SYMBOL_GPL(wm8994_reg_read);

/**
 * wm8994_bulk_read: Read multiple WM8994 registers
 *
 * @wm8994: Device to read from
 * @reg: First register
 * @count: Number of registers
62
 * @buf: Buffer to fill.  The data will be returned big endian.
M
Mark Brown 已提交
63 64 65 66
 */
int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg,
		     int count, u16 *buf)
{
67
	return regmap_bulk_read(wm8994->regmap, reg, buf, count);
M
Mark Brown 已提交
68 69 70 71 72 73 74 75 76 77 78 79
}

/**
 * wm8994_reg_write: Write a single WM8994 register.
 *
 * @wm8994: Device to write to.
 * @reg: Register to write to.
 * @val: Value to write.
 */
int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg,
		     unsigned short val)
{
80
	return regmap_write(wm8994->regmap, reg, val);
M
Mark Brown 已提交
81 82 83
}
EXPORT_SYMBOL_GPL(wm8994_reg_write);

84 85 86 87 88 89
/**
 * wm8994_bulk_write: Write multiple WM8994 registers
 *
 * @wm8994: Device to write to
 * @reg: First register
 * @count: Number of registers
90
 * @buf: Buffer to write from.  Data must be big-endian formatted.
91 92
 */
int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg,
M
Mark Brown 已提交
93
		      int count, const u16 *buf)
94
{
95
	return regmap_raw_write(wm8994->regmap, reg, buf, count * sizeof(u16));
96 97 98
}
EXPORT_SYMBOL_GPL(wm8994_bulk_write);

M
Mark Brown 已提交
99 100 101 102 103 104 105 106 107 108 109
/**
 * wm8994_set_bits: Set the value of a bitfield in a WM8994 register
 *
 * @wm8994: Device to write to.
 * @reg: Register to write to.
 * @mask: Mask of bits to set.
 * @val: Value to set (unshifted)
 */
int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg,
		    unsigned short mask, unsigned short val)
{
110
	return regmap_update_bits(wm8994->regmap, reg, mask, val);
M
Mark Brown 已提交
111 112 113 114
}
EXPORT_SYMBOL_GPL(wm8994_set_bits);

static struct mfd_cell wm8994_regulator_devs[] = {
115 116 117 118 119 120 121 122 123 124
	{
		.name = "wm8994-ldo",
		.id = 1,
		.pm_runtime_no_callbacks = true,
	},
	{
		.name = "wm8994-ldo",
		.id = 2,
		.pm_runtime_no_callbacks = true,
	},
M
Mark Brown 已提交
125 126
};

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static struct resource wm8994_codec_resources[] = {
	{
		.start = WM8994_IRQ_TEMP_SHUT,
		.end   = WM8994_IRQ_TEMP_WARN,
		.flags = IORESOURCE_IRQ,
	},
};

static struct resource wm8994_gpio_resources[] = {
	{
		.start = WM8994_IRQ_GPIO(1),
		.end   = WM8994_IRQ_GPIO(11),
		.flags = IORESOURCE_IRQ,
	},
};

M
Mark Brown 已提交
143
static struct mfd_cell wm8994_devs[] = {
144 145 146 147 148 149 150 151 152 153
	{
		.name = "wm8994-codec",
		.num_resources = ARRAY_SIZE(wm8994_codec_resources),
		.resources = wm8994_codec_resources,
	},

	{
		.name = "wm8994-gpio",
		.num_resources = ARRAY_SIZE(wm8994_gpio_resources),
		.resources = wm8994_gpio_resources,
154
		.pm_runtime_no_callbacks = true,
155
	},
M
Mark Brown 已提交
156 157 158 159 160 161 162
};

/*
 * Supplies for the main bulk of CODEC; the LDO supplies are ignored
 * and should be handled via the standard regulator API supply
 * management.
 */
M
Mark Brown 已提交
163 164 165 166 167 168 169 170 171 172 173 174
static const char *wm1811_main_supplies[] = {
	"DBVDD1",
	"DBVDD2",
	"DBVDD3",
	"DCVDD",
	"AVDD1",
	"AVDD2",
	"CPVDD",
	"SPKVDD1",
	"SPKVDD2",
};

M
Mark Brown 已提交
175 176 177 178 179 180 181 182 183 184
static const char *wm8994_main_supplies[] = {
	"DBVDD",
	"DCVDD",
	"AVDD1",
	"AVDD2",
	"CPVDD",
	"SPKVDD1",
	"SPKVDD2",
};

M
Mark Brown 已提交
185 186 187 188 189 190 191 192 193 194 195 196
static const char *wm8958_main_supplies[] = {
	"DBVDD1",
	"DBVDD2",
	"DBVDD3",
	"DCVDD",
	"AVDD1",
	"AVDD2",
	"CPVDD",
	"SPKVDD1",
	"SPKVDD2",
};

197
#ifdef CONFIG_PM_RUNTIME
198
static int wm8994_suspend(struct device *dev)
M
Mark Brown 已提交
199 200 201 202
{
	struct wm8994 *wm8994 = dev_get_drvdata(dev);
	int ret;

203
	/* Don't actually go through with the suspend if the CODEC is
204
	 * still active for accessory detect. */
205 206
	switch (wm8994->type) {
	case WM8958:
207
	case WM1811:
208 209 210 211 212 213 214 215 216 217 218 219
		ret = wm8994_reg_read(wm8994, WM8958_MIC_DETECT_1);
		if (ret < 0) {
			dev_err(dev, "Failed to read power status: %d\n", ret);
		} else if (ret & WM8958_MICD_ENA) {
			dev_dbg(dev, "CODEC still active, ignoring suspend\n");
			return 0;
		}
		break;
	default:
		break;
	}

220 221 222 223 224 225 226
	/* Disable LDO pulldowns while the device is suspended if we
	 * don't know that something will be driving them. */
	if (!wm8994->ldo_ena_always_driven)
		wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
				WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
				WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD);

227 228 229
	/* Explicitly put the device into reset in case regulators
	 * don't get disabled in order to ensure consistent restart.
	 */
230 231
	wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
			 wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
232

233 234
	regcache_mark_dirty(wm8994->regmap);

235 236 237
	/* Restore GPIO registers to prevent problems with mismatched
	 * pin configurations.
	 */
238 239
	ret = regcache_sync_region(wm8994->regmap, WM8994_GPIO_1,
				   WM8994_GPIO_11);
240 241 242
	if (ret != 0)
		dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);

243 244 245 246 247 248 249
	/* In case one of the GPIOs is used as a wake input. */
	ret = regcache_sync_region(wm8994->regmap,
				   WM8994_INTERRUPT_STATUS_1_MASK,
				   WM8994_INTERRUPT_STATUS_1_MASK);
	if (ret != 0)
		dev_err(dev, "Failed to restore interrupt mask: %d\n", ret);

250
	regcache_cache_only(wm8994->regmap, true);
251 252
	wm8994->suspended = true;

M
Mark Brown 已提交
253
	ret = regulator_bulk_disable(wm8994->num_supplies,
M
Mark Brown 已提交
254 255 256 257 258 259 260 261 262
				     wm8994->supplies);
	if (ret != 0) {
		dev_err(dev, "Failed to disable supplies: %d\n", ret);
		return ret;
	}

	return 0;
}

263
static int wm8994_resume(struct device *dev)
M
Mark Brown 已提交
264 265
{
	struct wm8994 *wm8994 = dev_get_drvdata(dev);
266
	int ret;
M
Mark Brown 已提交
267

268 269 270 271
	/* We may have lied to the PM core about suspending */
	if (!wm8994->suspended)
		return 0;

M
Mark Brown 已提交
272
	ret = regulator_bulk_enable(wm8994->num_supplies,
M
Mark Brown 已提交
273 274 275 276 277 278
				    wm8994->supplies);
	if (ret != 0) {
		dev_err(dev, "Failed to enable supplies: %d\n", ret);
		return ret;
	}

279
	regcache_cache_only(wm8994->regmap, false);
280 281 282 283
	ret = regcache_sync(wm8994->regmap);
	if (ret != 0) {
		dev_err(dev, "Failed to restore register map: %d\n", ret);
		goto err_enable;
284
	}
285

286 287 288 289 290
	/* Disable LDO pulldowns while the device is active */
	wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
			WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
			0);

291 292
	wm8994->suspended = false;

M
Mark Brown 已提交
293
	return 0;
294 295 296 297 298

err_enable:
	regulator_bulk_disable(wm8994->num_supplies, wm8994->supplies);

	return ret;
M
Mark Brown 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
}
#endif

#ifdef CONFIG_REGULATOR
static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
{
	struct wm8994_ldo_pdata *ldo_pdata;

	if (!pdata)
		return 0;

	ldo_pdata = &pdata->ldo[ldo];

	if (!ldo_pdata->init_data)
		return 0;

	return ldo_pdata->init_data->num_consumer_supplies != 0;
}
#else
static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
{
	return 0;
}
#endif

B
Bill Pemberton 已提交
324
static const struct reg_default wm8994_revc_patch[] = {
325 326 327 328 329 330
	{ 0x102, 0x3 },
	{ 0x56, 0x3 },
	{ 0x817, 0x0 },
	{ 0x102, 0x0 },
};

B
Bill Pemberton 已提交
331
static const struct reg_default wm8958_reva_patch[] = {
332 333 334 335 336 337
	{ 0x102, 0x3 },
	{ 0xcb, 0x81 },
	{ 0x817, 0x0 },
	{ 0x102, 0x0 },
};

B
Bill Pemberton 已提交
338
static const struct reg_default wm1811_reva_patch[] = {
339
	{ 0x102, 0x3 },
340
	{ 0x56, 0xc07 },
341 342 343 344 345
	{ 0x5d, 0x7e },
	{ 0x5e, 0x0 },
	{ 0x102, 0x0 },
};

M
Mark Brown 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
#ifdef CONFIG_OF
static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
{
	struct device_node *np = wm8994->dev->of_node;
	struct wm8994_pdata *pdata = &wm8994->pdata;
	int i;

	if (!np)
		return 0;

	if (of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_defaults,
				       ARRAY_SIZE(pdata->gpio_defaults)) >= 0) {
		for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
			if (wm8994->pdata.gpio_defaults[i] == 0)
				pdata->gpio_defaults[i]
					= WM8994_CONFIGURE_GPIO;
		}
	}

	of_property_read_u32_array(np, "wlf,micbias-cfg", pdata->micbias,
				   ARRAY_SIZE(pdata->micbias));

	pdata->lineout1_diff = true;
	pdata->lineout2_diff = true;
	if (of_find_property(np, "wlf,lineout1-se", NULL))
		pdata->lineout1_diff = false;
	if (of_find_property(np, "wlf,lineout2-se", NULL))
		pdata->lineout2_diff = false;

	if (of_find_property(np, "wlf,lineout1-feedback", NULL))
		pdata->lineout1fb = true;
	if (of_find_property(np, "wlf,lineout2-feedback", NULL))
		pdata->lineout2fb = true;

	if (of_find_property(np, "wlf,ldoena-always-driven", NULL))
		pdata->lineout2fb = true;

	pdata->ldo[0].enable = of_get_named_gpio(np, "wlf,ldo1ena", 0);
	if (pdata->ldo[0].enable < 0)
		pdata->ldo[0].enable = 0;

	pdata->ldo[1].enable = of_get_named_gpio(np, "wlf,ldo2ena", 0);
	if (pdata->ldo[1].enable < 0)
		pdata->ldo[1].enable = 0;

	return 0;
}
#else
static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
{
	return 0;
}
#endif

M
Mark Brown 已提交
400 401 402
/*
 * Instantiate the generic non-control parts of the device.
 */
B
Bill Pemberton 已提交
403
static int wm8994_device_init(struct wm8994 *wm8994, int irq)
M
Mark Brown 已提交
404
{
405
	struct wm8994_pdata *pdata;
406
	struct regmap_config *regmap_config;
407
	const struct reg_default *regmap_patch = NULL;
M
Mark Brown 已提交
408
	const char *devname;
409
	int ret, i, patch_regs = 0;
M
Mark Brown 已提交
410
	int pulls = 0;
M
Mark Brown 已提交
411

412 413 414 415 416 417
	if (dev_get_platdata(wm8994->dev)) {
		pdata = dev_get_platdata(wm8994->dev);
		wm8994->pdata = *pdata;
	}
	pdata = &wm8994->pdata;

M
Mark Brown 已提交
418 419 420 421
	ret = wm8994_set_pdata_from_of(wm8994);
	if (ret != 0)
		return ret;

M
Mark Brown 已提交
422 423 424 425 426 427
	dev_set_drvdata(wm8994->dev, wm8994);

	/* Add the on-chip regulators first for bootstrapping */
	ret = mfd_add_devices(wm8994->dev, -1,
			      wm8994_regulator_devs,
			      ARRAY_SIZE(wm8994_regulator_devs),
428
			      NULL, 0, NULL);
M
Mark Brown 已提交
429 430
	if (ret != 0) {
		dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
431
		goto err;
M
Mark Brown 已提交
432 433
	}

M
Mark Brown 已提交
434
	switch (wm8994->type) {
M
Mark Brown 已提交
435 436 437
	case WM1811:
		wm8994->num_supplies = ARRAY_SIZE(wm1811_main_supplies);
		break;
M
Mark Brown 已提交
438 439 440 441 442 443 444 445
	case WM8994:
		wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
		break;
	case WM8958:
		wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
		break;
	default:
		BUG();
446
		goto err;
M
Mark Brown 已提交
447 448
	}

M
Mark Brown 已提交
449 450 451
	wm8994->supplies = devm_kzalloc(wm8994->dev,
					sizeof(struct regulator_bulk_data) *
					wm8994->num_supplies, GFP_KERNEL);
452 453
	if (!wm8994->supplies) {
		ret = -ENOMEM;
454
		goto err;
455
	}
M
Mark Brown 已提交
456

M
Mark Brown 已提交
457
	switch (wm8994->type) {
M
Mark Brown 已提交
458 459 460 461
	case WM1811:
		for (i = 0; i < ARRAY_SIZE(wm1811_main_supplies); i++)
			wm8994->supplies[i].supply = wm1811_main_supplies[i];
		break;
M
Mark Brown 已提交
462 463 464 465 466 467 468 469 470 471
	case WM8994:
		for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
			wm8994->supplies[i].supply = wm8994_main_supplies[i];
		break;
	case WM8958:
		for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
			wm8994->supplies[i].supply = wm8958_main_supplies[i];
		break;
	default:
		BUG();
472
		goto err;
M
Mark Brown 已提交
473 474
	}
		
475
	ret = devm_regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
M
Mark Brown 已提交
476 477 478
				 wm8994->supplies);
	if (ret != 0) {
		dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
479
		goto err;
M
Mark Brown 已提交
480 481
	}

M
Mark Brown 已提交
482
	ret = regulator_bulk_enable(wm8994->num_supplies,
M
Mark Brown 已提交
483 484 485
				    wm8994->supplies);
	if (ret != 0) {
		dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
486
		goto err;
M
Mark Brown 已提交
487 488 489 490 491 492 493
	}

	ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
	if (ret < 0) {
		dev_err(wm8994->dev, "Failed to read ID register\n");
		goto err_enable;
	}
M
Mark Brown 已提交
494
	switch (ret) {
M
Mark Brown 已提交
495 496 497 498 499 500 501
	case 0x1811:
		devname = "WM1811";
		if (wm8994->type != WM1811)
			dev_warn(wm8994->dev, "Device registered as type %d\n",
				 wm8994->type);
		wm8994->type = WM1811;
		break;
M
Mark Brown 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	case 0x8994:
		devname = "WM8994";
		if (wm8994->type != WM8994)
			dev_warn(wm8994->dev, "Device registered as type %d\n",
				 wm8994->type);
		wm8994->type = WM8994;
		break;
	case 0x8958:
		devname = "WM8958";
		if (wm8994->type != WM8958)
			dev_warn(wm8994->dev, "Device registered as type %d\n",
				 wm8994->type);
		wm8994->type = WM8958;
		break;
	default:
M
Mark Brown 已提交
517 518 519 520 521 522 523 524 525 526 527 528
		dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
			ret);
		ret = -EINVAL;
		goto err_enable;
	}

	ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
	if (ret < 0) {
		dev_err(wm8994->dev, "Failed to read revision register: %d\n",
			ret);
		goto err_enable;
	}
529 530
	wm8994->revision = ret & WM8994_CHIP_REV_MASK;
	wm8994->cust_id = (ret & WM8994_CUST_ID_MASK) >> WM8994_CUST_ID_SHIFT;
M
Mark Brown 已提交
531

532 533
	switch (wm8994->type) {
	case WM8994:
534
		switch (wm8994->revision) {
535 536
		case 0:
		case 1:
M
Mark Brown 已提交
537 538
			dev_warn(wm8994->dev,
				 "revision %c not fully supported\n",
539
				 'A' + wm8994->revision);
540
			break;
541 542
		case 2:
		case 3:
543
		default:
544 545 546 547 548 549 550 551 552 553 554 555
			regmap_patch = wm8994_revc_patch;
			patch_regs = ARRAY_SIZE(wm8994_revc_patch);
			break;
		}
		break;

	case WM8958:
		switch (wm8994->revision) {
		case 0:
			regmap_patch = wm8958_reva_patch;
			patch_regs = ARRAY_SIZE(wm8958_reva_patch);
			break;
556 557 558
		default:
			break;
		}
M
Mark Brown 已提交
559
		break;
560

561 562
	case WM1811:
		/* Revision C did not change the relevant layer */
563 564
		if (wm8994->revision > 1)
			wm8994->revision++;
565 566 567

		regmap_patch = wm1811_reva_patch;
		patch_regs = ARRAY_SIZE(wm1811_reva_patch);
568
		break;
569

M
Mark Brown 已提交
570 571 572 573
	default:
		break;
	}

574 575
	dev_info(wm8994->dev, "%s revision %c CUST_ID %02x\n", devname,
		 'A' + wm8994->revision, wm8994->cust_id);
M
Mark Brown 已提交
576

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	switch (wm8994->type) {
	case WM1811:
		regmap_config = &wm1811_regmap_config;
		break;
	case WM8994:
		regmap_config = &wm8994_regmap_config;
		break;
	case WM8958:
		regmap_config = &wm8958_regmap_config;
		break;
	default:
		dev_err(wm8994->dev, "Unknown device type %d\n", wm8994->type);
		return -EINVAL;
	}

	ret = regmap_reinit_cache(wm8994->regmap, regmap_config);
	if (ret != 0) {
		dev_err(wm8994->dev, "Failed to reinit register cache: %d\n",
			ret);
		return ret;
	}
M
Mark Brown 已提交
598

599 600 601 602 603 604 605 606 607 608 609
	/* Explicitly put the device into reset in case regulators
	 * don't get disabled in order to ensure we know the device
	 * state.
	 */
	ret = wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
			       wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
	if (ret != 0) {
		dev_err(wm8994->dev, "Failed to reset device: %d\n", ret);
		return ret;
	}

610 611 612 613 614 615
	if (regmap_patch) {
		ret = regmap_register_patch(wm8994->regmap, regmap_patch,
					    patch_regs);
		if (ret != 0) {
			dev_err(wm8994->dev, "Failed to register patch: %d\n",
				ret);
616
			goto err;
617 618 619
		}
	}

620 621 622 623 624 625 626 627
	wm8994->irq_base = pdata->irq_base;
	wm8994->gpio_base = pdata->gpio_base;

	/* GPIO configuration is only applied if it's non-zero */
	for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
		if (pdata->gpio_defaults[i]) {
			wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
					0xffff, pdata->gpio_defaults[i]);
M
Mark Brown 已提交
628
		}
629
	}
630

631
	wm8994->ldo_ena_always_driven = pdata->ldo_ena_always_driven;
M
Mark Brown 已提交
632

633 634
	if (pdata->spkmode_pu)
		pulls |= WM8994_SPKMODE_PU;
M
Mark Brown 已提交
635

M
Mark Brown 已提交
636
	/* Disable unneeded pulls */
637
	wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
M
Mark Brown 已提交
638 639 640
			WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD |
			WM8994_SPKMODE_PU | WM8994_CSNADDR_PD,
			pulls);
641

M
Mark Brown 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	/* In some system designs where the regulators are not in use,
	 * we can achieve a small reduction in leakage currents by
	 * floating LDO outputs.  This bit makes no difference if the
	 * LDOs are enabled, it only affects cases where the LDOs were
	 * in operation and are then disabled.
	 */
	for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
		if (wm8994_ldo_in_use(pdata, i))
			wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
					WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
		else
			wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
					WM8994_LDO1_DISCH, 0);
	}

657 658
	wm8994_irq_init(wm8994);

M
Mark Brown 已提交
659 660
	ret = mfd_add_devices(wm8994->dev, -1,
			      wm8994_devs, ARRAY_SIZE(wm8994_devs),
661
			      NULL, 0, NULL);
M
Mark Brown 已提交
662 663
	if (ret != 0) {
		dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
664
		goto err_irq;
M
Mark Brown 已提交
665 666
	}

667
	pm_runtime_enable(wm8994->dev);
668
	pm_runtime_idle(wm8994->dev);
669

M
Mark Brown 已提交
670 671
	return 0;

672 673
err_irq:
	wm8994_irq_exit(wm8994);
M
Mark Brown 已提交
674
err_enable:
M
Mark Brown 已提交
675
	regulator_bulk_disable(wm8994->num_supplies,
M
Mark Brown 已提交
676
			       wm8994->supplies);
677
err:
M
Mark Brown 已提交
678 679 680 681
	mfd_remove_devices(wm8994->dev);
	return ret;
}

B
Bill Pemberton 已提交
682
static void wm8994_device_exit(struct wm8994 *wm8994)
M
Mark Brown 已提交
683
{
684
	pm_runtime_disable(wm8994->dev);
M
Mark Brown 已提交
685
	mfd_remove_devices(wm8994->dev);
686
	wm8994_irq_exit(wm8994);
M
Mark Brown 已提交
687
	regulator_bulk_disable(wm8994->num_supplies,
M
Mark Brown 已提交
688 689 690
			       wm8994->supplies);
}

691
static const struct of_device_id wm8994_of_match[] = {
692 693 694
	{ .compatible = "wlf,wm1811", .data = (void *)WM1811 },
	{ .compatible = "wlf,wm8994", .data = (void *)WM8994 },
	{ .compatible = "wlf,wm8958", .data = (void *)WM8958 },
695 696 697 698
	{ }
};
MODULE_DEVICE_TABLE(of, wm8994_of_match);

B
Bill Pemberton 已提交
699
static int wm8994_i2c_probe(struct i2c_client *i2c,
700
				      const struct i2c_device_id *id)
M
Mark Brown 已提交
701
{
M
Mark Brown 已提交
702
	const struct of_device_id *of_id;
M
Mark Brown 已提交
703
	struct wm8994 *wm8994;
704
	int ret;
M
Mark Brown 已提交
705

M
Mark Brown 已提交
706
	wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);
707
	if (wm8994 == NULL)
M
Mark Brown 已提交
708 709 710 711
		return -ENOMEM;

	i2c_set_clientdata(i2c, wm8994);
	wm8994->dev = &i2c->dev;
712
	wm8994->irq = i2c->irq;
M
Mark Brown 已提交
713 714 715 716 717 718 719 720

	if (i2c->dev.of_node) {
		of_id = of_match_device(wm8994_of_match, &i2c->dev);
		if (of_id)
			wm8994->type = (int)of_id->data;
	} else {
		wm8994->type = id->driver_data;
	}
M
Mark Brown 已提交
721

722
	wm8994->regmap = devm_regmap_init_i2c(i2c, &wm8994_base_regmap_config);
723 724 725 726 727 728 729
	if (IS_ERR(wm8994->regmap)) {
		ret = PTR_ERR(wm8994->regmap);
		dev_err(wm8994->dev, "Failed to allocate register map: %d\n",
			ret);
		return ret;
	}

M
Mark Brown 已提交
730
	return wm8994_device_init(wm8994, i2c->irq);
M
Mark Brown 已提交
731 732
}

B
Bill Pemberton 已提交
733
static int wm8994_i2c_remove(struct i2c_client *i2c)
M
Mark Brown 已提交
734 735 736 737 738 739 740 741 742
{
	struct wm8994 *wm8994 = i2c_get_clientdata(i2c);

	wm8994_device_exit(wm8994);

	return 0;
}

static const struct i2c_device_id wm8994_i2c_id[] = {
M
Mark Brown 已提交
743
	{ "wm1811", WM1811 },
744
	{ "wm1811a", WM1811 },
M
Mark Brown 已提交
745 746
	{ "wm8994", WM8994 },
	{ "wm8958", WM8958 },
M
Mark Brown 已提交
747 748 749 750
	{ }
};
MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);

751 752 753
static const struct dev_pm_ops wm8994_pm_ops = {
	SET_RUNTIME_PM_OPS(wm8994_suspend, wm8994_resume, NULL)
};
754

M
Mark Brown 已提交
755 756
static struct i2c_driver wm8994_i2c_driver = {
	.driver = {
757 758 759
		.name = "wm8994",
		.owner = THIS_MODULE,
		.pm = &wm8994_pm_ops,
M
Mark Brown 已提交
760
		.of_match_table = of_match_ptr(wm8994_of_match),
M
Mark Brown 已提交
761 762
	},
	.probe = wm8994_i2c_probe,
B
Bill Pemberton 已提交
763
	.remove = wm8994_i2c_remove,
M
Mark Brown 已提交
764 765 766
	.id_table = wm8994_i2c_id,
};

767
module_i2c_driver(wm8994_i2c_driver);
M
Mark Brown 已提交
768 769 770 771

MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");