162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * TI TPS65132 Regulator driver
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 2017 NVIDIA CORPORATION. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Author: Venkat Reddy Talla <vreddytalla@nvidia.com>
762306a36Sopenharmony_ci *		Laxman Dewangan <ldewangan@nvidia.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or
1062306a36Sopenharmony_ci * modify it under the terms of the GNU General Public License as
1162306a36Sopenharmony_ci * published by the Free Software Foundation; either version 2 of the
1262306a36Sopenharmony_ci * License, or (at your option) any later version.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
1562306a36Sopenharmony_ci * whether express or implied; without even the implied warranty of
1662306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1762306a36Sopenharmony_ci * General Public License for more details.
1862306a36Sopenharmony_ci */
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <linux/delay.h>
2162306a36Sopenharmony_ci#include <linux/err.h>
2262306a36Sopenharmony_ci#include <linux/gpio/consumer.h>
2362306a36Sopenharmony_ci#include <linux/i2c.h>
2462306a36Sopenharmony_ci#include <linux/module.h>
2562306a36Sopenharmony_ci#include <linux/regmap.h>
2662306a36Sopenharmony_ci#include <linux/regulator/driver.h>
2762306a36Sopenharmony_ci#include <linux/regulator/machine.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define TPS65132_REG_VPOS		0x00
3062306a36Sopenharmony_ci#define TPS65132_REG_VNEG		0x01
3162306a36Sopenharmony_ci#define TPS65132_REG_APPS_DISP_DISN	0x03
3262306a36Sopenharmony_ci#define TPS65132_REG_CONTROL		0x0FF
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#define TPS65132_VOUT_MASK		0x1F
3562306a36Sopenharmony_ci#define TPS65132_VOUT_N_VOLTAGE		0x15
3662306a36Sopenharmony_ci#define TPS65132_VOUT_VMIN		4000000
3762306a36Sopenharmony_ci#define TPS65132_VOUT_VMAX		6000000
3862306a36Sopenharmony_ci#define TPS65132_VOUT_STEP		100000
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define TPS65132_REG_APPS_DIS_VPOS		BIT(0)
4162306a36Sopenharmony_ci#define TPS65132_REG_APPS_DIS_VNEG		BIT(1)
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define TPS65132_REGULATOR_ID_VPOS	0
4462306a36Sopenharmony_ci#define TPS65132_REGULATOR_ID_VNEG	1
4562306a36Sopenharmony_ci#define TPS65132_MAX_REGULATORS		2
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define TPS65132_ACT_DIS_TIME_SLACK		1000
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistruct tps65132_reg_pdata {
5062306a36Sopenharmony_ci	struct gpio_desc *en_gpiod;
5162306a36Sopenharmony_ci	struct gpio_desc *act_dis_gpiod;
5262306a36Sopenharmony_ci	unsigned int act_dis_time_us;
5362306a36Sopenharmony_ci	int ena_gpio_state;
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct tps65132_regulator {
5762306a36Sopenharmony_ci	struct device *dev;
5862306a36Sopenharmony_ci	struct tps65132_reg_pdata reg_pdata[TPS65132_MAX_REGULATORS];
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cistatic int tps65132_regulator_enable(struct regulator_dev *rdev)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
6462306a36Sopenharmony_ci	int id = rdev_get_id(rdev);
6562306a36Sopenharmony_ci	struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
6662306a36Sopenharmony_ci	int ret;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (!IS_ERR(rpdata->en_gpiod)) {
6962306a36Sopenharmony_ci		gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
7062306a36Sopenharmony_ci		rpdata->ena_gpio_state = 1;
7162306a36Sopenharmony_ci	}
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	/* Hardware automatically enable discharge bit in enable */
7462306a36Sopenharmony_ci	if (rdev->constraints->active_discharge ==
7562306a36Sopenharmony_ci			REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
7662306a36Sopenharmony_ci		ret = regulator_set_active_discharge_regmap(rdev, false);
7762306a36Sopenharmony_ci		if (ret < 0) {
7862306a36Sopenharmony_ci			dev_err(tps->dev, "Failed to disable active discharge: %d\n",
7962306a36Sopenharmony_ci				ret);
8062306a36Sopenharmony_ci			return ret;
8162306a36Sopenharmony_ci		}
8262306a36Sopenharmony_ci	}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	return 0;
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_cistatic int tps65132_regulator_disable(struct regulator_dev *rdev)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
9062306a36Sopenharmony_ci	int id = rdev_get_id(rdev);
9162306a36Sopenharmony_ci	struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	if (!IS_ERR(rpdata->en_gpiod)) {
9462306a36Sopenharmony_ci		gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
9562306a36Sopenharmony_ci		rpdata->ena_gpio_state = 0;
9662306a36Sopenharmony_ci	}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	if (!IS_ERR(rpdata->act_dis_gpiod)) {
9962306a36Sopenharmony_ci		gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 1);
10062306a36Sopenharmony_ci		usleep_range(rpdata->act_dis_time_us, rpdata->act_dis_time_us +
10162306a36Sopenharmony_ci			     TPS65132_ACT_DIS_TIME_SLACK);
10262306a36Sopenharmony_ci		gpiod_set_value_cansleep(rpdata->act_dis_gpiod, 0);
10362306a36Sopenharmony_ci	}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	return 0;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic int tps65132_regulator_is_enabled(struct regulator_dev *rdev)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	struct tps65132_regulator *tps = rdev_get_drvdata(rdev);
11162306a36Sopenharmony_ci	int id = rdev_get_id(rdev);
11262306a36Sopenharmony_ci	struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[id];
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	if (!IS_ERR(rpdata->en_gpiod))
11562306a36Sopenharmony_ci		return rpdata->ena_gpio_state;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	return 1;
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic const struct regulator_ops tps65132_regulator_ops = {
12162306a36Sopenharmony_ci	.enable = tps65132_regulator_enable,
12262306a36Sopenharmony_ci	.disable = tps65132_regulator_disable,
12362306a36Sopenharmony_ci	.is_enabled = tps65132_regulator_is_enabled,
12462306a36Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
12562306a36Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear,
12662306a36Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
12762306a36Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
12862306a36Sopenharmony_ci	.set_active_discharge = regulator_set_active_discharge_regmap,
12962306a36Sopenharmony_ci};
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic int tps65132_of_parse_cb(struct device_node *np,
13262306a36Sopenharmony_ci				const struct regulator_desc *desc,
13362306a36Sopenharmony_ci				struct regulator_config *config)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	struct tps65132_regulator *tps = config->driver_data;
13662306a36Sopenharmony_ci	struct tps65132_reg_pdata *rpdata = &tps->reg_pdata[desc->id];
13762306a36Sopenharmony_ci	int ret;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	rpdata->en_gpiod = devm_fwnode_gpiod_get(tps->dev, of_fwnode_handle(np),
14062306a36Sopenharmony_ci						 "enable", GPIOD_ASIS,
14162306a36Sopenharmony_ci						 "enable");
14262306a36Sopenharmony_ci	if (IS_ERR(rpdata->en_gpiod)) {
14362306a36Sopenharmony_ci		ret = PTR_ERR(rpdata->en_gpiod);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci		/* Ignore the error other than probe defer */
14662306a36Sopenharmony_ci		if (ret == -EPROBE_DEFER)
14762306a36Sopenharmony_ci			return ret;
14862306a36Sopenharmony_ci		return 0;
14962306a36Sopenharmony_ci	}
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	rpdata->act_dis_gpiod = devm_fwnode_gpiod_get(tps->dev,
15262306a36Sopenharmony_ci						      of_fwnode_handle(np),
15362306a36Sopenharmony_ci						      "active-discharge",
15462306a36Sopenharmony_ci						      GPIOD_ASIS,
15562306a36Sopenharmony_ci						      "active-discharge");
15662306a36Sopenharmony_ci	if (IS_ERR(rpdata->act_dis_gpiod)) {
15762306a36Sopenharmony_ci		ret = PTR_ERR(rpdata->act_dis_gpiod);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci		/* Ignore the error other than probe defer */
16062306a36Sopenharmony_ci		if (ret == -EPROBE_DEFER)
16162306a36Sopenharmony_ci			return ret;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci		return 0;
16462306a36Sopenharmony_ci	}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	ret = of_property_read_u32(np, "ti,active-discharge-time-us",
16762306a36Sopenharmony_ci				   &rpdata->act_dis_time_us);
16862306a36Sopenharmony_ci	if (ret < 0) {
16962306a36Sopenharmony_ci		dev_err(tps->dev, "Failed to read active discharge time:%d\n",
17062306a36Sopenharmony_ci			ret);
17162306a36Sopenharmony_ci		return ret;
17262306a36Sopenharmony_ci	}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	return 0;
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#define TPS65132_REGULATOR_DESC(_id, _name)		\
17862306a36Sopenharmony_ci	[TPS65132_REGULATOR_ID_##_id] = {		\
17962306a36Sopenharmony_ci		.name = "tps65132-"#_name,		\
18062306a36Sopenharmony_ci		.supply_name = "vin",			\
18162306a36Sopenharmony_ci		.id = TPS65132_REGULATOR_ID_##_id,	\
18262306a36Sopenharmony_ci		.of_match = of_match_ptr(#_name),	\
18362306a36Sopenharmony_ci		.of_parse_cb	= tps65132_of_parse_cb,	\
18462306a36Sopenharmony_ci		.ops = &tps65132_regulator_ops,		\
18562306a36Sopenharmony_ci		.n_voltages = TPS65132_VOUT_N_VOLTAGE,	\
18662306a36Sopenharmony_ci		.min_uV = TPS65132_VOUT_VMIN,		\
18762306a36Sopenharmony_ci		.uV_step = TPS65132_VOUT_STEP,		\
18862306a36Sopenharmony_ci		.enable_time = 500,			\
18962306a36Sopenharmony_ci		.vsel_mask = TPS65132_VOUT_MASK,	\
19062306a36Sopenharmony_ci		.vsel_reg = TPS65132_REG_##_id,		\
19162306a36Sopenharmony_ci		.active_discharge_off = 0,			\
19262306a36Sopenharmony_ci		.active_discharge_on = TPS65132_REG_APPS_DIS_##_id, \
19362306a36Sopenharmony_ci		.active_discharge_mask = TPS65132_REG_APPS_DIS_##_id, \
19462306a36Sopenharmony_ci		.active_discharge_reg = TPS65132_REG_APPS_DISP_DISN, \
19562306a36Sopenharmony_ci		.type = REGULATOR_VOLTAGE,		\
19662306a36Sopenharmony_ci		.owner = THIS_MODULE,			\
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic const struct regulator_desc tps_regs_desc[TPS65132_MAX_REGULATORS] = {
20062306a36Sopenharmony_ci	TPS65132_REGULATOR_DESC(VPOS, outp),
20162306a36Sopenharmony_ci	TPS65132_REGULATOR_DESC(VNEG, outn),
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic const struct regmap_range tps65132_no_reg_ranges[] = {
20562306a36Sopenharmony_ci	regmap_reg_range(TPS65132_REG_APPS_DISP_DISN + 1,
20662306a36Sopenharmony_ci			 TPS65132_REG_CONTROL - 1),
20762306a36Sopenharmony_ci};
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_cistatic const struct regmap_access_table tps65132_no_reg_table = {
21062306a36Sopenharmony_ci	.no_ranges = tps65132_no_reg_ranges,
21162306a36Sopenharmony_ci	.n_no_ranges = ARRAY_SIZE(tps65132_no_reg_ranges),
21262306a36Sopenharmony_ci};
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_cistatic const struct regmap_config tps65132_regmap_config = {
21562306a36Sopenharmony_ci	.reg_bits	= 8,
21662306a36Sopenharmony_ci	.val_bits	= 8,
21762306a36Sopenharmony_ci	.max_register	= TPS65132_REG_CONTROL,
21862306a36Sopenharmony_ci	.cache_type	= REGCACHE_NONE,
21962306a36Sopenharmony_ci	.rd_table	= &tps65132_no_reg_table,
22062306a36Sopenharmony_ci	.wr_table	= &tps65132_no_reg_table,
22162306a36Sopenharmony_ci};
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_cistatic int tps65132_probe(struct i2c_client *client)
22462306a36Sopenharmony_ci{
22562306a36Sopenharmony_ci	struct device *dev = &client->dev;
22662306a36Sopenharmony_ci	struct tps65132_regulator *tps;
22762306a36Sopenharmony_ci	struct regulator_dev *rdev;
22862306a36Sopenharmony_ci	struct regmap *rmap;
22962306a36Sopenharmony_ci	struct regulator_config config = { };
23062306a36Sopenharmony_ci	int id;
23162306a36Sopenharmony_ci	int ret;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
23462306a36Sopenharmony_ci	if (!tps)
23562306a36Sopenharmony_ci		return -ENOMEM;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	rmap = devm_regmap_init_i2c(client, &tps65132_regmap_config);
23862306a36Sopenharmony_ci	if (IS_ERR(rmap)) {
23962306a36Sopenharmony_ci		ret = PTR_ERR(rmap);
24062306a36Sopenharmony_ci		dev_err(dev, "regmap init failed: %d\n", ret);
24162306a36Sopenharmony_ci		return ret;
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	i2c_set_clientdata(client, tps);
24562306a36Sopenharmony_ci	tps->dev = dev;
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	for (id = 0; id < TPS65132_MAX_REGULATORS; ++id) {
24862306a36Sopenharmony_ci		config.regmap = rmap;
24962306a36Sopenharmony_ci		config.dev = dev;
25062306a36Sopenharmony_ci		config.driver_data = tps;
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci		rdev = devm_regulator_register(dev, &tps_regs_desc[id],
25362306a36Sopenharmony_ci					       &config);
25462306a36Sopenharmony_ci		if (IS_ERR(rdev)) {
25562306a36Sopenharmony_ci			ret = PTR_ERR(rdev);
25662306a36Sopenharmony_ci			dev_err(dev, "regulator %s register failed: %d\n",
25762306a36Sopenharmony_ci				tps_regs_desc[id].name, ret);
25862306a36Sopenharmony_ci			return ret;
25962306a36Sopenharmony_ci		}
26062306a36Sopenharmony_ci	}
26162306a36Sopenharmony_ci	return 0;
26262306a36Sopenharmony_ci}
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_cistatic const struct i2c_device_id tps65132_id[] = {
26562306a36Sopenharmony_ci	{.name = "tps65132",},
26662306a36Sopenharmony_ci	{},
26762306a36Sopenharmony_ci};
26862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tps65132_id);
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_cistatic struct i2c_driver tps65132_i2c_driver = {
27162306a36Sopenharmony_ci	.driver = {
27262306a36Sopenharmony_ci		.name = "tps65132",
27362306a36Sopenharmony_ci		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
27462306a36Sopenharmony_ci	},
27562306a36Sopenharmony_ci	.probe = tps65132_probe,
27662306a36Sopenharmony_ci	.id_table = tps65132_id,
27762306a36Sopenharmony_ci};
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_cimodule_i2c_driver(tps65132_i2c_driver);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ciMODULE_DESCRIPTION("tps65132 regulator driver");
28262306a36Sopenharmony_ciMODULE_AUTHOR("Venkat Reddy Talla <vreddytalla@nvidia.com>");
28362306a36Sopenharmony_ciMODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
28462306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
285