2 * wm8350-core.c -- Device access for Wolfson WM8350
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood, Mark Brown
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
22 #include <linux/mfd/wm8350/core.h>
23 #include <linux/mfd/wm8350/audio.h>
24 #include <linux/mfd/wm8350/gpio.h>
25 #include <linux/mfd/wm8350/pmic.h>
26 #include <linux/mfd/wm8350/supply.h>
28 #define WM8350_UNLOCK_KEY 0x0013
29 #define WM8350_LOCK_KEY 0x0000
31 #define WM8350_CLOCK_CONTROL_1 0x28
32 #define WM8350_AIF_TEST 0x74
35 #define WM8350_BUS_DEBUG 0
37 #define dump(regs, src) do { \
41 for (i_ = 0; i_ < regs; i_++) \
42 printk(" 0x%4.4x", *src_++); \
46 #define dump(bytes, src)
49 #define WM8350_LOCK_DEBUG 0
51 #define ldbg(format, arg...) printk(format, ## arg)
53 #define ldbg(format, arg...)
59 static DEFINE_MUTEX(io_mutex);
60 static DEFINE_MUTEX(reg_lock_mutex);
61 static DEFINE_MUTEX(auxadc_mutex);
63 /* Perform a physical read from the device.
65 static int wm8350_phys_read(struct wm8350 *wm8350, u8 reg, int num_regs,
69 int bytes = num_regs * 2;
71 dev_dbg(wm8350->dev, "volatile read\n");
72 ret = wm8350->read_dev(wm8350, reg, bytes, (char *)dest);
74 for (i = reg; i < reg + num_regs; i++) {
75 /* Cache is CPU endian */
76 dest[i - reg] = be16_to_cpu(dest[i - reg]);
78 /* Satisfy non-volatile bits from cache */
79 dest[i - reg] &= wm8350_reg_io_map[i].vol;
80 dest[i - reg] |= wm8350->reg_cache[i];
82 /* Mask out non-readable bits */
83 dest[i - reg] &= wm8350_reg_io_map[i].readable;
91 static int wm8350_read(struct wm8350 *wm8350, u8 reg, int num_regs, u16 *dest)
94 int end = reg + num_regs;
96 int bytes = num_regs * 2;
98 if (wm8350->read_dev == NULL)
101 if ((reg + num_regs - 1) > WM8350_MAX_REGISTER) {
102 dev_err(wm8350->dev, "invalid reg %x\n",
108 "%s R%d(0x%2.2x) %d regs\n", __func__, reg, reg, num_regs);
111 /* we can _safely_ read any register, but warn if read not supported */
112 for (i = reg; i < end; i++) {
113 if (!wm8350_reg_io_map[i].readable)
114 dev_warn(wm8350->dev,
115 "reg R%d is not readable\n", i);
119 /* if any volatile registers are required, then read back all */
120 for (i = reg; i < end; i++)
121 if (wm8350_reg_io_map[i].vol)
122 return wm8350_phys_read(wm8350, reg, num_regs, dest);
124 /* no volatiles, then cache is good */
125 dev_dbg(wm8350->dev, "cache read\n");
126 memcpy(dest, &wm8350->reg_cache[reg], bytes);
127 dump(num_regs, dest);
131 static inline int is_reg_locked(struct wm8350 *wm8350, u8 reg)
133 if (reg == WM8350_SECURITY ||
134 wm8350->reg_cache[WM8350_SECURITY] == WM8350_UNLOCK_KEY)
137 if ((reg == WM8350_GPIO_CONFIGURATION_I_O) ||
138 (reg >= WM8350_GPIO_FUNCTION_SELECT_1 &&
139 reg <= WM8350_GPIO_FUNCTION_SELECT_4) ||
140 (reg >= WM8350_BATTERY_CHARGER_CONTROL_1 &&
141 reg <= WM8350_BATTERY_CHARGER_CONTROL_3))
146 static int wm8350_write(struct wm8350 *wm8350, u8 reg, int num_regs, u16 *src)
149 int end = reg + num_regs;
150 int bytes = num_regs * 2;
152 if (wm8350->write_dev == NULL)
155 if ((reg + num_regs - 1) > WM8350_MAX_REGISTER) {
156 dev_err(wm8350->dev, "invalid reg %x\n",
161 /* it's generally not a good idea to write to RO or locked registers */
162 for (i = reg; i < end; i++) {
163 if (!wm8350_reg_io_map[i].writable) {
165 "attempted write to read only reg R%d\n", i);
169 if (is_reg_locked(wm8350, i)) {
171 "attempted write to locked reg R%d\n", i);
175 src[i - reg] &= wm8350_reg_io_map[i].writable;
177 wm8350->reg_cache[i] =
178 (wm8350->reg_cache[i] & ~wm8350_reg_io_map[i].writable)
181 src[i - reg] = cpu_to_be16(src[i - reg]);
184 /* Actually write it out */
185 return wm8350->write_dev(wm8350, reg, bytes, (char *)src);
189 * Safe read, modify, write methods
191 int wm8350_clear_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
196 mutex_lock(&io_mutex);
197 err = wm8350_read(wm8350, reg, 1, &data);
199 dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
204 err = wm8350_write(wm8350, reg, 1, &data);
206 dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
208 mutex_unlock(&io_mutex);
211 EXPORT_SYMBOL_GPL(wm8350_clear_bits);
213 int wm8350_set_bits(struct wm8350 *wm8350, u16 reg, u16 mask)
218 mutex_lock(&io_mutex);
219 err = wm8350_read(wm8350, reg, 1, &data);
221 dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
226 err = wm8350_write(wm8350, reg, 1, &data);
228 dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
230 mutex_unlock(&io_mutex);
233 EXPORT_SYMBOL_GPL(wm8350_set_bits);
235 u16 wm8350_reg_read(struct wm8350 *wm8350, int reg)
240 mutex_lock(&io_mutex);
241 err = wm8350_read(wm8350, reg, 1, &data);
243 dev_err(wm8350->dev, "read from reg R%d failed\n", reg);
245 mutex_unlock(&io_mutex);
248 EXPORT_SYMBOL_GPL(wm8350_reg_read);
250 int wm8350_reg_write(struct wm8350 *wm8350, int reg, u16 val)
255 mutex_lock(&io_mutex);
256 ret = wm8350_write(wm8350, reg, 1, &data);
258 dev_err(wm8350->dev, "write to reg R%d failed\n", reg);
259 mutex_unlock(&io_mutex);
262 EXPORT_SYMBOL_GPL(wm8350_reg_write);
264 int wm8350_block_read(struct wm8350 *wm8350, int start_reg, int regs,
269 mutex_lock(&io_mutex);
270 err = wm8350_read(wm8350, start_reg, regs, dest);
272 dev_err(wm8350->dev, "block read starting from R%d failed\n",
274 mutex_unlock(&io_mutex);
277 EXPORT_SYMBOL_GPL(wm8350_block_read);
279 int wm8350_block_write(struct wm8350 *wm8350, int start_reg, int regs,
284 mutex_lock(&io_mutex);
285 ret = wm8350_write(wm8350, start_reg, regs, src);
287 dev_err(wm8350->dev, "block write starting at R%d failed\n",
289 mutex_unlock(&io_mutex);
292 EXPORT_SYMBOL_GPL(wm8350_block_write);
294 int wm8350_reg_lock(struct wm8350 *wm8350)
296 u16 key = WM8350_LOCK_KEY;
300 mutex_lock(&io_mutex);
301 ret = wm8350_write(wm8350, WM8350_SECURITY, 1, &key);
303 dev_err(wm8350->dev, "lock failed\n");
304 mutex_unlock(&io_mutex);
307 EXPORT_SYMBOL_GPL(wm8350_reg_lock);
309 int wm8350_reg_unlock(struct wm8350 *wm8350)
311 u16 key = WM8350_UNLOCK_KEY;
315 mutex_lock(&io_mutex);
316 ret = wm8350_write(wm8350, WM8350_SECURITY, 1, &key);
318 dev_err(wm8350->dev, "unlock failed\n");
319 mutex_unlock(&io_mutex);
322 EXPORT_SYMBOL_GPL(wm8350_reg_unlock);
325 * Cache is always host endian.
327 static int wm8350_create_cache(struct wm8350 *wm8350, int mode)
334 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_0
336 reg_map = wm8350_mode0_defaults;
339 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_1
341 reg_map = wm8350_mode1_defaults;
344 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_2
346 reg_map = wm8350_mode2_defaults;
349 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_3
351 reg_map = wm8350_mode3_defaults;
355 dev_err(wm8350->dev, "Configuration mode %d not supported\n",
361 kzalloc(sizeof(u16) * (WM8350_MAX_REGISTER + 1), GFP_KERNEL);
362 if (wm8350->reg_cache == NULL)
365 /* Read the initial cache state back from the device - this is
366 * a PMIC so the device many not be in a virgin state and we
367 * can't rely on the silicon values.
369 for (i = 0; i < WM8350_MAX_REGISTER; i++) {
370 /* audio register range */
371 if (wm8350_reg_io_map[i].readable &&
372 (i < WM8350_CLOCK_CONTROL_1 || i > WM8350_AIF_TEST)) {
373 ret = wm8350->read_dev(wm8350, i, 2, (char *)&value);
376 "failed to read initial cache value\n");
379 value = be16_to_cpu(value);
380 value &= wm8350_reg_io_map[i].readable;
381 wm8350->reg_cache[i] = value;
383 wm8350->reg_cache[i] = reg_map[i];
389 EXPORT_SYMBOL_GPL(wm8350_create_cache);
391 int wm8350_device_init(struct wm8350 *wm8350,
392 struct wm8350_platform_data *pdata)
395 u16 id1, id2, mask, mode;
397 /* get WM8350 revision and config mode */
398 wm8350->read_dev(wm8350, WM8350_RESET_ID, sizeof(id1), &id1);
399 wm8350->read_dev(wm8350, WM8350_ID, sizeof(id2), &id2);
401 id1 = be16_to_cpu(id1);
402 id2 = be16_to_cpu(id2);
405 dev_info(wm8350->dev, "Found Rev C device\n");
406 else if (id1 == 0x6143) {
407 switch ((id2 & WM8350_CHIP_REV_MASK) >> 12) {
409 dev_info(wm8350->dev, "Found Rev E device\n");
410 wm8350->rev = WM8350_REV_E;
413 dev_info(wm8350->dev, "Found Rev F device\n");
414 wm8350->rev = WM8350_REV_F;
417 dev_info(wm8350->dev, "Found Rev G device\n");
418 wm8350->rev = WM8350_REV_G;
421 /* For safety we refuse to run on unknown hardware */
422 dev_info(wm8350->dev, "Found unknown rev\n");
427 dev_info(wm8350->dev, "Device with ID %x is not a WM8350\n",
433 mode = id2 & WM8350_CONF_STS_MASK >> 10;
434 mask = id2 & WM8350_CUST_ID_MASK;
435 dev_info(wm8350->dev, "Config mode %d, ROM mask %d\n", mode, mask);
437 ret = wm8350_create_cache(wm8350, mode);
439 printk(KERN_ERR "wm8350: failed to create register cache\n");
444 ret = pdata->init(wm8350);
446 dev_err(wm8350->dev, "Platform init() failed: %d\n",
455 kfree(wm8350->reg_cache);
458 EXPORT_SYMBOL_GPL(wm8350_device_init);
460 void wm8350_device_exit(struct wm8350 *wm8350)
462 kfree(wm8350->reg_cache);
464 EXPORT_SYMBOL_GPL(wm8350_device_exit);
466 MODULE_LICENSE("GPL");