18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// SLG51000 High PSRR, Multi-Output Regulators
48c2ecf20Sopenharmony_ci// Copyright (C) 2019  Dialog Semiconductor
58c2ecf20Sopenharmony_ci//
68c2ecf20Sopenharmony_ci// Author: Eric Jeong <eric.jeong.opensource@diasemi.com>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/err.h>
98c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
108c2ecf20Sopenharmony_ci#include <linux/i2c.h>
118c2ecf20Sopenharmony_ci#include <linux/init.h>
128c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
138c2ecf20Sopenharmony_ci#include <linux/irq.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/regmap.h>
178c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
188c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
198c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
208c2ecf20Sopenharmony_ci#include "slg51000-regulator.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define SLG51000_SCTL_EVT               7
238c2ecf20Sopenharmony_ci#define SLG51000_MAX_EVT_REGISTER       8
248c2ecf20Sopenharmony_ci#define SLG51000_LDOHP_LV_MIN           1200000
258c2ecf20Sopenharmony_ci#define SLG51000_LDOHP_HV_MIN           2400000
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cienum slg51000_regulators {
288c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO1 = 0,
298c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO2,
308c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO3,
318c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO4,
328c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO5,
338c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO6,
348c2ecf20Sopenharmony_ci	SLG51000_REGULATOR_LDO7,
358c2ecf20Sopenharmony_ci	SLG51000_MAX_REGULATORS,
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistruct slg51000 {
398c2ecf20Sopenharmony_ci	struct device *dev;
408c2ecf20Sopenharmony_ci	struct regmap *regmap;
418c2ecf20Sopenharmony_ci	struct regulator_desc *rdesc[SLG51000_MAX_REGULATORS];
428c2ecf20Sopenharmony_ci	struct regulator_dev *rdev[SLG51000_MAX_REGULATORS];
438c2ecf20Sopenharmony_ci	struct gpio_desc *cs_gpiod;
448c2ecf20Sopenharmony_ci	int chip_irq;
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistruct slg51000_evt_sta {
488c2ecf20Sopenharmony_ci	unsigned int ereg;
498c2ecf20Sopenharmony_ci	unsigned int sreg;
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic const struct slg51000_evt_sta es_reg[SLG51000_MAX_EVT_REGISTER] = {
538c2ecf20Sopenharmony_ci	{SLG51000_LDO1_EVENT, SLG51000_LDO1_STATUS},
548c2ecf20Sopenharmony_ci	{SLG51000_LDO2_EVENT, SLG51000_LDO2_STATUS},
558c2ecf20Sopenharmony_ci	{SLG51000_LDO3_EVENT, SLG51000_LDO3_STATUS},
568c2ecf20Sopenharmony_ci	{SLG51000_LDO4_EVENT, SLG51000_LDO4_STATUS},
578c2ecf20Sopenharmony_ci	{SLG51000_LDO5_EVENT, SLG51000_LDO5_STATUS},
588c2ecf20Sopenharmony_ci	{SLG51000_LDO6_EVENT, SLG51000_LDO6_STATUS},
598c2ecf20Sopenharmony_ci	{SLG51000_LDO7_EVENT, SLG51000_LDO7_STATUS},
608c2ecf20Sopenharmony_ci	{SLG51000_SYSCTL_EVENT, SLG51000_SYSCTL_STATUS},
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct regmap_range slg51000_writeable_ranges[] = {
648c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_MATRIX_CONF_A,
658c2ecf20Sopenharmony_ci			 SLG51000_SYSCTL_MATRIX_CONF_A),
668c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_VSEL, SLG51000_LDO1_VSEL),
678c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_MINV, SLG51000_LDO1_MAXV),
688c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_IRQ_MASK, SLG51000_LDO1_IRQ_MASK),
698c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_VSEL, SLG51000_LDO2_VSEL),
708c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_MINV, SLG51000_LDO2_MAXV),
718c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_IRQ_MASK, SLG51000_LDO2_IRQ_MASK),
728c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_VSEL, SLG51000_LDO3_VSEL),
738c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_MINV, SLG51000_LDO3_MAXV),
748c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_IRQ_MASK, SLG51000_LDO3_IRQ_MASK),
758c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_VSEL, SLG51000_LDO4_VSEL),
768c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_MINV, SLG51000_LDO4_MAXV),
778c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_IRQ_MASK, SLG51000_LDO4_IRQ_MASK),
788c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_VSEL, SLG51000_LDO5_VSEL),
798c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_MINV, SLG51000_LDO5_MAXV),
808c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_IRQ_MASK, SLG51000_LDO5_IRQ_MASK),
818c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_VSEL, SLG51000_LDO6_VSEL),
828c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_MINV, SLG51000_LDO6_MAXV),
838c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_IRQ_MASK, SLG51000_LDO6_IRQ_MASK),
848c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_VSEL, SLG51000_LDO7_VSEL),
858c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_MINV, SLG51000_LDO7_MAXV),
868c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_IRQ_MASK, SLG51000_LDO7_IRQ_MASK),
878c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_IRQ_MASK, SLG51000_OTP_IRQ_MASK),
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic const struct regmap_range slg51000_readable_ranges[] = {
918c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_PATN_ID_B0,
928c2ecf20Sopenharmony_ci			 SLG51000_SYSCTL_PATN_ID_B2),
938c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_SYS_CONF_A,
948c2ecf20Sopenharmony_ci			 SLG51000_SYSCTL_SYS_CONF_A),
958c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_SYS_CONF_D,
968c2ecf20Sopenharmony_ci			 SLG51000_SYSCTL_MATRIX_CONF_B),
978c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_REFGEN_CONF_C,
988c2ecf20Sopenharmony_ci			 SLG51000_SYSCTL_UVLO_CONF_A),
998c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_FAULT_LOG1, SLG51000_SYSCTL_IRQ_MASK),
1008c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_IO_GPIO1_CONF, SLG51000_IO_GPIO_STATUS),
1018c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LUTARRAY_LUT_VAL_0,
1028c2ecf20Sopenharmony_ci			 SLG51000_LUTARRAY_LUT_VAL_11),
1038c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_MUXARRAY_INPUT_SEL_0,
1048c2ecf20Sopenharmony_ci			 SLG51000_MUXARRAY_INPUT_SEL_63),
1058c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_PWRSEQ_RESOURCE_EN_0,
1068c2ecf20Sopenharmony_ci			 SLG51000_PWRSEQ_INPUT_SENSE_CONF_B),
1078c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_VSEL, SLG51000_LDO1_VSEL),
1088c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_MINV, SLG51000_LDO1_MAXV),
1098c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_MISC1, SLG51000_LDO1_VSEL_ACTUAL),
1108c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_EVENT, SLG51000_LDO1_IRQ_MASK),
1118c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_VSEL, SLG51000_LDO2_VSEL),
1128c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_MINV, SLG51000_LDO2_MAXV),
1138c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_MISC1, SLG51000_LDO2_VSEL_ACTUAL),
1148c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_EVENT, SLG51000_LDO2_IRQ_MASK),
1158c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_VSEL, SLG51000_LDO3_VSEL),
1168c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_MINV, SLG51000_LDO3_MAXV),
1178c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_CONF1, SLG51000_LDO3_VSEL_ACTUAL),
1188c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_EVENT, SLG51000_LDO3_IRQ_MASK),
1198c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_VSEL, SLG51000_LDO4_VSEL),
1208c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_MINV, SLG51000_LDO4_MAXV),
1218c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_CONF1, SLG51000_LDO4_VSEL_ACTUAL),
1228c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_EVENT, SLG51000_LDO4_IRQ_MASK),
1238c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_VSEL, SLG51000_LDO5_VSEL),
1248c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_MINV, SLG51000_LDO5_MAXV),
1258c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_TRIM2, SLG51000_LDO5_TRIM2),
1268c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_CONF1, SLG51000_LDO5_VSEL_ACTUAL),
1278c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_EVENT, SLG51000_LDO5_IRQ_MASK),
1288c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_VSEL, SLG51000_LDO6_VSEL),
1298c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_MINV, SLG51000_LDO6_MAXV),
1308c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_TRIM2, SLG51000_LDO6_TRIM2),
1318c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_CONF1, SLG51000_LDO6_VSEL_ACTUAL),
1328c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_EVENT, SLG51000_LDO6_IRQ_MASK),
1338c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_VSEL, SLG51000_LDO7_VSEL),
1348c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_MINV, SLG51000_LDO7_MAXV),
1358c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_CONF1, SLG51000_LDO7_VSEL_ACTUAL),
1368c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_EVENT, SLG51000_LDO7_IRQ_MASK),
1378c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_EVENT, SLG51000_OTP_EVENT),
1388c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_IRQ_MASK, SLG51000_OTP_IRQ_MASK),
1398c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_LOCK_OTP_PROG, SLG51000_OTP_LOCK_CTRL),
1408c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LOCK_GLOBAL_LOCK_CTRL1,
1418c2ecf20Sopenharmony_ci			 SLG51000_LOCK_GLOBAL_LOCK_CTRL1),
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic const struct regmap_range slg51000_volatile_ranges[] = {
1458c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_FAULT_LOG1, SLG51000_SYSCTL_STATUS),
1468c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_IO_GPIO_STATUS, SLG51000_IO_GPIO_STATUS),
1478c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_EVENT, SLG51000_LDO1_STATUS),
1488c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_EVENT, SLG51000_LDO2_STATUS),
1498c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_EVENT, SLG51000_LDO3_STATUS),
1508c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_EVENT, SLG51000_LDO4_STATUS),
1518c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_EVENT, SLG51000_LDO5_STATUS),
1528c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_EVENT, SLG51000_LDO6_STATUS),
1538c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_EVENT, SLG51000_LDO7_STATUS),
1548c2ecf20Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_EVENT, SLG51000_OTP_EVENT),
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic const struct regmap_access_table slg51000_writeable_table = {
1588c2ecf20Sopenharmony_ci	.yes_ranges	= slg51000_writeable_ranges,
1598c2ecf20Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(slg51000_writeable_ranges),
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const struct regmap_access_table slg51000_readable_table = {
1638c2ecf20Sopenharmony_ci	.yes_ranges	= slg51000_readable_ranges,
1648c2ecf20Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(slg51000_readable_ranges),
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic const struct regmap_access_table slg51000_volatile_table = {
1688c2ecf20Sopenharmony_ci	.yes_ranges	= slg51000_volatile_ranges,
1698c2ecf20Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(slg51000_volatile_ranges),
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic const struct regmap_config slg51000_regmap_config = {
1738c2ecf20Sopenharmony_ci	.reg_bits = 16,
1748c2ecf20Sopenharmony_ci	.val_bits = 8,
1758c2ecf20Sopenharmony_ci	.max_register = 0x8000,
1768c2ecf20Sopenharmony_ci	.wr_table = &slg51000_writeable_table,
1778c2ecf20Sopenharmony_ci	.rd_table = &slg51000_readable_table,
1788c2ecf20Sopenharmony_ci	.volatile_table = &slg51000_volatile_table,
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic const struct regulator_ops slg51000_regl_ops = {
1828c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
1838c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
1848c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
1858c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
1868c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear,
1878c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
1888c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic const struct regulator_ops slg51000_switch_ops = {
1928c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
1938c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
1948c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic int slg51000_of_parse_cb(struct device_node *np,
1988c2ecf20Sopenharmony_ci				const struct regulator_desc *desc,
1998c2ecf20Sopenharmony_ci				struct regulator_config *config)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	struct gpio_desc *ena_gpiod;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(np), "enable", 0,
2048c2ecf20Sopenharmony_ci					   GPIOD_OUT_LOW |
2058c2ecf20Sopenharmony_ci						GPIOD_FLAGS_BIT_NONEXCLUSIVE,
2068c2ecf20Sopenharmony_ci					   "gpio-en-ldo");
2078c2ecf20Sopenharmony_ci	if (!IS_ERR(ena_gpiod))
2088c2ecf20Sopenharmony_ci		config->ena_gpiod = ena_gpiod;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define SLG51000_REGL_DESC(_id, _name, _s_name, _min, _step) \
2148c2ecf20Sopenharmony_ci	[SLG51000_REGULATOR_##_id] = {                             \
2158c2ecf20Sopenharmony_ci		.name = #_name,                                    \
2168c2ecf20Sopenharmony_ci		.supply_name = _s_name,				   \
2178c2ecf20Sopenharmony_ci		.id = SLG51000_REGULATOR_##_id,                    \
2188c2ecf20Sopenharmony_ci		.of_match = of_match_ptr(#_name),                  \
2198c2ecf20Sopenharmony_ci		.of_parse_cb = slg51000_of_parse_cb,               \
2208c2ecf20Sopenharmony_ci		.ops = &slg51000_regl_ops,                         \
2218c2ecf20Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),     \
2228c2ecf20Sopenharmony_ci		.n_voltages = 256,                                 \
2238c2ecf20Sopenharmony_ci		.min_uV = _min,                                    \
2248c2ecf20Sopenharmony_ci		.uV_step = _step,                                  \
2258c2ecf20Sopenharmony_ci		.linear_min_sel = 0,                               \
2268c2ecf20Sopenharmony_ci		.vsel_mask = SLG51000_VSEL_MASK,                   \
2278c2ecf20Sopenharmony_ci		.vsel_reg = SLG51000_##_id##_VSEL,                 \
2288c2ecf20Sopenharmony_ci		.enable_reg = SLG51000_SYSCTL_MATRIX_CONF_A,       \
2298c2ecf20Sopenharmony_ci		.enable_mask = BIT(SLG51000_REGULATOR_##_id),      \
2308c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,                         \
2318c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,                              \
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic struct regulator_desc regls_desc[SLG51000_MAX_REGULATORS] = {
2358c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO1, ldo1, NULL,   2400000,  5000),
2368c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO2, ldo2, NULL,   2400000,  5000),
2378c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO3, ldo3, "vin3", 1200000, 10000),
2388c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO4, ldo4, "vin4", 1200000, 10000),
2398c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO5, ldo5, "vin5",  400000,  5000),
2408c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO6, ldo6, "vin6",  400000,  5000),
2418c2ecf20Sopenharmony_ci	SLG51000_REGL_DESC(LDO7, ldo7, "vin7", 1200000, 10000),
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int slg51000_regulator_init(struct slg51000 *chip)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct regulator_config config = { };
2478c2ecf20Sopenharmony_ci	struct regulator_desc *rdesc;
2488c2ecf20Sopenharmony_ci	unsigned int reg, val;
2498c2ecf20Sopenharmony_ci	u8 vsel_range[2];
2508c2ecf20Sopenharmony_ci	int id, ret = 0;
2518c2ecf20Sopenharmony_ci	const unsigned int min_regs[SLG51000_MAX_REGULATORS] = {
2528c2ecf20Sopenharmony_ci		SLG51000_LDO1_MINV, SLG51000_LDO2_MINV, SLG51000_LDO3_MINV,
2538c2ecf20Sopenharmony_ci		SLG51000_LDO4_MINV, SLG51000_LDO5_MINV, SLG51000_LDO6_MINV,
2548c2ecf20Sopenharmony_ci		SLG51000_LDO7_MINV,
2558c2ecf20Sopenharmony_ci	};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	for (id = 0; id < SLG51000_MAX_REGULATORS; id++) {
2588c2ecf20Sopenharmony_ci		chip->rdesc[id] = &regls_desc[id];
2598c2ecf20Sopenharmony_ci		rdesc = chip->rdesc[id];
2608c2ecf20Sopenharmony_ci		config.regmap = chip->regmap;
2618c2ecf20Sopenharmony_ci		config.dev = chip->dev;
2628c2ecf20Sopenharmony_ci		config.driver_data = chip;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(chip->regmap, min_regs[id],
2658c2ecf20Sopenharmony_ci				       vsel_range, 2);
2668c2ecf20Sopenharmony_ci		if (ret < 0) {
2678c2ecf20Sopenharmony_ci			dev_err(chip->dev,
2688c2ecf20Sopenharmony_ci				"Failed to read the MIN register\n");
2698c2ecf20Sopenharmony_ci			return ret;
2708c2ecf20Sopenharmony_ci		}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		switch (id) {
2738c2ecf20Sopenharmony_ci		case SLG51000_REGULATOR_LDO1:
2748c2ecf20Sopenharmony_ci		case SLG51000_REGULATOR_LDO2:
2758c2ecf20Sopenharmony_ci			if (id == SLG51000_REGULATOR_LDO1)
2768c2ecf20Sopenharmony_ci				reg = SLG51000_LDO1_MISC1;
2778c2ecf20Sopenharmony_ci			else
2788c2ecf20Sopenharmony_ci				reg = SLG51000_LDO2_MISC1;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci			ret = regmap_read(chip->regmap, reg, &val);
2818c2ecf20Sopenharmony_ci			if (ret < 0) {
2828c2ecf20Sopenharmony_ci				dev_err(chip->dev,
2838c2ecf20Sopenharmony_ci					"Failed to read voltage range of ldo%d\n",
2848c2ecf20Sopenharmony_ci					id + 1);
2858c2ecf20Sopenharmony_ci				return ret;
2868c2ecf20Sopenharmony_ci			}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci			rdesc->linear_min_sel = vsel_range[0];
2898c2ecf20Sopenharmony_ci			rdesc->n_voltages = vsel_range[1] + 1;
2908c2ecf20Sopenharmony_ci			if (val & SLG51000_SEL_VRANGE_MASK)
2918c2ecf20Sopenharmony_ci				rdesc->min_uV = SLG51000_LDOHP_HV_MIN
2928c2ecf20Sopenharmony_ci						+ (vsel_range[0]
2938c2ecf20Sopenharmony_ci						   * rdesc->uV_step);
2948c2ecf20Sopenharmony_ci			else
2958c2ecf20Sopenharmony_ci				rdesc->min_uV = SLG51000_LDOHP_LV_MIN
2968c2ecf20Sopenharmony_ci						+ (vsel_range[0]
2978c2ecf20Sopenharmony_ci						   * rdesc->uV_step);
2988c2ecf20Sopenharmony_ci			break;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci		case SLG51000_REGULATOR_LDO5:
3018c2ecf20Sopenharmony_ci		case SLG51000_REGULATOR_LDO6:
3028c2ecf20Sopenharmony_ci			if (id == SLG51000_REGULATOR_LDO5)
3038c2ecf20Sopenharmony_ci				reg = SLG51000_LDO5_TRIM2;
3048c2ecf20Sopenharmony_ci			else
3058c2ecf20Sopenharmony_ci				reg = SLG51000_LDO6_TRIM2;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci			ret = regmap_read(chip->regmap, reg, &val);
3088c2ecf20Sopenharmony_ci			if (ret < 0) {
3098c2ecf20Sopenharmony_ci				dev_err(chip->dev,
3108c2ecf20Sopenharmony_ci					"Failed to read LDO mode register\n");
3118c2ecf20Sopenharmony_ci				return ret;
3128c2ecf20Sopenharmony_ci			}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci			if (val & SLG51000_SEL_BYP_MODE_MASK) {
3158c2ecf20Sopenharmony_ci				rdesc->ops = &slg51000_switch_ops;
3168c2ecf20Sopenharmony_ci				rdesc->n_voltages = 0;
3178c2ecf20Sopenharmony_ci				rdesc->min_uV = 0;
3188c2ecf20Sopenharmony_ci				rdesc->uV_step = 0;
3198c2ecf20Sopenharmony_ci				rdesc->linear_min_sel = 0;
3208c2ecf20Sopenharmony_ci				break;
3218c2ecf20Sopenharmony_ci			}
3228c2ecf20Sopenharmony_ci			fallthrough;	/* to the check below */
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci		default:
3258c2ecf20Sopenharmony_ci			rdesc->linear_min_sel = vsel_range[0];
3268c2ecf20Sopenharmony_ci			rdesc->n_voltages = vsel_range[1] + 1;
3278c2ecf20Sopenharmony_ci			rdesc->min_uV = rdesc->min_uV
3288c2ecf20Sopenharmony_ci					+ (vsel_range[0] * rdesc->uV_step);
3298c2ecf20Sopenharmony_ci			break;
3308c2ecf20Sopenharmony_ci		}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci		chip->rdev[id] = devm_regulator_register(chip->dev, rdesc,
3338c2ecf20Sopenharmony_ci							 &config);
3348c2ecf20Sopenharmony_ci		if (IS_ERR(chip->rdev[id])) {
3358c2ecf20Sopenharmony_ci			ret = PTR_ERR(chip->rdev[id]);
3368c2ecf20Sopenharmony_ci			dev_err(chip->dev,
3378c2ecf20Sopenharmony_ci				"Failed to register regulator(%s):%d\n",
3388c2ecf20Sopenharmony_ci				chip->rdesc[id]->name, ret);
3398c2ecf20Sopenharmony_ci			return ret;
3408c2ecf20Sopenharmony_ci		}
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	return 0;
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic irqreturn_t slg51000_irq_handler(int irq, void *data)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	struct slg51000 *chip = data;
3498c2ecf20Sopenharmony_ci	struct regmap *regmap = chip->regmap;
3508c2ecf20Sopenharmony_ci	enum { R0 = 0, R1, R2, REG_MAX };
3518c2ecf20Sopenharmony_ci	u8 evt[SLG51000_MAX_EVT_REGISTER][REG_MAX];
3528c2ecf20Sopenharmony_ci	int ret, i, handled = IRQ_NONE;
3538c2ecf20Sopenharmony_ci	unsigned int evt_otp, mask_otp;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* Read event[R0], status[R1] and mask[R2] register */
3568c2ecf20Sopenharmony_ci	for (i = 0; i < SLG51000_MAX_EVT_REGISTER; i++) {
3578c2ecf20Sopenharmony_ci		ret = regmap_bulk_read(regmap, es_reg[i].ereg, evt[i], REG_MAX);
3588c2ecf20Sopenharmony_ci		if (ret < 0) {
3598c2ecf20Sopenharmony_ci			dev_err(chip->dev,
3608c2ecf20Sopenharmony_ci				"Failed to read event registers(%d)\n", ret);
3618c2ecf20Sopenharmony_ci			return IRQ_NONE;
3628c2ecf20Sopenharmony_ci		}
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	ret = regmap_read(regmap, SLG51000_OTP_EVENT, &evt_otp);
3668c2ecf20Sopenharmony_ci	if (ret < 0) {
3678c2ecf20Sopenharmony_ci		dev_err(chip->dev,
3688c2ecf20Sopenharmony_ci			"Failed to read otp event registers(%d)\n", ret);
3698c2ecf20Sopenharmony_ci		return IRQ_NONE;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	ret = regmap_read(regmap, SLG51000_OTP_IRQ_MASK, &mask_otp);
3738c2ecf20Sopenharmony_ci	if (ret < 0) {
3748c2ecf20Sopenharmony_ci		dev_err(chip->dev,
3758c2ecf20Sopenharmony_ci			"Failed to read otp mask register(%d)\n", ret);
3768c2ecf20Sopenharmony_ci		return IRQ_NONE;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if ((evt_otp & SLG51000_EVT_CRC_MASK) &&
3808c2ecf20Sopenharmony_ci	    !(mask_otp & SLG51000_IRQ_CRC_MASK)) {
3818c2ecf20Sopenharmony_ci		dev_info(chip->dev,
3828c2ecf20Sopenharmony_ci			 "OTP has been read or OTP crc is not zero\n");
3838c2ecf20Sopenharmony_ci		handled = IRQ_HANDLED;
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	for (i = 0; i < SLG51000_MAX_REGULATORS; i++) {
3878c2ecf20Sopenharmony_ci		if (!(evt[i][R2] & SLG51000_IRQ_ILIM_FLAG_MASK) &&
3888c2ecf20Sopenharmony_ci		    (evt[i][R0] & SLG51000_EVT_ILIM_FLAG_MASK)) {
3898c2ecf20Sopenharmony_ci			regulator_notifier_call_chain(chip->rdev[i],
3908c2ecf20Sopenharmony_ci					    REGULATOR_EVENT_OVER_CURRENT, NULL);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci			if (evt[i][R1] & SLG51000_STA_ILIM_FLAG_MASK)
3938c2ecf20Sopenharmony_ci				dev_warn(chip->dev,
3948c2ecf20Sopenharmony_ci					 "Over-current limit(ldo%d)\n", i + 1);
3958c2ecf20Sopenharmony_ci			handled = IRQ_HANDLED;
3968c2ecf20Sopenharmony_ci		}
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (!(evt[SLG51000_SCTL_EVT][R2] & SLG51000_IRQ_HIGH_TEMP_WARN_MASK) &&
4008c2ecf20Sopenharmony_ci	    (evt[SLG51000_SCTL_EVT][R0] & SLG51000_EVT_HIGH_TEMP_WARN_MASK)) {
4018c2ecf20Sopenharmony_ci		for (i = 0; i < SLG51000_MAX_REGULATORS; i++) {
4028c2ecf20Sopenharmony_ci			if (!(evt[i][R1] & SLG51000_STA_ILIM_FLAG_MASK) &&
4038c2ecf20Sopenharmony_ci			    (evt[i][R1] & SLG51000_STA_VOUT_OK_FLAG_MASK)) {
4048c2ecf20Sopenharmony_ci				regulator_notifier_call_chain(chip->rdev[i],
4058c2ecf20Sopenharmony_ci					       REGULATOR_EVENT_OVER_TEMP, NULL);
4068c2ecf20Sopenharmony_ci			}
4078c2ecf20Sopenharmony_ci		}
4088c2ecf20Sopenharmony_ci		handled = IRQ_HANDLED;
4098c2ecf20Sopenharmony_ci		if (evt[SLG51000_SCTL_EVT][R1] &
4108c2ecf20Sopenharmony_ci		    SLG51000_STA_HIGH_TEMP_WARN_MASK)
4118c2ecf20Sopenharmony_ci			dev_warn(chip->dev, "High temperature warning!\n");
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	return handled;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic void slg51000_clear_fault_log(struct slg51000 *chip)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	unsigned int val = 0;
4208c2ecf20Sopenharmony_ci	int ret = 0;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	ret = regmap_read(chip->regmap, SLG51000_SYSCTL_FAULT_LOG1, &val);
4238c2ecf20Sopenharmony_ci	if (ret < 0) {
4248c2ecf20Sopenharmony_ci		dev_err(chip->dev, "Failed to read Fault log register\n");
4258c2ecf20Sopenharmony_ci		return;
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (val & SLG51000_FLT_OVER_TEMP_MASK)
4298c2ecf20Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_OVER_TEMP\n");
4308c2ecf20Sopenharmony_ci	if (val & SLG51000_FLT_POWER_SEQ_CRASH_REQ_MASK)
4318c2ecf20Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_POWER_SEQ_CRASH_REQ\n");
4328c2ecf20Sopenharmony_ci	if (val & SLG51000_FLT_RST_MASK)
4338c2ecf20Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_RST\n");
4348c2ecf20Sopenharmony_ci	if (val & SLG51000_FLT_POR_MASK)
4358c2ecf20Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_POR\n");
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int slg51000_i2c_probe(struct i2c_client *client)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
4418c2ecf20Sopenharmony_ci	struct slg51000 *chip;
4428c2ecf20Sopenharmony_ci	struct gpio_desc *cs_gpiod;
4438c2ecf20Sopenharmony_ci	int error, ret;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	chip = devm_kzalloc(dev, sizeof(struct slg51000), GFP_KERNEL);
4468c2ecf20Sopenharmony_ci	if (!chip)
4478c2ecf20Sopenharmony_ci		return -ENOMEM;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	cs_gpiod = devm_gpiod_get_optional(dev, "dlg,cs",
4508c2ecf20Sopenharmony_ci					   GPIOD_OUT_HIGH |
4518c2ecf20Sopenharmony_ci						GPIOD_FLAGS_BIT_NONEXCLUSIVE);
4528c2ecf20Sopenharmony_ci	if (IS_ERR(cs_gpiod))
4538c2ecf20Sopenharmony_ci		return PTR_ERR(cs_gpiod);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	if (cs_gpiod) {
4568c2ecf20Sopenharmony_ci		dev_info(dev, "Found chip selector property\n");
4578c2ecf20Sopenharmony_ci		chip->cs_gpiod = cs_gpiod;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, chip);
4638c2ecf20Sopenharmony_ci	chip->chip_irq = client->irq;
4648c2ecf20Sopenharmony_ci	chip->dev = dev;
4658c2ecf20Sopenharmony_ci	chip->regmap = devm_regmap_init_i2c(client, &slg51000_regmap_config);
4668c2ecf20Sopenharmony_ci	if (IS_ERR(chip->regmap)) {
4678c2ecf20Sopenharmony_ci		error = PTR_ERR(chip->regmap);
4688c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to allocate register map: %d\n",
4698c2ecf20Sopenharmony_ci			error);
4708c2ecf20Sopenharmony_ci		return error;
4718c2ecf20Sopenharmony_ci	}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	ret = slg51000_regulator_init(chip);
4748c2ecf20Sopenharmony_ci	if (ret < 0) {
4758c2ecf20Sopenharmony_ci		dev_err(chip->dev, "Failed to init regulator(%d)\n", ret);
4768c2ecf20Sopenharmony_ci		return ret;
4778c2ecf20Sopenharmony_ci	}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	slg51000_clear_fault_log(chip);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	if (chip->chip_irq) {
4828c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(dev, chip->chip_irq, NULL,
4838c2ecf20Sopenharmony_ci						slg51000_irq_handler,
4848c2ecf20Sopenharmony_ci						(IRQF_TRIGGER_HIGH |
4858c2ecf20Sopenharmony_ci						IRQF_ONESHOT),
4868c2ecf20Sopenharmony_ci						"slg51000-irq", chip);
4878c2ecf20Sopenharmony_ci		if (ret != 0) {
4888c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to request IRQ: %d\n",
4898c2ecf20Sopenharmony_ci				chip->chip_irq);
4908c2ecf20Sopenharmony_ci			return ret;
4918c2ecf20Sopenharmony_ci		}
4928c2ecf20Sopenharmony_ci	} else {
4938c2ecf20Sopenharmony_ci		dev_info(dev, "No IRQ configured\n");
4948c2ecf20Sopenharmony_ci	}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	return ret;
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_cistatic const struct i2c_device_id slg51000_i2c_id[] = {
5008c2ecf20Sopenharmony_ci	{"slg51000", 0},
5018c2ecf20Sopenharmony_ci	{},
5028c2ecf20Sopenharmony_ci};
5038c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, slg51000_i2c_id);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic struct i2c_driver slg51000_regulator_driver = {
5068c2ecf20Sopenharmony_ci	.driver = {
5078c2ecf20Sopenharmony_ci		.name = "slg51000-regulator",
5088c2ecf20Sopenharmony_ci	},
5098c2ecf20Sopenharmony_ci	.probe_new = slg51000_i2c_probe,
5108c2ecf20Sopenharmony_ci	.id_table = slg51000_i2c_id,
5118c2ecf20Sopenharmony_ci};
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cimodule_i2c_driver(slg51000_regulator_driver);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ciMODULE_AUTHOR("Eric Jeong <eric.jeong.opensource@diasemi.com>");
5168c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SLG51000 regulator driver");
5178c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
5188c2ecf20Sopenharmony_ci
519