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/bug.h>
19 #include <linux/device.h>
20 #include <linux/delay.h>
21 #include <linux/interrupt.h>
22 #include <linux/workqueue.h>
24 #include <linux/mfd/wm8350/core.h>
25 #include <linux/mfd/wm8350/audio.h>
26 #include <linux/mfd/wm8350/comparator.h>
27 #include <linux/mfd/wm8350/gpio.h>
28 #include <linux/mfd/wm8350/pmic.h>
29 #include <linux/mfd/wm8350/rtc.h>
30 #include <linux/mfd/wm8350/supply.h>
31 #include <linux/mfd/wm8350/wdt.h>
33 #define WM8350_UNLOCK_KEY 0x0013
34 #define WM8350_LOCK_KEY 0x0000
36 #define WM8350_CLOCK_CONTROL_1 0x28
37 #define WM8350_AIF_TEST 0x74
40 #define WM8350_BUS_DEBUG 0
42 #define dump(regs, src) do { \
46 for (i_ = 0; i_ < regs; i_++) \
47 printk(" 0x%4.4x", *src_++); \
51 #define dump(bytes, src)
54 #define WM8350_LOCK_DEBUG 0
56 #define ldbg(format, arg...) printk(format, ## arg)
58 #define ldbg(format, arg...)
64 static DEFINE_MUTEX(io_mutex
);
65 static DEFINE_MUTEX(reg_lock_mutex
);
67 /* Perform a physical read from the device.
69 static int wm8350_phys_read(struct wm8350
*wm8350
, u8 reg
, int num_regs
,
73 int bytes
= num_regs
* 2;
75 dev_dbg(wm8350
->dev
, "volatile read\n");
76 ret
= wm8350
->read_dev(wm8350
, reg
, bytes
, (char *)dest
);
78 for (i
= reg
; i
< reg
+ num_regs
; i
++) {
79 /* Cache is CPU endian */
80 dest
[i
- reg
] = be16_to_cpu(dest
[i
- reg
]);
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_FUNCTION_SELECT_1
&&
138 reg
<= WM8350_GPIO_FUNCTION_SELECT_4
) ||
139 (reg
>= WM8350_BATTERY_CHARGER_CONTROL_1
&&
140 reg
<= WM8350_BATTERY_CHARGER_CONTROL_3
))
145 static int wm8350_write(struct wm8350
*wm8350
, u8 reg
, int num_regs
, u16
*src
)
148 int end
= reg
+ num_regs
;
149 int bytes
= num_regs
* 2;
151 if (wm8350
->write_dev
== NULL
)
154 if ((reg
+ num_regs
- 1) > WM8350_MAX_REGISTER
) {
155 dev_err(wm8350
->dev
, "invalid reg %x\n",
160 /* it's generally not a good idea to write to RO or locked registers */
161 for (i
= reg
; i
< end
; i
++) {
162 if (!wm8350_reg_io_map
[i
].writable
) {
164 "attempted write to read only reg R%d\n", i
);
168 if (is_reg_locked(wm8350
, i
)) {
170 "attempted write to locked reg R%d\n", i
);
174 src
[i
- reg
] &= wm8350_reg_io_map
[i
].writable
;
176 wm8350
->reg_cache
[i
] =
177 (wm8350
->reg_cache
[i
] & ~wm8350_reg_io_map
[i
].writable
)
180 src
[i
- reg
] = cpu_to_be16(src
[i
- reg
]);
183 /* Actually write it out */
184 return wm8350
->write_dev(wm8350
, reg
, bytes
, (char *)src
);
188 * Safe read, modify, write methods
190 int wm8350_clear_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
195 mutex_lock(&io_mutex
);
196 err
= wm8350_read(wm8350
, reg
, 1, &data
);
198 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
203 err
= wm8350_write(wm8350
, reg
, 1, &data
);
205 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
207 mutex_unlock(&io_mutex
);
210 EXPORT_SYMBOL_GPL(wm8350_clear_bits
);
212 int wm8350_set_bits(struct wm8350
*wm8350
, u16 reg
, u16 mask
)
217 mutex_lock(&io_mutex
);
218 err
= wm8350_read(wm8350
, reg
, 1, &data
);
220 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
225 err
= wm8350_write(wm8350
, reg
, 1, &data
);
227 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
229 mutex_unlock(&io_mutex
);
232 EXPORT_SYMBOL_GPL(wm8350_set_bits
);
234 u16
wm8350_reg_read(struct wm8350
*wm8350
, int reg
)
239 mutex_lock(&io_mutex
);
240 err
= wm8350_read(wm8350
, reg
, 1, &data
);
242 dev_err(wm8350
->dev
, "read from reg R%d failed\n", reg
);
244 mutex_unlock(&io_mutex
);
247 EXPORT_SYMBOL_GPL(wm8350_reg_read
);
249 int wm8350_reg_write(struct wm8350
*wm8350
, int reg
, u16 val
)
254 mutex_lock(&io_mutex
);
255 ret
= wm8350_write(wm8350
, reg
, 1, &data
);
257 dev_err(wm8350
->dev
, "write to reg R%d failed\n", reg
);
258 mutex_unlock(&io_mutex
);
261 EXPORT_SYMBOL_GPL(wm8350_reg_write
);
263 int wm8350_block_read(struct wm8350
*wm8350
, int start_reg
, int regs
,
268 mutex_lock(&io_mutex
);
269 err
= wm8350_read(wm8350
, start_reg
, regs
, dest
);
271 dev_err(wm8350
->dev
, "block read starting from R%d failed\n",
273 mutex_unlock(&io_mutex
);
276 EXPORT_SYMBOL_GPL(wm8350_block_read
);
278 int wm8350_block_write(struct wm8350
*wm8350
, int start_reg
, int regs
,
283 mutex_lock(&io_mutex
);
284 ret
= wm8350_write(wm8350
, start_reg
, regs
, src
);
286 dev_err(wm8350
->dev
, "block write starting at R%d failed\n",
288 mutex_unlock(&io_mutex
);
291 EXPORT_SYMBOL_GPL(wm8350_block_write
);
296 * The WM8350 has a hardware lock which can be used to prevent writes to
297 * some registers (generally those which can cause particularly serious
298 * problems if misused). This function enables that lock.
300 int wm8350_reg_lock(struct wm8350
*wm8350
)
302 u16 key
= WM8350_LOCK_KEY
;
306 mutex_lock(&io_mutex
);
307 ret
= wm8350_write(wm8350
, WM8350_SECURITY
, 1, &key
);
309 dev_err(wm8350
->dev
, "lock failed\n");
310 mutex_unlock(&io_mutex
);
313 EXPORT_SYMBOL_GPL(wm8350_reg_lock
);
316 * wm8350_reg_unlock()
318 * The WM8350 has a hardware lock which can be used to prevent writes to
319 * some registers (generally those which can cause particularly serious
320 * problems if misused). This function disables that lock so updates
321 * can be performed. For maximum safety this should be done only when
324 int wm8350_reg_unlock(struct wm8350
*wm8350
)
326 u16 key
= WM8350_UNLOCK_KEY
;
330 mutex_lock(&io_mutex
);
331 ret
= wm8350_write(wm8350
, WM8350_SECURITY
, 1, &key
);
333 dev_err(wm8350
->dev
, "unlock failed\n");
334 mutex_unlock(&io_mutex
);
337 EXPORT_SYMBOL_GPL(wm8350_reg_unlock
);
339 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
);
367 schedule_timeout_interruptible(1);
368 reg
= wm8350_reg_read(wm8350
, WM8350_DIGITISER_CONTROL_1
);
369 } while ((reg
& WM8350_AUXADC_POLL
) && --tries
);
372 dev_err(wm8350
->dev
, "adc chn %d read timeout\n", channel
);
374 result
= wm8350_reg_read(wm8350
,
375 WM8350_AUX1_READBACK
+ channel
);
377 /* Turn off the ADC */
378 reg
= wm8350_reg_read(wm8350
, WM8350_POWER_MGMT_5
);
379 wm8350_reg_write(wm8350
, WM8350_POWER_MGMT_5
,
380 reg
& ~WM8350_AUXADC_ENA
);
382 mutex_unlock(&wm8350
->auxadc_mutex
);
384 return result
& WM8350_AUXADC_DATA1_MASK
;
386 EXPORT_SYMBOL_GPL(wm8350_read_auxadc
);
389 * Cache is always host endian.
391 static int wm8350_create_cache(struct wm8350
*wm8350
, int type
, int mode
)
400 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_0
402 reg_map
= wm8350_mode0_defaults
;
405 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_1
407 reg_map
= wm8350_mode1_defaults
;
410 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_2
412 reg_map
= wm8350_mode2_defaults
;
415 #ifdef CONFIG_MFD_WM8350_CONFIG_MODE_3
417 reg_map
= wm8350_mode3_defaults
;
422 "WM8350 configuration mode %d not supported\n",
430 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_0
432 reg_map
= wm8351_mode0_defaults
;
435 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_1
437 reg_map
= wm8351_mode1_defaults
;
440 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_2
442 reg_map
= wm8351_mode2_defaults
;
445 #ifdef CONFIG_MFD_WM8351_CONFIG_MODE_3
447 reg_map
= wm8351_mode3_defaults
;
452 "WM8351 configuration mode %d not supported\n",
460 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_0
462 reg_map
= wm8352_mode0_defaults
;
465 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_1
467 reg_map
= wm8352_mode1_defaults
;
470 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_2
472 reg_map
= wm8352_mode2_defaults
;
475 #ifdef CONFIG_MFD_WM8352_CONFIG_MODE_3
477 reg_map
= wm8352_mode3_defaults
;
482 "WM8352 configuration mode %d not supported\n",
490 "WM835x configuration mode %d not supported\n",
496 kmalloc(sizeof(u16
) * (WM8350_MAX_REGISTER
+ 1), GFP_KERNEL
);
497 if (wm8350
->reg_cache
== NULL
)
500 /* Read the initial cache state back from the device - this is
501 * a PMIC so the device many not be in a virgin state and we
502 * can't rely on the silicon values.
504 ret
= wm8350
->read_dev(wm8350
, 0,
505 sizeof(u16
) * (WM8350_MAX_REGISTER
+ 1),
509 "failed to read initial cache values\n");
513 /* Mask out uncacheable/unreadable bits and the audio. */
514 for (i
= 0; i
< WM8350_MAX_REGISTER
; i
++) {
515 if (wm8350_reg_io_map
[i
].readable
&&
516 (i
< WM8350_CLOCK_CONTROL_1
|| i
> WM8350_AIF_TEST
)) {
517 value
= be16_to_cpu(wm8350
->reg_cache
[i
]);
518 value
&= wm8350_reg_io_map
[i
].readable
;
519 wm8350
->reg_cache
[i
] = value
;
521 wm8350
->reg_cache
[i
] = reg_map
[i
];
529 * Register a client device. This is non-fatal since there is no need to
530 * fail the entire device init due to a single platform device failing.
532 static void wm8350_client_dev_register(struct wm8350
*wm8350
,
534 struct platform_device
**pdev
)
538 *pdev
= platform_device_alloc(name
, -1);
540 dev_err(wm8350
->dev
, "Failed to allocate %s\n", name
);
544 (*pdev
)->dev
.parent
= wm8350
->dev
;
545 platform_set_drvdata(*pdev
, wm8350
);
546 ret
= platform_device_add(*pdev
);
548 dev_err(wm8350
->dev
, "Failed to register %s: %d\n", name
, ret
);
549 platform_device_put(*pdev
);
554 int wm8350_device_init(struct wm8350
*wm8350
, int irq
,
555 struct wm8350_platform_data
*pdata
)
558 u16 id1
, id2
, mask_rev
;
559 u16 cust_id
, mode
, chip_rev
;
561 /* get WM8350 revision and config mode */
562 ret
= wm8350
->read_dev(wm8350
, WM8350_RESET_ID
, sizeof(id1
), &id1
);
564 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
568 ret
= wm8350
->read_dev(wm8350
, WM8350_ID
, sizeof(id2
), &id2
);
570 dev_err(wm8350
->dev
, "Failed to read ID: %d\n", ret
);
574 ret
= wm8350
->read_dev(wm8350
, WM8350_REVISION
, sizeof(mask_rev
),
577 dev_err(wm8350
->dev
, "Failed to read revision: %d\n", ret
);
581 id1
= be16_to_cpu(id1
);
582 id2
= be16_to_cpu(id2
);
583 mask_rev
= be16_to_cpu(mask_rev
);
587 "Device with ID %x is not a WM8350\n", id1
);
592 mode
= id2
& WM8350_CONF_STS_MASK
>> 10;
593 cust_id
= id2
& WM8350_CUST_ID_MASK
;
594 chip_rev
= (id2
& WM8350_CHIP_REV_MASK
) >> 12;
595 dev_info(wm8350
->dev
,
596 "CONF_STS %d, CUST_ID %d, MASK_REV %d, CHIP_REV %d\n",
597 mode
, cust_id
, mask_rev
, chip_rev
);
600 dev_err(wm8350
->dev
, "Unsupported CUST_ID\n");
607 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
608 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
612 dev_info(wm8350
->dev
, "WM8350 Rev E\n");
615 dev_info(wm8350
->dev
, "WM8350 Rev F\n");
618 dev_info(wm8350
->dev
, "WM8350 Rev G\n");
619 wm8350
->power
.rev_g_coeff
= 1;
622 dev_info(wm8350
->dev
, "WM8350 Rev H\n");
623 wm8350
->power
.rev_g_coeff
= 1;
626 /* For safety we refuse to run on unknown hardware */
627 dev_err(wm8350
->dev
, "Unknown WM8350 CHIP_REV\n");
634 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_4
;
635 wm8350
->pmic
.max_isink
= WM8350_ISINK_A
;
639 dev_info(wm8350
->dev
, "WM8351 Rev A\n");
640 wm8350
->power
.rev_g_coeff
= 1;
644 dev_info(wm8350
->dev
, "WM8351 Rev B\n");
645 wm8350
->power
.rev_g_coeff
= 1;
649 dev_err(wm8350
->dev
, "Unknown WM8351 CHIP_REV\n");
656 wm8350
->pmic
.max_dcdc
= WM8350_DCDC_6
;
657 wm8350
->pmic
.max_isink
= WM8350_ISINK_B
;
661 dev_info(wm8350
->dev
, "WM8352 Rev A\n");
662 wm8350
->power
.rev_g_coeff
= 1;
666 dev_err(wm8350
->dev
, "Unknown WM8352 CHIP_REV\n");
673 dev_err(wm8350
->dev
, "Unknown MASK_REV\n");
678 ret
= wm8350_create_cache(wm8350
, mask_rev
, mode
);
680 dev_err(wm8350
->dev
, "Failed to create register cache\n");
684 mutex_init(&wm8350
->auxadc_mutex
);
686 ret
= wm8350_irq_init(wm8350
, irq
, pdata
);
690 if (pdata
&& pdata
->init
) {
691 ret
= pdata
->init(wm8350
);
693 dev_err(wm8350
->dev
, "Platform init() failed: %d\n",
699 wm8350_reg_write(wm8350
, WM8350_SYSTEM_INTERRUPTS_MASK
, 0x0);
701 wm8350_client_dev_register(wm8350
, "wm8350-codec",
702 &(wm8350
->codec
.pdev
));
703 wm8350_client_dev_register(wm8350
, "wm8350-gpio",
704 &(wm8350
->gpio
.pdev
));
705 wm8350_client_dev_register(wm8350
, "wm8350-hwmon",
706 &(wm8350
->hwmon
.pdev
));
707 wm8350_client_dev_register(wm8350
, "wm8350-power",
708 &(wm8350
->power
.pdev
));
709 wm8350_client_dev_register(wm8350
, "wm8350-rtc", &(wm8350
->rtc
.pdev
));
710 wm8350_client_dev_register(wm8350
, "wm8350-wdt", &(wm8350
->wdt
.pdev
));
715 wm8350_irq_exit(wm8350
);
717 kfree(wm8350
->reg_cache
);
720 EXPORT_SYMBOL_GPL(wm8350_device_init
);
722 void wm8350_device_exit(struct wm8350
*wm8350
)
726 for (i
= 0; i
< ARRAY_SIZE(wm8350
->pmic
.led
); i
++)
727 platform_device_unregister(wm8350
->pmic
.led
[i
].pdev
);
729 for (i
= 0; i
< ARRAY_SIZE(wm8350
->pmic
.pdev
); i
++)
730 platform_device_unregister(wm8350
->pmic
.pdev
[i
]);
732 platform_device_unregister(wm8350
->wdt
.pdev
);
733 platform_device_unregister(wm8350
->rtc
.pdev
);
734 platform_device_unregister(wm8350
->power
.pdev
);
735 platform_device_unregister(wm8350
->hwmon
.pdev
);
736 platform_device_unregister(wm8350
->gpio
.pdev
);
737 platform_device_unregister(wm8350
->codec
.pdev
);
739 wm8350_irq_exit(wm8350
);
741 kfree(wm8350
->reg_cache
);
743 EXPORT_SYMBOL_GPL(wm8350_device_exit
);
745 MODULE_DESCRIPTION("WM8350 AudioPlus PMIC core driver");
746 MODULE_LICENSE("GPL");