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/workqueue.h>
25 #include <linux/mfd/wm8350/core.h>
26 #include <linux/mfd/wm8350/audio.h>
27 #include <linux/mfd/wm8350/comparator.h>
28 #include <linux/mfd/wm8350/gpio.h>
29 #include <linux/mfd/wm8350/pmic.h>
30 #include <linux/mfd/wm8350/rtc.h>
31 #include <linux/mfd/wm8350/supply.h>
32 #include <linux/mfd/wm8350/wdt.h>
34 #define WM8350_UNLOCK_KEY 0x0013
35 #define WM8350_LOCK_KEY 0x0000
37 #define WM8350_CLOCK_CONTROL_1 0x28
38 #define WM8350_AIF_TEST 0x74
41 #define WM8350_BUS_DEBUG 0
43 #define dump(regs, src) do { \
47 for (i_ = 0; i_ < regs; i_++) \
48 printk(" 0x%4.4x", *src_++); \
52 #define dump(bytes, src)
55 #define WM8350_LOCK_DEBUG 0
57 #define ldbg(format, arg...) printk(format, ## arg)
59 #define ldbg(format, arg...)
65 static DEFINE_MUTEX(io_mutex
);
66 static DEFINE_MUTEX(reg_lock_mutex
);
68 /* Perform a physical read from the device.
70 static int wm8350_phys_read(struct wm8350
*wm8350
, u8 reg
, int num_regs
,
74 int bytes
= num_regs
* 2;
76 dev_dbg(wm8350
->dev
, "volatile read\n");
77 ret
= wm8350
->read_dev(wm8350
, reg
, bytes
, (char *)dest
);
79 for (i
= reg
; i
< reg
+ num_regs
; i
++) {
80 /* Cache is CPU endian */
81 dest
[i
- reg
] = be16_to_cpu(dest
[i
- reg
]);
83 /* Mask out non-readable bits */
84 dest
[i
- reg
] &= wm8350_reg_io_map
[i
].readable
;
92 static int wm8350_read(struct wm8350
*wm8350
, u8 reg
, int num_regs
, u16
*dest
)
95 int end
= reg
+ num_regs
;
97 int bytes
= num_regs
* 2;
99 if (wm8350
->read_dev
== NULL
)
102 if ((reg
+ num_regs
- 1) > WM8350_MAX_REGISTER
) {
103 dev_err(wm8350
->dev
, "invalid reg %x\n",
109 "%s R%d(0x%2.2x) %d regs\n", __func__
, reg
, reg
, num_regs
);
112 /* we can _safely_ read any register, but warn if read not supported */
113 for (i
= reg
; i
< end
; i
++) {
114 if (!wm8350_reg_io_map
[i
].readable
)
115 dev_warn(wm8350
->dev
,
116 "reg R%d is not readable\n", i
);
120 /* if any volatile registers are required, then read back all */
121 for (i
= reg
; i
< end
; i
++)
122 if (wm8350_reg_io_map
[i
].vol
)
123 return wm8350_phys_read(wm8350
, reg
, num_regs
, dest
);
125 /* no volatiles, then cache is good */
126 dev_dbg(wm8350
->dev
, "cache read\n");
127 memcpy(dest
, &wm8350
->reg_cache
[reg
], bytes
);
128 dump(num_regs
, dest
);
132 static inline int is_reg_locked(struct wm8350
*wm8350
, u8 reg
)
134 if (reg
== WM8350_SECURITY
||
135 wm8350
->reg_cache
[WM8350_SECURITY
] == WM8350_UNLOCK_KEY
)
138 if ((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
);
297 * The WM8350 has a hardware lock which can be used to prevent writes to
298 * some registers (generally those which can cause particularly serious
299 * problems if misused). This function enables that lock.
301 int wm8350_reg_lock(struct wm8350
*wm8350
)
303 u16 key
= WM8350_LOCK_KEY
;
307 mutex_lock(&io_mutex
);
308 ret
= wm8350_write(wm8350
, WM8350_SECURITY
, 1, &key
);
310 dev_err(wm8350
->dev
, "lock failed\n");
311 mutex_unlock(&io_mutex
);
314 EXPORT_SYMBOL_GPL(wm8350_reg_lock
);
317 * wm8350_reg_unlock()
319 * The WM8350 has a hardware lock which can be used to prevent writes to
320 * some registers (generally those which can cause particularly serious
321 * problems if misused). This function disables that lock so updates
322 * can be performed. For maximum safety this should be done only when
325 int wm8350_reg_unlock(struct wm8350
*wm8350
)
327 u16 key
= WM8350_UNLOCK_KEY
;
331 mutex_lock(&io_mutex
);
332 ret
= wm8350_write(wm8350
, WM8350_SECURITY
, 1, &key
);
334 dev_err(wm8350
->dev
, "unlock failed\n");
335 mutex_unlock(&io_mutex
);
338 EXPORT_SYMBOL_GPL(wm8350_reg_unlock
);
340 int wm8350_read_auxadc(struct wm8350
*wm8350
, int channel
, int scale
, int vref
)
344 if (channel
< WM8350_AUXADC_AUX1
|| channel
> WM8350_AUXADC_TEMP
)
346 if (channel
>= WM8350_AUXADC_USB
&& channel
<= WM8350_AUXADC_TEMP
347 && (scale
!= 0 || vref
!= 0))
350 mutex_lock(&wm8350
->auxadc_mutex
);
352 /* Turn on the ADC */
353 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
354 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
, reg
| WM8350_AUXADC_ENA
);
359 wm8350_reg_write(wm8350
, WM8350_AUX1_READBACK
+ channel
, reg
);
362 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
363 reg
|= 1 << channel
| WM8350_AUXADC_POLL
;
364 wm8350_reg_write(wm8350
, WM8350_DIGITISER_CONTROL_1
, reg
);
366 /* If a late IRQ left the completion signalled then consume
368 try_wait_for_completion(&wm8350
->auxadc_done
);
370 /* We ignore the result of the completion and just check for a
371 * conversion result, allowing us to soldier on if the IRQ
372 * infrastructure is not set up for the chip. */
373 wait_for_completion_timeout(&wm8350
->auxadc_done
, msecs_to_jiffies(5));
375 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
376 if (reg
& WM8350_AUXADC_POLL
)
377 dev_err(wm8350
->dev
, "adc chn %d read timeout\n", channel
);
379 result
= wm8350_reg_read(wm8350
,
380 WM8350_AUX1_READBACK
+ channel
);
382 /* Turn off the ADC */
383 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
384 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
,
385 reg
& ~WM8350_AUXADC_ENA
);
387 mutex_unlock(&wm8350
->auxadc_mutex
);
389 return result
& WM8350_AUXADC_DATA1_MASK
;
391 EXPORT_SYMBOL_GPL(wm8350_read_auxadc
);
393 static irqreturn_t
wm8350_auxadc_irq(int irq
, void *irq_data
)
395 struct wm8350
*wm8350
= irq_data
;
397 complete(&wm8350
->auxadc_done
);
403 * Cache is always host endian.
405 static int wm8350_create_cache(struct wm8350
*wm8350
, int type
, int mode
)
414 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_0
416 reg_map
= wm8350_mode0_defaults
;
419 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_1
421 reg_map
= wm8350_mode1_defaults
;
424 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_2
426 reg_map
= wm8350_mode2_defaults
;
429 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_3
431 reg_map
= wm8350_mode3_defaults
;
436 "WM8350 configuration mode %d not supported\n",
444 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_0
446 reg_map
= wm8351_mode0_defaults
;
449 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_1
451 reg_map
= wm8351_mode1_defaults
;
454 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_2
456 reg_map
= wm8351_mode2_defaults
;
459 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_3
461 reg_map
= wm8351_mode3_defaults
;
466 "WM8351 configuration mode %d not supported\n",
474 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_0
476 reg_map
= wm8352_mode0_defaults
;
479 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_1
481 reg_map
= wm8352_mode1_defaults
;
484 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_2
486 reg_map
= wm8352_mode2_defaults
;
489 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_3
491 reg_map
= wm8352_mode3_defaults
;
496 "WM8352 configuration mode %d not supported\n",
504 "WM835x configuration mode %d not supported\n",
510 kmalloc(sizeof(u16
) * (WM8350_MAX_REGISTER
+ 1), GFP_KERNEL
);
511 if (wm8350
->reg_cache
== NULL
)
514 /* Read the initial cache state back from the device - this is
515 * a PMIC so the device many not be in a virgin state and we
516 * can't rely on the silicon values.
518 ret
= wm8350
->read_dev(wm8350
, 0,
519 sizeof(u16
) * (WM8350_MAX_REGISTER
+ 1),
523 "failed to read initial cache values\n");
527 /* Mask out uncacheable/unreadable bits and the audio. */
528 for (i
= 0; i
< WM8350_MAX_REGISTER
; i
++) {
529 if (wm8350_reg_io_map
[i
].readable
&&
530 (i
< WM8350_CLOCK_CONTROL_1
|| i
> WM8350_AIF_TEST
)) {
531 value
= be16_to_cpu(wm8350
->reg_cache
[i
]);
532 value
&= wm8350_reg_io_map
[i
].readable
;
533 wm8350
->reg_cache
[i
] = value
;
535 wm8350
->reg_cache
[i
] = reg_map
[i
];
539 kfree(wm8350
->reg_cache
);
544 * Register a client device. This is non-fatal since there is no need to
545 * fail the entire device init due to a single platform device failing.
547 static void wm8350_client_dev_register(struct wm8350
*wm8350
,
549 struct platform_device
**pdev
)
553 *pdev
= platform_device_alloc(name
, -1);
555 dev_err(wm8350
->dev
, "Failed to allocate %s\n", name
);
559 (*pdev
)->dev
.parent
= wm8350
->dev
;
560 platform_set_drvdata(*pdev
, wm8350
);
561 ret
= platform_device_add(*pdev
);
563 dev_err(wm8350
->dev
, "Failed to register %s: %d\n", name
, ret
);
564 platform_device_put(*pdev
);
569 int wm8350_device_init(struct wm8350
*wm8350
, int irq
,
570 struct wm8350_platform_data
*pdata
)
573 u16 id1
, id2
, mask_rev
;
574 u16 cust_id
, mode
, chip_rev
;
576 dev_set_drvdata(wm8350
->dev
, wm8350
);
578 /* get WM8350 revision and config mode */
579 ret
= wm8350
->read_dev(wm8350
, WM8350_RESET_ID
, sizeof(id1
), &id1
);
581 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
585 ret
= wm8350
->read_dev(wm8350
, WM8350_ID
, sizeof(id2
), &id2
);
587 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
591 ret
= wm8350
->read_dev(wm8350
, WM8350_REVISION
, sizeof(mask_rev
),
594 dev_err(wm8350
->dev
, "Failed to read revision: %d\n", ret
);
598 id1
= be16_to_cpu(id1
);
599 id2
= be16_to_cpu(id2
);
600 mask_rev
= be16_to_cpu(mask_rev
);
604 "Device with ID %x is not a WM8350\n", id1
);
609 mode
= id2
& WM8350_CONF_STS_MASK
>> 10;
610 cust_id
= id2
& WM8350_CUST_ID_MASK
;
611 chip_rev
= (id2
& WM8350_CHIP_REV_MASK
) >> 12;
612 dev_info(wm8350
->dev
,
613 "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
614 mode
, cust_id
, mask_rev
, chip_rev
);
617 dev_err(wm8350
->dev
, "Unsupported CUST_ID\n");
624 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
625 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
629 dev_info(wm8350
->dev
, "WM8350 Rev E\n");
632 dev_info(wm8350
->dev
, "WM8350 Rev F\n");
635 dev_info(wm8350
->dev
, "WM8350 Rev G\n");
636 wm8350
->power
.rev_g_coeff
= 1;
639 dev_info(wm8350
->dev
, "WM8350 Rev H\n");
640 wm8350
->power
.rev_g_coeff
= 1;
643 /* For safety we refuse to run on unknown hardware */
644 dev_err(wm8350
->dev
, "Unknown WM8350 CHIP_REV\n");
651 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_4
;
652 wm8350
->pmic
.max_isink
= WM8350_ISINK_A
;
656 dev_info(wm8350
->dev
, "WM8351 Rev A\n");
657 wm8350
->power
.rev_g_coeff
= 1;
661 dev_info(wm8350
->dev
, "WM8351 Rev B\n");
662 wm8350
->power
.rev_g_coeff
= 1;
666 dev_err(wm8350
->dev
, "Unknown WM8351 CHIP_REV\n");
673 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
674 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
678 dev_info(wm8350
->dev
, "WM8352 Rev A\n");
679 wm8350
->power
.rev_g_coeff
= 1;
683 dev_err(wm8350
->dev
, "Unknown WM8352 CHIP_REV\n");
690 dev_err(wm8350
->dev
, "Unknown MASK_REV\n");
695 ret
= wm8350_create_cache(wm8350
, mask_rev
, mode
);
697 dev_err(wm8350
->dev
, "Failed to create register cache\n");
701 mutex_init(&wm8350
->auxadc_mutex
);
702 init_completion(&wm8350
->auxadc_done
);
704 ret
= wm8350_irq_init(wm8350
, irq
, pdata
);
708 if (wm8350
->irq_base
) {
709 ret
= request_threaded_irq(wm8350
->irq_base
+
710 WM8350_IRQ_AUXADC_DATARDY
,
711 NULL
, wm8350_auxadc_irq
, 0,
714 dev_warn(wm8350
->dev
,
715 "Failed to request AUXADC IRQ: %d\n", ret
);
718 if (pdata
&& pdata
->init
) {
719 ret
= pdata
->init(wm8350
);
721 dev_err(wm8350
->dev
, "Platform init() failed: %d\n",
727 wm8350_reg_write(wm8350
, WM8350_SYSTEM_INTERRUPTS_MASK
, 0x0);
729 wm8350_client_dev_register(wm8350
, "wm8350-codec",
730 &(wm8350
->codec
.pdev
));
731 wm8350_client_dev_register(wm8350
, "wm8350-gpio",
732 &(wm8350
->gpio
.pdev
));
733 wm8350_client_dev_register(wm8350
, "wm8350-hwmon",
734 &(wm8350
->hwmon
.pdev
));
735 wm8350_client_dev_register(wm8350
, "wm8350-power",
736 &(wm8350
->power
.pdev
));
737 wm8350_client_dev_register(wm8350
, "wm8350-rtc", &(wm8350
->rtc
.pdev
));
738 wm8350_client_dev_register(wm8350
, "wm8350-wdt", &(wm8350
->wdt
.pdev
));
743 wm8350_irq_exit(wm8350
);
745 kfree(wm8350
->reg_cache
);
749 EXPORT_SYMBOL_GPL(wm8350_device_init
);
751 void wm8350_device_exit(struct wm8350
*wm8350
)
755 for (i
= 0; i
< ARRAY_SIZE(wm8350
->pmic
.led
); i
++)
756 platform_device_unregister(wm8350
->pmic
.led
[i
].pdev
);
758 for (i
= 0; i
< ARRAY_SIZE(wm8350
->pmic
.pdev
); i
++)
759 platform_device_unregister(wm8350
->pmic
.pdev
[i
]);
761 platform_device_unregister(wm8350
->wdt
.pdev
);
762 platform_device_unregister(wm8350
->rtc
.pdev
);
763 platform_device_unregister(wm8350
->power
.pdev
);
764 platform_device_unregister(wm8350
->hwmon
.pdev
);
765 platform_device_unregister(wm8350
->gpio
.pdev
);
766 platform_device_unregister(wm8350
->codec
.pdev
);
768 if (wm8350
->irq_base
)
769 free_irq(wm8350
->irq_base
+ WM8350_IRQ_AUXADC_DATARDY
, wm8350
);
771 wm8350_irq_exit(wm8350
);
773 kfree(wm8350
->reg_cache
);
775 EXPORT_SYMBOL_GPL(wm8350_device_exit
);
777 MODULE_DESCRIPTION("WM8350 AudioPlus PMIC core driver");
778 MODULE_LICENSE("GPL");