162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci//
362306a36Sopenharmony_ci// SLG51000 High PSRR, Multi-Output Regulators
462306a36Sopenharmony_ci// Copyright (C) 2019  Dialog Semiconductor
562306a36Sopenharmony_ci//
662306a36Sopenharmony_ci// Author: Eric Jeong <eric.jeong.opensource@diasemi.com>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/err.h>
962306a36Sopenharmony_ci#include <linux/gpio/consumer.h>
1062306a36Sopenharmony_ci#include <linux/i2c.h>
1162306a36Sopenharmony_ci#include <linux/init.h>
1262306a36Sopenharmony_ci#include <linux/interrupt.h>
1362306a36Sopenharmony_ci#include <linux/irq.h>
1462306a36Sopenharmony_ci#include <linux/module.h>
1562306a36Sopenharmony_ci#include <linux/of.h>
1662306a36Sopenharmony_ci#include <linux/regmap.h>
1762306a36Sopenharmony_ci#include <linux/regulator/driver.h>
1862306a36Sopenharmony_ci#include <linux/regulator/machine.h>
1962306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h>
2062306a36Sopenharmony_ci#include "slg51000-regulator.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define SLG51000_SCTL_EVT               7
2362306a36Sopenharmony_ci#define SLG51000_MAX_EVT_REGISTER       8
2462306a36Sopenharmony_ci#define SLG51000_LDOHP_LV_MIN           1200000
2562306a36Sopenharmony_ci#define SLG51000_LDOHP_HV_MIN           2400000
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cienum slg51000_regulators {
2862306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO1 = 0,
2962306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO2,
3062306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO3,
3162306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO4,
3262306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO5,
3362306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO6,
3462306a36Sopenharmony_ci	SLG51000_REGULATOR_LDO7,
3562306a36Sopenharmony_ci	SLG51000_MAX_REGULATORS,
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistruct slg51000 {
3962306a36Sopenharmony_ci	struct device *dev;
4062306a36Sopenharmony_ci	struct regmap *regmap;
4162306a36Sopenharmony_ci	struct regulator_desc *rdesc[SLG51000_MAX_REGULATORS];
4262306a36Sopenharmony_ci	struct regulator_dev *rdev[SLG51000_MAX_REGULATORS];
4362306a36Sopenharmony_ci	struct gpio_desc *cs_gpiod;
4462306a36Sopenharmony_ci	int chip_irq;
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistruct slg51000_evt_sta {
4862306a36Sopenharmony_ci	unsigned int ereg;
4962306a36Sopenharmony_ci	unsigned int sreg;
5062306a36Sopenharmony_ci};
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic const struct slg51000_evt_sta es_reg[SLG51000_MAX_EVT_REGISTER] = {
5362306a36Sopenharmony_ci	{SLG51000_LDO1_EVENT, SLG51000_LDO1_STATUS},
5462306a36Sopenharmony_ci	{SLG51000_LDO2_EVENT, SLG51000_LDO2_STATUS},
5562306a36Sopenharmony_ci	{SLG51000_LDO3_EVENT, SLG51000_LDO3_STATUS},
5662306a36Sopenharmony_ci	{SLG51000_LDO4_EVENT, SLG51000_LDO4_STATUS},
5762306a36Sopenharmony_ci	{SLG51000_LDO5_EVENT, SLG51000_LDO5_STATUS},
5862306a36Sopenharmony_ci	{SLG51000_LDO6_EVENT, SLG51000_LDO6_STATUS},
5962306a36Sopenharmony_ci	{SLG51000_LDO7_EVENT, SLG51000_LDO7_STATUS},
6062306a36Sopenharmony_ci	{SLG51000_SYSCTL_EVENT, SLG51000_SYSCTL_STATUS},
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic const struct regmap_range slg51000_writeable_ranges[] = {
6462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_MATRIX_CONF_A,
6562306a36Sopenharmony_ci			 SLG51000_SYSCTL_MATRIX_CONF_A),
6662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_VSEL, SLG51000_LDO1_VSEL),
6762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_MINV, SLG51000_LDO1_MAXV),
6862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_IRQ_MASK, SLG51000_LDO1_IRQ_MASK),
6962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_VSEL, SLG51000_LDO2_VSEL),
7062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_MINV, SLG51000_LDO2_MAXV),
7162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_IRQ_MASK, SLG51000_LDO2_IRQ_MASK),
7262306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_VSEL, SLG51000_LDO3_VSEL),
7362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_MINV, SLG51000_LDO3_MAXV),
7462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_IRQ_MASK, SLG51000_LDO3_IRQ_MASK),
7562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_VSEL, SLG51000_LDO4_VSEL),
7662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_MINV, SLG51000_LDO4_MAXV),
7762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_IRQ_MASK, SLG51000_LDO4_IRQ_MASK),
7862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_VSEL, SLG51000_LDO5_VSEL),
7962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_MINV, SLG51000_LDO5_MAXV),
8062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_IRQ_MASK, SLG51000_LDO5_IRQ_MASK),
8162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_VSEL, SLG51000_LDO6_VSEL),
8262306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_MINV, SLG51000_LDO6_MAXV),
8362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_IRQ_MASK, SLG51000_LDO6_IRQ_MASK),
8462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_VSEL, SLG51000_LDO7_VSEL),
8562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_MINV, SLG51000_LDO7_MAXV),
8662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_IRQ_MASK, SLG51000_LDO7_IRQ_MASK),
8762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_IRQ_MASK, SLG51000_OTP_IRQ_MASK),
8862306a36Sopenharmony_ci};
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cistatic const struct regmap_range slg51000_readable_ranges[] = {
9162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_PATN_ID_B0,
9262306a36Sopenharmony_ci			 SLG51000_SYSCTL_PATN_ID_B2),
9362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_SYS_CONF_A,
9462306a36Sopenharmony_ci			 SLG51000_SYSCTL_SYS_CONF_A),
9562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_SYS_CONF_D,
9662306a36Sopenharmony_ci			 SLG51000_SYSCTL_MATRIX_CONF_B),
9762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_REFGEN_CONF_C,
9862306a36Sopenharmony_ci			 SLG51000_SYSCTL_UVLO_CONF_A),
9962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_FAULT_LOG1, SLG51000_SYSCTL_IRQ_MASK),
10062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_IO_GPIO1_CONF, SLG51000_IO_GPIO_STATUS),
10162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LUTARRAY_LUT_VAL_0,
10262306a36Sopenharmony_ci			 SLG51000_LUTARRAY_LUT_VAL_11),
10362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_MUXARRAY_INPUT_SEL_0,
10462306a36Sopenharmony_ci			 SLG51000_MUXARRAY_INPUT_SEL_63),
10562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_PWRSEQ_RESOURCE_EN_0,
10662306a36Sopenharmony_ci			 SLG51000_PWRSEQ_INPUT_SENSE_CONF_B),
10762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_VSEL, SLG51000_LDO1_VSEL),
10862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_MINV, SLG51000_LDO1_MAXV),
10962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_MISC1, SLG51000_LDO1_VSEL_ACTUAL),
11062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_EVENT, SLG51000_LDO1_IRQ_MASK),
11162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_VSEL, SLG51000_LDO2_VSEL),
11262306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_MINV, SLG51000_LDO2_MAXV),
11362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_MISC1, SLG51000_LDO2_VSEL_ACTUAL),
11462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_EVENT, SLG51000_LDO2_IRQ_MASK),
11562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_VSEL, SLG51000_LDO3_VSEL),
11662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_MINV, SLG51000_LDO3_MAXV),
11762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_CONF1, SLG51000_LDO3_VSEL_ACTUAL),
11862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_EVENT, SLG51000_LDO3_IRQ_MASK),
11962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_VSEL, SLG51000_LDO4_VSEL),
12062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_MINV, SLG51000_LDO4_MAXV),
12162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_CONF1, SLG51000_LDO4_VSEL_ACTUAL),
12262306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_EVENT, SLG51000_LDO4_IRQ_MASK),
12362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_VSEL, SLG51000_LDO5_VSEL),
12462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_MINV, SLG51000_LDO5_MAXV),
12562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_TRIM2, SLG51000_LDO5_TRIM2),
12662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_CONF1, SLG51000_LDO5_VSEL_ACTUAL),
12762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_EVENT, SLG51000_LDO5_IRQ_MASK),
12862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_VSEL, SLG51000_LDO6_VSEL),
12962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_MINV, SLG51000_LDO6_MAXV),
13062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_TRIM2, SLG51000_LDO6_TRIM2),
13162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_CONF1, SLG51000_LDO6_VSEL_ACTUAL),
13262306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_EVENT, SLG51000_LDO6_IRQ_MASK),
13362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_VSEL, SLG51000_LDO7_VSEL),
13462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_MINV, SLG51000_LDO7_MAXV),
13562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_CONF1, SLG51000_LDO7_VSEL_ACTUAL),
13662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_EVENT, SLG51000_LDO7_IRQ_MASK),
13762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_EVENT, SLG51000_OTP_EVENT),
13862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_IRQ_MASK, SLG51000_OTP_IRQ_MASK),
13962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_LOCK_OTP_PROG, SLG51000_OTP_LOCK_CTRL),
14062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LOCK_GLOBAL_LOCK_CTRL1,
14162306a36Sopenharmony_ci			 SLG51000_LOCK_GLOBAL_LOCK_CTRL1),
14262306a36Sopenharmony_ci};
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistatic const struct regmap_range slg51000_volatile_ranges[] = {
14562306a36Sopenharmony_ci	regmap_reg_range(SLG51000_SYSCTL_FAULT_LOG1, SLG51000_SYSCTL_STATUS),
14662306a36Sopenharmony_ci	regmap_reg_range(SLG51000_IO_GPIO_STATUS, SLG51000_IO_GPIO_STATUS),
14762306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO1_EVENT, SLG51000_LDO1_STATUS),
14862306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO2_EVENT, SLG51000_LDO2_STATUS),
14962306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO3_EVENT, SLG51000_LDO3_STATUS),
15062306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO4_EVENT, SLG51000_LDO4_STATUS),
15162306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO5_EVENT, SLG51000_LDO5_STATUS),
15262306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO6_EVENT, SLG51000_LDO6_STATUS),
15362306a36Sopenharmony_ci	regmap_reg_range(SLG51000_LDO7_EVENT, SLG51000_LDO7_STATUS),
15462306a36Sopenharmony_ci	regmap_reg_range(SLG51000_OTP_EVENT, SLG51000_OTP_EVENT),
15562306a36Sopenharmony_ci};
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cistatic const struct regmap_access_table slg51000_writeable_table = {
15862306a36Sopenharmony_ci	.yes_ranges	= slg51000_writeable_ranges,
15962306a36Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(slg51000_writeable_ranges),
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic const struct regmap_access_table slg51000_readable_table = {
16362306a36Sopenharmony_ci	.yes_ranges	= slg51000_readable_ranges,
16462306a36Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(slg51000_readable_ranges),
16562306a36Sopenharmony_ci};
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_cistatic const struct regmap_access_table slg51000_volatile_table = {
16862306a36Sopenharmony_ci	.yes_ranges	= slg51000_volatile_ranges,
16962306a36Sopenharmony_ci	.n_yes_ranges	= ARRAY_SIZE(slg51000_volatile_ranges),
17062306a36Sopenharmony_ci};
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_cistatic const struct regmap_config slg51000_regmap_config = {
17362306a36Sopenharmony_ci	.reg_bits = 16,
17462306a36Sopenharmony_ci	.val_bits = 8,
17562306a36Sopenharmony_ci	.max_register = 0x8000,
17662306a36Sopenharmony_ci	.wr_table = &slg51000_writeable_table,
17762306a36Sopenharmony_ci	.rd_table = &slg51000_readable_table,
17862306a36Sopenharmony_ci	.volatile_table = &slg51000_volatile_table,
17962306a36Sopenharmony_ci};
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic const struct regulator_ops slg51000_regl_ops = {
18262306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
18362306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
18462306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
18562306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
18662306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear,
18762306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
18862306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
18962306a36Sopenharmony_ci};
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_cistatic const struct regulator_ops slg51000_switch_ops = {
19262306a36Sopenharmony_ci	.enable = regulator_enable_regmap,
19362306a36Sopenharmony_ci	.disable = regulator_disable_regmap,
19462306a36Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
19562306a36Sopenharmony_ci};
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_cistatic int slg51000_of_parse_cb(struct device_node *np,
19862306a36Sopenharmony_ci				const struct regulator_desc *desc,
19962306a36Sopenharmony_ci				struct regulator_config *config)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	struct gpio_desc *ena_gpiod;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(np), "enable", 0,
20462306a36Sopenharmony_ci					   GPIOD_OUT_LOW |
20562306a36Sopenharmony_ci						GPIOD_FLAGS_BIT_NONEXCLUSIVE,
20662306a36Sopenharmony_ci					   "gpio-en-ldo");
20762306a36Sopenharmony_ci	if (!IS_ERR(ena_gpiod))
20862306a36Sopenharmony_ci		config->ena_gpiod = ena_gpiod;
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	return 0;
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci#define SLG51000_REGL_DESC(_id, _name, _s_name, _min, _step) \
21462306a36Sopenharmony_ci	[SLG51000_REGULATOR_##_id] = {                             \
21562306a36Sopenharmony_ci		.name = #_name,                                    \
21662306a36Sopenharmony_ci		.supply_name = _s_name,				   \
21762306a36Sopenharmony_ci		.id = SLG51000_REGULATOR_##_id,                    \
21862306a36Sopenharmony_ci		.of_match = of_match_ptr(#_name),                  \
21962306a36Sopenharmony_ci		.of_parse_cb = slg51000_of_parse_cb,               \
22062306a36Sopenharmony_ci		.ops = &slg51000_regl_ops,                         \
22162306a36Sopenharmony_ci		.regulators_node = of_match_ptr("regulators"),     \
22262306a36Sopenharmony_ci		.n_voltages = 256,                                 \
22362306a36Sopenharmony_ci		.min_uV = _min,                                    \
22462306a36Sopenharmony_ci		.uV_step = _step,                                  \
22562306a36Sopenharmony_ci		.linear_min_sel = 0,                               \
22662306a36Sopenharmony_ci		.vsel_mask = SLG51000_VSEL_MASK,                   \
22762306a36Sopenharmony_ci		.vsel_reg = SLG51000_##_id##_VSEL,                 \
22862306a36Sopenharmony_ci		.enable_reg = SLG51000_SYSCTL_MATRIX_CONF_A,       \
22962306a36Sopenharmony_ci		.enable_mask = BIT(SLG51000_REGULATOR_##_id),      \
23062306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,                         \
23162306a36Sopenharmony_ci		.owner = THIS_MODULE,                              \
23262306a36Sopenharmony_ci	}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cistatic struct regulator_desc regls_desc[SLG51000_MAX_REGULATORS] = {
23562306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO1, ldo1, NULL,   2400000,  5000),
23662306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO2, ldo2, NULL,   2400000,  5000),
23762306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO3, ldo3, "vin3", 1200000, 10000),
23862306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO4, ldo4, "vin4", 1200000, 10000),
23962306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO5, ldo5, "vin5",  400000,  5000),
24062306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO6, ldo6, "vin6",  400000,  5000),
24162306a36Sopenharmony_ci	SLG51000_REGL_DESC(LDO7, ldo7, "vin7", 1200000, 10000),
24262306a36Sopenharmony_ci};
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_cistatic int slg51000_regulator_init(struct slg51000 *chip)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci	struct regulator_config config = { };
24762306a36Sopenharmony_ci	struct regulator_desc *rdesc;
24862306a36Sopenharmony_ci	unsigned int reg, val;
24962306a36Sopenharmony_ci	u8 vsel_range[2];
25062306a36Sopenharmony_ci	int id, ret = 0;
25162306a36Sopenharmony_ci	const unsigned int min_regs[SLG51000_MAX_REGULATORS] = {
25262306a36Sopenharmony_ci		SLG51000_LDO1_MINV, SLG51000_LDO2_MINV, SLG51000_LDO3_MINV,
25362306a36Sopenharmony_ci		SLG51000_LDO4_MINV, SLG51000_LDO5_MINV, SLG51000_LDO6_MINV,
25462306a36Sopenharmony_ci		SLG51000_LDO7_MINV,
25562306a36Sopenharmony_ci	};
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	for (id = 0; id < SLG51000_MAX_REGULATORS; id++) {
25862306a36Sopenharmony_ci		chip->rdesc[id] = &regls_desc[id];
25962306a36Sopenharmony_ci		rdesc = chip->rdesc[id];
26062306a36Sopenharmony_ci		config.regmap = chip->regmap;
26162306a36Sopenharmony_ci		config.dev = chip->dev;
26262306a36Sopenharmony_ci		config.driver_data = chip;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci		ret = regmap_bulk_read(chip->regmap, min_regs[id],
26562306a36Sopenharmony_ci				       vsel_range, 2);
26662306a36Sopenharmony_ci		if (ret < 0) {
26762306a36Sopenharmony_ci			dev_err(chip->dev,
26862306a36Sopenharmony_ci				"Failed to read the MIN register\n");
26962306a36Sopenharmony_ci			return ret;
27062306a36Sopenharmony_ci		}
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci		switch (id) {
27362306a36Sopenharmony_ci		case SLG51000_REGULATOR_LDO1:
27462306a36Sopenharmony_ci		case SLG51000_REGULATOR_LDO2:
27562306a36Sopenharmony_ci			if (id == SLG51000_REGULATOR_LDO1)
27662306a36Sopenharmony_ci				reg = SLG51000_LDO1_MISC1;
27762306a36Sopenharmony_ci			else
27862306a36Sopenharmony_ci				reg = SLG51000_LDO2_MISC1;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci			ret = regmap_read(chip->regmap, reg, &val);
28162306a36Sopenharmony_ci			if (ret < 0) {
28262306a36Sopenharmony_ci				dev_err(chip->dev,
28362306a36Sopenharmony_ci					"Failed to read voltage range of ldo%d\n",
28462306a36Sopenharmony_ci					id + 1);
28562306a36Sopenharmony_ci				return ret;
28662306a36Sopenharmony_ci			}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci			rdesc->linear_min_sel = vsel_range[0];
28962306a36Sopenharmony_ci			rdesc->n_voltages = vsel_range[1] + 1;
29062306a36Sopenharmony_ci			if (val & SLG51000_SEL_VRANGE_MASK)
29162306a36Sopenharmony_ci				rdesc->min_uV = SLG51000_LDOHP_HV_MIN
29262306a36Sopenharmony_ci						+ (vsel_range[0]
29362306a36Sopenharmony_ci						   * rdesc->uV_step);
29462306a36Sopenharmony_ci			else
29562306a36Sopenharmony_ci				rdesc->min_uV = SLG51000_LDOHP_LV_MIN
29662306a36Sopenharmony_ci						+ (vsel_range[0]
29762306a36Sopenharmony_ci						   * rdesc->uV_step);
29862306a36Sopenharmony_ci			break;
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci		case SLG51000_REGULATOR_LDO5:
30162306a36Sopenharmony_ci		case SLG51000_REGULATOR_LDO6:
30262306a36Sopenharmony_ci			if (id == SLG51000_REGULATOR_LDO5)
30362306a36Sopenharmony_ci				reg = SLG51000_LDO5_TRIM2;
30462306a36Sopenharmony_ci			else
30562306a36Sopenharmony_ci				reg = SLG51000_LDO6_TRIM2;
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci			ret = regmap_read(chip->regmap, reg, &val);
30862306a36Sopenharmony_ci			if (ret < 0) {
30962306a36Sopenharmony_ci				dev_err(chip->dev,
31062306a36Sopenharmony_ci					"Failed to read LDO mode register\n");
31162306a36Sopenharmony_ci				return ret;
31262306a36Sopenharmony_ci			}
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci			if (val & SLG51000_SEL_BYP_MODE_MASK) {
31562306a36Sopenharmony_ci				rdesc->ops = &slg51000_switch_ops;
31662306a36Sopenharmony_ci				rdesc->n_voltages = 0;
31762306a36Sopenharmony_ci				rdesc->min_uV = 0;
31862306a36Sopenharmony_ci				rdesc->uV_step = 0;
31962306a36Sopenharmony_ci				rdesc->linear_min_sel = 0;
32062306a36Sopenharmony_ci				break;
32162306a36Sopenharmony_ci			}
32262306a36Sopenharmony_ci			fallthrough;	/* to the check below */
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci		default:
32562306a36Sopenharmony_ci			rdesc->linear_min_sel = vsel_range[0];
32662306a36Sopenharmony_ci			rdesc->n_voltages = vsel_range[1] + 1;
32762306a36Sopenharmony_ci			rdesc->min_uV = rdesc->min_uV
32862306a36Sopenharmony_ci					+ (vsel_range[0] * rdesc->uV_step);
32962306a36Sopenharmony_ci			break;
33062306a36Sopenharmony_ci		}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci		chip->rdev[id] = devm_regulator_register(chip->dev, rdesc,
33362306a36Sopenharmony_ci							 &config);
33462306a36Sopenharmony_ci		if (IS_ERR(chip->rdev[id])) {
33562306a36Sopenharmony_ci			ret = PTR_ERR(chip->rdev[id]);
33662306a36Sopenharmony_ci			dev_err(chip->dev,
33762306a36Sopenharmony_ci				"Failed to register regulator(%s):%d\n",
33862306a36Sopenharmony_ci				chip->rdesc[id]->name, ret);
33962306a36Sopenharmony_ci			return ret;
34062306a36Sopenharmony_ci		}
34162306a36Sopenharmony_ci	}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	return 0;
34462306a36Sopenharmony_ci}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_cistatic irqreturn_t slg51000_irq_handler(int irq, void *data)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	struct slg51000 *chip = data;
34962306a36Sopenharmony_ci	struct regmap *regmap = chip->regmap;
35062306a36Sopenharmony_ci	enum { R0 = 0, R1, R2, REG_MAX };
35162306a36Sopenharmony_ci	u8 evt[SLG51000_MAX_EVT_REGISTER][REG_MAX];
35262306a36Sopenharmony_ci	int ret, i, handled = IRQ_NONE;
35362306a36Sopenharmony_ci	unsigned int evt_otp, mask_otp;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	/* Read event[R0], status[R1] and mask[R2] register */
35662306a36Sopenharmony_ci	for (i = 0; i < SLG51000_MAX_EVT_REGISTER; i++) {
35762306a36Sopenharmony_ci		ret = regmap_bulk_read(regmap, es_reg[i].ereg, evt[i], REG_MAX);
35862306a36Sopenharmony_ci		if (ret < 0) {
35962306a36Sopenharmony_ci			dev_err(chip->dev,
36062306a36Sopenharmony_ci				"Failed to read event registers(%d)\n", ret);
36162306a36Sopenharmony_ci			return IRQ_NONE;
36262306a36Sopenharmony_ci		}
36362306a36Sopenharmony_ci	}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	ret = regmap_read(regmap, SLG51000_OTP_EVENT, &evt_otp);
36662306a36Sopenharmony_ci	if (ret < 0) {
36762306a36Sopenharmony_ci		dev_err(chip->dev,
36862306a36Sopenharmony_ci			"Failed to read otp event registers(%d)\n", ret);
36962306a36Sopenharmony_ci		return IRQ_NONE;
37062306a36Sopenharmony_ci	}
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	ret = regmap_read(regmap, SLG51000_OTP_IRQ_MASK, &mask_otp);
37362306a36Sopenharmony_ci	if (ret < 0) {
37462306a36Sopenharmony_ci		dev_err(chip->dev,
37562306a36Sopenharmony_ci			"Failed to read otp mask register(%d)\n", ret);
37662306a36Sopenharmony_ci		return IRQ_NONE;
37762306a36Sopenharmony_ci	}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	if ((evt_otp & SLG51000_EVT_CRC_MASK) &&
38062306a36Sopenharmony_ci	    !(mask_otp & SLG51000_IRQ_CRC_MASK)) {
38162306a36Sopenharmony_ci		dev_info(chip->dev,
38262306a36Sopenharmony_ci			 "OTP has been read or OTP crc is not zero\n");
38362306a36Sopenharmony_ci		handled = IRQ_HANDLED;
38462306a36Sopenharmony_ci	}
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	for (i = 0; i < SLG51000_MAX_REGULATORS; i++) {
38762306a36Sopenharmony_ci		if (!(evt[i][R2] & SLG51000_IRQ_ILIM_FLAG_MASK) &&
38862306a36Sopenharmony_ci		    (evt[i][R0] & SLG51000_EVT_ILIM_FLAG_MASK)) {
38962306a36Sopenharmony_ci			regulator_notifier_call_chain(chip->rdev[i],
39062306a36Sopenharmony_ci					    REGULATOR_EVENT_OVER_CURRENT, NULL);
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci			if (evt[i][R1] & SLG51000_STA_ILIM_FLAG_MASK)
39362306a36Sopenharmony_ci				dev_warn(chip->dev,
39462306a36Sopenharmony_ci					 "Over-current limit(ldo%d)\n", i + 1);
39562306a36Sopenharmony_ci			handled = IRQ_HANDLED;
39662306a36Sopenharmony_ci		}
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	if (!(evt[SLG51000_SCTL_EVT][R2] & SLG51000_IRQ_HIGH_TEMP_WARN_MASK) &&
40062306a36Sopenharmony_ci	    (evt[SLG51000_SCTL_EVT][R0] & SLG51000_EVT_HIGH_TEMP_WARN_MASK)) {
40162306a36Sopenharmony_ci		for (i = 0; i < SLG51000_MAX_REGULATORS; i++) {
40262306a36Sopenharmony_ci			if (!(evt[i][R1] & SLG51000_STA_ILIM_FLAG_MASK) &&
40362306a36Sopenharmony_ci			    (evt[i][R1] & SLG51000_STA_VOUT_OK_FLAG_MASK)) {
40462306a36Sopenharmony_ci				regulator_notifier_call_chain(chip->rdev[i],
40562306a36Sopenharmony_ci					       REGULATOR_EVENT_OVER_TEMP, NULL);
40662306a36Sopenharmony_ci			}
40762306a36Sopenharmony_ci		}
40862306a36Sopenharmony_ci		handled = IRQ_HANDLED;
40962306a36Sopenharmony_ci		if (evt[SLG51000_SCTL_EVT][R1] &
41062306a36Sopenharmony_ci		    SLG51000_STA_HIGH_TEMP_WARN_MASK)
41162306a36Sopenharmony_ci			dev_warn(chip->dev, "High temperature warning!\n");
41262306a36Sopenharmony_ci	}
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	return handled;
41562306a36Sopenharmony_ci}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_cistatic void slg51000_clear_fault_log(struct slg51000 *chip)
41862306a36Sopenharmony_ci{
41962306a36Sopenharmony_ci	unsigned int val = 0;
42062306a36Sopenharmony_ci	int ret = 0;
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	ret = regmap_read(chip->regmap, SLG51000_SYSCTL_FAULT_LOG1, &val);
42362306a36Sopenharmony_ci	if (ret < 0) {
42462306a36Sopenharmony_ci		dev_err(chip->dev, "Failed to read Fault log register\n");
42562306a36Sopenharmony_ci		return;
42662306a36Sopenharmony_ci	}
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	if (val & SLG51000_FLT_OVER_TEMP_MASK)
42962306a36Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_OVER_TEMP\n");
43062306a36Sopenharmony_ci	if (val & SLG51000_FLT_POWER_SEQ_CRASH_REQ_MASK)
43162306a36Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_POWER_SEQ_CRASH_REQ\n");
43262306a36Sopenharmony_ci	if (val & SLG51000_FLT_RST_MASK)
43362306a36Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_RST\n");
43462306a36Sopenharmony_ci	if (val & SLG51000_FLT_POR_MASK)
43562306a36Sopenharmony_ci		dev_dbg(chip->dev, "Fault log: FLT_POR\n");
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_cistatic int slg51000_i2c_probe(struct i2c_client *client)
43962306a36Sopenharmony_ci{
44062306a36Sopenharmony_ci	struct device *dev = &client->dev;
44162306a36Sopenharmony_ci	struct slg51000 *chip;
44262306a36Sopenharmony_ci	struct gpio_desc *cs_gpiod;
44362306a36Sopenharmony_ci	int error, ret;
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	chip = devm_kzalloc(dev, sizeof(struct slg51000), GFP_KERNEL);
44662306a36Sopenharmony_ci	if (!chip)
44762306a36Sopenharmony_ci		return -ENOMEM;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	cs_gpiod = devm_gpiod_get_optional(dev, "dlg,cs",
45062306a36Sopenharmony_ci					   GPIOD_OUT_HIGH |
45162306a36Sopenharmony_ci						GPIOD_FLAGS_BIT_NONEXCLUSIVE);
45262306a36Sopenharmony_ci	if (IS_ERR(cs_gpiod))
45362306a36Sopenharmony_ci		return PTR_ERR(cs_gpiod);
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci	if (cs_gpiod) {
45662306a36Sopenharmony_ci		dev_info(dev, "Found chip selector property\n");
45762306a36Sopenharmony_ci		chip->cs_gpiod = cs_gpiod;
45862306a36Sopenharmony_ci	}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	usleep_range(10000, 11000);
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	i2c_set_clientdata(client, chip);
46362306a36Sopenharmony_ci	chip->chip_irq = client->irq;
46462306a36Sopenharmony_ci	chip->dev = dev;
46562306a36Sopenharmony_ci	chip->regmap = devm_regmap_init_i2c(client, &slg51000_regmap_config);
46662306a36Sopenharmony_ci	if (IS_ERR(chip->regmap)) {
46762306a36Sopenharmony_ci		error = PTR_ERR(chip->regmap);
46862306a36Sopenharmony_ci		dev_err(dev, "Failed to allocate register map: %d\n",
46962306a36Sopenharmony_ci			error);
47062306a36Sopenharmony_ci		return error;
47162306a36Sopenharmony_ci	}
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	ret = slg51000_regulator_init(chip);
47462306a36Sopenharmony_ci	if (ret < 0) {
47562306a36Sopenharmony_ci		dev_err(chip->dev, "Failed to init regulator(%d)\n", ret);
47662306a36Sopenharmony_ci		return ret;
47762306a36Sopenharmony_ci	}
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci	slg51000_clear_fault_log(chip);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	if (chip->chip_irq) {
48262306a36Sopenharmony_ci		ret = devm_request_threaded_irq(dev, chip->chip_irq, NULL,
48362306a36Sopenharmony_ci						slg51000_irq_handler,
48462306a36Sopenharmony_ci						(IRQF_TRIGGER_HIGH |
48562306a36Sopenharmony_ci						IRQF_ONESHOT),
48662306a36Sopenharmony_ci						"slg51000-irq", chip);
48762306a36Sopenharmony_ci		if (ret != 0) {
48862306a36Sopenharmony_ci			dev_err(dev, "Failed to request IRQ: %d\n",
48962306a36Sopenharmony_ci				chip->chip_irq);
49062306a36Sopenharmony_ci			return ret;
49162306a36Sopenharmony_ci		}
49262306a36Sopenharmony_ci	} else {
49362306a36Sopenharmony_ci		dev_info(dev, "No IRQ configured\n");
49462306a36Sopenharmony_ci	}
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	return ret;
49762306a36Sopenharmony_ci}
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_cistatic const struct i2c_device_id slg51000_i2c_id[] = {
50062306a36Sopenharmony_ci	{"slg51000", 0},
50162306a36Sopenharmony_ci	{},
50262306a36Sopenharmony_ci};
50362306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, slg51000_i2c_id);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_cistatic struct i2c_driver slg51000_regulator_driver = {
50662306a36Sopenharmony_ci	.driver = {
50762306a36Sopenharmony_ci		.name = "slg51000-regulator",
50862306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
50962306a36Sopenharmony_ci	},
51062306a36Sopenharmony_ci	.probe = slg51000_i2c_probe,
51162306a36Sopenharmony_ci	.id_table = slg51000_i2c_id,
51262306a36Sopenharmony_ci};
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_cimodule_i2c_driver(slg51000_regulator_driver);
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ciMODULE_AUTHOR("Eric Jeong <eric.jeong.opensource@diasemi.com>");
51762306a36Sopenharmony_ciMODULE_DESCRIPTION("SLG51000 regulator driver");
51862306a36Sopenharmony_ciMODULE_LICENSE("GPL");
51962306a36Sopenharmony_ci
520