wm8400-core.c 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Core driver for WM8400.
 *
 * Copyright 2008 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.
 *
 */

15
#include <linux/module.h>
16
#include <linux/bug.h>
M
Mark Brown 已提交
17
#include <linux/err.h>
18 19
#include <linux/i2c.h>
#include <linux/kernel.h>
M
Mark Brown 已提交
20
#include <linux/mfd/core.h>
21 22
#include <linux/mfd/wm8400-private.h>
#include <linux/mfd/wm8400-audio.h>
M
Mark Brown 已提交
23
#include <linux/regmap.h>
24
#include <linux/slab.h>
25

26
static bool wm8400_volatile(struct device *dev, unsigned int reg)
27
{
28 29 30 31 32 33 34
	switch (reg) {
	case WM8400_INTERRUPT_STATUS_1:
	case WM8400_INTERRUPT_LEVELS:
	case WM8400_SHUTDOWN_REASON:
		return true;
	default:
		return false;
35 36 37 38 39 40 41 42 43 44 45 46 47
	}
}

/**
 * wm8400_reg_read - Single register read
 *
 * @wm8400: Pointer to wm8400 control structure
 * @reg:    Register to read
 *
 * @return  Read value
 */
u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg)
{
48 49
	unsigned int val;
	int ret;
50

51 52 53
	ret = regmap_read(wm8400->regmap, reg, &val);
	if (ret < 0)
		return ret;
54 55 56 57 58 59 60

	return val;
}
EXPORT_SYMBOL_GPL(wm8400_reg_read);

int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
{
61
	return regmap_bulk_read(wm8400->regmap, reg, data, count);
62
}
M
Mark Brown 已提交
63
EXPORT_SYMBOL_GPL(wm8400_block_read);
64

M
Mark Brown 已提交
65 66
static int wm8400_register_codec(struct wm8400 *wm8400)
{
67
	const struct mfd_cell cell = {
M
Mark Brown 已提交
68
		.name = "wm8400-codec",
69 70
		.platform_data = wm8400,
		.pdata_size = sizeof(*wm8400),
M
Mark Brown 已提交
71 72
	};

73
	return devm_mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0, NULL);
M
Mark Brown 已提交
74 75
}

76 77 78 79 80 81 82 83 84 85
/*
 * wm8400_init - Generic initialisation
 *
 * The WM8400 can be configured as either an I2C or SPI device.  Probe
 * functions for each bus set up the accessors then call into this to
 * set up the device itself.
 */
static int wm8400_init(struct wm8400 *wm8400,
		       struct wm8400_platform_data *pdata)
{
86 87
	unsigned int reg;
	int ret;
88

89
	dev_set_drvdata(wm8400->dev, wm8400);
90 91

	/* Check that this is actually a WM8400 */
92
	ret = regmap_read(wm8400->regmap, WM8400_RESET_ID, &reg);
93 94 95 96
	if (ret != 0) {
		dev_err(wm8400->dev, "Chip ID register read failed\n");
		return -EIO;
	}
97 98 99
	if (reg != 0x6172) {
		dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
			reg);
100 101 102
		return -ENODEV;
	}

103
	ret = regmap_read(wm8400->regmap, WM8400_ID, &reg);
104 105 106 107 108 109 110
	if (ret != 0) {
		dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
		return ret;
	}
	reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
	dev_info(wm8400->dev, "WM8400 revision %x\n", reg);

M
Mark Brown 已提交
111 112 113
	ret = wm8400_register_codec(wm8400);
	if (ret != 0) {
		dev_err(wm8400->dev, "Failed to register codec\n");
114
		return ret;
M
Mark Brown 已提交
115 116
	}

117 118
	if (pdata && pdata->platform_init) {
		ret = pdata->platform_init(wm8400->dev);
M
Mark Brown 已提交
119
		if (ret != 0) {
120 121
			dev_err(wm8400->dev, "Platform init failed: %d\n",
				ret);
122
			return ret;
M
Mark Brown 已提交
123
		}
124 125 126
	} else
		dev_warn(wm8400->dev, "No platform initialisation supplied\n");

M
Mark Brown 已提交
127
	return 0;
128 129
}

M
Mark Brown 已提交
130 131 132 133
static const struct regmap_config wm8400_regmap_config = {
	.reg_bits = 8,
	.val_bits = 16,
	.max_register = WM8400_REGISTER_COUNT - 1,
134 135 136 137

	.volatile_reg = wm8400_volatile,

	.cache_type = REGCACHE_RBTREE,
M
Mark Brown 已提交
138
};
139

140 141 142 143 144 145 146 147 148 149
/**
 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
 * their default values.
 */
void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
{
	regmap_reinit_cache(wm8400->regmap, &wm8400_regmap_config);
}
EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);

M
Mark Brown 已提交
150
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
151 152 153 154 155
static int wm8400_i2c_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	struct wm8400 *wm8400;

156
	wm8400 = devm_kzalloc(&i2c->dev, sizeof(struct wm8400), GFP_KERNEL);
157 158
	if (!wm8400)
		return -ENOMEM;
159

160
	wm8400->regmap = devm_regmap_init_i2c(i2c, &wm8400_regmap_config);
161 162
	if (IS_ERR(wm8400->regmap))
		return PTR_ERR(wm8400->regmap);
M
Mark Brown 已提交
163

164 165 166
	wm8400->dev = &i2c->dev;
	i2c_set_clientdata(i2c, wm8400);

167
	return wm8400_init(wm8400, dev_get_platdata(&i2c->dev));
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
}

static const struct i2c_device_id wm8400_i2c_id[] = {
       { "wm8400", 0 },
       { }
};
MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id);

static struct i2c_driver wm8400_i2c_driver = {
	.driver = {
		.name = "WM8400",
	},
	.probe    = wm8400_i2c_probe,
	.id_table = wm8400_i2c_id,
};
#endif

static int __init wm8400_module_init(void)
{
	int ret = -ENODEV;

#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	ret = i2c_add_driver(&wm8400_i2c_driver);
	if (ret != 0)
		pr_err("Failed to register I2C driver: %d\n", ret);
#endif

	return ret;
}
197
subsys_initcall(wm8400_module_init);
198 199 200 201 202 203 204 205 206 207 208

static void __exit wm8400_module_exit(void)
{
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	i2c_del_driver(&wm8400_i2c_driver);
#endif
}
module_exit(wm8400_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");