1 // SPDX-License-Identifier: GPL-2.0
3 * MFD core driver for Intel Broxton Whiskey Cove PMIC
5 * Copyright (C) 2015-2017, 2022 Intel Corporation. All rights reserved.
8 #include <linux/acpi.h>
9 #include <linux/array_size.h>
10 #include <linux/bits.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/gfp_types.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/kstrtox.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/intel_soc_pmic.h>
21 #include <linux/mfd/intel_soc_pmic_bxtwc.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/module.h>
24 #include <linux/platform_data/x86/intel_scu_ipc.h>
25 #include <linux/platform_device.h>
27 #include <linux/regmap.h>
28 #include <linux/sysfs.h>
29 #include <linux/types.h>
31 /* PMIC device registers */
32 #define REG_ADDR_MASK GENMASK(15, 8)
33 #define REG_ADDR_SHIFT 8
34 #define REG_OFFSET_MASK GENMASK(7, 0)
36 /* Interrupt Status Registers */
37 #define BXTWC_IRQLVL1 0x4E02
39 #define BXTWC_PWRBTNIRQ 0x4E03
40 #define BXTWC_THRM0IRQ 0x4E04
41 #define BXTWC_THRM1IRQ 0x4E05
42 #define BXTWC_THRM2IRQ 0x4E06
43 #define BXTWC_BCUIRQ 0x4E07
44 #define BXTWC_ADCIRQ 0x4E08
45 #define BXTWC_CHGR0IRQ 0x4E09
46 #define BXTWC_CHGR1IRQ 0x4E0A
47 #define BXTWC_GPIOIRQ0 0x4E0B
48 #define BXTWC_GPIOIRQ1 0x4E0C
49 #define BXTWC_CRITIRQ 0x4E0D
50 #define BXTWC_TMUIRQ 0x4FB6
52 /* Interrupt MASK Registers */
53 #define BXTWC_MIRQLVL1 0x4E0E
54 #define BXTWC_MIRQLVL1_MCHGR BIT(5)
56 #define BXTWC_MPWRBTNIRQ 0x4E0F
57 #define BXTWC_MTHRM0IRQ 0x4E12
58 #define BXTWC_MTHRM1IRQ 0x4E13
59 #define BXTWC_MTHRM2IRQ 0x4E14
60 #define BXTWC_MBCUIRQ 0x4E15
61 #define BXTWC_MADCIRQ 0x4E16
62 #define BXTWC_MCHGR0IRQ 0x4E17
63 #define BXTWC_MCHGR1IRQ 0x4E18
64 #define BXTWC_MGPIO0IRQ 0x4E19
65 #define BXTWC_MGPIO1IRQ 0x4E1A
66 #define BXTWC_MCRITIRQ 0x4E1B
67 #define BXTWC_MTMUIRQ 0x4FB7
69 /* Whiskey Cove PMIC share same ACPI ID between different platforms */
70 #define BROXTON_PMIC_WC_HRV 4
72 #define PMC_PMIC_ACCESS 0xFF
73 #define PMC_PMIC_READ 0x0
74 #define PMC_PMIC_WRITE 0x1
77 BXTWC_PWRBTN_LVL1_IRQ
= 0,
87 enum bxtwc_irqs_pwrbtn
{
100 enum bxtwc_irqs_chgr
{
106 enum bxtwc_irqs_tmu
{
110 enum bxtwc_irqs_crit
{
114 static const struct regmap_irq bxtwc_regmap_irqs
[] = {
115 REGMAP_IRQ_REG(BXTWC_PWRBTN_LVL1_IRQ
, 0, BIT(0)),
116 REGMAP_IRQ_REG(BXTWC_TMU_LVL1_IRQ
, 0, BIT(1)),
117 REGMAP_IRQ_REG(BXTWC_THRM_LVL1_IRQ
, 0, BIT(2)),
118 REGMAP_IRQ_REG(BXTWC_BCU_LVL1_IRQ
, 0, BIT(3)),
119 REGMAP_IRQ_REG(BXTWC_ADC_LVL1_IRQ
, 0, BIT(4)),
120 REGMAP_IRQ_REG(BXTWC_CHGR_LVL1_IRQ
, 0, BIT(5)),
121 REGMAP_IRQ_REG(BXTWC_GPIO_LVL1_IRQ
, 0, BIT(6)),
122 REGMAP_IRQ_REG(BXTWC_CRIT_LVL1_IRQ
, 0, BIT(7)),
125 static const struct regmap_irq bxtwc_regmap_irqs_pwrbtn
[] = {
126 REGMAP_IRQ_REG(BXTWC_PWRBTN_IRQ
, 0, BIT(0)),
129 static const struct regmap_irq bxtwc_regmap_irqs_bcu
[] = {
130 REGMAP_IRQ_REG(BXTWC_BCU_IRQ
, 0, GENMASK(4, 0)),
133 static const struct regmap_irq bxtwc_regmap_irqs_adc
[] = {
134 REGMAP_IRQ_REG(BXTWC_ADC_IRQ
, 0, GENMASK(7, 0)),
137 static const struct regmap_irq bxtwc_regmap_irqs_chgr
[] = {
138 REGMAP_IRQ_REG(BXTWC_USBC_IRQ
, 0, BIT(5)),
139 REGMAP_IRQ_REG(BXTWC_CHGR0_IRQ
, 0, GENMASK(4, 0)),
140 REGMAP_IRQ_REG(BXTWC_CHGR1_IRQ
, 1, GENMASK(4, 0)),
143 static const struct regmap_irq bxtwc_regmap_irqs_tmu
[] = {
144 REGMAP_IRQ_REG(BXTWC_TMU_IRQ
, 0, GENMASK(2, 1)),
147 static const struct regmap_irq bxtwc_regmap_irqs_crit
[] = {
148 REGMAP_IRQ_REG(BXTWC_CRIT_IRQ
, 0, GENMASK(1, 0)),
151 static const struct regmap_irq_chip bxtwc_regmap_irq_chip
= {
152 .name
= "bxtwc_irq_chip",
153 .status_base
= BXTWC_IRQLVL1
,
154 .mask_base
= BXTWC_MIRQLVL1
,
155 .irqs
= bxtwc_regmap_irqs
,
156 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs
),
160 static const struct regmap_irq_chip bxtwc_regmap_irq_chip_pwrbtn
= {
161 .name
= "bxtwc_irq_chip_pwrbtn",
162 .domain_suffix
= "PWRBTN",
163 .status_base
= BXTWC_PWRBTNIRQ
,
164 .mask_base
= BXTWC_MPWRBTNIRQ
,
165 .irqs
= bxtwc_regmap_irqs_pwrbtn
,
166 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_pwrbtn
),
170 static const struct regmap_irq_chip bxtwc_regmap_irq_chip_tmu
= {
171 .name
= "bxtwc_irq_chip_tmu",
172 .domain_suffix
= "TMU",
173 .status_base
= BXTWC_TMUIRQ
,
174 .mask_base
= BXTWC_MTMUIRQ
,
175 .irqs
= bxtwc_regmap_irqs_tmu
,
176 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_tmu
),
180 static const struct regmap_irq_chip bxtwc_regmap_irq_chip_bcu
= {
181 .name
= "bxtwc_irq_chip_bcu",
182 .domain_suffix
= "BCU",
183 .status_base
= BXTWC_BCUIRQ
,
184 .mask_base
= BXTWC_MBCUIRQ
,
185 .irqs
= bxtwc_regmap_irqs_bcu
,
186 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_bcu
),
190 static const struct regmap_irq_chip bxtwc_regmap_irq_chip_adc
= {
191 .name
= "bxtwc_irq_chip_adc",
192 .domain_suffix
= "ADC",
193 .status_base
= BXTWC_ADCIRQ
,
194 .mask_base
= BXTWC_MADCIRQ
,
195 .irqs
= bxtwc_regmap_irqs_adc
,
196 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_adc
),
200 static const struct regmap_irq_chip bxtwc_regmap_irq_chip_chgr
= {
201 .name
= "bxtwc_irq_chip_chgr",
202 .domain_suffix
= "CHGR",
203 .status_base
= BXTWC_CHGR0IRQ
,
204 .mask_base
= BXTWC_MCHGR0IRQ
,
205 .irqs
= bxtwc_regmap_irqs_chgr
,
206 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_chgr
),
210 static const struct regmap_irq_chip bxtwc_regmap_irq_chip_crit
= {
211 .name
= "bxtwc_irq_chip_crit",
212 .domain_suffix
= "CRIT",
213 .status_base
= BXTWC_CRITIRQ
,
214 .mask_base
= BXTWC_MCRITIRQ
,
215 .irqs
= bxtwc_regmap_irqs_crit
,
216 .num_irqs
= ARRAY_SIZE(bxtwc_regmap_irqs_crit
),
220 static const struct resource gpio_resources
[] = {
221 DEFINE_RES_IRQ_NAMED(BXTWC_GPIO_LVL1_IRQ
, "GPIO"),
224 static const struct resource adc_resources
[] = {
225 DEFINE_RES_IRQ_NAMED(BXTWC_ADC_IRQ
, "ADC"),
228 static const struct resource usbc_resources
[] = {
229 DEFINE_RES_IRQ(BXTWC_USBC_IRQ
),
232 static const struct resource charger_resources
[] = {
233 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR0_IRQ
, "CHARGER"),
234 DEFINE_RES_IRQ_NAMED(BXTWC_CHGR1_IRQ
, "CHARGER1"),
237 static const struct resource thermal_resources
[] = {
238 DEFINE_RES_IRQ(BXTWC_THRM_LVL1_IRQ
),
241 static const struct resource bcu_resources
[] = {
242 DEFINE_RES_IRQ_NAMED(BXTWC_BCU_IRQ
, "BCU"),
245 static const struct resource tmu_resources
[] = {
246 DEFINE_RES_IRQ_NAMED(BXTWC_TMU_IRQ
, "TMU"),
249 static struct mfd_cell bxt_wc_dev
[] = {
251 .name
= "bxt_wcove_thermal",
252 .num_resources
= ARRAY_SIZE(thermal_resources
),
253 .resources
= thermal_resources
,
256 .name
= "bxt_wcove_gpio",
257 .num_resources
= ARRAY_SIZE(gpio_resources
),
258 .resources
= gpio_resources
,
261 .name
= "bxt_wcove_region",
265 static const struct mfd_cell bxt_wc_tmu_dev
[] = {
267 .name
= "bxt_wcove_tmu",
268 .num_resources
= ARRAY_SIZE(tmu_resources
),
269 .resources
= tmu_resources
,
273 static const struct mfd_cell bxt_wc_bcu_dev
[] = {
275 .name
= "bxt_wcove_bcu",
276 .num_resources
= ARRAY_SIZE(bcu_resources
),
277 .resources
= bcu_resources
,
281 static const struct mfd_cell bxt_wc_adc_dev
[] = {
283 .name
= "bxt_wcove_gpadc",
284 .num_resources
= ARRAY_SIZE(adc_resources
),
285 .resources
= adc_resources
,
289 static struct mfd_cell bxt_wc_chgr_dev
[] = {
291 .name
= "bxt_wcove_usbc",
292 .num_resources
= ARRAY_SIZE(usbc_resources
),
293 .resources
= usbc_resources
,
296 .name
= "bxt_wcove_ext_charger",
297 .num_resources
= ARRAY_SIZE(charger_resources
),
298 .resources
= charger_resources
,
302 static int regmap_ipc_byte_reg_read(void *context
, unsigned int reg
,
309 struct intel_soc_pmic
*pmic
= context
;
314 if (reg
& REG_ADDR_MASK
)
315 i2c_addr
= (reg
& REG_ADDR_MASK
) >> REG_ADDR_SHIFT
;
317 i2c_addr
= BXTWC_DEVICE1_ADDR
;
319 reg
&= REG_OFFSET_MASK
;
322 ipc_in
[1] = i2c_addr
;
323 ret
= intel_scu_ipc_dev_command(pmic
->scu
, PMC_PMIC_ACCESS
,
324 PMC_PMIC_READ
, ipc_in
, sizeof(ipc_in
),
325 ipc_out
, sizeof(ipc_out
));
334 static int regmap_ipc_byte_reg_write(void *context
, unsigned int reg
,
339 struct intel_soc_pmic
*pmic
= context
;
344 if (reg
& REG_ADDR_MASK
)
345 i2c_addr
= (reg
& REG_ADDR_MASK
) >> REG_ADDR_SHIFT
;
347 i2c_addr
= BXTWC_DEVICE1_ADDR
;
349 reg
&= REG_OFFSET_MASK
;
352 ipc_in
[1] = i2c_addr
;
354 return intel_scu_ipc_dev_command(pmic
->scu
, PMC_PMIC_ACCESS
,
355 PMC_PMIC_WRITE
, ipc_in
, sizeof(ipc_in
),
359 /* sysfs interfaces to r/w PMIC registers, required by initial script */
360 static unsigned long bxtwc_reg_addr
;
361 static ssize_t
addr_show(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 return sysfs_emit(buf
, "0x%lx\n", bxtwc_reg_addr
);
367 static ssize_t
addr_store(struct device
*dev
,
368 struct device_attribute
*attr
, const char *buf
, size_t count
)
372 ret
= kstrtoul(buf
, 0, &bxtwc_reg_addr
);
378 static DEVICE_ATTR_ADMIN_RW(addr
);
380 static ssize_t
val_show(struct device
*dev
,
381 struct device_attribute
*attr
, char *buf
)
385 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
387 ret
= regmap_read(pmic
->regmap
, bxtwc_reg_addr
, &val
);
389 dev_err(dev
, "Failed to read 0x%lx\n", bxtwc_reg_addr
);
393 return sysfs_emit(buf
, "0x%02x\n", val
);
396 static ssize_t
val_store(struct device
*dev
,
397 struct device_attribute
*attr
, const char *buf
, size_t count
)
401 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
403 ret
= kstrtouint(buf
, 0, &val
);
407 ret
= regmap_write(pmic
->regmap
, bxtwc_reg_addr
, val
);
409 dev_err(dev
, "Failed to write value 0x%02x to address 0x%lx",
410 val
, bxtwc_reg_addr
);
415 static DEVICE_ATTR_ADMIN_RW(val
);
417 static struct attribute
*bxtwc_attrs
[] = {
422 ATTRIBUTE_GROUPS(bxtwc
);
424 static const struct regmap_config bxtwc_regmap_config
= {
427 .reg_write
= regmap_ipc_byte_reg_write
,
428 .reg_read
= regmap_ipc_byte_reg_read
,
431 static int bxtwc_add_chained_irq_chip(struct intel_soc_pmic
*pmic
,
432 struct regmap_irq_chip_data
*pdata
,
433 int pirq
, int irq_flags
,
434 const struct regmap_irq_chip
*chip
,
435 struct regmap_irq_chip_data
**data
)
437 struct device
*dev
= pmic
->dev
;
440 irq
= regmap_irq_get_virq(pdata
, pirq
);
442 return dev_err_probe(dev
, irq
, "Failed to get parent vIRQ(%d) for chip %s\n",
445 ret
= devm_regmap_add_irq_chip(dev
, pmic
->regmap
, irq
, irq_flags
, 0, chip
, data
);
447 return dev_err_probe(dev
, ret
, "Failed to add %s IRQ chip\n", chip
->name
);
452 static int bxtwc_add_chained_devices(struct intel_soc_pmic
*pmic
,
453 const struct mfd_cell
*cells
, int n_devs
,
454 struct regmap_irq_chip_data
*pdata
,
455 int pirq
, int irq_flags
,
456 const struct regmap_irq_chip
*chip
,
457 struct regmap_irq_chip_data
**data
)
459 struct device
*dev
= pmic
->dev
;
460 struct irq_domain
*domain
;
463 ret
= bxtwc_add_chained_irq_chip(pmic
, pdata
, pirq
, irq_flags
, chip
, data
);
467 domain
= regmap_irq_get_domain(*data
);
469 return devm_mfd_add_devices(dev
, PLATFORM_DEVID_NONE
, cells
, n_devs
, NULL
, 0, domain
);
472 static int bxtwc_probe(struct platform_device
*pdev
)
474 struct device
*dev
= &pdev
->dev
;
477 unsigned long long hrv
;
478 struct intel_soc_pmic
*pmic
;
480 status
= acpi_evaluate_integer(ACPI_HANDLE(dev
), "_HRV", NULL
, &hrv
);
481 if (ACPI_FAILURE(status
))
482 return dev_err_probe(dev
, -ENODEV
, "Failed to get PMIC hardware revision\n");
483 if (hrv
!= BROXTON_PMIC_WC_HRV
)
484 return dev_err_probe(dev
, -ENODEV
, "Invalid PMIC hardware revision: %llu\n", hrv
);
486 pmic
= devm_kzalloc(dev
, sizeof(*pmic
), GFP_KERNEL
);
490 ret
= platform_get_irq(pdev
, 0);
495 platform_set_drvdata(pdev
, pmic
);
498 pmic
->scu
= devm_intel_scu_ipc_dev_get(dev
);
500 return -EPROBE_DEFER
;
502 pmic
->regmap
= devm_regmap_init(dev
, NULL
, pmic
, &bxtwc_regmap_config
);
503 if (IS_ERR(pmic
->regmap
))
504 return dev_err_probe(dev
, PTR_ERR(pmic
->regmap
), "Failed to initialise regmap\n");
506 ret
= devm_regmap_add_irq_chip(dev
, pmic
->regmap
, pmic
->irq
,
507 IRQF_ONESHOT
| IRQF_SHARED
,
508 0, &bxtwc_regmap_irq_chip
,
509 &pmic
->irq_chip_data
);
511 return dev_err_probe(dev
, ret
, "Failed to add IRQ chip\n");
513 ret
= bxtwc_add_chained_devices(pmic
, bxt_wc_tmu_dev
, ARRAY_SIZE(bxt_wc_tmu_dev
),
517 &bxtwc_regmap_irq_chip_tmu
,
518 &pmic
->irq_chip_data_tmu
);
522 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
523 BXTWC_PWRBTN_LVL1_IRQ
,
525 &bxtwc_regmap_irq_chip_pwrbtn
,
526 &pmic
->irq_chip_data_pwrbtn
);
530 ret
= bxtwc_add_chained_devices(pmic
, bxt_wc_bcu_dev
, ARRAY_SIZE(bxt_wc_bcu_dev
),
534 &bxtwc_regmap_irq_chip_bcu
,
535 &pmic
->irq_chip_data_bcu
);
539 ret
= bxtwc_add_chained_devices(pmic
, bxt_wc_adc_dev
, ARRAY_SIZE(bxt_wc_adc_dev
),
543 &bxtwc_regmap_irq_chip_adc
,
544 &pmic
->irq_chip_data_adc
);
548 ret
= bxtwc_add_chained_devices(pmic
, bxt_wc_chgr_dev
, ARRAY_SIZE(bxt_wc_chgr_dev
),
552 &bxtwc_regmap_irq_chip_chgr
,
553 &pmic
->irq_chip_data_chgr
);
557 /* Add chained IRQ handler for CRIT IRQs */
558 ret
= bxtwc_add_chained_irq_chip(pmic
, pmic
->irq_chip_data
,
561 &bxtwc_regmap_irq_chip_crit
,
562 &pmic
->irq_chip_data_crit
);
566 ret
= devm_mfd_add_devices(dev
, PLATFORM_DEVID_NONE
, bxt_wc_dev
, ARRAY_SIZE(bxt_wc_dev
),
569 return dev_err_probe(dev
, ret
, "Failed to add devices\n");
572 * There is a known H/W 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 * place the software workaround here to unmask it again in order
576 * to re-enable the charger interrupt.
578 regmap_update_bits(pmic
->regmap
, BXTWC_MIRQLVL1
, BXTWC_MIRQLVL1_MCHGR
, 0);
583 static void bxtwc_shutdown(struct platform_device
*pdev
)
585 struct intel_soc_pmic
*pmic
= platform_get_drvdata(pdev
);
587 disable_irq(pmic
->irq
);
590 static int bxtwc_suspend(struct device
*dev
)
592 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
594 disable_irq(pmic
->irq
);
599 static int bxtwc_resume(struct device
*dev
)
601 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
);
603 enable_irq(pmic
->irq
);
607 static DEFINE_SIMPLE_DEV_PM_OPS(bxtwc_pm_ops
, bxtwc_suspend
, bxtwc_resume
);
609 static const struct acpi_device_id bxtwc_acpi_ids
[] = {
613 MODULE_DEVICE_TABLE(acpi
, bxtwc_acpi_ids
);
615 static struct platform_driver bxtwc_driver
= {
616 .probe
= bxtwc_probe
,
617 .shutdown
= bxtwc_shutdown
,
619 .name
= "intel_soc_pmic_bxtwc",
620 .pm
= pm_sleep_ptr(&bxtwc_pm_ops
),
621 .acpi_match_table
= bxtwc_acpi_ids
,
622 .dev_groups
= bxtwc_groups
,
626 module_platform_driver(bxtwc_driver
);
628 MODULE_DESCRIPTION("Intel Broxton Whiskey Cove PMIC MFD core driver");
629 MODULE_LICENSE("GPL v2");
630 MODULE_AUTHOR("Qipeng Zha <qipeng.zha@intel.com>");