2 * wm8994-core.c -- Device access for Wolfson WM8994
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
21 #include <linux/mfd/core.h>
23 #include <linux/of_device.h>
24 #include <linux/of_gpio.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regmap.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/regulator/machine.h>
30 #include <linux/mfd/wm8994/core.h>
31 #include <linux/mfd/wm8994/pdata.h>
32 #include <linux/mfd/wm8994/registers.h>
37 * wm8994_reg_read: Read a single WM8994 register.
39 * @wm8994: Device to read from.
40 * @reg: Register to read.
42 int wm8994_reg_read(struct wm8994
*wm8994
, unsigned short reg
)
47 ret
= regmap_read(wm8994
->regmap
, reg
, &val
);
54 EXPORT_SYMBOL_GPL(wm8994_reg_read
);
57 * wm8994_bulk_read: Read multiple WM8994 registers
59 * @wm8994: Device to read from
60 * @reg: First register
61 * @count: Number of registers
62 * @buf: Buffer to fill. The data will be returned big endian.
64 int wm8994_bulk_read(struct wm8994
*wm8994
, unsigned short reg
,
67 return regmap_bulk_read(wm8994
->regmap
, reg
, buf
, count
);
71 * wm8994_reg_write: Write a single WM8994 register.
73 * @wm8994: Device to write to.
74 * @reg: Register to write to.
75 * @val: Value to write.
77 int wm8994_reg_write(struct wm8994
*wm8994
, unsigned short reg
,
80 return regmap_write(wm8994
->regmap
, reg
, val
);
82 EXPORT_SYMBOL_GPL(wm8994_reg_write
);
85 * wm8994_bulk_write: Write multiple WM8994 registers
87 * @wm8994: Device to write to
88 * @reg: First register
89 * @count: Number of registers
90 * @buf: Buffer to write from. Data must be big-endian formatted.
92 int wm8994_bulk_write(struct wm8994
*wm8994
, unsigned short reg
,
93 int count
, const u16
*buf
)
95 return regmap_raw_write(wm8994
->regmap
, reg
, buf
, count
* sizeof(u16
));
97 EXPORT_SYMBOL_GPL(wm8994_bulk_write
);
100 * wm8994_set_bits: Set the value of a bitfield in a WM8994 register
102 * @wm8994: Device to write to.
103 * @reg: Register to write to.
104 * @mask: Mask of bits to set.
105 * @val: Value to set (unshifted)
107 int wm8994_set_bits(struct wm8994
*wm8994
, unsigned short reg
,
108 unsigned short mask
, unsigned short val
)
110 return regmap_update_bits(wm8994
->regmap
, reg
, mask
, val
);
112 EXPORT_SYMBOL_GPL(wm8994_set_bits
);
114 static struct mfd_cell wm8994_regulator_devs
[] = {
116 .name
= "wm8994-ldo",
118 .pm_runtime_no_callbacks
= true,
121 .name
= "wm8994-ldo",
123 .pm_runtime_no_callbacks
= true,
127 static struct resource wm8994_codec_resources
[] = {
129 .start
= WM8994_IRQ_TEMP_SHUT
,
130 .end
= WM8994_IRQ_TEMP_WARN
,
131 .flags
= IORESOURCE_IRQ
,
135 static struct resource wm8994_gpio_resources
[] = {
137 .start
= WM8994_IRQ_GPIO(1),
138 .end
= WM8994_IRQ_GPIO(11),
139 .flags
= IORESOURCE_IRQ
,
143 static struct mfd_cell wm8994_devs
[] = {
145 .name
= "wm8994-codec",
146 .num_resources
= ARRAY_SIZE(wm8994_codec_resources
),
147 .resources
= wm8994_codec_resources
,
151 .name
= "wm8994-gpio",
152 .num_resources
= ARRAY_SIZE(wm8994_gpio_resources
),
153 .resources
= wm8994_gpio_resources
,
154 .pm_runtime_no_callbacks
= true,
159 * Supplies for the main bulk of CODEC; the LDO supplies are ignored
160 * and should be handled via the standard regulator API supply
163 static const char *wm1811_main_supplies
[] = {
175 static const char *wm8994_main_supplies
[] = {
185 static const char *wm8958_main_supplies
[] = {
197 #ifdef CONFIG_PM_RUNTIME
198 static int wm8994_suspend(struct device
*dev
)
200 struct wm8994
*wm8994
= dev_get_drvdata(dev
);
203 /* Don't actually go through with the suspend if the CODEC is
204 * still active for accessory detect. */
205 switch (wm8994
->type
) {
208 ret
= wm8994_reg_read(wm8994
, WM8958_MIC_DETECT_1
);
210 dev_err(dev
, "Failed to read power status: %d\n", ret
);
211 } else if (ret
& WM8958_MICD_ENA
) {
212 dev_dbg(dev
, "CODEC still active, ignoring suspend\n");
220 /* Disable LDO pulldowns while the device is suspended if we
221 * don't know that something will be driving them. */
222 if (!wm8994
->ldo_ena_always_driven
)
223 wm8994_set_bits(wm8994
, WM8994_PULL_CONTROL_2
,
224 WM8994_LDO1ENA_PD
| WM8994_LDO2ENA_PD
,
225 WM8994_LDO1ENA_PD
| WM8994_LDO2ENA_PD
);
227 /* Explicitly put the device into reset in case regulators
228 * don't get disabled in order to ensure consistent restart.
230 wm8994_reg_write(wm8994
, WM8994_SOFTWARE_RESET
,
231 wm8994_reg_read(wm8994
, WM8994_SOFTWARE_RESET
));
233 regcache_mark_dirty(wm8994
->regmap
);
235 /* Restore GPIO registers to prevent problems with mismatched
236 * pin configurations.
238 ret
= regcache_sync_region(wm8994
->regmap
, WM8994_GPIO_1
,
241 dev_err(dev
, "Failed to restore GPIO registers: %d\n", ret
);
243 /* In case one of the GPIOs is used as a wake input. */
244 ret
= regcache_sync_region(wm8994
->regmap
,
245 WM8994_INTERRUPT_STATUS_1_MASK
,
246 WM8994_INTERRUPT_STATUS_1_MASK
);
248 dev_err(dev
, "Failed to restore interrupt mask: %d\n", ret
);
250 regcache_cache_only(wm8994
->regmap
, true);
251 wm8994
->suspended
= true;
253 ret
= regulator_bulk_disable(wm8994
->num_supplies
,
256 dev_err(dev
, "Failed to disable supplies: %d\n", ret
);
263 static int wm8994_resume(struct device
*dev
)
265 struct wm8994
*wm8994
= dev_get_drvdata(dev
);
268 /* We may have lied to the PM core about suspending */
269 if (!wm8994
->suspended
)
272 ret
= regulator_bulk_enable(wm8994
->num_supplies
,
275 dev_err(dev
, "Failed to enable supplies: %d\n", ret
);
279 regcache_cache_only(wm8994
->regmap
, false);
280 ret
= regcache_sync(wm8994
->regmap
);
282 dev_err(dev
, "Failed to restore register map: %d\n", ret
);
286 /* Disable LDO pulldowns while the device is active */
287 wm8994_set_bits(wm8994
, WM8994_PULL_CONTROL_2
,
288 WM8994_LDO1ENA_PD
| WM8994_LDO2ENA_PD
,
291 wm8994
->suspended
= false;
296 regulator_bulk_disable(wm8994
->num_supplies
, wm8994
->supplies
);
302 #ifdef CONFIG_REGULATOR
303 static int wm8994_ldo_in_use(struct wm8994_pdata
*pdata
, int ldo
)
305 struct wm8994_ldo_pdata
*ldo_pdata
;
310 ldo_pdata
= &pdata
->ldo
[ldo
];
312 if (!ldo_pdata
->init_data
)
315 return ldo_pdata
->init_data
->num_consumer_supplies
!= 0;
318 static int wm8994_ldo_in_use(struct wm8994_pdata
*pdata
, int ldo
)
324 static const struct reg_default wm8994_revc_patch
[] = {
331 static const struct reg_default wm8958_reva_patch
[] = {
338 static const struct reg_default wm1811_reva_patch
[] = {
347 static int wm8994_set_pdata_from_of(struct wm8994
*wm8994
)
349 struct device_node
*np
= wm8994
->dev
->of_node
;
350 struct wm8994_pdata
*pdata
= &wm8994
->pdata
;
356 if (of_property_read_u32_array(np
, "wlf,gpio-cfg", pdata
->gpio_defaults
,
357 ARRAY_SIZE(pdata
->gpio_defaults
)) >= 0) {
358 for (i
= 0; i
< ARRAY_SIZE(pdata
->gpio_defaults
); i
++) {
359 if (wm8994
->pdata
.gpio_defaults
[i
] == 0)
360 pdata
->gpio_defaults
[i
]
361 = WM8994_CONFIGURE_GPIO
;
365 of_property_read_u32_array(np
, "wlf,micbias-cfg", pdata
->micbias
,
366 ARRAY_SIZE(pdata
->micbias
));
368 pdata
->lineout1_diff
= true;
369 pdata
->lineout2_diff
= true;
370 if (of_find_property(np
, "wlf,lineout1-se", NULL
))
371 pdata
->lineout1_diff
= false;
372 if (of_find_property(np
, "wlf,lineout2-se", NULL
))
373 pdata
->lineout2_diff
= false;
375 if (of_find_property(np
, "wlf,lineout1-feedback", NULL
))
376 pdata
->lineout1fb
= true;
377 if (of_find_property(np
, "wlf,lineout2-feedback", NULL
))
378 pdata
->lineout2fb
= true;
380 if (of_find_property(np
, "wlf,ldoena-always-driven", NULL
))
381 pdata
->lineout2fb
= true;
383 pdata
->ldo
[0].enable
= of_get_named_gpio(np
, "wlf,ldo1ena", 0);
384 if (pdata
->ldo
[0].enable
< 0)
385 pdata
->ldo
[0].enable
= 0;
387 pdata
->ldo
[1].enable
= of_get_named_gpio(np
, "wlf,ldo2ena", 0);
388 if (pdata
->ldo
[1].enable
< 0)
389 pdata
->ldo
[1].enable
= 0;
394 static int wm8994_set_pdata_from_of(struct wm8994
*wm8994
)
401 * Instantiate the generic non-control parts of the device.
403 static int wm8994_device_init(struct wm8994
*wm8994
, int irq
)
405 struct wm8994_pdata
*pdata
;
406 struct regmap_config
*regmap_config
;
407 const struct reg_default
*regmap_patch
= NULL
;
409 int ret
, i
, patch_regs
= 0;
412 if (dev_get_platdata(wm8994
->dev
)) {
413 pdata
= dev_get_platdata(wm8994
->dev
);
414 wm8994
->pdata
= *pdata
;
416 pdata
= &wm8994
->pdata
;
418 ret
= wm8994_set_pdata_from_of(wm8994
);
422 dev_set_drvdata(wm8994
->dev
, wm8994
);
424 /* Add the on-chip regulators first for bootstrapping */
425 ret
= mfd_add_devices(wm8994
->dev
, -1,
426 wm8994_regulator_devs
,
427 ARRAY_SIZE(wm8994_regulator_devs
),
430 dev_err(wm8994
->dev
, "Failed to add children: %d\n", ret
);
434 switch (wm8994
->type
) {
436 wm8994
->num_supplies
= ARRAY_SIZE(wm1811_main_supplies
);
439 wm8994
->num_supplies
= ARRAY_SIZE(wm8994_main_supplies
);
442 wm8994
->num_supplies
= ARRAY_SIZE(wm8958_main_supplies
);
449 wm8994
->supplies
= devm_kzalloc(wm8994
->dev
,
450 sizeof(struct regulator_bulk_data
) *
451 wm8994
->num_supplies
, GFP_KERNEL
);
452 if (!wm8994
->supplies
) {
457 switch (wm8994
->type
) {
459 for (i
= 0; i
< ARRAY_SIZE(wm1811_main_supplies
); i
++)
460 wm8994
->supplies
[i
].supply
= wm1811_main_supplies
[i
];
463 for (i
= 0; i
< ARRAY_SIZE(wm8994_main_supplies
); i
++)
464 wm8994
->supplies
[i
].supply
= wm8994_main_supplies
[i
];
467 for (i
= 0; i
< ARRAY_SIZE(wm8958_main_supplies
); i
++)
468 wm8994
->supplies
[i
].supply
= wm8958_main_supplies
[i
];
475 ret
= devm_regulator_bulk_get(wm8994
->dev
, wm8994
->num_supplies
,
478 dev_err(wm8994
->dev
, "Failed to get supplies: %d\n", ret
);
482 ret
= regulator_bulk_enable(wm8994
->num_supplies
,
485 dev_err(wm8994
->dev
, "Failed to enable supplies: %d\n", ret
);
489 ret
= wm8994_reg_read(wm8994
, WM8994_SOFTWARE_RESET
);
491 dev_err(wm8994
->dev
, "Failed to read ID register\n");
497 if (wm8994
->type
!= WM1811
)
498 dev_warn(wm8994
->dev
, "Device registered as type %d\n",
500 wm8994
->type
= WM1811
;
504 if (wm8994
->type
!= WM8994
)
505 dev_warn(wm8994
->dev
, "Device registered as type %d\n",
507 wm8994
->type
= WM8994
;
511 if (wm8994
->type
!= WM8958
)
512 dev_warn(wm8994
->dev
, "Device registered as type %d\n",
514 wm8994
->type
= WM8958
;
517 dev_err(wm8994
->dev
, "Device is not a WM8994, ID is %x\n",
523 ret
= wm8994_reg_read(wm8994
, WM8994_CHIP_REVISION
);
525 dev_err(wm8994
->dev
, "Failed to read revision register: %d\n",
529 wm8994
->revision
= ret
& WM8994_CHIP_REV_MASK
;
530 wm8994
->cust_id
= (ret
& WM8994_CUST_ID_MASK
) >> WM8994_CUST_ID_SHIFT
;
532 switch (wm8994
->type
) {
534 switch (wm8994
->revision
) {
537 dev_warn(wm8994
->dev
,
538 "revision %c not fully supported\n",
539 'A' + wm8994
->revision
);
544 regmap_patch
= wm8994_revc_patch
;
545 patch_regs
= ARRAY_SIZE(wm8994_revc_patch
);
551 switch (wm8994
->revision
) {
553 regmap_patch
= wm8958_reva_patch
;
554 patch_regs
= ARRAY_SIZE(wm8958_reva_patch
);
562 /* Revision C did not change the relevant layer */
563 if (wm8994
->revision
> 1)
566 regmap_patch
= wm1811_reva_patch
;
567 patch_regs
= ARRAY_SIZE(wm1811_reva_patch
);
574 dev_info(wm8994
->dev
, "%s revision %c CUST_ID %02x\n", devname
,
575 'A' + wm8994
->revision
, wm8994
->cust_id
);
577 switch (wm8994
->type
) {
579 regmap_config
= &wm1811_regmap_config
;
582 regmap_config
= &wm8994_regmap_config
;
585 regmap_config
= &wm8958_regmap_config
;
588 dev_err(wm8994
->dev
, "Unknown device type %d\n", wm8994
->type
);
592 ret
= regmap_reinit_cache(wm8994
->regmap
, regmap_config
);
594 dev_err(wm8994
->dev
, "Failed to reinit register cache: %d\n",
599 /* Explicitly put the device into reset in case regulators
600 * don't get disabled in order to ensure we know the device
603 ret
= wm8994_reg_write(wm8994
, WM8994_SOFTWARE_RESET
,
604 wm8994_reg_read(wm8994
, WM8994_SOFTWARE_RESET
));
606 dev_err(wm8994
->dev
, "Failed to reset device: %d\n", ret
);
611 ret
= regmap_register_patch(wm8994
->regmap
, regmap_patch
,
614 dev_err(wm8994
->dev
, "Failed to register patch: %d\n",
620 wm8994
->irq_base
= pdata
->irq_base
;
621 wm8994
->gpio_base
= pdata
->gpio_base
;
623 /* GPIO configuration is only applied if it's non-zero */
624 for (i
= 0; i
< ARRAY_SIZE(pdata
->gpio_defaults
); i
++) {
625 if (pdata
->gpio_defaults
[i
]) {
626 wm8994_set_bits(wm8994
, WM8994_GPIO_1
+ i
,
627 0xffff, pdata
->gpio_defaults
[i
]);
631 wm8994
->ldo_ena_always_driven
= pdata
->ldo_ena_always_driven
;
633 if (pdata
->spkmode_pu
)
634 pulls
|= WM8994_SPKMODE_PU
;
636 /* Disable unneeded pulls */
637 wm8994_set_bits(wm8994
, WM8994_PULL_CONTROL_2
,
638 WM8994_LDO1ENA_PD
| WM8994_LDO2ENA_PD
|
639 WM8994_SPKMODE_PU
| WM8994_CSNADDR_PD
,
642 /* In some system designs where the regulators are not in use,
643 * we can achieve a small reduction in leakage currents by
644 * floating LDO outputs. This bit makes no difference if the
645 * LDOs are enabled, it only affects cases where the LDOs were
646 * in operation and are then disabled.
648 for (i
= 0; i
< WM8994_NUM_LDO_REGS
; i
++) {
649 if (wm8994_ldo_in_use(pdata
, i
))
650 wm8994_set_bits(wm8994
, WM8994_LDO_1
+ i
,
651 WM8994_LDO1_DISCH
, WM8994_LDO1_DISCH
);
653 wm8994_set_bits(wm8994
, WM8994_LDO_1
+ i
,
654 WM8994_LDO1_DISCH
, 0);
657 wm8994_irq_init(wm8994
);
659 ret
= mfd_add_devices(wm8994
->dev
, -1,
660 wm8994_devs
, ARRAY_SIZE(wm8994_devs
),
663 dev_err(wm8994
->dev
, "Failed to add children: %d\n", ret
);
667 pm_runtime_enable(wm8994
->dev
);
668 pm_runtime_idle(wm8994
->dev
);
673 wm8994_irq_exit(wm8994
);
675 regulator_bulk_disable(wm8994
->num_supplies
,
678 mfd_remove_devices(wm8994
->dev
);
682 static void wm8994_device_exit(struct wm8994
*wm8994
)
684 pm_runtime_disable(wm8994
->dev
);
685 mfd_remove_devices(wm8994
->dev
);
686 wm8994_irq_exit(wm8994
);
687 regulator_bulk_disable(wm8994
->num_supplies
,
691 static const struct of_device_id wm8994_of_match
[] = {
692 { .compatible
= "wlf,wm1811", .data
= (void *)WM1811
},
693 { .compatible
= "wlf,wm8994", .data
= (void *)WM8994
},
694 { .compatible
= "wlf,wm8958", .data
= (void *)WM8958
},
697 MODULE_DEVICE_TABLE(of
, wm8994_of_match
);
699 static int wm8994_i2c_probe(struct i2c_client
*i2c
,
700 const struct i2c_device_id
*id
)
702 const struct of_device_id
*of_id
;
703 struct wm8994
*wm8994
;
706 wm8994
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8994
), GFP_KERNEL
);
710 i2c_set_clientdata(i2c
, wm8994
);
711 wm8994
->dev
= &i2c
->dev
;
712 wm8994
->irq
= i2c
->irq
;
714 if (i2c
->dev
.of_node
) {
715 of_id
= of_match_device(wm8994_of_match
, &i2c
->dev
);
717 wm8994
->type
= (int)of_id
->data
;
719 wm8994
->type
= id
->driver_data
;
722 wm8994
->regmap
= devm_regmap_init_i2c(i2c
, &wm8994_base_regmap_config
);
723 if (IS_ERR(wm8994
->regmap
)) {
724 ret
= PTR_ERR(wm8994
->regmap
);
725 dev_err(wm8994
->dev
, "Failed to allocate register map: %d\n",
730 return wm8994_device_init(wm8994
, i2c
->irq
);
733 static int wm8994_i2c_remove(struct i2c_client
*i2c
)
735 struct wm8994
*wm8994
= i2c_get_clientdata(i2c
);
737 wm8994_device_exit(wm8994
);
742 static const struct i2c_device_id wm8994_i2c_id
[] = {
743 { "wm1811", WM1811
},
744 { "wm1811a", WM1811
},
745 { "wm8994", WM8994
},
746 { "wm8958", WM8958
},
749 MODULE_DEVICE_TABLE(i2c
, wm8994_i2c_id
);
751 static const struct dev_pm_ops wm8994_pm_ops
= {
752 SET_RUNTIME_PM_OPS(wm8994_suspend
, wm8994_resume
, NULL
)
755 static struct i2c_driver wm8994_i2c_driver
= {
758 .owner
= THIS_MODULE
,
759 .pm
= &wm8994_pm_ops
,
760 .of_match_table
= of_match_ptr(wm8994_of_match
),
762 .probe
= wm8994_i2c_probe
,
763 .remove
= wm8994_i2c_remove
,
764 .id_table
= wm8994_i2c_id
,
767 module_i2c_driver(wm8994_i2c_driver
);
769 MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
770 MODULE_LICENSE("GPL");
771 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");