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/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(io_mutex
);
64 static DEFINE_MUTEX(reg_lock_mutex
);
66 /* Perform a physical read from the device.
68 static int wm8350_phys_read(struct wm8350
*wm8350
, u8 reg
, int num_regs
,
72 int bytes
= num_regs
* 2;
74 dev_dbg(wm8350
->dev
, "volatile read\n");
75 ret
= regmap_raw_read(wm8350
->regmap
, reg
, dest
, bytes
);
77 for (i
= reg
; i
< reg
+ num_regs
; i
++) {
78 /* Cache is CPU endian */
79 dest
[i
- reg
] = be16_to_cpu(dest
[i
- reg
]);
81 /* Mask out non-readable bits */
82 dest
[i
- reg
] &= wm8350_reg_io_map
[i
].readable
;
90 static int wm8350_read(struct wm8350
*wm8350
, u8 reg
, int num_regs
, u16
*dest
)
93 int end
= reg
+ num_regs
;
95 int bytes
= num_regs
* 2;
97 if ((reg
+ num_regs
- 1) > WM8350_MAX_REGISTER
) {
98 dev_err(wm8350
->dev
, "invalid reg %x\n",
104 "%s R%d(0x%2.2x) %d regs\n", __func__
, reg
, reg
, num_regs
);
107 /* we can _safely_ read any register, but warn if read not supported */
108 for (i
= reg
; i
< end
; i
++) {
109 if (!wm8350_reg_io_map
[i
].readable
)
110 dev_warn(wm8350
->dev
,
111 "reg R%d is not readable\n", i
);
115 /* if any volatile registers are required, then read back all */
116 for (i
= reg
; i
< end
; i
++)
117 if (wm8350_reg_io_map
[i
].vol
)
118 return wm8350_phys_read(wm8350
, reg
, num_regs
, dest
);
120 /* no volatiles, then cache is good */
121 dev_dbg(wm8350
->dev
, "cache read\n");
122 memcpy(dest
, &wm8350
->reg_cache
[reg
], bytes
);
123 dump(num_regs
, dest
);
127 static inline int is_reg_locked(struct wm8350
*wm8350
, u8 reg
)
129 if (reg
== WM8350_SECURITY
||
130 wm8350
->reg_cache
[WM8350_SECURITY
] == WM8350_UNLOCK_KEY
)
133 if ((reg
>= WM8350_GPIO_FUNCTION_SELECT_1
&&
134 reg
<= WM8350_GPIO_FUNCTION_SELECT_4
) ||
135 (reg
>= WM8350_BATTERY_CHARGER_CONTROL_1
&&
136 reg
<= WM8350_BATTERY_CHARGER_CONTROL_3
))
141 static int wm8350_write(struct wm8350
*wm8350
, u8 reg
, int num_regs
, u16
*src
)
144 int end
= reg
+ num_regs
;
145 int bytes
= num_regs
* 2;
147 if ((reg
+ num_regs
- 1) > WM8350_MAX_REGISTER
) {
148 dev_err(wm8350
->dev
, "invalid reg %x\n",
153 /* it's generally not a good idea to write to RO or locked registers */
154 for (i
= reg
; i
< end
; i
++) {
155 if (!wm8350_reg_io_map
[i
].writable
) {
157 "attempted write to read only reg R%d\n", i
);
161 if (is_reg_locked(wm8350
, i
)) {
163 "attempted write to locked reg R%d\n", i
);
167 src
[i
- reg
] &= wm8350_reg_io_map
[i
].writable
;
169 wm8350
->reg_cache
[i
] =
170 (wm8350
->reg_cache
[i
] & ~wm8350_reg_io_map
[i
].writable
)
173 src
[i
- reg
] = cpu_to_be16(src
[i
- reg
]);
176 /* Actually write it out */
177 return regmap_raw_write(wm8350
->regmap
, reg
, src
, bytes
);
181 * Safe read, modify, write methods
183 int wm8350_clear_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
188 mutex_lock(&io_mutex
);
189 err
= wm8350_read(wm8350
, reg
, 1, &data
);
191 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
196 err
= wm8350_write(wm8350
, reg
, 1, &data
);
198 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
200 mutex_unlock(&io_mutex
);
203 EXPORT_SYMBOL_GPL(wm8350_clear_bits
);
205 int wm8350_set_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
210 mutex_lock(&io_mutex
);
211 err
= wm8350_read(wm8350
, reg
, 1, &data
);
213 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
218 err
= wm8350_write(wm8350
, reg
, 1, &data
);
220 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
222 mutex_unlock(&io_mutex
);
225 EXPORT_SYMBOL_GPL(wm8350_set_bits
);
227 u16
wm8350_reg_read(struct wm8350
*wm8350
, int reg
)
232 mutex_lock(&io_mutex
);
233 err
= wm8350_read(wm8350
, reg
, 1, &data
);
235 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
237 mutex_unlock(&io_mutex
);
240 EXPORT_SYMBOL_GPL(wm8350_reg_read
);
242 int wm8350_reg_write(struct wm8350
*wm8350
, int reg
, u16 val
)
247 mutex_lock(&io_mutex
);
248 ret
= wm8350_write(wm8350
, reg
, 1, &data
);
250 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
251 mutex_unlock(&io_mutex
);
254 EXPORT_SYMBOL_GPL(wm8350_reg_write
);
256 int wm8350_block_read(struct wm8350
*wm8350
, int start_reg
, int regs
,
261 mutex_lock(&io_mutex
);
262 err
= wm8350_read(wm8350
, start_reg
, regs
, dest
);
264 dev_err(wm8350
->dev
, "block read starting from R%d failed\n",
266 mutex_unlock(&io_mutex
);
269 EXPORT_SYMBOL_GPL(wm8350_block_read
);
271 int wm8350_block_write(struct wm8350
*wm8350
, int start_reg
, int regs
,
276 mutex_lock(&io_mutex
);
277 ret
= wm8350_write(wm8350
, start_reg
, regs
, src
);
279 dev_err(wm8350
->dev
, "block write starting at R%d failed\n",
281 mutex_unlock(&io_mutex
);
284 EXPORT_SYMBOL_GPL(wm8350_block_write
);
289 * The WM8350 has a hardware lock which can be used to prevent writes to
290 * some registers (generally those which can cause particularly serious
291 * problems if misused). This function enables that lock.
293 int wm8350_reg_lock(struct wm8350
*wm8350
)
297 mutex_lock(®_lock_mutex
);
301 ret
= wm8350_reg_write(wm8350
, WM8350_SECURITY
, WM8350_LOCK_KEY
);
303 dev_err(wm8350
->dev
, "lock failed\n");
305 wm8350
->unlocked
= false;
307 mutex_unlock(®_lock_mutex
);
311 EXPORT_SYMBOL_GPL(wm8350_reg_lock
);
314 * wm8350_reg_unlock()
316 * The WM8350 has a hardware lock which can be used to prevent writes to
317 * some registers (generally those which can cause particularly serious
318 * problems if misused). This function disables that lock so updates
319 * can be performed. For maximum safety this should be done only when
322 int wm8350_reg_unlock(struct wm8350
*wm8350
)
326 mutex_lock(®_lock_mutex
);
330 ret
= wm8350_reg_write(wm8350
, WM8350_SECURITY
, WM8350_UNLOCK_KEY
);
332 dev_err(wm8350
->dev
, "unlock failed\n");
334 wm8350
->unlocked
= true;
336 mutex_unlock(®_lock_mutex
);
340 EXPORT_SYMBOL_GPL(wm8350_reg_unlock
);
342 int wm8350_read_auxadc(struct wm8350
*wm8350
, int channel
, int scale
, int vref
)
346 if (channel
< WM8350_AUXADC_AUX1
|| channel
> WM8350_AUXADC_TEMP
)
348 if (channel
>= WM8350_AUXADC_USB
&& channel
<= WM8350_AUXADC_TEMP
349 && (scale
!= 0 || vref
!= 0))
352 mutex_lock(&wm8350
->auxadc_mutex
);
354 /* Turn on the ADC */
355 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
356 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
, reg
| WM8350_AUXADC_ENA
);
361 wm8350_reg_write(wm8350
, WM8350_AUX1_READBACK
+ channel
, reg
);
364 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
365 reg
|= 1 << channel
| WM8350_AUXADC_POLL
;
366 wm8350_reg_write(wm8350
, WM8350_DIGITISER_CONTROL_1
, reg
);
368 /* If a late IRQ left the completion signalled then consume
370 try_wait_for_completion(&wm8350
->auxadc_done
);
372 /* We ignore the result of the completion and just check for a
373 * conversion result, allowing us to soldier on if the IRQ
374 * infrastructure is not set up for the chip. */
375 wait_for_completion_timeout(&wm8350
->auxadc_done
, msecs_to_jiffies(5));
377 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
378 if (reg
& WM8350_AUXADC_POLL
)
379 dev_err(wm8350
->dev
, "adc chn %d read timeout\n", channel
);
381 result
= wm8350_reg_read(wm8350
,
382 WM8350_AUX1_READBACK
+ channel
);
384 /* Turn off the ADC */
385 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
386 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
,
387 reg
& ~WM8350_AUXADC_ENA
);
389 mutex_unlock(&wm8350
->auxadc_mutex
);
391 return result
& WM8350_AUXADC_DATA1_MASK
;
393 EXPORT_SYMBOL_GPL(wm8350_read_auxadc
);
395 static irqreturn_t
wm8350_auxadc_irq(int irq
, void *irq_data
)
397 struct wm8350
*wm8350
= irq_data
;
399 complete(&wm8350
->auxadc_done
);
405 * Cache is always host endian.
407 static int wm8350_create_cache(struct wm8350
*wm8350
, int type
, int mode
)
416 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_0
418 reg_map
= wm8350_mode0_defaults
;
421 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_1
423 reg_map
= wm8350_mode1_defaults
;
426 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_2
428 reg_map
= wm8350_mode2_defaults
;
431 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_3
433 reg_map
= wm8350_mode3_defaults
;
438 "WM8350 configuration mode %d not supported\n",
446 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_0
448 reg_map
= wm8351_mode0_defaults
;
451 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_1
453 reg_map
= wm8351_mode1_defaults
;
456 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_2
458 reg_map
= wm8351_mode2_defaults
;
461 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_3
463 reg_map
= wm8351_mode3_defaults
;
468 "WM8351 configuration mode %d not supported\n",
476 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_0
478 reg_map
= wm8352_mode0_defaults
;
481 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_1
483 reg_map
= wm8352_mode1_defaults
;
486 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_2
488 reg_map
= wm8352_mode2_defaults
;
491 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_3
493 reg_map
= wm8352_mode3_defaults
;
498 "WM8352 configuration mode %d not supported\n",
506 "WM835x configuration mode %d not supported\n",
512 kmalloc(sizeof(u16
) * (WM8350_MAX_REGISTER
+ 1), GFP_KERNEL
);
513 if (wm8350
->reg_cache
== NULL
)
516 /* Read the initial cache state back from the device - this is
517 * a PMIC so the device many not be in a virgin state and we
518 * can't rely on the silicon values.
520 ret
= regmap_raw_read(wm8350
->regmap
, 0, wm8350
->reg_cache
,
521 sizeof(u16
) * (WM8350_MAX_REGISTER
+ 1));
524 "failed to read initial cache values\n");
528 /* Mask out uncacheable/unreadable bits and the audio. */
529 for (i
= 0; i
< WM8350_MAX_REGISTER
; i
++) {
530 if (wm8350_reg_io_map
[i
].readable
&&
531 (i
< WM8350_CLOCK_CONTROL_1
|| i
> WM8350_AIF_TEST
)) {
532 value
= be16_to_cpu(wm8350
->reg_cache
[i
]);
533 value
&= wm8350_reg_io_map
[i
].readable
;
534 wm8350
->reg_cache
[i
] = value
;
536 wm8350
->reg_cache
[i
] = reg_map
[i
];
540 kfree(wm8350
->reg_cache
);
545 * Register a client device. This is non-fatal since there is no need to
546 * fail the entire device init due to a single platform device failing.
548 static void wm8350_client_dev_register(struct wm8350
*wm8350
,
550 struct platform_device
**pdev
)
554 *pdev
= platform_device_alloc(name
, -1);
556 dev_err(wm8350
->dev
, "Failed to allocate %s\n", name
);
560 (*pdev
)->dev
.parent
= wm8350
->dev
;
561 platform_set_drvdata(*pdev
, wm8350
);
562 ret
= platform_device_add(*pdev
);
564 dev_err(wm8350
->dev
, "Failed to register %s: %d\n", name
, ret
);
565 platform_device_put(*pdev
);
570 int wm8350_device_init(struct wm8350
*wm8350
, int irq
,
571 struct wm8350_platform_data
*pdata
)
574 unsigned int id1
, id2
, mask_rev
;
575 unsigned int cust_id
, mode
, chip_rev
;
577 dev_set_drvdata(wm8350
->dev
, wm8350
);
579 /* get WM8350 revision and config mode */
580 ret
= regmap_read(wm8350
->regmap
, WM8350_RESET_ID
, &id1
);
582 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
586 ret
= regmap_read(wm8350
->regmap
, WM8350_ID
, &id2
);
588 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
592 ret
= regmap_read(wm8350
->regmap
, WM8350_REVISION
, &mask_rev
);
594 dev_err(wm8350
->dev
, "Failed to read revision: %d\n", ret
);
600 "Device with ID %x is not a WM8350\n", id1
);
605 mode
= id2
& WM8350_CONF_STS_MASK
>> 10;
606 cust_id
= id2
& WM8350_CUST_ID_MASK
;
607 chip_rev
= (id2
& WM8350_CHIP_REV_MASK
) >> 12;
608 dev_info(wm8350
->dev
,
609 "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
610 mode
, cust_id
, mask_rev
, chip_rev
);
613 dev_err(wm8350
->dev
, "Unsupported CUST_ID\n");
620 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
621 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
625 dev_info(wm8350
->dev
, "WM8350 Rev E\n");
628 dev_info(wm8350
->dev
, "WM8350 Rev F\n");
631 dev_info(wm8350
->dev
, "WM8350 Rev G\n");
632 wm8350
->power
.rev_g_coeff
= 1;
635 dev_info(wm8350
->dev
, "WM8350 Rev H\n");
636 wm8350
->power
.rev_g_coeff
= 1;
639 /* For safety we refuse to run on unknown hardware */
640 dev_err(wm8350
->dev
, "Unknown WM8350 CHIP_REV\n");
647 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_4
;
648 wm8350
->pmic
.max_isink
= WM8350_ISINK_A
;
652 dev_info(wm8350
->dev
, "WM8351 Rev A\n");
653 wm8350
->power
.rev_g_coeff
= 1;
657 dev_info(wm8350
->dev
, "WM8351 Rev B\n");
658 wm8350
->power
.rev_g_coeff
= 1;
662 dev_err(wm8350
->dev
, "Unknown WM8351 CHIP_REV\n");
669 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
670 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
674 dev_info(wm8350
->dev
, "WM8352 Rev A\n");
675 wm8350
->power
.rev_g_coeff
= 1;
679 dev_err(wm8350
->dev
, "Unknown WM8352 CHIP_REV\n");
686 dev_err(wm8350
->dev
, "Unknown MASK_REV\n");
691 ret
= wm8350_create_cache(wm8350
, mask_rev
, mode
);
693 dev_err(wm8350
->dev
, "Failed to create register cache\n");
697 mutex_init(&wm8350
->auxadc_mutex
);
698 init_completion(&wm8350
->auxadc_done
);
700 ret
= wm8350_irq_init(wm8350
, irq
, pdata
);
704 if (wm8350
->irq_base
) {
705 ret
= request_threaded_irq(wm8350
->irq_base
+
706 WM8350_IRQ_AUXADC_DATARDY
,
707 NULL
, wm8350_auxadc_irq
, 0,
710 dev_warn(wm8350
->dev
,
711 "Failed to request AUXADC IRQ: %d\n", ret
);
714 if (pdata
&& pdata
->init
) {
715 ret
= pdata
->init(wm8350
);
717 dev_err(wm8350
->dev
, "Platform init() failed: %d\n",
723 wm8350_reg_write(wm8350
, WM8350_SYSTEM_INTERRUPTS_MASK
, 0x0);
725 wm8350_client_dev_register(wm8350
, "wm8350-codec",
726 &(wm8350
->codec
.pdev
));
727 wm8350_client_dev_register(wm8350
, "wm8350-gpio",
728 &(wm8350
->gpio
.pdev
));
729 wm8350_client_dev_register(wm8350
, "wm8350-hwmon",
730 &(wm8350
->hwmon
.pdev
));
731 wm8350_client_dev_register(wm8350
, "wm8350-power",
732 &(wm8350
->power
.pdev
));
733 wm8350_client_dev_register(wm8350
, "wm8350-rtc", &(wm8350
->rtc
.pdev
));
734 wm8350_client_dev_register(wm8350
, "wm8350-wdt", &(wm8350
->wdt
.pdev
));
739 wm8350_irq_exit(wm8350
);
741 kfree(wm8350
->reg_cache
);
745 EXPORT_SYMBOL_GPL(wm8350_device_init
);
747 void wm8350_device_exit(struct wm8350
*wm8350
)
751 for (i
= 0; i
< ARRAY_SIZE(wm8350
->pmic
.led
); i
++)
752 platform_device_unregister(wm8350
->pmic
.led
[i
].pdev
);
754 for (i
= 0; i
< ARRAY_SIZE(wm8350
->pmic
.pdev
); i
++)
755 platform_device_unregister(wm8350
->pmic
.pdev
[i
]);
757 platform_device_unregister(wm8350
->wdt
.pdev
);
758 platform_device_unregister(wm8350
->rtc
.pdev
);
759 platform_device_unregister(wm8350
->power
.pdev
);
760 platform_device_unregister(wm8350
->hwmon
.pdev
);
761 platform_device_unregister(wm8350
->gpio
.pdev
);
762 platform_device_unregister(wm8350
->codec
.pdev
);
764 if (wm8350
->irq_base
)
765 free_irq(wm8350
->irq_base
+ WM8350_IRQ_AUXADC_DATARDY
, wm8350
);
767 wm8350_irq_exit(wm8350
);
769 kfree(wm8350
->reg_cache
);
771 EXPORT_SYMBOL_GPL(wm8350_device_exit
);
773 MODULE_DESCRIPTION("WM8350 AudioPlus PMIC core driver");
774 MODULE_LICENSE("GPL");