162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Core functions for TI TPS6594/TPS6593/LP8764 PMICs
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/completion.h>
962306a36Sopenharmony_ci#include <linux/delay.h>
1062306a36Sopenharmony_ci#include <linux/interrupt.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/of.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/mfd/core.h>
1562306a36Sopenharmony_ci#include <linux/mfd/tps6594.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define TPS6594_CRC_SYNC_TIMEOUT_MS 150
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci/* Completion to synchronize CRC feature enabling on all PMICs */
2062306a36Sopenharmony_cistatic DECLARE_COMPLETION(tps6594_crc_comp);
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic const struct resource tps6594_regulator_resources[] = {
2362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK1_OV, TPS6594_IRQ_NAME_BUCK1_OV),
2462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK1_UV, TPS6594_IRQ_NAME_BUCK1_UV),
2562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK1_SC, TPS6594_IRQ_NAME_BUCK1_SC),
2662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK1_ILIM, TPS6594_IRQ_NAME_BUCK1_ILIM),
2762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK2_OV, TPS6594_IRQ_NAME_BUCK2_OV),
2862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK2_UV, TPS6594_IRQ_NAME_BUCK2_UV),
2962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK2_SC, TPS6594_IRQ_NAME_BUCK2_SC),
3062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK2_ILIM, TPS6594_IRQ_NAME_BUCK2_ILIM),
3162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK3_OV, TPS6594_IRQ_NAME_BUCK3_OV),
3262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK3_UV, TPS6594_IRQ_NAME_BUCK3_UV),
3362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK3_SC, TPS6594_IRQ_NAME_BUCK3_SC),
3462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK3_ILIM, TPS6594_IRQ_NAME_BUCK3_ILIM),
3562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK4_OV, TPS6594_IRQ_NAME_BUCK4_OV),
3662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK4_UV, TPS6594_IRQ_NAME_BUCK4_UV),
3762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK4_SC, TPS6594_IRQ_NAME_BUCK4_SC),
3862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK4_ILIM, TPS6594_IRQ_NAME_BUCK4_ILIM),
3962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK5_OV, TPS6594_IRQ_NAME_BUCK5_OV),
4062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK5_UV, TPS6594_IRQ_NAME_BUCK5_UV),
4162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK5_SC, TPS6594_IRQ_NAME_BUCK5_SC),
4262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BUCK5_ILIM, TPS6594_IRQ_NAME_BUCK5_ILIM),
4362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO1_OV, TPS6594_IRQ_NAME_LDO1_OV),
4462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO1_UV, TPS6594_IRQ_NAME_LDO1_UV),
4562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO1_SC, TPS6594_IRQ_NAME_LDO1_SC),
4662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO1_ILIM, TPS6594_IRQ_NAME_LDO1_ILIM),
4762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO2_OV, TPS6594_IRQ_NAME_LDO2_OV),
4862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO2_UV, TPS6594_IRQ_NAME_LDO2_UV),
4962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO2_SC, TPS6594_IRQ_NAME_LDO2_SC),
5062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO2_ILIM, TPS6594_IRQ_NAME_LDO2_ILIM),
5162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO3_OV, TPS6594_IRQ_NAME_LDO3_OV),
5262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO3_UV, TPS6594_IRQ_NAME_LDO3_UV),
5362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO3_SC, TPS6594_IRQ_NAME_LDO3_SC),
5462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO3_ILIM, TPS6594_IRQ_NAME_LDO3_ILIM),
5562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO4_OV, TPS6594_IRQ_NAME_LDO4_OV),
5662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO4_UV, TPS6594_IRQ_NAME_LDO4_UV),
5762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO4_SC, TPS6594_IRQ_NAME_LDO4_SC),
5862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_LDO4_ILIM, TPS6594_IRQ_NAME_LDO4_ILIM),
5962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VCCA_OV, TPS6594_IRQ_NAME_VCCA_OV),
6062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VCCA_UV, TPS6594_IRQ_NAME_VCCA_UV),
6162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VMON1_OV, TPS6594_IRQ_NAME_VMON1_OV),
6262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VMON1_UV, TPS6594_IRQ_NAME_VMON1_UV),
6362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VMON1_RV, TPS6594_IRQ_NAME_VMON1_RV),
6462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VMON2_OV, TPS6594_IRQ_NAME_VMON2_OV),
6562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VMON2_UV, TPS6594_IRQ_NAME_VMON2_UV),
6662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VMON2_RV, TPS6594_IRQ_NAME_VMON2_RV),
6762306a36Sopenharmony_ci};
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic const struct resource tps6594_pinctrl_resources[] = {
7062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO9, TPS6594_IRQ_NAME_GPIO9),
7162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO10, TPS6594_IRQ_NAME_GPIO10),
7262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO11, TPS6594_IRQ_NAME_GPIO11),
7362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO1, TPS6594_IRQ_NAME_GPIO1),
7462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO2, TPS6594_IRQ_NAME_GPIO2),
7562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO3, TPS6594_IRQ_NAME_GPIO3),
7662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO4, TPS6594_IRQ_NAME_GPIO4),
7762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO5, TPS6594_IRQ_NAME_GPIO5),
7862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO6, TPS6594_IRQ_NAME_GPIO6),
7962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO7, TPS6594_IRQ_NAME_GPIO7),
8062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_GPIO8, TPS6594_IRQ_NAME_GPIO8),
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic const struct resource tps6594_pfsm_resources[] = {
8462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_NPWRON_START, TPS6594_IRQ_NAME_NPWRON_START),
8562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_ENABLE, TPS6594_IRQ_NAME_ENABLE),
8662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_FSD, TPS6594_IRQ_NAME_FSD),
8762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_SOFT_REBOOT, TPS6594_IRQ_NAME_SOFT_REBOOT),
8862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BIST_PASS, TPS6594_IRQ_NAME_BIST_PASS),
8962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_EXT_CLK, TPS6594_IRQ_NAME_EXT_CLK),
9062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_TWARN, TPS6594_IRQ_NAME_TWARN),
9162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_TSD_ORD, TPS6594_IRQ_NAME_TSD_ORD),
9262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_BIST_FAIL, TPS6594_IRQ_NAME_BIST_FAIL),
9362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_REG_CRC_ERR, TPS6594_IRQ_NAME_REG_CRC_ERR),
9462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_RECOV_CNT, TPS6594_IRQ_NAME_RECOV_CNT),
9562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_SPMI_ERR, TPS6594_IRQ_NAME_SPMI_ERR),
9662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_NPWRON_LONG, TPS6594_IRQ_NAME_NPWRON_LONG),
9762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_NINT_READBACK, TPS6594_IRQ_NAME_NINT_READBACK),
9862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_NRSTOUT_READBACK, TPS6594_IRQ_NAME_NRSTOUT_READBACK),
9962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_TSD_IMM, TPS6594_IRQ_NAME_TSD_IMM),
10062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_VCCA_OVP, TPS6594_IRQ_NAME_VCCA_OVP),
10162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_PFSM_ERR, TPS6594_IRQ_NAME_PFSM_ERR),
10262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_IMM_SHUTDOWN, TPS6594_IRQ_NAME_IMM_SHUTDOWN),
10362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_ORD_SHUTDOWN, TPS6594_IRQ_NAME_ORD_SHUTDOWN),
10462306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_MCU_PWR_ERR, TPS6594_IRQ_NAME_MCU_PWR_ERR),
10562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_SOC_PWR_ERR, TPS6594_IRQ_NAME_SOC_PWR_ERR),
10662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_COMM_FRM_ERR, TPS6594_IRQ_NAME_COMM_FRM_ERR),
10762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_COMM_CRC_ERR, TPS6594_IRQ_NAME_COMM_CRC_ERR),
10862306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_COMM_ADR_ERR, TPS6594_IRQ_NAME_COMM_ADR_ERR),
10962306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_EN_DRV_READBACK, TPS6594_IRQ_NAME_EN_DRV_READBACK),
11062306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_NRSTOUT_SOC_READBACK,
11162306a36Sopenharmony_ci			     TPS6594_IRQ_NAME_NRSTOUT_SOC_READBACK),
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic const struct resource tps6594_esm_resources[] = {
11562306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_ESM_SOC_PIN, TPS6594_IRQ_NAME_ESM_SOC_PIN),
11662306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_ESM_SOC_FAIL, TPS6594_IRQ_NAME_ESM_SOC_FAIL),
11762306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_ESM_SOC_RST, TPS6594_IRQ_NAME_ESM_SOC_RST),
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic const struct resource tps6594_rtc_resources[] = {
12162306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_TIMER, TPS6594_IRQ_NAME_TIMER),
12262306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_ALARM, TPS6594_IRQ_NAME_ALARM),
12362306a36Sopenharmony_ci	DEFINE_RES_IRQ_NAMED(TPS6594_IRQ_POWER_UP, TPS6594_IRQ_NAME_POWERUP),
12462306a36Sopenharmony_ci};
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic const struct mfd_cell tps6594_common_cells[] = {
12762306a36Sopenharmony_ci	MFD_CELL_RES("tps6594-regulator", tps6594_regulator_resources),
12862306a36Sopenharmony_ci	MFD_CELL_RES("tps6594-pinctrl", tps6594_pinctrl_resources),
12962306a36Sopenharmony_ci	MFD_CELL_RES("tps6594-pfsm", tps6594_pfsm_resources),
13062306a36Sopenharmony_ci	MFD_CELL_RES("tps6594-esm", tps6594_esm_resources),
13162306a36Sopenharmony_ci};
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic const struct mfd_cell tps6594_rtc_cells[] = {
13462306a36Sopenharmony_ci	MFD_CELL_RES("tps6594-rtc", tps6594_rtc_resources),
13562306a36Sopenharmony_ci};
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cistatic const struct regmap_irq tps6594_irqs[] = {
13862306a36Sopenharmony_ci	/* INT_BUCK1_2 register */
13962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK1_OV, 0, TPS6594_BIT_BUCKX_OV_INT(0)),
14062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK1_UV, 0, TPS6594_BIT_BUCKX_UV_INT(0)),
14162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK1_SC, 0, TPS6594_BIT_BUCKX_SC_INT(0)),
14262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK1_ILIM, 0, TPS6594_BIT_BUCKX_ILIM_INT(0)),
14362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK2_OV, 0, TPS6594_BIT_BUCKX_OV_INT(1)),
14462306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK2_UV, 0, TPS6594_BIT_BUCKX_UV_INT(1)),
14562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK2_SC, 0, TPS6594_BIT_BUCKX_SC_INT(1)),
14662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK2_ILIM, 0, TPS6594_BIT_BUCKX_ILIM_INT(1)),
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	/* INT_BUCK3_4 register */
14962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK3_OV, 1, TPS6594_BIT_BUCKX_OV_INT(2)),
15062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK3_UV, 1, TPS6594_BIT_BUCKX_UV_INT(2)),
15162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK3_SC, 1, TPS6594_BIT_BUCKX_SC_INT(2)),
15262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK3_ILIM, 1, TPS6594_BIT_BUCKX_ILIM_INT(2)),
15362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK4_OV, 1, TPS6594_BIT_BUCKX_OV_INT(3)),
15462306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK4_UV, 1, TPS6594_BIT_BUCKX_UV_INT(3)),
15562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK4_SC, 1, TPS6594_BIT_BUCKX_SC_INT(3)),
15662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK4_ILIM, 1, TPS6594_BIT_BUCKX_ILIM_INT(3)),
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	/* INT_BUCK5 register */
15962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK5_OV, 2, TPS6594_BIT_BUCKX_OV_INT(4)),
16062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK5_UV, 2, TPS6594_BIT_BUCKX_UV_INT(4)),
16162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK5_SC, 2, TPS6594_BIT_BUCKX_SC_INT(4)),
16262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BUCK5_ILIM, 2, TPS6594_BIT_BUCKX_ILIM_INT(4)),
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	/* INT_LDO1_2 register */
16562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO1_OV, 3, TPS6594_BIT_LDOX_OV_INT(0)),
16662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO1_UV, 3, TPS6594_BIT_LDOX_UV_INT(0)),
16762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO1_SC, 3, TPS6594_BIT_LDOX_SC_INT(0)),
16862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO1_ILIM, 3, TPS6594_BIT_LDOX_ILIM_INT(0)),
16962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO2_OV, 3, TPS6594_BIT_LDOX_OV_INT(1)),
17062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO2_UV, 3, TPS6594_BIT_LDOX_UV_INT(1)),
17162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO2_SC, 3, TPS6594_BIT_LDOX_SC_INT(1)),
17262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO2_ILIM, 3, TPS6594_BIT_LDOX_ILIM_INT(1)),
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	/* INT_LDO3_4 register */
17562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO3_OV, 4, TPS6594_BIT_LDOX_OV_INT(2)),
17662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO3_UV, 4, TPS6594_BIT_LDOX_UV_INT(2)),
17762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO3_SC, 4, TPS6594_BIT_LDOX_SC_INT(2)),
17862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO3_ILIM, 4, TPS6594_BIT_LDOX_ILIM_INT(2)),
17962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO4_OV, 4, TPS6594_BIT_LDOX_OV_INT(3)),
18062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO4_UV, 4, TPS6594_BIT_LDOX_UV_INT(3)),
18162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO4_SC, 4, TPS6594_BIT_LDOX_SC_INT(3)),
18262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_LDO4_ILIM, 4, TPS6594_BIT_LDOX_ILIM_INT(3)),
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	/* INT_VMON register */
18562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VCCA_OV, 5, TPS6594_BIT_VCCA_OV_INT),
18662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VCCA_UV, 5, TPS6594_BIT_VCCA_UV_INT),
18762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VMON1_OV, 5, TPS6594_BIT_VMON1_OV_INT),
18862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VMON1_UV, 5, TPS6594_BIT_VMON1_UV_INT),
18962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VMON1_RV, 5, TPS6594_BIT_VMON1_RV_INT),
19062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VMON2_OV, 5, TPS6594_BIT_VMON2_OV_INT),
19162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VMON2_UV, 5, TPS6594_BIT_VMON2_UV_INT),
19262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VMON2_RV, 5, TPS6594_BIT_VMON2_RV_INT),
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	/* INT_GPIO register */
19562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO9, 6, TPS6594_BIT_GPIO9_INT),
19662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO10, 6, TPS6594_BIT_GPIO10_INT),
19762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO11, 6, TPS6594_BIT_GPIO11_INT),
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	/* INT_GPIO1_8 register */
20062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO1, 7, TPS6594_BIT_GPIOX_INT(0)),
20162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO2, 7, TPS6594_BIT_GPIOX_INT(1)),
20262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO3, 7, TPS6594_BIT_GPIOX_INT(2)),
20362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO4, 7, TPS6594_BIT_GPIOX_INT(3)),
20462306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO5, 7, TPS6594_BIT_GPIOX_INT(4)),
20562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO6, 7, TPS6594_BIT_GPIOX_INT(5)),
20662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO7, 7, TPS6594_BIT_GPIOX_INT(6)),
20762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_GPIO8, 7, TPS6594_BIT_GPIOX_INT(7)),
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	/* INT_STARTUP register */
21062306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_NPWRON_START, 8, TPS6594_BIT_NPWRON_START_INT),
21162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_ENABLE, 8, TPS6594_BIT_ENABLE_INT),
21262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_FSD, 8, TPS6594_BIT_FSD_INT),
21362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_SOFT_REBOOT, 8, TPS6594_BIT_SOFT_REBOOT_INT),
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	/* INT_MISC register */
21662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BIST_PASS, 9, TPS6594_BIT_BIST_PASS_INT),
21762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_EXT_CLK, 9, TPS6594_BIT_EXT_CLK_INT),
21862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_TWARN, 9, TPS6594_BIT_TWARN_INT),
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	/* INT_MODERATE_ERR register */
22162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_TSD_ORD, 10, TPS6594_BIT_TSD_ORD_INT),
22262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_BIST_FAIL, 10, TPS6594_BIT_BIST_FAIL_INT),
22362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_REG_CRC_ERR, 10, TPS6594_BIT_REG_CRC_ERR_INT),
22462306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_RECOV_CNT, 10, TPS6594_BIT_RECOV_CNT_INT),
22562306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_SPMI_ERR, 10, TPS6594_BIT_SPMI_ERR_INT),
22662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_NPWRON_LONG, 10, TPS6594_BIT_NPWRON_LONG_INT),
22762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_NINT_READBACK, 10, TPS6594_BIT_NINT_READBACK_INT),
22862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_NRSTOUT_READBACK, 10, TPS6594_BIT_NRSTOUT_READBACK_INT),
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	/* INT_SEVERE_ERR register */
23162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_TSD_IMM, 11, TPS6594_BIT_TSD_IMM_INT),
23262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_VCCA_OVP, 11, TPS6594_BIT_VCCA_OVP_INT),
23362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_PFSM_ERR, 11, TPS6594_BIT_PFSM_ERR_INT),
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	/* INT_FSM_ERR register */
23662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_IMM_SHUTDOWN, 12, TPS6594_BIT_IMM_SHUTDOWN_INT),
23762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_ORD_SHUTDOWN, 12, TPS6594_BIT_ORD_SHUTDOWN_INT),
23862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_MCU_PWR_ERR, 12, TPS6594_BIT_MCU_PWR_ERR_INT),
23962306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_SOC_PWR_ERR, 12, TPS6594_BIT_SOC_PWR_ERR_INT),
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	/* INT_COMM_ERR register */
24262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_COMM_FRM_ERR, 13, TPS6594_BIT_COMM_FRM_ERR_INT),
24362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_COMM_CRC_ERR, 13, TPS6594_BIT_COMM_CRC_ERR_INT),
24462306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_COMM_ADR_ERR, 13, TPS6594_BIT_COMM_ADR_ERR_INT),
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci	/* INT_READBACK_ERR register */
24762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_EN_DRV_READBACK, 14, TPS6594_BIT_EN_DRV_READBACK_INT),
24862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_NRSTOUT_SOC_READBACK, 14, TPS6594_BIT_NRSTOUT_SOC_READBACK_INT),
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	/* INT_ESM register */
25162306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_ESM_SOC_PIN, 15, TPS6594_BIT_ESM_SOC_PIN_INT),
25262306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_ESM_SOC_FAIL, 15, TPS6594_BIT_ESM_SOC_FAIL_INT),
25362306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_ESM_SOC_RST, 15, TPS6594_BIT_ESM_SOC_RST_INT),
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	/* RTC_STATUS register */
25662306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_TIMER, 16, TPS6594_BIT_TIMER),
25762306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_ALARM, 16, TPS6594_BIT_ALARM),
25862306a36Sopenharmony_ci	REGMAP_IRQ_REG(TPS6594_IRQ_POWER_UP, 16, TPS6594_BIT_POWER_UP),
25962306a36Sopenharmony_ci};
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_cistatic const unsigned int tps6594_irq_reg[] = {
26262306a36Sopenharmony_ci	TPS6594_REG_INT_BUCK1_2,
26362306a36Sopenharmony_ci	TPS6594_REG_INT_BUCK3_4,
26462306a36Sopenharmony_ci	TPS6594_REG_INT_BUCK5,
26562306a36Sopenharmony_ci	TPS6594_REG_INT_LDO1_2,
26662306a36Sopenharmony_ci	TPS6594_REG_INT_LDO3_4,
26762306a36Sopenharmony_ci	TPS6594_REG_INT_VMON,
26862306a36Sopenharmony_ci	TPS6594_REG_INT_GPIO,
26962306a36Sopenharmony_ci	TPS6594_REG_INT_GPIO1_8,
27062306a36Sopenharmony_ci	TPS6594_REG_INT_STARTUP,
27162306a36Sopenharmony_ci	TPS6594_REG_INT_MISC,
27262306a36Sopenharmony_ci	TPS6594_REG_INT_MODERATE_ERR,
27362306a36Sopenharmony_ci	TPS6594_REG_INT_SEVERE_ERR,
27462306a36Sopenharmony_ci	TPS6594_REG_INT_FSM_ERR,
27562306a36Sopenharmony_ci	TPS6594_REG_INT_COMM_ERR,
27662306a36Sopenharmony_ci	TPS6594_REG_INT_READBACK_ERR,
27762306a36Sopenharmony_ci	TPS6594_REG_INT_ESM,
27862306a36Sopenharmony_ci	TPS6594_REG_RTC_STATUS,
27962306a36Sopenharmony_ci};
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_cistatic inline unsigned int tps6594_get_irq_reg(struct regmap_irq_chip_data *data,
28262306a36Sopenharmony_ci					       unsigned int base, int index)
28362306a36Sopenharmony_ci{
28462306a36Sopenharmony_ci	return tps6594_irq_reg[index];
28562306a36Sopenharmony_ci};
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_cistatic int tps6594_handle_post_irq(void *irq_drv_data)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	struct tps6594 *tps = irq_drv_data;
29062306a36Sopenharmony_ci	int ret = 0;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	/*
29362306a36Sopenharmony_ci	 * When CRC is enabled, writing to a read-only bit triggers an error,
29462306a36Sopenharmony_ci	 * and COMM_ADR_ERR_INT bit is set. Besides, bits indicating interrupts
29562306a36Sopenharmony_ci	 * (that must be cleared) and read-only bits are sometimes grouped in
29662306a36Sopenharmony_ci	 * the same register.
29762306a36Sopenharmony_ci	 * Since regmap clears interrupts by doing a write per register, clearing
29862306a36Sopenharmony_ci	 * an interrupt bit in a register containing also a read-only bit makes
29962306a36Sopenharmony_ci	 * COMM_ADR_ERR_INT bit set. Clear immediately this bit to avoid raising
30062306a36Sopenharmony_ci	 * a new interrupt.
30162306a36Sopenharmony_ci	 */
30262306a36Sopenharmony_ci	if (tps->use_crc)
30362306a36Sopenharmony_ci		ret = regmap_write_bits(tps->regmap, TPS6594_REG_INT_COMM_ERR,
30462306a36Sopenharmony_ci					TPS6594_BIT_COMM_ADR_ERR_INT,
30562306a36Sopenharmony_ci					TPS6594_BIT_COMM_ADR_ERR_INT);
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci	return ret;
30862306a36Sopenharmony_ci};
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_cistatic struct regmap_irq_chip tps6594_irq_chip = {
31162306a36Sopenharmony_ci	.ack_base = TPS6594_REG_INT_BUCK1_2,
31262306a36Sopenharmony_ci	.ack_invert = 1,
31362306a36Sopenharmony_ci	.clear_ack = 1,
31462306a36Sopenharmony_ci	.init_ack_masked = 1,
31562306a36Sopenharmony_ci	.num_regs = ARRAY_SIZE(tps6594_irq_reg),
31662306a36Sopenharmony_ci	.irqs = tps6594_irqs,
31762306a36Sopenharmony_ci	.num_irqs = ARRAY_SIZE(tps6594_irqs),
31862306a36Sopenharmony_ci	.get_irq_reg = tps6594_get_irq_reg,
31962306a36Sopenharmony_ci	.handle_post_irq = tps6594_handle_post_irq,
32062306a36Sopenharmony_ci};
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_cibool tps6594_is_volatile_reg(struct device *dev, unsigned int reg)
32362306a36Sopenharmony_ci{
32462306a36Sopenharmony_ci	return (reg >= TPS6594_REG_INT_TOP && reg <= TPS6594_REG_STAT_READBACK_ERR) ||
32562306a36Sopenharmony_ci	       reg == TPS6594_REG_RTC_STATUS;
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tps6594_is_volatile_reg);
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cistatic int tps6594_check_crc_mode(struct tps6594 *tps, bool primary_pmic)
33062306a36Sopenharmony_ci{
33162306a36Sopenharmony_ci	int ret;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	/*
33462306a36Sopenharmony_ci	 * Check if CRC is enabled.
33562306a36Sopenharmony_ci	 * Once CRC is enabled, it can't be disabled until next power cycle.
33662306a36Sopenharmony_ci	 */
33762306a36Sopenharmony_ci	tps->use_crc = true;
33862306a36Sopenharmony_ci	ret = regmap_test_bits(tps->regmap, TPS6594_REG_SERIAL_IF_CONFIG,
33962306a36Sopenharmony_ci			       TPS6594_BIT_I2C1_SPI_CRC_EN);
34062306a36Sopenharmony_ci	if (ret == 0) {
34162306a36Sopenharmony_ci		ret = -EIO;
34262306a36Sopenharmony_ci	} else if (ret > 0) {
34362306a36Sopenharmony_ci		dev_info(tps->dev, "CRC feature enabled on %s PMIC",
34462306a36Sopenharmony_ci			 primary_pmic ? "primary" : "secondary");
34562306a36Sopenharmony_ci		ret = 0;
34662306a36Sopenharmony_ci	}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	return ret;
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_cistatic int tps6594_set_crc_feature(struct tps6594 *tps)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	int ret;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	ret = tps6594_check_crc_mode(tps, true);
35662306a36Sopenharmony_ci	if (ret) {
35762306a36Sopenharmony_ci		/*
35862306a36Sopenharmony_ci		 * If CRC is not already enabled, force PFSM I2C_2 trigger to enable it
35962306a36Sopenharmony_ci		 * on primary PMIC.
36062306a36Sopenharmony_ci		 */
36162306a36Sopenharmony_ci		tps->use_crc = false;
36262306a36Sopenharmony_ci		ret = regmap_write_bits(tps->regmap, TPS6594_REG_FSM_I2C_TRIGGERS,
36362306a36Sopenharmony_ci					TPS6594_BIT_TRIGGER_I2C(2), TPS6594_BIT_TRIGGER_I2C(2));
36462306a36Sopenharmony_ci		if (ret)
36562306a36Sopenharmony_ci			return ret;
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci		/*
36862306a36Sopenharmony_ci		 * Wait for PFSM to process trigger.
36962306a36Sopenharmony_ci		 * The datasheet indicates 2 ms, and clock specification is +/-5%.
37062306a36Sopenharmony_ci		 * 4 ms should provide sufficient margin.
37162306a36Sopenharmony_ci		 */
37262306a36Sopenharmony_ci		usleep_range(4000, 5000);
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci		ret = tps6594_check_crc_mode(tps, true);
37562306a36Sopenharmony_ci	}
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	return ret;
37862306a36Sopenharmony_ci}
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_cistatic int tps6594_enable_crc(struct tps6594 *tps)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	struct device *dev = tps->dev;
38362306a36Sopenharmony_ci	unsigned int is_primary;
38462306a36Sopenharmony_ci	unsigned long timeout = msecs_to_jiffies(TPS6594_CRC_SYNC_TIMEOUT_MS);
38562306a36Sopenharmony_ci	int ret;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	/*
38862306a36Sopenharmony_ci	 * CRC mode can be used with I2C or SPI protocols.
38962306a36Sopenharmony_ci	 * If this mode is specified for primary PMIC, it will also be applied to secondary PMICs
39062306a36Sopenharmony_ci	 * through SPMI serial interface.
39162306a36Sopenharmony_ci	 * In this multi-PMIC synchronization scheme, the primary PMIC is the controller device
39262306a36Sopenharmony_ci	 * on the SPMI bus, and the secondary PMICs are the target devices on the SPMI bus.
39362306a36Sopenharmony_ci	 */
39462306a36Sopenharmony_ci	is_primary = of_property_read_bool(dev->of_node, "ti,primary-pmic");
39562306a36Sopenharmony_ci	if (is_primary) {
39662306a36Sopenharmony_ci		/* Enable CRC feature on primary PMIC */
39762306a36Sopenharmony_ci		ret = tps6594_set_crc_feature(tps);
39862306a36Sopenharmony_ci		if (ret)
39962306a36Sopenharmony_ci			return ret;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci		/* Notify secondary PMICs that CRC feature is enabled */
40262306a36Sopenharmony_ci		complete_all(&tps6594_crc_comp);
40362306a36Sopenharmony_ci	} else {
40462306a36Sopenharmony_ci		/* Wait for CRC feature enabling event from primary PMIC */
40562306a36Sopenharmony_ci		ret = wait_for_completion_interruptible_timeout(&tps6594_crc_comp, timeout);
40662306a36Sopenharmony_ci		if (ret == 0)
40762306a36Sopenharmony_ci			ret = -ETIMEDOUT;
40862306a36Sopenharmony_ci		else if (ret > 0)
40962306a36Sopenharmony_ci			ret = tps6594_check_crc_mode(tps, false);
41062306a36Sopenharmony_ci	}
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci	return ret;
41362306a36Sopenharmony_ci}
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ciint tps6594_device_init(struct tps6594 *tps, bool enable_crc)
41662306a36Sopenharmony_ci{
41762306a36Sopenharmony_ci	struct device *dev = tps->dev;
41862306a36Sopenharmony_ci	int ret;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	if (enable_crc) {
42162306a36Sopenharmony_ci		ret = tps6594_enable_crc(tps);
42262306a36Sopenharmony_ci		if (ret)
42362306a36Sopenharmony_ci			return dev_err_probe(dev, ret, "Failed to enable CRC\n");
42462306a36Sopenharmony_ci	}
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	/* Keep PMIC in ACTIVE state */
42762306a36Sopenharmony_ci	ret = regmap_set_bits(tps->regmap, TPS6594_REG_FSM_NSLEEP_TRIGGERS,
42862306a36Sopenharmony_ci			      TPS6594_BIT_NSLEEP1B | TPS6594_BIT_NSLEEP2B);
42962306a36Sopenharmony_ci	if (ret)
43062306a36Sopenharmony_ci		return dev_err_probe(dev, ret, "Failed to set PMIC state\n");
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci	tps6594_irq_chip.irq_drv_data = tps;
43362306a36Sopenharmony_ci	tps6594_irq_chip.name = devm_kasprintf(dev, GFP_KERNEL, "%s-%ld-0x%02x",
43462306a36Sopenharmony_ci					       dev->driver->name, tps->chip_id, tps->reg);
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	if (!tps6594_irq_chip.name)
43762306a36Sopenharmony_ci		return -ENOMEM;
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	ret = devm_regmap_add_irq_chip(dev, tps->regmap, tps->irq, IRQF_SHARED | IRQF_ONESHOT,
44062306a36Sopenharmony_ci				       0, &tps6594_irq_chip, &tps->irq_data);
44162306a36Sopenharmony_ci	if (ret)
44262306a36Sopenharmony_ci		return dev_err_probe(dev, ret, "Failed to add regmap IRQ\n");
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, tps6594_common_cells,
44562306a36Sopenharmony_ci				   ARRAY_SIZE(tps6594_common_cells), NULL, 0,
44662306a36Sopenharmony_ci				   regmap_irq_get_domain(tps->irq_data));
44762306a36Sopenharmony_ci	if (ret)
44862306a36Sopenharmony_ci		return dev_err_probe(dev, ret, "Failed to add common child devices\n");
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	/* No RTC for LP8764 */
45162306a36Sopenharmony_ci	if (tps->chip_id != LP8764) {
45262306a36Sopenharmony_ci		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, tps6594_rtc_cells,
45362306a36Sopenharmony_ci					   ARRAY_SIZE(tps6594_rtc_cells), NULL, 0,
45462306a36Sopenharmony_ci					   regmap_irq_get_domain(tps->irq_data));
45562306a36Sopenharmony_ci		if (ret)
45662306a36Sopenharmony_ci			return dev_err_probe(dev, ret, "Failed to add RTC child device\n");
45762306a36Sopenharmony_ci	}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	return 0;
46062306a36Sopenharmony_ci}
46162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(tps6594_device_init);
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ciMODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");
46462306a36Sopenharmony_ciMODULE_DESCRIPTION("TPS6594 Driver");
46562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
466