2 * MFD core driver for Intel Broxton Whiskey Cove PMIC
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/module.h>
17 #include <linux/acpi.h>
18 #include <linux/err.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mfd/intel_soc_pmic.h>
24 #include <linux/mfd/intel_soc_pmic_bxtwc.h>
25 #include <asm/intel_pmc_ipc.h>
27 /* PMIC device registers */
28 #define REG_ADDR_MASK 0xFF00
29 #define REG_ADDR_SHIFT 8
30 #define REG_OFFSET_MASK 0xFF
32 /* Interrupt Status Registers */
33 #define BXTWC_IRQLVL1 0x4E02
35 #define BXTWC_PWRBTNIRQ 0x4E03
36 #define BXTWC_THRM0IRQ 0x4E04
37 #define BXTWC_THRM1IRQ 0x4E05
38 #define BXTWC_THRM2IRQ 0x4E06
39 #define BXTWC_BCUIRQ 0x4E07
40 #define BXTWC_ADCIRQ 0x4E08
41 #define BXTWC_CHGR0IRQ 0x4E09
42 #define BXTWC_CHGR1IRQ 0x4E0A
43 #define BXTWC_GPIOIRQ0 0x4E0B
44 #define BXTWC_GPIOIRQ1 0x4E0C
45 #define BXTWC_CRITIRQ 0x4E0D
46 #define BXTWC_TMUIRQ 0x4FB6
48 /* Interrupt MASK Registers */
49 #define BXTWC_MIRQLVL1 0x4E0E
50 #define BXTWC_MIRQLVL1_MCHGR BIT(5)
52 #define BXTWC_MPWRBTNIRQ 0x4E0F
53 #define BXTWC_MTHRM0IRQ 0x4E12
54 #define BXTWC_MTHRM1IRQ 0x4E13
55 #define BXTWC_MTHRM2IRQ 0x4E14
56 #define BXTWC_MBCUIRQ 0x4E15
57 #define BXTWC_MADCIRQ 0x4E16
58 #define BXTWC_MCHGR0IRQ 0x4E17
59 #define BXTWC_MCHGR1IRQ 0x4E18
60 #define BXTWC_MGPIO0IRQ 0x4E19
61 #define BXTWC_MGPIO1IRQ 0x4E1A
62 #define BXTWC_MCRITIRQ 0x4E1B
63 #define BXTWC_MTMUIRQ 0x4FB7
65 /* Whiskey Cove PMIC share same ACPI ID between different platforms */
66 #define BROXTON_PMIC_WC_HRV 4
69 BXTWC_PWRBTN_LVL1_IRQ
= 0,
79 enum bxtwc_irqs_pwrbtn
{
92 enum bxtwc_irqs_chgr
{
102 enum bxtwc_irqs_crit
{
106 static const struct regmap_irq bxtwc_regmap_irqs
[] = {
107 REGMAP_IRQ_REG(BXTWC_PWRBTN_LVL1_IRQ
, 0, BIT(0)),
108 REGMAP_IRQ_REG(BXTWC_TMU_LVL1_IRQ
, 0, BIT(1)),
109 REGMAP_IRQ_REG(BXTWC_THRM_LVL1_IRQ
, 0, BIT(2)),
110 REGMAP_IRQ_REG(BXTWC_BCU_LVL1_IRQ
, 0, BIT(3)),
111 REGMAP_IRQ_REG(BXTWC_ADC_LVL1_IRQ
, 0, BIT(4)),
112 REGMAP_IRQ_REG(BXTWC_CHGR_LVL1_IRQ
, 0, BIT(5)),
113 REGMAP_IRQ_REG(BXTWC_GPIO_LVL1_IRQ
, 0, BIT(6)),
114 REGMAP_IRQ_REG(BXTWC_CRIT_LVL1_IRQ
, 0, BIT(7)),
117 static const struct regmap_irq bxtwc_regmap_irqs_pwrbtn
[] = {
118 REGMAP_IRQ_REG(BXTWC_PWRBTN_IRQ
, 0, 0x01),
121 static const struct regmap_irq bxtwc_regmap_irqs_bcu
[] = {
122 REGMAP_IRQ_REG(BXTWC_BCU_IRQ
, 0, 0x1f),
125 static const struct regmap_irq bxtwc_regmap_irqs_adc
[] = {
126 REGMAP_IRQ_REG(BXTWC_ADC_IRQ
, 0, 0xff),
129 static const struct regmap_irq bxtwc_regmap_irqs_chgr
[] = {
130 REGMAP_IRQ_REG(BXTWC_USBC_IRQ
, 0, 0x20),
131 REGMAP_IRQ_REG(BXTWC_CHGR0_IRQ
, 0, 0x1f),
132 REGMAP_IRQ_REG(BXTWC_CHGR1_IRQ
, 1, 0x1f),
135 static const struct regmap_irq bxtwc_regmap_irqs_tmu
[] = {
136 REGMAP_IRQ_REG(BXTWC_TMU_IRQ
, 0, 0x06),
139 static const struct regmap_irq bxtwc_regmap_irqs_crit
[] = {
140 REGMAP_IRQ_REG(BXTWC_CRIT_IRQ
, 0, 0x03),
143 static struct regmap_irq_chip bxtwc_regmap_irq_chip
= {
144 .name
= "bxtwc_irq_chip",
145 .status_base
= BXTWC_IRQLVL1
,
146 .mask_base
= BXTWC_MIRQLVL1
,
147 .irqs
= bxtwc_regmap_irqs
,
148 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs
),
152 static struct regmap_irq_chip bxtwc_regmap_irq_chip_pwrbtn
= {
153 .name
= "bxtwc_irq_chip_pwrbtn",
154 .status_base
= BXTWC_PWRBTNIRQ
,
155 .mask_base
= BXTWC_MPWRBTNIRQ
,
156 .irqs
= bxtwc_regmap_irqs_pwrbtn
,
157 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_pwrbtn
),
161 static struct regmap_irq_chip bxtwc_regmap_irq_chip_tmu
= {
162 .name
= "bxtwc_irq_chip_tmu",
163 .status_base
= BXTWC_TMUIRQ
,
164 .mask_base
= BXTWC_MTMUIRQ
,
165 .irqs
= bxtwc_regmap_irqs_tmu
,
166 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_tmu
),
170 static struct regmap_irq_chip bxtwc_regmap_irq_chip_bcu
= {
171 .name
= "bxtwc_irq_chip_bcu",
172 .status_base
= BXTWC_BCUIRQ
,
173 .mask_base
= BXTWC_MBCUIRQ
,
174 .irqs
= bxtwc_regmap_irqs_bcu
,
175 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_bcu
),
179 static struct regmap_irq_chip bxtwc_regmap_irq_chip_adc
= {
180 .name
= "bxtwc_irq_chip_adc",
181 .status_base
= BXTWC_ADCIRQ
,
182 .mask_base
= BXTWC_MADCIRQ
,
183 .irqs
= bxtwc_regmap_irqs_adc
,
184 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_adc
),
188 static struct regmap_irq_chip bxtwc_regmap_irq_chip_chgr
= {
189 .name
= "bxtwc_irq_chip_chgr",
190 .status_base
= BXTWC_CHGR0IRQ
,
191 .mask_base
= BXTWC_MCHGR0IRQ
,
192 .irqs
= bxtwc_regmap_irqs_chgr
,
193 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_chgr
),
197 static struct regmap_irq_chip bxtwc_regmap_irq_chip_crit
= {
198 .name
= "bxtwc_irq_chip_crit",
199 .status_base
= BXTWC_CRITIRQ
,
200 .mask_base
= BXTWC_MCRITIRQ
,
201 .irqs
= bxtwc_regmap_irqs_crit
,
202 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_crit
),
206 static struct resource gpio_resources
[] = {
207 DEFINE_RES_IRQ_NAMED(BXTWC_GPIO_LVL1_IRQ
, "GPIO"),
210 static struct resource adc_resources
[] = {
211 DEFINE_RES_IRQ_NAMED(BXTWC_ADC_IRQ
, "ADC"),
214 static struct resource usbc_resources
[] = {
215 DEFINE_RES_IRQ(BXTWC_USBC_IRQ
),
218 static struct resource charger_resources
[] = {
219 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR0_IRQ
, "CHARGER"),
220 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR1_IRQ
, "CHARGER1"),
223 static struct resource thermal_resources
[] = {
224 DEFINE_RES_IRQ(BXTWC_THRM_LVL1_IRQ
),
227 static struct resource bcu_resources
[] = {
228 DEFINE_RES_IRQ_NAMED(BXTWC_BCU_IRQ
, "BCU"),
231 static struct resource tmu_resources
[] = {
232 DEFINE_RES_IRQ_NAMED(BXTWC_TMU_IRQ
, "TMU"),
235 static struct mfd_cell bxt_wc_dev
[] = {
237 .name
= "bxt_wcove_gpadc",
238 .num_resources
= ARRAY_SIZE(adc_resources
),
239 .resources
= adc_resources
,
242 .name
= "bxt_wcove_thermal",
243 .num_resources
= ARRAY_SIZE(thermal_resources
),
244 .resources
= thermal_resources
,
247 .name
= "bxt_wcove_usbc",
248 .num_resources
= ARRAY_SIZE(usbc_resources
),
249 .resources
= usbc_resources
,
252 .name
= "bxt_wcove_ext_charger",
253 .num_resources
= ARRAY_SIZE(charger_resources
),
254 .resources
= charger_resources
,
257 .name
= "bxt_wcove_bcu",
258 .num_resources
= ARRAY_SIZE(bcu_resources
),
259 .resources
= bcu_resources
,
262 .name
= "bxt_wcove_tmu",
263 .num_resources
= ARRAY_SIZE(tmu_resources
),
264 .resources
= tmu_resources
,
268 .name
= "bxt_wcove_gpio",
269 .num_resources
= ARRAY_SIZE(gpio_resources
),
270 .resources
= gpio_resources
,
273 .name
= "bxt_wcove_region",
277 static int regmap_ipc_byte_reg_read(void *context
, unsigned int reg
,
284 struct intel_soc_pmic
*pmic
= context
;
289 if (reg
& REG_ADDR_MASK
)
290 i2c_addr
= (reg
& REG_ADDR_MASK
) >> REG_ADDR_SHIFT
;
292 i2c_addr
= BXTWC_DEVICE1_ADDR
;
294 reg
&= REG_OFFSET_MASK
;
297 ipc_in
[1] = i2c_addr
;
298 ret
= intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS
,
299 PMC_IPC_PMIC_ACCESS_READ
,
300 ipc_in
, sizeof(ipc_in
), (u32
*)ipc_out
, 1);
302 dev_err(pmic
->dev
, "Failed to read from PMIC\n");
310 static int regmap_ipc_byte_reg_write(void *context
, unsigned int reg
,
316 struct intel_soc_pmic
*pmic
= context
;
321 if (reg
& REG_ADDR_MASK
)
322 i2c_addr
= (reg
& REG_ADDR_MASK
) >> REG_ADDR_SHIFT
;
324 i2c_addr
= BXTWC_DEVICE1_ADDR
;
326 reg
&= REG_OFFSET_MASK
;
329 ipc_in
[1] = i2c_addr
;
331 ret
= intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS
,
332 PMC_IPC_PMIC_ACCESS_WRITE
,
333 ipc_in
, sizeof(ipc_in
), NULL
, 0);
335 dev_err(pmic
->dev
, "Failed to write to PMIC\n");
342 /* sysfs interfaces to r/w PMIC registers, required by initial script */
343 static unsigned long bxtwc_reg_addr
;
344 static ssize_t
bxtwc_reg_show(struct device
*dev
,
345 struct device_attribute
*attr
, char *buf
)
347 return sprintf(buf
, "0x%lx\n", bxtwc_reg_addr
);
350 static ssize_t
bxtwc_reg_store(struct device
*dev
,
351 struct device_attribute
*attr
, const char *buf
, size_t count
)
353 if (kstrtoul(buf
, 0, &bxtwc_reg_addr
)) {
354 dev_err(dev
, "Invalid register address\n");
357 return (ssize_t
)count
;
360 static ssize_t
bxtwc_val_show(struct device
*dev
,
361 struct device_attribute
*attr
, char *buf
)
365 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
367 ret
= regmap_read(pmic
->regmap
, bxtwc_reg_addr
, &val
);
369 dev_err(dev
, "Failed to read 0x%lx\n", bxtwc_reg_addr
);
373 return sprintf(buf
, "0x%02x\n", val
);
376 static ssize_t
bxtwc_val_store(struct device
*dev
,
377 struct device_attribute
*attr
, const char *buf
, size_t count
)
381 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
383 ret
= kstrtouint(buf
, 0, &val
);
387 ret
= regmap_write(pmic
->regmap
, bxtwc_reg_addr
, val
);
389 dev_err(dev
, "Failed to write value 0x%02x to address 0x%lx",
390 val
, bxtwc_reg_addr
);
396 static DEVICE_ATTR(addr
, S_IWUSR
| S_IRUSR
, bxtwc_reg_show
, bxtwc_reg_store
);
397 static DEVICE_ATTR(val
, S_IWUSR
| S_IRUSR
, bxtwc_val_show
, bxtwc_val_store
);
398 static struct attribute
*bxtwc_attrs
[] = {
404 static const struct attribute_group bxtwc_group
= {
405 .attrs
= bxtwc_attrs
,
408 static const struct regmap_config bxtwc_regmap_config
= {
411 .reg_write
= regmap_ipc_byte_reg_write
,
412 .reg_read
= regmap_ipc_byte_reg_read
,
415 static int bxtwc_add_chained_irq_chip(struct intel_soc_pmic
*pmic
,
416 struct regmap_irq_chip_data
*pdata
,
417 int pirq
, int irq_flags
,
418 const struct regmap_irq_chip
*chip
,
419 struct regmap_irq_chip_data
**data
)
423 irq
= regmap_irq_get_virq(pdata
, pirq
);
426 "Failed to get parent vIRQ(%d) for chip %s, ret:%d\n",
427 pirq
, chip
->name
, irq
);
431 return devm_regmap_add_irq_chip(pmic
->dev
, pmic
->regmap
, irq
, irq_flags
,
435 static int bxtwc_probe(struct platform_device
*pdev
)
440 unsigned long long hrv
;
441 struct intel_soc_pmic
*pmic
;
443 handle
= ACPI_HANDLE(&pdev
->dev
);
444 status
= acpi_evaluate_integer(handle
, "_HRV", NULL
, &hrv
);
445 if (ACPI_FAILURE(status
)) {
446 dev_err(&pdev
->dev
, "Failed to get PMIC hardware revision\n");
449 if (hrv
!= BROXTON_PMIC_WC_HRV
) {
450 dev_err(&pdev
->dev
, "Invalid PMIC hardware revision: %llu\n",
455 pmic
= devm_kzalloc(&pdev
->dev
, sizeof(*pmic
), GFP_KERNEL
);
459 ret
= platform_get_irq(pdev
, 0);
461 dev_err(&pdev
->dev
, "Invalid IRQ\n");
466 dev_set_drvdata(&pdev
->dev
, pmic
);
467 pmic
->dev
= &pdev
->dev
;
469 pmic
->regmap
= devm_regmap_init(&pdev
->dev
, NULL
, pmic
,
470 &bxtwc_regmap_config
);
471 if (IS_ERR(pmic
->regmap
)) {
472 ret
= PTR_ERR(pmic
->regmap
);
473 dev_err(&pdev
->dev
, "Failed to initialise regmap: %d\n", ret
);
477 ret
= devm_regmap_add_irq_chip(&pdev
->dev
, pmic
->regmap
, pmic
->irq
,
478 IRQF_ONESHOT
| IRQF_SHARED
,
479 0, &bxtwc_regmap_irq_chip
,
480 &pmic
->irq_chip_data
);
482 dev_err(&pdev
->dev
, "Failed to add IRQ chip\n");
486 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
487 BXTWC_PWRBTN_LVL1_IRQ
,
489 &bxtwc_regmap_irq_chip_pwrbtn
,
490 &pmic
->irq_chip_data_pwrbtn
);
492 dev_err(&pdev
->dev
, "Failed to add PWRBTN IRQ chip\n");
496 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
499 &bxtwc_regmap_irq_chip_tmu
,
500 &pmic
->irq_chip_data_tmu
);
502 dev_err(&pdev
->dev
, "Failed to add TMU IRQ chip\n");
506 /* Add chained IRQ handler for BCU IRQs */
507 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
510 &bxtwc_regmap_irq_chip_bcu
,
511 &pmic
->irq_chip_data_bcu
);
515 dev_err(&pdev
->dev
, "Failed to add BUC IRQ chip\n");
519 /* Add chained IRQ handler for ADC IRQs */
520 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
523 &bxtwc_regmap_irq_chip_adc
,
524 &pmic
->irq_chip_data_adc
);
528 dev_err(&pdev
->dev
, "Failed to add ADC IRQ chip\n");
532 /* Add chained IRQ handler for CHGR IRQs */
533 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
536 &bxtwc_regmap_irq_chip_chgr
,
537 &pmic
->irq_chip_data_chgr
);
541 dev_err(&pdev
->dev
, "Failed to add CHGR IRQ chip\n");
545 /* Add chained IRQ handler for CRIT IRQs */
546 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
549 &bxtwc_regmap_irq_chip_crit
,
550 &pmic
->irq_chip_data_crit
);
554 dev_err(&pdev
->dev
, "Failed to add CRIT IRQ chip\n");
558 ret
= devm_mfd_add_devices(&pdev
->dev
, PLATFORM_DEVID_NONE
, bxt_wc_dev
,
559 ARRAY_SIZE(bxt_wc_dev
), NULL
, 0, NULL
);
561 dev_err(&pdev
->dev
, "Failed to add devices\n");
565 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &bxtwc_group
);
567 dev_err(&pdev
->dev
, "Failed to create sysfs group %d\n", ret
);
572 * There is known hw bug. Upon reset BIT 5 of register
573 * BXTWC_CHGR_LVL1_IRQ is 0 which is the expected value. However,
574 * later it's set to 1(masked) automatically by hardware. So we
575 * have the software workaround here to unmaksed it in order to let
576 * charger interrutp work.
578 regmap_update_bits(pmic
->regmap
, BXTWC_MIRQLVL1
,
579 BXTWC_MIRQLVL1_MCHGR
, 0);
584 static int bxtwc_remove(struct platform_device
*pdev
)
586 sysfs_remove_group(&pdev
->dev
.kobj
, &bxtwc_group
);
591 static void bxtwc_shutdown(struct platform_device
*pdev
)
593 struct intel_soc_pmic
*pmic
= dev_get_drvdata(&pdev
->dev
);
595 disable_irq(pmic
->irq
);
598 #ifdef CONFIG_PM_SLEEP
599 static int bxtwc_suspend(struct device
*dev
)
601 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
603 disable_irq(pmic
->irq
);
608 static int bxtwc_resume(struct device
*dev
)
610 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
612 enable_irq(pmic
->irq
);
616 static SIMPLE_DEV_PM_OPS(bxtwc_pm_ops
, bxtwc_suspend
, bxtwc_resume
);
618 static const struct acpi_device_id bxtwc_acpi_ids
[] = {
622 MODULE_DEVICE_TABLE(acpi
, bxtwc_acpi_ids
);
624 static struct platform_driver bxtwc_driver
= {
625 .probe
= bxtwc_probe
,
626 .remove
= bxtwc_remove
,
627 .shutdown
= bxtwc_shutdown
,
629 .name
= "BXTWC PMIC",
631 .acpi_match_table
= ACPI_PTR(bxtwc_acpi_ids
),
635 module_platform_driver(bxtwc_driver
);
637 MODULE_LICENSE("GPL v2");
638 MODULE_AUTHOR("Qipeng Zha<qipeng.zha@intel.com>");