18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * tps65910.c  --  TI TPS6591x chip family multi-function driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2010 Texas Instruments Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Graeme Gregory <gg@slimlogic.co.uk>
88c2ecf20Sopenharmony_ci * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/i2c.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/irq.h>
178c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
188c2ecf20Sopenharmony_ci#include <linux/mfd/core.h>
198c2ecf20Sopenharmony_ci#include <linux/regmap.h>
208c2ecf20Sopenharmony_ci#include <linux/mfd/tps65910.h>
218c2ecf20Sopenharmony_ci#include <linux/of.h>
228c2ecf20Sopenharmony_ci#include <linux/of_device.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic struct resource rtc_resources[] = {
258c2ecf20Sopenharmony_ci	{
268c2ecf20Sopenharmony_ci		.start  = TPS65910_IRQ_RTC_ALARM,
278c2ecf20Sopenharmony_ci		.end    = TPS65910_IRQ_RTC_ALARM,
288c2ecf20Sopenharmony_ci		.flags  = IORESOURCE_IRQ,
298c2ecf20Sopenharmony_ci	}
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic const struct mfd_cell tps65910s[] = {
338c2ecf20Sopenharmony_ci	{
348c2ecf20Sopenharmony_ci		.name = "tps65910-gpio",
358c2ecf20Sopenharmony_ci	},
368c2ecf20Sopenharmony_ci	{
378c2ecf20Sopenharmony_ci		.name = "tps65910-pmic",
388c2ecf20Sopenharmony_ci	},
398c2ecf20Sopenharmony_ci	{
408c2ecf20Sopenharmony_ci		.name = "tps65910-rtc",
418c2ecf20Sopenharmony_ci		.num_resources = ARRAY_SIZE(rtc_resources),
428c2ecf20Sopenharmony_ci		.resources = &rtc_resources[0],
438c2ecf20Sopenharmony_ci	},
448c2ecf20Sopenharmony_ci	{
458c2ecf20Sopenharmony_ci		.name = "tps65910-power",
468c2ecf20Sopenharmony_ci	},
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic const struct regmap_irq tps65911_irqs[] = {
518c2ecf20Sopenharmony_ci	/* INT_STS */
528c2ecf20Sopenharmony_ci	[TPS65911_IRQ_PWRHOLD_F] = {
538c2ecf20Sopenharmony_ci		.mask = INT_MSK_PWRHOLD_F_IT_MSK_MASK,
548c2ecf20Sopenharmony_ci		.reg_offset = 0,
558c2ecf20Sopenharmony_ci	},
568c2ecf20Sopenharmony_ci	[TPS65911_IRQ_VBAT_VMHI] = {
578c2ecf20Sopenharmony_ci		.mask = INT_MSK_VMBHI_IT_MSK_MASK,
588c2ecf20Sopenharmony_ci		.reg_offset = 0,
598c2ecf20Sopenharmony_ci	},
608c2ecf20Sopenharmony_ci	[TPS65911_IRQ_PWRON] = {
618c2ecf20Sopenharmony_ci		.mask = INT_MSK_PWRON_IT_MSK_MASK,
628c2ecf20Sopenharmony_ci		.reg_offset = 0,
638c2ecf20Sopenharmony_ci	},
648c2ecf20Sopenharmony_ci	[TPS65911_IRQ_PWRON_LP] = {
658c2ecf20Sopenharmony_ci		.mask = INT_MSK_PWRON_LP_IT_MSK_MASK,
668c2ecf20Sopenharmony_ci		.reg_offset = 0,
678c2ecf20Sopenharmony_ci	},
688c2ecf20Sopenharmony_ci	[TPS65911_IRQ_PWRHOLD_R] = {
698c2ecf20Sopenharmony_ci		.mask = INT_MSK_PWRHOLD_R_IT_MSK_MASK,
708c2ecf20Sopenharmony_ci		.reg_offset = 0,
718c2ecf20Sopenharmony_ci	},
728c2ecf20Sopenharmony_ci	[TPS65911_IRQ_HOTDIE] = {
738c2ecf20Sopenharmony_ci		.mask = INT_MSK_HOTDIE_IT_MSK_MASK,
748c2ecf20Sopenharmony_ci		.reg_offset = 0,
758c2ecf20Sopenharmony_ci	},
768c2ecf20Sopenharmony_ci	[TPS65911_IRQ_RTC_ALARM] = {
778c2ecf20Sopenharmony_ci		.mask = INT_MSK_RTC_ALARM_IT_MSK_MASK,
788c2ecf20Sopenharmony_ci		.reg_offset = 0,
798c2ecf20Sopenharmony_ci	},
808c2ecf20Sopenharmony_ci	[TPS65911_IRQ_RTC_PERIOD] = {
818c2ecf20Sopenharmony_ci		.mask = INT_MSK_RTC_PERIOD_IT_MSK_MASK,
828c2ecf20Sopenharmony_ci		.reg_offset = 0,
838c2ecf20Sopenharmony_ci	},
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* INT_STS2 */
868c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO0_R] = {
878c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO0_R_IT_MSK_MASK,
888c2ecf20Sopenharmony_ci		.reg_offset = 1,
898c2ecf20Sopenharmony_ci	},
908c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO0_F] = {
918c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO0_F_IT_MSK_MASK,
928c2ecf20Sopenharmony_ci		.reg_offset = 1,
938c2ecf20Sopenharmony_ci	},
948c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO1_R] = {
958c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO1_R_IT_MSK_MASK,
968c2ecf20Sopenharmony_ci		.reg_offset = 1,
978c2ecf20Sopenharmony_ci	},
988c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO1_F] = {
998c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO1_F_IT_MSK_MASK,
1008c2ecf20Sopenharmony_ci		.reg_offset = 1,
1018c2ecf20Sopenharmony_ci	},
1028c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO2_R] = {
1038c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO2_R_IT_MSK_MASK,
1048c2ecf20Sopenharmony_ci		.reg_offset = 1,
1058c2ecf20Sopenharmony_ci	},
1068c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO2_F] = {
1078c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO2_F_IT_MSK_MASK,
1088c2ecf20Sopenharmony_ci		.reg_offset = 1,
1098c2ecf20Sopenharmony_ci	},
1108c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO3_R] = {
1118c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO3_R_IT_MSK_MASK,
1128c2ecf20Sopenharmony_ci		.reg_offset = 1,
1138c2ecf20Sopenharmony_ci	},
1148c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO3_F] = {
1158c2ecf20Sopenharmony_ci		.mask = INT_MSK2_GPIO3_F_IT_MSK_MASK,
1168c2ecf20Sopenharmony_ci		.reg_offset = 1,
1178c2ecf20Sopenharmony_ci	},
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* INT_STS2 */
1208c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO4_R] = {
1218c2ecf20Sopenharmony_ci		.mask = INT_MSK3_GPIO4_R_IT_MSK_MASK,
1228c2ecf20Sopenharmony_ci		.reg_offset = 2,
1238c2ecf20Sopenharmony_ci	},
1248c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO4_F] = {
1258c2ecf20Sopenharmony_ci		.mask = INT_MSK3_GPIO4_F_IT_MSK_MASK,
1268c2ecf20Sopenharmony_ci		.reg_offset = 2,
1278c2ecf20Sopenharmony_ci	},
1288c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO5_R] = {
1298c2ecf20Sopenharmony_ci		.mask = INT_MSK3_GPIO5_R_IT_MSK_MASK,
1308c2ecf20Sopenharmony_ci		.reg_offset = 2,
1318c2ecf20Sopenharmony_ci	},
1328c2ecf20Sopenharmony_ci	[TPS65911_IRQ_GPIO5_F] = {
1338c2ecf20Sopenharmony_ci		.mask = INT_MSK3_GPIO5_F_IT_MSK_MASK,
1348c2ecf20Sopenharmony_ci		.reg_offset = 2,
1358c2ecf20Sopenharmony_ci	},
1368c2ecf20Sopenharmony_ci	[TPS65911_IRQ_WTCHDG] = {
1378c2ecf20Sopenharmony_ci		.mask = INT_MSK3_WTCHDG_IT_MSK_MASK,
1388c2ecf20Sopenharmony_ci		.reg_offset = 2,
1398c2ecf20Sopenharmony_ci	},
1408c2ecf20Sopenharmony_ci	[TPS65911_IRQ_VMBCH2_H] = {
1418c2ecf20Sopenharmony_ci		.mask = INT_MSK3_VMBCH2_H_IT_MSK_MASK,
1428c2ecf20Sopenharmony_ci		.reg_offset = 2,
1438c2ecf20Sopenharmony_ci	},
1448c2ecf20Sopenharmony_ci	[TPS65911_IRQ_VMBCH2_L] = {
1458c2ecf20Sopenharmony_ci		.mask = INT_MSK3_VMBCH2_L_IT_MSK_MASK,
1468c2ecf20Sopenharmony_ci		.reg_offset = 2,
1478c2ecf20Sopenharmony_ci	},
1488c2ecf20Sopenharmony_ci	[TPS65911_IRQ_PWRDN] = {
1498c2ecf20Sopenharmony_ci		.mask = INT_MSK3_PWRDN_IT_MSK_MASK,
1508c2ecf20Sopenharmony_ci		.reg_offset = 2,
1518c2ecf20Sopenharmony_ci	},
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic const struct regmap_irq tps65910_irqs[] = {
1558c2ecf20Sopenharmony_ci	/* INT_STS */
1568c2ecf20Sopenharmony_ci	[TPS65910_IRQ_VBAT_VMBDCH] = {
1578c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_VMBDCH_IT_MSK_MASK,
1588c2ecf20Sopenharmony_ci		.reg_offset = 0,
1598c2ecf20Sopenharmony_ci	},
1608c2ecf20Sopenharmony_ci	[TPS65910_IRQ_VBAT_VMHI] = {
1618c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_VMBHI_IT_MSK_MASK,
1628c2ecf20Sopenharmony_ci		.reg_offset = 0,
1638c2ecf20Sopenharmony_ci	},
1648c2ecf20Sopenharmony_ci	[TPS65910_IRQ_PWRON] = {
1658c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_PWRON_IT_MSK_MASK,
1668c2ecf20Sopenharmony_ci		.reg_offset = 0,
1678c2ecf20Sopenharmony_ci	},
1688c2ecf20Sopenharmony_ci	[TPS65910_IRQ_PWRON_LP] = {
1698c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_PWRON_LP_IT_MSK_MASK,
1708c2ecf20Sopenharmony_ci		.reg_offset = 0,
1718c2ecf20Sopenharmony_ci	},
1728c2ecf20Sopenharmony_ci	[TPS65910_IRQ_PWRHOLD] = {
1738c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_PWRHOLD_IT_MSK_MASK,
1748c2ecf20Sopenharmony_ci		.reg_offset = 0,
1758c2ecf20Sopenharmony_ci	},
1768c2ecf20Sopenharmony_ci	[TPS65910_IRQ_HOTDIE] = {
1778c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_HOTDIE_IT_MSK_MASK,
1788c2ecf20Sopenharmony_ci		.reg_offset = 0,
1798c2ecf20Sopenharmony_ci	},
1808c2ecf20Sopenharmony_ci	[TPS65910_IRQ_RTC_ALARM] = {
1818c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_RTC_ALARM_IT_MSK_MASK,
1828c2ecf20Sopenharmony_ci		.reg_offset = 0,
1838c2ecf20Sopenharmony_ci	},
1848c2ecf20Sopenharmony_ci	[TPS65910_IRQ_RTC_PERIOD] = {
1858c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK_RTC_PERIOD_IT_MSK_MASK,
1868c2ecf20Sopenharmony_ci		.reg_offset = 0,
1878c2ecf20Sopenharmony_ci	},
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	/* INT_STS2 */
1908c2ecf20Sopenharmony_ci	[TPS65910_IRQ_GPIO_R] = {
1918c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK2_GPIO0_F_IT_MSK_MASK,
1928c2ecf20Sopenharmony_ci		.reg_offset = 1,
1938c2ecf20Sopenharmony_ci	},
1948c2ecf20Sopenharmony_ci	[TPS65910_IRQ_GPIO_F] = {
1958c2ecf20Sopenharmony_ci		.mask = TPS65910_INT_MSK2_GPIO0_R_IT_MSK_MASK,
1968c2ecf20Sopenharmony_ci		.reg_offset = 1,
1978c2ecf20Sopenharmony_ci	},
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic struct regmap_irq_chip tps65911_irq_chip = {
2018c2ecf20Sopenharmony_ci	.name = "tps65910",
2028c2ecf20Sopenharmony_ci	.irqs = tps65911_irqs,
2038c2ecf20Sopenharmony_ci	.num_irqs = ARRAY_SIZE(tps65911_irqs),
2048c2ecf20Sopenharmony_ci	.num_regs = 3,
2058c2ecf20Sopenharmony_ci	.irq_reg_stride = 2,
2068c2ecf20Sopenharmony_ci	.status_base = TPS65910_INT_STS,
2078c2ecf20Sopenharmony_ci	.mask_base = TPS65910_INT_MSK,
2088c2ecf20Sopenharmony_ci	.ack_base = TPS65910_INT_STS,
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic struct regmap_irq_chip tps65910_irq_chip = {
2128c2ecf20Sopenharmony_ci	.name = "tps65910",
2138c2ecf20Sopenharmony_ci	.irqs = tps65910_irqs,
2148c2ecf20Sopenharmony_ci	.num_irqs = ARRAY_SIZE(tps65910_irqs),
2158c2ecf20Sopenharmony_ci	.num_regs = 2,
2168c2ecf20Sopenharmony_ci	.irq_reg_stride = 2,
2178c2ecf20Sopenharmony_ci	.status_base = TPS65910_INT_STS,
2188c2ecf20Sopenharmony_ci	.mask_base = TPS65910_INT_MSK,
2198c2ecf20Sopenharmony_ci	.ack_base = TPS65910_INT_STS,
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic int tps65910_irq_init(struct tps65910 *tps65910, int irq,
2238c2ecf20Sopenharmony_ci		    struct tps65910_platform_data *pdata)
2248c2ecf20Sopenharmony_ci{
2258c2ecf20Sopenharmony_ci	int ret;
2268c2ecf20Sopenharmony_ci	static struct regmap_irq_chip *tps6591x_irqs_chip;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if (!irq) {
2298c2ecf20Sopenharmony_ci		dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n");
2308c2ecf20Sopenharmony_ci		return -EINVAL;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (!pdata) {
2348c2ecf20Sopenharmony_ci		dev_warn(tps65910->dev, "No interrupt support, no pdata\n");
2358c2ecf20Sopenharmony_ci		return -EINVAL;
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	switch (tps65910_chip_id(tps65910)) {
2398c2ecf20Sopenharmony_ci	case TPS65910:
2408c2ecf20Sopenharmony_ci		tps6591x_irqs_chip = &tps65910_irq_chip;
2418c2ecf20Sopenharmony_ci		break;
2428c2ecf20Sopenharmony_ci	case TPS65911:
2438c2ecf20Sopenharmony_ci		tps6591x_irqs_chip = &tps65911_irq_chip;
2448c2ecf20Sopenharmony_ci		break;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	tps65910->chip_irq = irq;
2488c2ecf20Sopenharmony_ci	ret = devm_regmap_add_irq_chip(tps65910->dev, tps65910->regmap,
2498c2ecf20Sopenharmony_ci				       tps65910->chip_irq,
2508c2ecf20Sopenharmony_ci				       IRQF_ONESHOT, pdata->irq_base,
2518c2ecf20Sopenharmony_ci				       tps6591x_irqs_chip, &tps65910->irq_data);
2528c2ecf20Sopenharmony_ci	if (ret < 0) {
2538c2ecf20Sopenharmony_ci		dev_warn(tps65910->dev, "Failed to add irq_chip %d\n", ret);
2548c2ecf20Sopenharmony_ci		tps65910->chip_irq = 0;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	return ret;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic bool is_volatile_reg(struct device *dev, unsigned int reg)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	struct tps65910 *tps65910 = dev_get_drvdata(dev);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/*
2648c2ecf20Sopenharmony_ci	 * Caching all regulator registers.
2658c2ecf20Sopenharmony_ci	 * All regualator register address range is same for
2668c2ecf20Sopenharmony_ci	 * TPS65910 and TPS65911
2678c2ecf20Sopenharmony_ci	 */
2688c2ecf20Sopenharmony_ci	if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
2698c2ecf20Sopenharmony_ci		/* Check for non-existing register */
2708c2ecf20Sopenharmony_ci		if (tps65910_chip_id(tps65910) == TPS65910)
2718c2ecf20Sopenharmony_ci			if ((reg == TPS65911_VDDCTRL_OP) ||
2728c2ecf20Sopenharmony_ci				(reg == TPS65911_VDDCTRL_SR))
2738c2ecf20Sopenharmony_ci				return true;
2748c2ecf20Sopenharmony_ci		return false;
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci	return true;
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic const struct regmap_config tps65910_regmap_config = {
2808c2ecf20Sopenharmony_ci	.reg_bits = 8,
2818c2ecf20Sopenharmony_ci	.val_bits = 8,
2828c2ecf20Sopenharmony_ci	.volatile_reg = is_volatile_reg,
2838c2ecf20Sopenharmony_ci	.max_register = TPS65910_MAX_REGISTER - 1,
2848c2ecf20Sopenharmony_ci	.cache_type = REGCACHE_RBTREE,
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int tps65910_ck32k_init(struct tps65910 *tps65910,
2888c2ecf20Sopenharmony_ci					struct tps65910_board *pmic_pdata)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	int ret;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (!pmic_pdata->en_ck32k_xtal)
2938c2ecf20Sopenharmony_ci		return 0;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	ret = tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
2968c2ecf20Sopenharmony_ci						DEVCTRL_CK32K_CTRL_MASK);
2978c2ecf20Sopenharmony_ci	if (ret < 0) {
2988c2ecf20Sopenharmony_ci		dev_err(tps65910->dev, "clear ck32k_ctrl failed: %d\n", ret);
2998c2ecf20Sopenharmony_ci		return ret;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	return 0;
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cistatic int tps65910_sleepinit(struct tps65910 *tps65910,
3068c2ecf20Sopenharmony_ci		struct tps65910_board *pmic_pdata)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct device *dev;
3098c2ecf20Sopenharmony_ci	int ret;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (!pmic_pdata->en_dev_slp)
3128c2ecf20Sopenharmony_ci		return 0;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	dev = tps65910->dev;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/* enabling SLEEP device state */
3178c2ecf20Sopenharmony_ci	ret = tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
3188c2ecf20Sopenharmony_ci				DEVCTRL_DEV_SLP_MASK);
3198c2ecf20Sopenharmony_ci	if (ret < 0) {
3208c2ecf20Sopenharmony_ci		dev_err(dev, "set dev_slp failed: %d\n", ret);
3218c2ecf20Sopenharmony_ci		goto err_sleep_init;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (pmic_pdata->slp_keepon.therm_keepon) {
3258c2ecf20Sopenharmony_ci		ret = tps65910_reg_set_bits(tps65910,
3268c2ecf20Sopenharmony_ci				TPS65910_SLEEP_KEEP_RES_ON,
3278c2ecf20Sopenharmony_ci				SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
3288c2ecf20Sopenharmony_ci		if (ret < 0) {
3298c2ecf20Sopenharmony_ci			dev_err(dev, "set therm_keepon failed: %d\n", ret);
3308c2ecf20Sopenharmony_ci			goto disable_dev_slp;
3318c2ecf20Sopenharmony_ci		}
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	if (pmic_pdata->slp_keepon.clkout32k_keepon) {
3358c2ecf20Sopenharmony_ci		ret = tps65910_reg_set_bits(tps65910,
3368c2ecf20Sopenharmony_ci				TPS65910_SLEEP_KEEP_RES_ON,
3378c2ecf20Sopenharmony_ci				SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
3388c2ecf20Sopenharmony_ci		if (ret < 0) {
3398c2ecf20Sopenharmony_ci			dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
3408c2ecf20Sopenharmony_ci			goto disable_dev_slp;
3418c2ecf20Sopenharmony_ci		}
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (pmic_pdata->slp_keepon.i2chs_keepon) {
3458c2ecf20Sopenharmony_ci		ret = tps65910_reg_set_bits(tps65910,
3468c2ecf20Sopenharmony_ci				TPS65910_SLEEP_KEEP_RES_ON,
3478c2ecf20Sopenharmony_ci				SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
3488c2ecf20Sopenharmony_ci		if (ret < 0) {
3498c2ecf20Sopenharmony_ci			dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
3508c2ecf20Sopenharmony_ci			goto disable_dev_slp;
3518c2ecf20Sopenharmony_ci		}
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	return 0;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cidisable_dev_slp:
3578c2ecf20Sopenharmony_ci	tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
3588c2ecf20Sopenharmony_ci				DEVCTRL_DEV_SLP_MASK);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cierr_sleep_init:
3618c2ecf20Sopenharmony_ci	return ret;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
3658c2ecf20Sopenharmony_cistatic const struct of_device_id tps65910_of_match[] = {
3668c2ecf20Sopenharmony_ci	{ .compatible = "ti,tps65910", .data = (void *)TPS65910},
3678c2ecf20Sopenharmony_ci	{ .compatible = "ti,tps65911", .data = (void *)TPS65911},
3688c2ecf20Sopenharmony_ci	{ },
3698c2ecf20Sopenharmony_ci};
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
3728c2ecf20Sopenharmony_ci						unsigned long *chip_id)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	struct device_node *np = client->dev.of_node;
3758c2ecf20Sopenharmony_ci	struct tps65910_board *board_info;
3768c2ecf20Sopenharmony_ci	unsigned int prop;
3778c2ecf20Sopenharmony_ci	const struct of_device_id *match;
3788c2ecf20Sopenharmony_ci	int ret;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	match = of_match_device(tps65910_of_match, &client->dev);
3818c2ecf20Sopenharmony_ci	if (!match) {
3828c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to find matching dt id\n");
3838c2ecf20Sopenharmony_ci		return NULL;
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	*chip_id  = (unsigned long)match->data;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
3898c2ecf20Sopenharmony_ci			GFP_KERNEL);
3908c2ecf20Sopenharmony_ci	if (!board_info)
3918c2ecf20Sopenharmony_ci		return NULL;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
3948c2ecf20Sopenharmony_ci	if (!ret)
3958c2ecf20Sopenharmony_ci		board_info->vmbch_threshold = prop;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
3988c2ecf20Sopenharmony_ci	if (!ret)
3998c2ecf20Sopenharmony_ci		board_info->vmbch2_threshold = prop;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	prop = of_property_read_bool(np, "ti,en-ck32k-xtal");
4028c2ecf20Sopenharmony_ci	board_info->en_ck32k_xtal = prop;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	prop = of_property_read_bool(np, "ti,sleep-enable");
4058c2ecf20Sopenharmony_ci	board_info->en_dev_slp = prop;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	prop = of_property_read_bool(np, "ti,sleep-keep-therm");
4088c2ecf20Sopenharmony_ci	board_info->slp_keepon.therm_keepon = prop;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	prop = of_property_read_bool(np, "ti,sleep-keep-ck32k");
4118c2ecf20Sopenharmony_ci	board_info->slp_keepon.clkout32k_keepon = prop;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	prop = of_property_read_bool(np, "ti,sleep-keep-hsclk");
4148c2ecf20Sopenharmony_ci	board_info->slp_keepon.i2chs_keepon = prop;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	board_info->irq = client->irq;
4178c2ecf20Sopenharmony_ci	board_info->irq_base = -1;
4188c2ecf20Sopenharmony_ci	board_info->pm_off = of_property_read_bool(np,
4198c2ecf20Sopenharmony_ci			"ti,system-power-controller");
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	return board_info;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci#else
4248c2ecf20Sopenharmony_cistatic inline
4258c2ecf20Sopenharmony_cistruct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
4268c2ecf20Sopenharmony_ci					 unsigned long *chip_id)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	return NULL;
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci#endif
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic struct i2c_client *tps65910_i2c_client;
4338c2ecf20Sopenharmony_cistatic void tps65910_power_off(void)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	struct tps65910 *tps65910;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	tps65910 = dev_get_drvdata(&tps65910_i2c_client->dev);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	if (tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
4408c2ecf20Sopenharmony_ci			DEVCTRL_PWR_OFF_MASK) < 0)
4418c2ecf20Sopenharmony_ci		return;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
4448c2ecf20Sopenharmony_ci			DEVCTRL_DEV_ON_MASK);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic int tps65910_i2c_probe(struct i2c_client *i2c,
4488c2ecf20Sopenharmony_ci			      const struct i2c_device_id *id)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct tps65910 *tps65910;
4518c2ecf20Sopenharmony_ci	struct tps65910_board *pmic_plat_data;
4528c2ecf20Sopenharmony_ci	struct tps65910_board *of_pmic_plat_data = NULL;
4538c2ecf20Sopenharmony_ci	struct tps65910_platform_data *init_data;
4548c2ecf20Sopenharmony_ci	unsigned long chip_id = id->driver_data;
4558c2ecf20Sopenharmony_ci	int ret;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	pmic_plat_data = dev_get_platdata(&i2c->dev);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	if (!pmic_plat_data && i2c->dev.of_node) {
4608c2ecf20Sopenharmony_ci		pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
4618c2ecf20Sopenharmony_ci		of_pmic_plat_data = pmic_plat_data;
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	if (!pmic_plat_data)
4658c2ecf20Sopenharmony_ci		return -EINVAL;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
4688c2ecf20Sopenharmony_ci	if (init_data == NULL)
4698c2ecf20Sopenharmony_ci		return -ENOMEM;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
4728c2ecf20Sopenharmony_ci	if (tps65910 == NULL)
4738c2ecf20Sopenharmony_ci		return -ENOMEM;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	tps65910->of_plat_data = of_pmic_plat_data;
4768c2ecf20Sopenharmony_ci	i2c_set_clientdata(i2c, tps65910);
4778c2ecf20Sopenharmony_ci	tps65910->dev = &i2c->dev;
4788c2ecf20Sopenharmony_ci	tps65910->i2c_client = i2c;
4798c2ecf20Sopenharmony_ci	tps65910->id = chip_id;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* Work around silicon erratum SWCZ010: the tps65910 may miss the
4828c2ecf20Sopenharmony_ci	 * first I2C transfer. So issue a dummy transfer before the first
4838c2ecf20Sopenharmony_ci	 * real transfer.
4848c2ecf20Sopenharmony_ci	 */
4858c2ecf20Sopenharmony_ci	i2c_master_send(i2c, "", 1);
4868c2ecf20Sopenharmony_ci	tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
4878c2ecf20Sopenharmony_ci	if (IS_ERR(tps65910->regmap)) {
4888c2ecf20Sopenharmony_ci		ret = PTR_ERR(tps65910->regmap);
4898c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
4908c2ecf20Sopenharmony_ci		return ret;
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	init_data->irq = pmic_plat_data->irq;
4948c2ecf20Sopenharmony_ci	init_data->irq_base = pmic_plat_data->irq_base;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	tps65910_irq_init(tps65910, init_data->irq, init_data);
4978c2ecf20Sopenharmony_ci	tps65910_ck32k_init(tps65910, pmic_plat_data);
4988c2ecf20Sopenharmony_ci	tps65910_sleepinit(tps65910, pmic_plat_data);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (pmic_plat_data->pm_off && !pm_power_off) {
5018c2ecf20Sopenharmony_ci		tps65910_i2c_client = i2c;
5028c2ecf20Sopenharmony_ci		pm_power_off = tps65910_power_off;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	ret = devm_mfd_add_devices(tps65910->dev, -1,
5068c2ecf20Sopenharmony_ci				   tps65910s, ARRAY_SIZE(tps65910s),
5078c2ecf20Sopenharmony_ci				   NULL, 0,
5088c2ecf20Sopenharmony_ci				   regmap_irq_get_domain(tps65910->irq_data));
5098c2ecf20Sopenharmony_ci	if (ret < 0) {
5108c2ecf20Sopenharmony_ci		dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
5118c2ecf20Sopenharmony_ci		return ret;
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	return ret;
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic const struct i2c_device_id tps65910_i2c_id[] = {
5188c2ecf20Sopenharmony_ci       { "tps65910", TPS65910 },
5198c2ecf20Sopenharmony_ci       { "tps65911", TPS65911 },
5208c2ecf20Sopenharmony_ci       { }
5218c2ecf20Sopenharmony_ci};
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic struct i2c_driver tps65910_i2c_driver = {
5248c2ecf20Sopenharmony_ci	.driver = {
5258c2ecf20Sopenharmony_ci		   .name = "tps65910",
5268c2ecf20Sopenharmony_ci		   .of_match_table = of_match_ptr(tps65910_of_match),
5278c2ecf20Sopenharmony_ci	},
5288c2ecf20Sopenharmony_ci	.probe = tps65910_i2c_probe,
5298c2ecf20Sopenharmony_ci	.id_table = tps65910_i2c_id,
5308c2ecf20Sopenharmony_ci};
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_cistatic int __init tps65910_i2c_init(void)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci	return i2c_add_driver(&tps65910_i2c_driver);
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci/* init early so consumer devices can complete system boot */
5378c2ecf20Sopenharmony_cisubsys_initcall(tps65910_i2c_init);
538