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
34 #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_MPWRTNIRQ 0x4E0F
52 #define BXTWC_MIRQLVL1_MCHGR BIT(5)
54 #define BXTWC_MTHRM0IRQ 0x4E12
55 #define BXTWC_MTHRM1IRQ 0x4E13
56 #define BXTWC_MTHRM2IRQ 0x4E14
57 #define BXTWC_MBCUIRQ 0x4E15
58 #define BXTWC_MADCIRQ 0x4E16
59 #define BXTWC_MCHGR0IRQ 0x4E17
60 #define BXTWC_MCHGR1IRQ 0x4E18
61 #define BXTWC_MGPIO0IRQ 0x4E19
62 #define BXTWC_MGPIO1IRQ 0x4E1A
63 #define BXTWC_MCRITIRQ 0x4E1B
64 #define BXTWC_MTMUIRQ 0x4FB7
66 /* Whiskey Cove PMIC share same ACPI ID between different platforms */
67 #define BROXTON_PMIC_WC_HRV 4
69 /* Manage in two IRQ chips since mask registers are not consecutive */
72 BXTWC_PWRBTN_LVL1_IRQ
= 0,
93 enum bxtwc_irqs_chgr
{
103 enum bxtwc_irqs_crit
{
107 static const struct regmap_irq bxtwc_regmap_irqs
[] = {
108 REGMAP_IRQ_REG(BXTWC_PWRBTN_LVL1_IRQ
, 0, BIT(0)),
109 REGMAP_IRQ_REG(BXTWC_TMU_LVL1_IRQ
, 0, BIT(1)),
110 REGMAP_IRQ_REG(BXTWC_THRM_LVL1_IRQ
, 0, BIT(2)),
111 REGMAP_IRQ_REG(BXTWC_BCU_LVL1_IRQ
, 0, BIT(3)),
112 REGMAP_IRQ_REG(BXTWC_ADC_LVL1_IRQ
, 0, BIT(4)),
113 REGMAP_IRQ_REG(BXTWC_CHGR_LVL1_IRQ
, 0, BIT(5)),
114 REGMAP_IRQ_REG(BXTWC_GPIO_LVL1_IRQ
, 0, BIT(6)),
115 REGMAP_IRQ_REG(BXTWC_CRIT_LVL1_IRQ
, 0, BIT(7)),
116 REGMAP_IRQ_REG(BXTWC_PWRBTN_IRQ
, 1, 0x03),
119 static const struct regmap_irq bxtwc_regmap_irqs_bcu
[] = {
120 REGMAP_IRQ_REG(BXTWC_BCU_IRQ
, 0, 0x1f),
123 static const struct regmap_irq bxtwc_regmap_irqs_adc
[] = {
124 REGMAP_IRQ_REG(BXTWC_ADC_IRQ
, 0, 0xff),
127 static const struct regmap_irq bxtwc_regmap_irqs_chgr
[] = {
128 REGMAP_IRQ_REG(BXTWC_USBC_IRQ
, 0, BIT(5)),
129 REGMAP_IRQ_REG(BXTWC_CHGR0_IRQ
, 0, 0x1f),
130 REGMAP_IRQ_REG(BXTWC_CHGR1_IRQ
, 1, 0x1f),
133 static const struct regmap_irq bxtwc_regmap_irqs_tmu
[] = {
134 REGMAP_IRQ_REG(BXTWC_TMU_IRQ
, 0, 0x06),
137 static const struct regmap_irq bxtwc_regmap_irqs_crit
[] = {
138 REGMAP_IRQ_REG(BXTWC_CRIT_IRQ
, 0, 0x03),
141 static struct regmap_irq_chip bxtwc_regmap_irq_chip
= {
142 .name
= "bxtwc_irq_chip",
143 .status_base
= BXTWC_IRQLVL1
,
144 .mask_base
= BXTWC_MIRQLVL1
,
145 .irqs
= bxtwc_regmap_irqs
,
146 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs
),
150 static struct regmap_irq_chip bxtwc_regmap_irq_chip_tmu
= {
151 .name
= "bxtwc_irq_chip_tmu",
152 .status_base
= BXTWC_TMUIRQ
,
153 .mask_base
= BXTWC_MTMUIRQ
,
154 .irqs
= bxtwc_regmap_irqs_tmu
,
155 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_tmu
),
159 static struct regmap_irq_chip bxtwc_regmap_irq_chip_bcu
= {
160 .name
= "bxtwc_irq_chip_bcu",
161 .status_base
= BXTWC_BCUIRQ
,
162 .mask_base
= BXTWC_MBCUIRQ
,
163 .irqs
= bxtwc_regmap_irqs_bcu
,
164 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_bcu
),
168 static struct regmap_irq_chip bxtwc_regmap_irq_chip_adc
= {
169 .name
= "bxtwc_irq_chip_adc",
170 .status_base
= BXTWC_ADCIRQ
,
171 .mask_base
= BXTWC_MADCIRQ
,
172 .irqs
= bxtwc_regmap_irqs_adc
,
173 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_adc
),
177 static struct regmap_irq_chip bxtwc_regmap_irq_chip_chgr
= {
178 .name
= "bxtwc_irq_chip_chgr",
179 .status_base
= BXTWC_CHGR0IRQ
,
180 .mask_base
= BXTWC_MCHGR0IRQ
,
181 .irqs
= bxtwc_regmap_irqs_chgr
,
182 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_chgr
),
186 static struct regmap_irq_chip bxtwc_regmap_irq_chip_crit
= {
187 .name
= "bxtwc_irq_chip_crit",
188 .status_base
= BXTWC_CRITIRQ
,
189 .mask_base
= BXTWC_MCRITIRQ
,
190 .irqs
= bxtwc_regmap_irqs_crit
,
191 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_crit
),
195 static struct resource gpio_resources
[] = {
196 DEFINE_RES_IRQ_NAMED(BXTWC_GPIO_LVL1_IRQ
, "GPIO"),
199 static struct resource adc_resources
[] = {
200 DEFINE_RES_IRQ_NAMED(BXTWC_ADC_IRQ
, "ADC"),
203 static struct resource usbc_resources
[] = {
204 DEFINE_RES_IRQ(BXTWC_USBC_IRQ
),
207 static struct resource charger_resources
[] = {
208 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR0_IRQ
, "CHARGER"),
209 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR1_IRQ
, "CHARGER1"),
212 static struct resource thermal_resources
[] = {
213 DEFINE_RES_IRQ(BXTWC_THRM_LVL1_IRQ
),
216 static struct resource bcu_resources
[] = {
217 DEFINE_RES_IRQ_NAMED(BXTWC_BCU_IRQ
, "BCU"),
220 static struct resource tmu_resources
[] = {
221 DEFINE_RES_IRQ_NAMED(BXTWC_TMU_IRQ
, "TMU"),
224 static struct mfd_cell bxt_wc_dev
[] = {
226 .name
= "bxt_wcove_gpadc",
227 .num_resources
= ARRAY_SIZE(adc_resources
),
228 .resources
= adc_resources
,
231 .name
= "bxt_wcove_thermal",
232 .num_resources
= ARRAY_SIZE(thermal_resources
),
233 .resources
= thermal_resources
,
236 .name
= "bxt_wcove_usbc",
237 .num_resources
= ARRAY_SIZE(usbc_resources
),
238 .resources
= usbc_resources
,
241 .name
= "bxt_wcove_ext_charger",
242 .num_resources
= ARRAY_SIZE(charger_resources
),
243 .resources
= charger_resources
,
246 .name
= "bxt_wcove_bcu",
247 .num_resources
= ARRAY_SIZE(bcu_resources
),
248 .resources
= bcu_resources
,
251 .name
= "bxt_wcove_tmu",
252 .num_resources
= ARRAY_SIZE(tmu_resources
),
253 .resources
= tmu_resources
,
257 .name
= "bxt_wcove_gpio",
258 .num_resources
= ARRAY_SIZE(gpio_resources
),
259 .resources
= gpio_resources
,
262 .name
= "bxt_wcove_region",
266 static int regmap_ipc_byte_reg_read(void *context
, unsigned int reg
,
273 struct intel_soc_pmic
*pmic
= context
;
278 if (reg
& REG_ADDR_MASK
)
279 i2c_addr
= (reg
& REG_ADDR_MASK
) >> REG_ADDR_SHIFT
;
281 i2c_addr
= BXTWC_DEVICE1_ADDR
;
283 reg
&= REG_OFFSET_MASK
;
286 ipc_in
[1] = i2c_addr
;
287 ret
= intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS
,
288 PMC_IPC_PMIC_ACCESS_READ
,
289 ipc_in
, sizeof(ipc_in
), (u32
*)ipc_out
, 1);
291 dev_err(pmic
->dev
, "Failed to read from PMIC\n");
299 static int regmap_ipc_byte_reg_write(void *context
, unsigned int reg
,
305 struct intel_soc_pmic
*pmic
= context
;
310 if (reg
& REG_ADDR_MASK
)
311 i2c_addr
= (reg
& REG_ADDR_MASK
) >> REG_ADDR_SHIFT
;
313 i2c_addr
= BXTWC_DEVICE1_ADDR
;
315 reg
&= REG_OFFSET_MASK
;
318 ipc_in
[1] = i2c_addr
;
320 ret
= intel_pmc_ipc_command(PMC_IPC_PMIC_ACCESS
,
321 PMC_IPC_PMIC_ACCESS_WRITE
,
322 ipc_in
, sizeof(ipc_in
), NULL
, 0);
324 dev_err(pmic
->dev
, "Failed to write to PMIC\n");
331 /* sysfs interfaces to r/w PMIC registers, required by initial script */
332 static unsigned long bxtwc_reg_addr
;
333 static ssize_t
bxtwc_reg_show(struct device
*dev
,
334 struct device_attribute
*attr
, char *buf
)
336 return sprintf(buf
, "0x%lx\n", bxtwc_reg_addr
);
339 static ssize_t
bxtwc_reg_store(struct device
*dev
,
340 struct device_attribute
*attr
, const char *buf
, size_t count
)
342 if (kstrtoul(buf
, 0, &bxtwc_reg_addr
)) {
343 dev_err(dev
, "Invalid register address\n");
346 return (ssize_t
)count
;
349 static ssize_t
bxtwc_val_show(struct device
*dev
,
350 struct device_attribute
*attr
, char *buf
)
354 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
356 ret
= regmap_read(pmic
->regmap
, bxtwc_reg_addr
, &val
);
358 dev_err(dev
, "Failed to read 0x%lx\n", bxtwc_reg_addr
);
362 return sprintf(buf
, "0x%02x\n", val
);
365 static ssize_t
bxtwc_val_store(struct device
*dev
,
366 struct device_attribute
*attr
, const char *buf
, size_t count
)
370 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
372 ret
= kstrtouint(buf
, 0, &val
);
376 ret
= regmap_write(pmic
->regmap
, bxtwc_reg_addr
, val
);
378 dev_err(dev
, "Failed to write value 0x%02x to address 0x%lx",
379 val
, bxtwc_reg_addr
);
385 static DEVICE_ATTR(addr
, S_IWUSR
| S_IRUSR
, bxtwc_reg_show
, bxtwc_reg_store
);
386 static DEVICE_ATTR(val
, S_IWUSR
| S_IRUSR
, bxtwc_val_show
, bxtwc_val_store
);
387 static struct attribute
*bxtwc_attrs
[] = {
393 static const struct attribute_group bxtwc_group
= {
394 .attrs
= bxtwc_attrs
,
397 static const struct regmap_config bxtwc_regmap_config
= {
400 .reg_write
= regmap_ipc_byte_reg_write
,
401 .reg_read
= regmap_ipc_byte_reg_read
,
404 static int bxtwc_add_chained_irq_chip(struct intel_soc_pmic
*pmic
,
405 struct regmap_irq_chip_data
*pdata
,
406 int pirq
, int irq_flags
,
407 const struct regmap_irq_chip
*chip
,
408 struct regmap_irq_chip_data
**data
)
412 irq
= regmap_irq_get_virq(pdata
, pirq
);
415 "Failed to get parent vIRQ(%d) for chip %s, ret:%d\n",
416 pirq
, chip
->name
, irq
);
420 return devm_regmap_add_irq_chip(pmic
->dev
, pmic
->regmap
, irq
, irq_flags
,
424 static int bxtwc_probe(struct platform_device
*pdev
)
429 unsigned long long hrv
;
430 struct intel_soc_pmic
*pmic
;
432 handle
= ACPI_HANDLE(&pdev
->dev
);
433 status
= acpi_evaluate_integer(handle
, "_HRV", NULL
, &hrv
);
434 if (ACPI_FAILURE(status
)) {
435 dev_err(&pdev
->dev
, "Failed to get PMIC hardware revision\n");
438 if (hrv
!= BROXTON_PMIC_WC_HRV
) {
439 dev_err(&pdev
->dev
, "Invalid PMIC hardware revision: %llu\n",
444 pmic
= devm_kzalloc(&pdev
->dev
, sizeof(*pmic
), GFP_KERNEL
);
448 ret
= platform_get_irq(pdev
, 0);
450 dev_err(&pdev
->dev
, "Invalid IRQ\n");
455 dev_set_drvdata(&pdev
->dev
, pmic
);
456 pmic
->dev
= &pdev
->dev
;
458 pmic
->regmap
= devm_regmap_init(&pdev
->dev
, NULL
, pmic
,
459 &bxtwc_regmap_config
);
460 if (IS_ERR(pmic
->regmap
)) {
461 ret
= PTR_ERR(pmic
->regmap
);
462 dev_err(&pdev
->dev
, "Failed to initialise regmap: %d\n", ret
);
466 ret
= devm_regmap_add_irq_chip(&pdev
->dev
, pmic
->regmap
, pmic
->irq
,
467 IRQF_ONESHOT
| IRQF_SHARED
,
468 0, &bxtwc_regmap_irq_chip
,
469 &pmic
->irq_chip_data
);
471 dev_err(&pdev
->dev
, "Failed to add IRQ chip\n");
475 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
478 &bxtwc_regmap_irq_chip_tmu
,
479 &pmic
->irq_chip_data_tmu
);
481 dev_err(&pdev
->dev
, "Failed to add TMU IRQ chip\n");
485 /* Add chained IRQ handler for BCU IRQs */
486 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
489 &bxtwc_regmap_irq_chip_bcu
,
490 &pmic
->irq_chip_data_bcu
);
494 dev_err(&pdev
->dev
, "Failed to add BUC IRQ chip\n");
498 /* Add chained IRQ handler for ADC IRQs */
499 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
502 &bxtwc_regmap_irq_chip_adc
,
503 &pmic
->irq_chip_data_adc
);
507 dev_err(&pdev
->dev
, "Failed to add ADC IRQ chip\n");
511 /* Add chained IRQ handler for CHGR IRQs */
512 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
515 &bxtwc_regmap_irq_chip_chgr
,
516 &pmic
->irq_chip_data_chgr
);
520 dev_err(&pdev
->dev
, "Failed to add CHGR IRQ chip\n");
524 /* Add chained IRQ handler for CRIT IRQs */
525 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
528 &bxtwc_regmap_irq_chip_crit
,
529 &pmic
->irq_chip_data_crit
);
533 dev_err(&pdev
->dev
, "Failed to add CRIT IRQ chip\n");
537 ret
= devm_mfd_add_devices(&pdev
->dev
, PLATFORM_DEVID_NONE
, bxt_wc_dev
,
538 ARRAY_SIZE(bxt_wc_dev
), NULL
, 0, NULL
);
540 dev_err(&pdev
->dev
, "Failed to add devices\n");
544 ret
= sysfs_create_group(&pdev
->dev
.kobj
, &bxtwc_group
);
546 dev_err(&pdev
->dev
, "Failed to create sysfs group %d\n", ret
);
551 * There is known hw bug. Upon reset BIT 5 of register
552 * BXTWC_CHGR_LVL1_IRQ is 0 which is the expected value. However,
553 * later it's set to 1(masked) automatically by hardware. So we
554 * have the software workaround here to unmaksed it in order to let
555 * charger interrutp work.
557 regmap_update_bits(pmic
->regmap
, BXTWC_MIRQLVL1
,
558 BXTWC_MIRQLVL1_MCHGR
, 0);
563 static int bxtwc_remove(struct platform_device
*pdev
)
565 sysfs_remove_group(&pdev
->dev
.kobj
, &bxtwc_group
);
570 static void bxtwc_shutdown(struct platform_device
*pdev
)
572 struct intel_soc_pmic
*pmic
= dev_get_drvdata(&pdev
->dev
);
574 disable_irq(pmic
->irq
);
577 #ifdef CONFIG_PM_SLEEP
578 static int bxtwc_suspend(struct device
*dev
)
580 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
582 disable_irq(pmic
->irq
);
587 static int bxtwc_resume(struct device
*dev
)
589 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
591 enable_irq(pmic
->irq
);
595 static SIMPLE_DEV_PM_OPS(bxtwc_pm_ops
, bxtwc_suspend
, bxtwc_resume
);
597 static const struct acpi_device_id bxtwc_acpi_ids
[] = {
601 MODULE_DEVICE_TABLE(acpi
, bxtwc_acpi_ids
);
603 static struct platform_driver bxtwc_driver
= {
604 .probe
= bxtwc_probe
,
605 .remove
= bxtwc_remove
,
606 .shutdown
= bxtwc_shutdown
,
608 .name
= "BXTWC PMIC",
610 .acpi_match_table
= ACPI_PTR(bxtwc_acpi_ids
),
614 module_platform_driver(bxtwc_driver
);
616 MODULE_LICENSE("GPL v2");
617 MODULE_AUTHOR("Qipeng Zha<qipeng.zha@intel.com>");