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/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/bug.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/regmap.h>
24 #include <linux/workqueue.h>
26 #include <linux/mfd/wm8350/core.h>
27 #include <linux/mfd/wm8350/audio.h>
28 #include <linux/mfd/wm8350/comparator.h>
29 #include <linux/mfd/wm8350/gpio.h>
30 #include <linux/mfd/wm8350/pmic.h>
31 #include <linux/mfd/wm8350/rtc.h>
32 #include <linux/mfd/wm8350/supply.h>
33 #include <linux/mfd/wm8350/wdt.h>
35 #define WM8350_CLOCK_CONTROL_1 0x28
36 #define WM8350_AIF_TEST 0x74
39 #define WM8350_BUS_DEBUG 0
41 #define dump(regs, src) do { \
45 for (i_ = 0; i_ < regs; i_++) \
46 printk(" 0x%4.4x", *src_++); \
50 #define dump(bytes, src)
53 #define WM8350_LOCK_DEBUG 0
55 #define ldbg(format, arg...) printk(format, ## arg)
57 #define ldbg(format, arg...)
63 static DEFINE_MUTEX(reg_lock_mutex
);
66 * Safe read, modify, write methods
68 int wm8350_clear_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
70 return regmap_update_bits(wm8350
->regmap
, reg
, mask
, 0);
72 EXPORT_SYMBOL_GPL(wm8350_clear_bits
);
74 int wm8350_set_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
76 return regmap_update_bits(wm8350
->regmap
, reg
, mask
, mask
);
78 EXPORT_SYMBOL_GPL(wm8350_set_bits
);
80 u16
wm8350_reg_read(struct wm8350
*wm8350
, int reg
)
85 err
= regmap_read(wm8350
->regmap
, reg
, &data
);
87 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
91 EXPORT_SYMBOL_GPL(wm8350_reg_read
);
93 int wm8350_reg_write(struct wm8350
*wm8350
, int reg
, u16 val
)
97 ret
= regmap_write(wm8350
->regmap
, reg
, val
);
100 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
103 EXPORT_SYMBOL_GPL(wm8350_reg_write
);
105 int wm8350_block_read(struct wm8350
*wm8350
, int start_reg
, int regs
,
110 err
= regmap_bulk_read(wm8350
->regmap
, start_reg
, dest
, regs
);
112 dev_err(wm8350
->dev
, "block read starting from R%d failed\n",
117 EXPORT_SYMBOL_GPL(wm8350_block_read
);
119 int wm8350_block_write(struct wm8350
*wm8350
, int start_reg
, int regs
,
124 ret
= regmap_bulk_write(wm8350
->regmap
, start_reg
, src
, regs
);
126 dev_err(wm8350
->dev
, "block write starting at R%d failed\n",
131 EXPORT_SYMBOL_GPL(wm8350_block_write
);
136 * The WM8350 has a hardware lock which can be used to prevent writes to
137 * some registers (generally those which can cause particularly serious
138 * problems if misused). This function enables that lock.
140 int wm8350_reg_lock(struct wm8350
*wm8350
)
144 mutex_lock(®_lock_mutex
);
148 ret
= wm8350_reg_write(wm8350
, WM8350_SECURITY
, WM8350_LOCK_KEY
);
150 dev_err(wm8350
->dev
, "lock failed\n");
152 wm8350
->unlocked
= false;
154 mutex_unlock(®_lock_mutex
);
158 EXPORT_SYMBOL_GPL(wm8350_reg_lock
);
161 * wm8350_reg_unlock()
163 * The WM8350 has a hardware lock which can be used to prevent writes to
164 * some registers (generally those which can cause particularly serious
165 * problems if misused). This function disables that lock so updates
166 * can be performed. For maximum safety this should be done only when
169 int wm8350_reg_unlock(struct wm8350
*wm8350
)
173 mutex_lock(®_lock_mutex
);
177 ret
= wm8350_reg_write(wm8350
, WM8350_SECURITY
, WM8350_UNLOCK_KEY
);
179 dev_err(wm8350
->dev
, "unlock failed\n");
181 wm8350
->unlocked
= true;
183 mutex_unlock(®_lock_mutex
);
187 EXPORT_SYMBOL_GPL(wm8350_reg_unlock
);
189 int wm8350_read_auxadc(struct wm8350
*wm8350
, int channel
, int scale
, int vref
)
193 if (channel
< WM8350_AUXADC_AUX1
|| channel
> WM8350_AUXADC_TEMP
)
195 if (channel
>= WM8350_AUXADC_USB
&& channel
<= WM8350_AUXADC_TEMP
196 && (scale
!= 0 || vref
!= 0))
199 mutex_lock(&wm8350
->auxadc_mutex
);
201 /* Turn on the ADC */
202 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
203 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
, reg
| WM8350_AUXADC_ENA
);
208 wm8350_reg_write(wm8350
, WM8350_AUX1_READBACK
+ channel
, reg
);
211 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
212 reg
|= 1 << channel
| WM8350_AUXADC_POLL
;
213 wm8350_reg_write(wm8350
, WM8350_DIGITISER_CONTROL_1
, reg
);
215 /* If a late IRQ left the completion signalled then consume
217 try_wait_for_completion(&wm8350
->auxadc_done
);
219 /* We ignore the result of the completion and just check for a
220 * conversion result, allowing us to soldier on if the IRQ
221 * infrastructure is not set up for the chip. */
222 wait_for_completion_timeout(&wm8350
->auxadc_done
, msecs_to_jiffies(5));
224 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
225 if (reg
& WM8350_AUXADC_POLL
)
226 dev_err(wm8350
->dev
, "adc chn %d read timeout\n", channel
);
228 result
= wm8350_reg_read(wm8350
,
229 WM8350_AUX1_READBACK
+ channel
);
231 /* Turn off the ADC */
232 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
233 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
,
234 reg
& ~WM8350_AUXADC_ENA
);
236 mutex_unlock(&wm8350
->auxadc_mutex
);
238 return result
& WM8350_AUXADC_DATA1_MASK
;
240 EXPORT_SYMBOL_GPL(wm8350_read_auxadc
);
242 static irqreturn_t
wm8350_auxadc_irq(int irq
, void *irq_data
)
244 struct wm8350
*wm8350
= irq_data
;
246 complete(&wm8350
->auxadc_done
);
252 * Register a client device. This is non-fatal since there is no need to
253 * fail the entire device init due to a single platform device failing.
255 static void wm8350_client_dev_register(struct wm8350
*wm8350
,
257 struct platform_device
**pdev
)
261 *pdev
= platform_device_alloc(name
, -1);
263 dev_err(wm8350
->dev
, "Failed to allocate %s\n", name
);
267 (*pdev
)->dev
.parent
= wm8350
->dev
;
268 platform_set_drvdata(*pdev
, wm8350
);
269 ret
= platform_device_add(*pdev
);
271 dev_err(wm8350
->dev
, "Failed to register %s: %d\n", name
, ret
);
272 platform_device_put(*pdev
);
277 int wm8350_device_init(struct wm8350
*wm8350
, int irq
,
278 struct wm8350_platform_data
*pdata
)
281 unsigned int id1
, id2
, mask_rev
;
282 unsigned int cust_id
, mode
, chip_rev
;
284 dev_set_drvdata(wm8350
->dev
, wm8350
);
286 /* get WM8350 revision and config mode */
287 ret
= regmap_read(wm8350
->regmap
, WM8350_RESET_ID
, &id1
);
289 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
293 ret
= regmap_read(wm8350
->regmap
, WM8350_ID
, &id2
);
295 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
299 ret
= regmap_read(wm8350
->regmap
, WM8350_REVISION
, &mask_rev
);
301 dev_err(wm8350
->dev
, "Failed to read revision: %d\n", ret
);
307 "Device with ID %x is not a WM8350\n", id1
);
312 mode
= (id2
& WM8350_CONF_STS_MASK
) >> 10;
313 cust_id
= id2
& WM8350_CUST_ID_MASK
;
314 chip_rev
= (id2
& WM8350_CHIP_REV_MASK
) >> 12;
315 dev_info(wm8350
->dev
,
316 "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
317 mode
, cust_id
, mask_rev
, chip_rev
);
320 dev_err(wm8350
->dev
, "Unsupported CUST_ID\n");
327 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
328 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
332 dev_info(wm8350
->dev
, "WM8350 Rev E\n");
335 dev_info(wm8350
->dev
, "WM8350 Rev F\n");
338 dev_info(wm8350
->dev
, "WM8350 Rev G\n");
339 wm8350
->power
.rev_g_coeff
= 1;
342 dev_info(wm8350
->dev
, "WM8350 Rev H\n");
343 wm8350
->power
.rev_g_coeff
= 1;
346 /* For safety we refuse to run on unknown hardware */
347 dev_err(wm8350
->dev
, "Unknown WM8350 CHIP_REV\n");
354 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_4
;
355 wm8350
->pmic
.max_isink
= WM8350_ISINK_A
;
359 dev_info(wm8350
->dev
, "WM8351 Rev A\n");
360 wm8350
->power
.rev_g_coeff
= 1;
364 dev_info(wm8350
->dev
, "WM8351 Rev B\n");
365 wm8350
->power
.rev_g_coeff
= 1;
369 dev_err(wm8350
->dev
, "Unknown WM8351 CHIP_REV\n");
376 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
377 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
381 dev_info(wm8350
->dev
, "WM8352 Rev A\n");
382 wm8350
->power
.rev_g_coeff
= 1;
386 dev_err(wm8350
->dev
, "Unknown WM8352 CHIP_REV\n");
393 dev_err(wm8350
->dev
, "Unknown MASK_REV\n");
398 mutex_init(&wm8350
->auxadc_mutex
);
399 init_completion(&wm8350
->auxadc_done
);
401 ret
= wm8350_irq_init(wm8350
, irq
, pdata
);
405 if (wm8350
->irq_base
) {
406 ret
= request_threaded_irq(wm8350
->irq_base
+
407 WM8350_IRQ_AUXADC_DATARDY
,
408 NULL
, wm8350_auxadc_irq
,
412 dev_warn(wm8350
->dev
,
413 "Failed to request AUXADC IRQ: %d\n", ret
);
416 if (pdata
&& pdata
->init
) {
417 ret
= pdata
->init(wm8350
);
419 dev_err(wm8350
->dev
, "Platform init() failed: %d\n",
425 wm8350_reg_write(wm8350
, WM8350_SYSTEM_INTERRUPTS_MASK
, 0x0);
427 wm8350_client_dev_register(wm8350
, "wm8350-codec",
428 &(wm8350
->codec
.pdev
));
429 wm8350_client_dev_register(wm8350
, "wm8350-gpio",
430 &(wm8350
->gpio
.pdev
));
431 wm8350_client_dev_register(wm8350
, "wm8350-hwmon",
432 &(wm8350
->hwmon
.pdev
));
433 wm8350_client_dev_register(wm8350
, "wm8350-power",
434 &(wm8350
->power
.pdev
));
435 wm8350_client_dev_register(wm8350
, "wm8350-rtc", &(wm8350
->rtc
.pdev
));
436 wm8350_client_dev_register(wm8350
, "wm8350-wdt", &(wm8350
->wdt
.pdev
));
441 wm8350_irq_exit(wm8350
);
445 EXPORT_SYMBOL_GPL(wm8350_device_init
);