2 * Core driver for WM8400.
4 * Copyright 2008 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
15 #include <linux/bug.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/wm8400-private.h>
20 #include <linux/mfd/wm8400-audio.h>
23 u16 readable; /* Mask of readable bits */
24 u16 writable; /* Mask of writable bits */
25 u16 vol; /* Mask of volatile bits */
26 int is_codec; /* Register controlled by codec reset */
27 u16 default_val; /* Value on reset */
29 { 0xFFFF, 0xFFFF, 0x0000, 0, 0x6172 }, /* R0 */
30 { 0x7000, 0x0000, 0x8000, 0, 0x0000 }, /* R1 */
31 { 0xFF17, 0xFF17, 0x0000, 0, 0x0000 }, /* R2 */
32 { 0xEBF3, 0xEBF3, 0x0000, 1, 0x6000 }, /* R3 */
33 { 0x3CF3, 0x3CF3, 0x0000, 1, 0x0000 }, /* R4 */
34 { 0xF1F8, 0xF1F8, 0x0000, 1, 0x4050 }, /* R5 */
35 { 0xFC1F, 0xFC1F, 0x0000, 1, 0x4000 }, /* R6 */
36 { 0xDFDE, 0xDFDE, 0x0000, 1, 0x01C8 }, /* R7 */
37 { 0xFCFC, 0xFCFC, 0x0000, 1, 0x0000 }, /* R8 */
38 { 0xEFFF, 0xEFFF, 0x0000, 1, 0x0040 }, /* R9 */
39 { 0xEFFF, 0xEFFF, 0x0000, 1, 0x0040 }, /* R10 */
40 { 0x27F7, 0x27F7, 0x0000, 1, 0x0004 }, /* R11 */
41 { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R12 */
42 { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R13 */
43 { 0x1FEF, 0x1FEF, 0x0000, 1, 0x0000 }, /* R14 */
44 { 0x0163, 0x0163, 0x0000, 1, 0x0100 }, /* R15 */
45 { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R16 */
46 { 0x01FF, 0x01FF, 0x0000, 1, 0x00C0 }, /* R17 */
47 { 0x1FFF, 0x0FFF, 0x0000, 1, 0x0000 }, /* R18 */
48 { 0xFFFF, 0xFFFF, 0x0000, 1, 0x1000 }, /* R19 */
49 { 0xFFFF, 0xFFFF, 0x0000, 1, 0x1010 }, /* R20 */
50 { 0xFFFF, 0xFFFF, 0x0000, 1, 0x1010 }, /* R21 */
51 { 0x0FDD, 0x0FDD, 0x0000, 1, 0x8000 }, /* R22 */
52 { 0x1FFF, 0x1FFF, 0x0000, 1, 0x0800 }, /* R23 */
53 { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R24 */
54 { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R25 */
55 { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R26 */
56 { 0x0000, 0x01DF, 0x0000, 1, 0x008B }, /* R27 */
57 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R28 */
58 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R29 */
59 { 0x0000, 0x0077, 0x0000, 1, 0x0066 }, /* R30 */
60 { 0x0000, 0x0033, 0x0000, 1, 0x0022 }, /* R31 */
61 { 0x0000, 0x01FF, 0x0000, 1, 0x0079 }, /* R32 */
62 { 0x0000, 0x01FF, 0x0000, 1, 0x0079 }, /* R33 */
63 { 0x0000, 0x0003, 0x0000, 1, 0x0003 }, /* R34 */
64 { 0x0000, 0x01FF, 0x0000, 1, 0x0003 }, /* R35 */
65 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R36 */
66 { 0x0000, 0x003F, 0x0000, 1, 0x0100 }, /* R37 */
67 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R38 */
68 { 0x0000, 0x000F, 0x0000, 0, 0x0000 }, /* R39 */
69 { 0x0000, 0x00FF, 0x0000, 1, 0x0000 }, /* R40 */
70 { 0x0000, 0x01B7, 0x0000, 1, 0x0000 }, /* R41 */
71 { 0x0000, 0x01B7, 0x0000, 1, 0x0000 }, /* R42 */
72 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R43 */
73 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R44 */
74 { 0x0000, 0x00FD, 0x0000, 1, 0x0000 }, /* R45 */
75 { 0x0000, 0x00FD, 0x0000, 1, 0x0000 }, /* R46 */
76 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R47 */
77 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R48 */
78 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R49 */
79 { 0x0000, 0x01FF, 0x0000, 1, 0x0000 }, /* R50 */
80 { 0x0000, 0x01B3, 0x0000, 1, 0x0180 }, /* R51 */
81 { 0x0000, 0x0077, 0x0000, 1, 0x0000 }, /* R52 */
82 { 0x0000, 0x0077, 0x0000, 1, 0x0000 }, /* R53 */
83 { 0x0000, 0x00FF, 0x0000, 1, 0x0000 }, /* R54 */
84 { 0x0000, 0x0001, 0x0000, 1, 0x0000 }, /* R55 */
85 { 0x0000, 0x003F, 0x0000, 1, 0x0000 }, /* R56 */
86 { 0x0000, 0x004F, 0x0000, 1, 0x0000 }, /* R57 */
87 { 0x0000, 0x00FD, 0x0000, 1, 0x0000 }, /* R58 */
88 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R59 */
89 { 0x1FFF, 0x1FFF, 0x0000, 1, 0x0000 }, /* R60 */
90 { 0xFFFF, 0xFFFF, 0x0000, 1, 0x0000 }, /* R61 */
91 { 0x03FF, 0x03FF, 0x0000, 1, 0x0000 }, /* R62 */
92 { 0x007F, 0x007F, 0x0000, 1, 0x0000 }, /* R63 */
93 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R64 */
94 { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R65 */
95 { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R66 */
96 { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R67 */
97 { 0xDFFF, 0xDFFF, 0x0000, 0, 0x0000 }, /* R68 */
98 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R69 */
99 { 0xFFFF, 0xFFFF, 0x0000, 0, 0x4400 }, /* R70 */
100 { 0x23FF, 0x23FF, 0x0000, 0, 0x0000 }, /* R71 */
101 { 0xFFFF, 0xFFFF, 0x0000, 0, 0x4400 }, /* R72 */
102 { 0x23FF, 0x23FF, 0x0000, 0, 0x0000 }, /* R73 */
103 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R74 */
104 { 0x000E, 0x000E, 0x0000, 0, 0x0008 }, /* R75 */
105 { 0xE00F, 0xE00F, 0x0000, 0, 0x0000 }, /* R76 */
106 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R77 */
107 { 0x03C0, 0x03C0, 0x0000, 0, 0x02C0 }, /* R78 */
108 { 0xFFFF, 0x0000, 0xffff, 0, 0x0000 }, /* R79 */
109 { 0xFFFF, 0xFFFF, 0x0000, 0, 0x0000 }, /* R80 */
110 { 0xFFFF, 0x0000, 0xffff, 0, 0x0000 }, /* R81 */
111 { 0x2BFF, 0x0000, 0xffff, 0, 0x0000 }, /* R82 */
112 { 0x0000, 0x0000, 0x0000, 0, 0x0000 }, /* R83 */
113 { 0x80FF, 0x80FF, 0x0000, 0, 0x00ff }, /* R84 */
116 static int wm8400_read(struct wm8400 *wm8400, u8 reg, int num_regs, u16 *dest)
120 BUG_ON(reg + num_regs - 1 > ARRAY_SIZE(wm8400->reg_cache));
122 /* If there are any volatile reads then read back the entire block */
123 for (i = reg; i < reg + num_regs; i++)
124 if (reg_data[i].vol) {
125 ret = wm8400->read_dev(wm8400->io_data, reg,
129 for (i = 0; i < num_regs; i++)
130 dest[i] = be16_to_cpu(dest[i]);
135 /* Otherwise use the cache */
136 memcpy(dest, &wm8400->reg_cache[reg], num_regs * sizeof(u16));
141 static int wm8400_write(struct wm8400 *wm8400, u8 reg, int num_regs,
146 BUG_ON(reg + num_regs - 1 > ARRAY_SIZE(wm8400->reg_cache));
148 for (i = 0; i < num_regs; i++) {
149 BUG_ON(!reg_data[reg + i].writable);
150 wm8400->reg_cache[reg + i] = src[i];
151 src[i] = cpu_to_be16(src[i]);
154 /* Do the actual I/O */
155 ret = wm8400->write_dev(wm8400->io_data, reg, num_regs, src);
163 * wm8400_reg_read - Single register read
165 * @wm8400: Pointer to wm8400 control structure
166 * @reg: Register to read
170 u16 wm8400_reg_read(struct wm8400 *wm8400, u8 reg)
174 mutex_lock(&wm8400->io_lock);
176 wm8400_read(wm8400, reg, 1, &val);
178 mutex_unlock(&wm8400->io_lock);
182 EXPORT_SYMBOL_GPL(wm8400_reg_read);
184 int wm8400_block_read(struct wm8400 *wm8400, u8 reg, int count, u16 *data)
188 mutex_lock(&wm8400->io_lock);
190 ret = wm8400_read(wm8400, reg, count, data);
192 mutex_unlock(&wm8400->io_lock);
196 EXPORT_SYMBOL_GPL(wm8400_block_read);
199 * wm8400_set_bits - Bitmask write
201 * @wm8400: Pointer to wm8400 control structure
202 * @reg: Register to access
203 * @mask: Mask of bits to change
204 * @val: Value to set for masked bits
206 int wm8400_set_bits(struct wm8400 *wm8400, u8 reg, u16 mask, u16 val)
211 mutex_lock(&wm8400->io_lock);
213 ret = wm8400_read(wm8400, reg, 1, &tmp);
214 tmp = (tmp & ~mask) | val;
216 ret = wm8400_write(wm8400, reg, 1, &tmp);
218 mutex_unlock(&wm8400->io_lock);
222 EXPORT_SYMBOL_GPL(wm8400_set_bits);
225 * wm8400_reset_codec_reg_cache - Reset cached codec registers to
226 * their default values.
228 void wm8400_reset_codec_reg_cache(struct wm8400 *wm8400)
232 mutex_lock(&wm8400->io_lock);
234 /* Reset all codec registers to their initial value */
235 for (i = 0; i < ARRAY_SIZE(wm8400->reg_cache); i++)
236 if (reg_data[i].is_codec)
237 wm8400->reg_cache[i] = reg_data[i].default_val;
239 mutex_unlock(&wm8400->io_lock);
241 EXPORT_SYMBOL_GPL(wm8400_reset_codec_reg_cache);
243 static int wm8400_register_codec(struct wm8400 *wm8400)
245 struct mfd_cell cell = {
246 .name = "wm8400-codec",
247 .driver_data = wm8400,
250 return mfd_add_devices(wm8400->dev, -1, &cell, 1, NULL, 0);
254 * wm8400_init - Generic initialisation
256 * The WM8400 can be configured as either an I2C or SPI device. Probe
257 * functions for each bus set up the accessors then call into this to
258 * set up the device itself.
260 static int wm8400_init(struct wm8400 *wm8400,
261 struct wm8400_platform_data *pdata)
266 mutex_init(&wm8400->io_lock);
268 wm8400->dev->driver_data = wm8400;
270 /* Check that this is actually a WM8400 */
271 ret = wm8400->read_dev(wm8400->io_data, WM8400_RESET_ID, 1, ®);
273 dev_err(wm8400->dev, "Chip ID register read failed\n");
276 if (be16_to_cpu(reg) != reg_data[WM8400_RESET_ID].default_val) {
277 dev_err(wm8400->dev, "Device is not a WM8400, ID is %x\n",
282 /* We don't know what state the hardware is in and since this
283 * is a PMIC we can't reset it safely so initialise the register
284 * cache from the hardware.
286 ret = wm8400->read_dev(wm8400->io_data, 0,
287 ARRAY_SIZE(wm8400->reg_cache),
290 dev_err(wm8400->dev, "Register cache read failed\n");
293 for (i = 0; i < ARRAY_SIZE(wm8400->reg_cache); i++)
294 wm8400->reg_cache[i] = be16_to_cpu(wm8400->reg_cache[i]);
296 /* If the codec is in reset use hard coded values */
297 if (!(wm8400->reg_cache[WM8400_POWER_MANAGEMENT_1] & WM8400_CODEC_ENA))
298 for (i = 0; i < ARRAY_SIZE(wm8400->reg_cache); i++)
299 if (reg_data[i].is_codec)
300 wm8400->reg_cache[i] = reg_data[i].default_val;
302 ret = wm8400_read(wm8400, WM8400_ID, 1, ®);
304 dev_err(wm8400->dev, "ID register read failed: %d\n", ret);
307 reg = (reg & WM8400_CHIP_REV_MASK) >> WM8400_CHIP_REV_SHIFT;
308 dev_info(wm8400->dev, "WM8400 revision %x\n", reg);
310 ret = wm8400_register_codec(wm8400);
312 dev_err(wm8400->dev, "Failed to register codec\n");
316 if (pdata && pdata->platform_init) {
317 ret = pdata->platform_init(wm8400->dev);
319 dev_err(wm8400->dev, "Platform init failed: %d\n",
324 dev_warn(wm8400->dev, "No platform initialisation supplied\n");
329 mfd_remove_devices(wm8400->dev);
333 static void wm8400_release(struct wm8400 *wm8400)
335 mfd_remove_devices(wm8400->dev);
338 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
339 static int wm8400_i2c_read(void *io_data, char reg, int count, u16 *dest)
341 struct i2c_client *i2c = io_data;
342 struct i2c_msg xfer[2];
346 xfer[0].addr = i2c->addr;
352 xfer[1].addr = i2c->addr;
353 xfer[1].flags = I2C_M_RD;
354 xfer[1].len = count * sizeof(u16);
355 xfer[1].buf = (u8 *)dest;
357 ret = i2c_transfer(i2c->adapter, xfer, 2);
366 static int wm8400_i2c_write(void *io_data, char reg, int count, const u16 *src)
368 struct i2c_client *i2c = io_data;
372 /* We add 1 byte for device register - ideally I2C would gather. */
373 msg = kmalloc((count * sizeof(u16)) + 1, GFP_KERNEL);
378 memcpy(&msg[1], src, count * sizeof(u16));
380 ret = i2c_master_send(i2c, msg, (count * sizeof(u16)) + 1);
382 if (ret == (count * 2) + 1)
392 static int wm8400_i2c_probe(struct i2c_client *i2c,
393 const struct i2c_device_id *id)
395 struct wm8400 *wm8400;
398 wm8400 = kzalloc(sizeof(struct wm8400), GFP_KERNEL);
399 if (wm8400 == NULL) {
404 wm8400->io_data = i2c;
405 wm8400->read_dev = wm8400_i2c_read;
406 wm8400->write_dev = wm8400_i2c_write;
407 wm8400->dev = &i2c->dev;
408 i2c_set_clientdata(i2c, wm8400);
410 ret = wm8400_init(wm8400, i2c->dev.platform_data);
417 i2c_set_clientdata(i2c, NULL);
423 static int wm8400_i2c_remove(struct i2c_client *i2c)
425 struct wm8400 *wm8400 = i2c_get_clientdata(i2c);
427 wm8400_release(wm8400);
428 i2c_set_clientdata(i2c, NULL);
434 static const struct i2c_device_id wm8400_i2c_id[] = {
438 MODULE_DEVICE_TABLE(i2c, wm8400_i2c_id);
440 static struct i2c_driver wm8400_i2c_driver = {
443 .owner = THIS_MODULE,
445 .probe = wm8400_i2c_probe,
446 .remove = wm8400_i2c_remove,
447 .id_table = wm8400_i2c_id,
451 static int __init wm8400_module_init(void)
455 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
456 ret = i2c_add_driver(&wm8400_i2c_driver);
458 pr_err("Failed to register I2C driver: %d\n", ret);
463 module_init(wm8400_module_init);
465 static void __exit wm8400_module_exit(void)
467 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
468 i2c_del_driver(&wm8400_i2c_driver);
471 module_exit(wm8400_module_exit);
473 MODULE_LICENSE("GPL");
474 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");