1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * wm8350-core.c -- Device access for Wolfson WM8350
5 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
7 * Author: Liam Girdwood, Mark Brown
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/bug.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/regmap.h>
19 #include <linux/workqueue.h>
21 #include <linux/mfd/wm8350/core.h>
22 #include <linux/mfd/wm8350/audio.h>
23 #include <linux/mfd/wm8350/comparator.h>
24 #include <linux/mfd/wm8350/gpio.h>
25 #include <linux/mfd/wm8350/pmic.h>
26 #include <linux/mfd/wm8350/rtc.h>
27 #include <linux/mfd/wm8350/supply.h>
28 #include <linux/mfd/wm8350/wdt.h>
30 #define WM8350_CLOCK_CONTROL_1 0x28
31 #define WM8350_AIF_TEST 0x74
34 #define WM8350_BUS_DEBUG 0
36 #define dump(regs, src) do { \
40 for (i_ = 0; i_ < regs; i_++) \
41 printk(" 0x%4.4x", *src_++); \
45 #define dump(bytes, src)
48 #define WM8350_LOCK_DEBUG 0
50 #define ldbg(format, arg...) printk(format, ## arg)
52 #define ldbg(format, arg...)
58 static DEFINE_MUTEX(reg_lock_mutex
);
61 * Safe read, modify, write methods
63 int wm8350_clear_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
65 return regmap_update_bits(wm8350
->regmap
, reg
, mask
, 0);
67 EXPORT_SYMBOL_GPL(wm8350_clear_bits
);
69 int wm8350_set_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
71 return regmap_update_bits(wm8350
->regmap
, reg
, mask
, mask
);
73 EXPORT_SYMBOL_GPL(wm8350_set_bits
);
75 u16
wm8350_reg_read(struct wm8350
*wm8350
, int reg
)
80 err
= regmap_read(wm8350
->regmap
, reg
, &data
);
82 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
86 EXPORT_SYMBOL_GPL(wm8350_reg_read
);
88 int wm8350_reg_write(struct wm8350
*wm8350
, int reg
, u16 val
)
92 ret
= regmap_write(wm8350
->regmap
, reg
, val
);
95 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
98 EXPORT_SYMBOL_GPL(wm8350_reg_write
);
100 int wm8350_block_read(struct wm8350
*wm8350
, int start_reg
, int regs
,
105 err
= regmap_bulk_read(wm8350
->regmap
, start_reg
, dest
, regs
);
107 dev_err(wm8350
->dev
, "block read starting from R%d failed\n",
112 EXPORT_SYMBOL_GPL(wm8350_block_read
);
114 int wm8350_block_write(struct wm8350
*wm8350
, int start_reg
, int regs
,
119 ret
= regmap_bulk_write(wm8350
->regmap
, start_reg
, src
, regs
);
121 dev_err(wm8350
->dev
, "block write starting at R%d failed\n",
126 EXPORT_SYMBOL_GPL(wm8350_block_write
);
131 * The WM8350 has a hardware lock which can be used to prevent writes to
132 * some registers (generally those which can cause particularly serious
133 * problems if misused). This function enables that lock.
135 int wm8350_reg_lock(struct wm8350
*wm8350
)
139 mutex_lock(®_lock_mutex
);
143 ret
= wm8350_reg_write(wm8350
, WM8350_SECURITY
, WM8350_LOCK_KEY
);
145 dev_err(wm8350
->dev
, "lock failed\n");
147 wm8350
->unlocked
= false;
149 mutex_unlock(®_lock_mutex
);
153 EXPORT_SYMBOL_GPL(wm8350_reg_lock
);
156 * wm8350_reg_unlock()
158 * The WM8350 has a hardware lock which can be used to prevent writes to
159 * some registers (generally those which can cause particularly serious
160 * problems if misused). This function disables that lock so updates
161 * can be performed. For maximum safety this should be done only when
164 int wm8350_reg_unlock(struct wm8350
*wm8350
)
168 mutex_lock(®_lock_mutex
);
172 ret
= wm8350_reg_write(wm8350
, WM8350_SECURITY
, WM8350_UNLOCK_KEY
);
174 dev_err(wm8350
->dev
, "unlock failed\n");
176 wm8350
->unlocked
= true;
178 mutex_unlock(®_lock_mutex
);
182 EXPORT_SYMBOL_GPL(wm8350_reg_unlock
);
184 int wm8350_read_auxadc(struct wm8350
*wm8350
, int channel
, int scale
, int vref
)
188 if (channel
< WM8350_AUXADC_AUX1
|| channel
> WM8350_AUXADC_TEMP
)
190 if (channel
>= WM8350_AUXADC_USB
&& channel
<= WM8350_AUXADC_TEMP
191 && (scale
!= 0 || vref
!= 0))
194 mutex_lock(&wm8350
->auxadc_mutex
);
196 /* Turn on the ADC */
197 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
198 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
, reg
| WM8350_AUXADC_ENA
);
203 wm8350_reg_write(wm8350
, WM8350_AUX1_READBACK
+ channel
, reg
);
206 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
207 reg
|= 1 << channel
| WM8350_AUXADC_POLL
;
208 wm8350_reg_write(wm8350
, WM8350_DIGITISER_CONTROL_1
, reg
);
210 /* If a late IRQ left the completion signalled then consume
212 try_wait_for_completion(&wm8350
->auxadc_done
);
214 /* We ignore the result of the completion and just check for a
215 * conversion result, allowing us to soldier on if the IRQ
216 * infrastructure is not set up for the chip. */
217 wait_for_completion_timeout(&wm8350
->auxadc_done
, msecs_to_jiffies(5));
219 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
220 if (reg
& WM8350_AUXADC_POLL
)
221 dev_err(wm8350
->dev
, "adc chn %d read timeout\n", channel
);
223 result
= wm8350_reg_read(wm8350
,
224 WM8350_AUX1_READBACK
+ channel
);
226 /* Turn off the ADC */
227 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
228 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
,
229 reg
& ~WM8350_AUXADC_ENA
);
231 mutex_unlock(&wm8350
->auxadc_mutex
);
233 return result
& WM8350_AUXADC_DATA1_MASK
;
235 EXPORT_SYMBOL_GPL(wm8350_read_auxadc
);
237 static irqreturn_t
wm8350_auxadc_irq(int irq
, void *irq_data
)
239 struct wm8350
*wm8350
= irq_data
;
241 complete(&wm8350
->auxadc_done
);
247 * Register a client device. This is non-fatal since there is no need to
248 * fail the entire device init due to a single platform device failing.
250 static void wm8350_client_dev_register(struct wm8350
*wm8350
,
252 struct platform_device
**pdev
)
256 *pdev
= platform_device_alloc(name
, -1);
258 dev_err(wm8350
->dev
, "Failed to allocate %s\n", name
);
262 (*pdev
)->dev
.parent
= wm8350
->dev
;
263 platform_set_drvdata(*pdev
, wm8350
);
264 ret
= platform_device_add(*pdev
);
266 dev_err(wm8350
->dev
, "Failed to register %s: %d\n", name
, ret
);
267 platform_device_put(*pdev
);
272 int wm8350_device_init(struct wm8350
*wm8350
, int irq
,
273 struct wm8350_platform_data
*pdata
)
276 unsigned int id1
, id2
, mask_rev
;
277 unsigned int cust_id
, mode
, chip_rev
;
279 dev_set_drvdata(wm8350
->dev
, wm8350
);
281 /* get WM8350 revision and config mode */
282 ret
= regmap_read(wm8350
->regmap
, WM8350_RESET_ID
, &id1
);
284 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
288 ret
= regmap_read(wm8350
->regmap
, WM8350_ID
, &id2
);
290 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
294 ret
= regmap_read(wm8350
->regmap
, WM8350_REVISION
, &mask_rev
);
296 dev_err(wm8350
->dev
, "Failed to read revision: %d\n", ret
);
302 "Device with ID %x is not a WM8350\n", id1
);
307 mode
= (id2
& WM8350_CONF_STS_MASK
) >> 10;
308 cust_id
= id2
& WM8350_CUST_ID_MASK
;
309 chip_rev
= (id2
& WM8350_CHIP_REV_MASK
) >> 12;
310 dev_info(wm8350
->dev
,
311 "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
312 mode
, cust_id
, mask_rev
, chip_rev
);
315 dev_err(wm8350
->dev
, "Unsupported CUST_ID\n");
322 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
323 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
327 dev_info(wm8350
->dev
, "WM8350 Rev E\n");
330 dev_info(wm8350
->dev
, "WM8350 Rev F\n");
333 dev_info(wm8350
->dev
, "WM8350 Rev G\n");
334 wm8350
->power
.rev_g_coeff
= 1;
337 dev_info(wm8350
->dev
, "WM8350 Rev H\n");
338 wm8350
->power
.rev_g_coeff
= 1;
341 /* For safety we refuse to run on unknown hardware */
342 dev_err(wm8350
->dev
, "Unknown WM8350 CHIP_REV\n");
349 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_4
;
350 wm8350
->pmic
.max_isink
= WM8350_ISINK_A
;
354 dev_info(wm8350
->dev
, "WM8351 Rev A\n");
355 wm8350
->power
.rev_g_coeff
= 1;
359 dev_info(wm8350
->dev
, "WM8351 Rev B\n");
360 wm8350
->power
.rev_g_coeff
= 1;
364 dev_err(wm8350
->dev
, "Unknown WM8351 CHIP_REV\n");
371 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
372 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
376 dev_info(wm8350
->dev
, "WM8352 Rev A\n");
377 wm8350
->power
.rev_g_coeff
= 1;
381 dev_err(wm8350
->dev
, "Unknown WM8352 CHIP_REV\n");
388 dev_err(wm8350
->dev
, "Unknown MASK_REV\n");
393 mutex_init(&wm8350
->auxadc_mutex
);
394 init_completion(&wm8350
->auxadc_done
);
396 ret
= wm8350_irq_init(wm8350
, irq
, pdata
);
400 if (wm8350
->irq_base
) {
401 ret
= request_threaded_irq(wm8350
->irq_base
+
402 WM8350_IRQ_AUXADC_DATARDY
,
403 NULL
, wm8350_auxadc_irq
,
407 dev_warn(wm8350
->dev
,
408 "Failed to request AUXADC IRQ: %d\n", ret
);
411 if (pdata
&& pdata
->init
) {
412 ret
= pdata
->init(wm8350
);
414 dev_err(wm8350
->dev
, "Platform init() failed: %d\n",
420 wm8350_reg_write(wm8350
, WM8350_SYSTEM_INTERRUPTS_MASK
, 0x0);
422 wm8350_client_dev_register(wm8350
, "wm8350-codec",
423 &(wm8350
->codec
.pdev
));
424 wm8350_client_dev_register(wm8350
, "wm8350-gpio",
425 &(wm8350
->gpio
.pdev
));
426 wm8350_client_dev_register(wm8350
, "wm8350-hwmon",
427 &(wm8350
->hwmon
.pdev
));
428 wm8350_client_dev_register(wm8350
, "wm8350-power",
429 &(wm8350
->power
.pdev
));
430 wm8350_client_dev_register(wm8350
, "wm8350-rtc", &(wm8350
->rtc
.pdev
));
431 wm8350_client_dev_register(wm8350
, "wm8350-wdt", &(wm8350
->wdt
.pdev
));
436 wm8350_irq_exit(wm8350
);
440 EXPORT_SYMBOL_GPL(wm8350_device_init
);