18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// SY8827N regulator driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (C) 2020 Synaptics Incorporated
68c2ecf20Sopenharmony_ci//
78c2ecf20Sopenharmony_ci// Author: Jisheng Zhang <jszhang@kernel.org>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/of_device.h>
138c2ecf20Sopenharmony_ci#include <linux/regmap.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
158c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define SY8827N_VSEL0		0
188c2ecf20Sopenharmony_ci#define   SY8827N_BUCK_EN	(1 << 7)
198c2ecf20Sopenharmony_ci#define   SY8827N_MODE		(1 << 6)
208c2ecf20Sopenharmony_ci#define SY8827N_VSEL1		1
218c2ecf20Sopenharmony_ci#define SY8827N_CTRL		2
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define SY8827N_NVOLTAGES	64
248c2ecf20Sopenharmony_ci#define SY8827N_VSELMIN		600000
258c2ecf20Sopenharmony_ci#define SY8827N_VSELSTEP	12500
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct sy8827n_device_info {
288c2ecf20Sopenharmony_ci	struct device *dev;
298c2ecf20Sopenharmony_ci	struct regulator_desc desc;
308c2ecf20Sopenharmony_ci	struct regulator_init_data *regulator;
318c2ecf20Sopenharmony_ci	struct gpio_desc *en_gpio;
328c2ecf20Sopenharmony_ci	unsigned int vsel_reg;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int sy8827n_set_mode(struct regulator_dev *rdev, unsigned int mode)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct sy8827n_device_info *di = rdev_get_drvdata(rdev);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	switch (mode) {
408c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
418c2ecf20Sopenharmony_ci		regmap_update_bits(rdev->regmap, di->vsel_reg,
428c2ecf20Sopenharmony_ci				   SY8827N_MODE, SY8827N_MODE);
438c2ecf20Sopenharmony_ci		break;
448c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
458c2ecf20Sopenharmony_ci		regmap_update_bits(rdev->regmap, di->vsel_reg,
468c2ecf20Sopenharmony_ci				   SY8827N_MODE, 0);
478c2ecf20Sopenharmony_ci		break;
488c2ecf20Sopenharmony_ci	default:
498c2ecf20Sopenharmony_ci		return -EINVAL;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci	return 0;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic unsigned int sy8827n_get_mode(struct regulator_dev *rdev)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct sy8827n_device_info *di = rdev_get_drvdata(rdev);
578c2ecf20Sopenharmony_ci	u32 val;
588c2ecf20Sopenharmony_ci	int ret = 0;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, di->vsel_reg, &val);
618c2ecf20Sopenharmony_ci	if (ret < 0)
628c2ecf20Sopenharmony_ci		return ret;
638c2ecf20Sopenharmony_ci	if (val & SY8827N_MODE)
648c2ecf20Sopenharmony_ci		return REGULATOR_MODE_FAST;
658c2ecf20Sopenharmony_ci	else
668c2ecf20Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic const struct regulator_ops sy8827n_regulator_ops = {
708c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
718c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
728c2ecf20Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
738c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear,
748c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
758c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
768c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
778c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
788c2ecf20Sopenharmony_ci	.set_mode = sy8827n_set_mode,
798c2ecf20Sopenharmony_ci	.get_mode = sy8827n_get_mode,
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic int sy8827n_regulator_register(struct sy8827n_device_info *di,
838c2ecf20Sopenharmony_ci			struct regulator_config *config)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct regulator_desc *rdesc = &di->desc;
868c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	rdesc->name = "sy8827n-reg";
898c2ecf20Sopenharmony_ci	rdesc->supply_name = "vin";
908c2ecf20Sopenharmony_ci	rdesc->ops = &sy8827n_regulator_ops;
918c2ecf20Sopenharmony_ci	rdesc->type = REGULATOR_VOLTAGE;
928c2ecf20Sopenharmony_ci	rdesc->n_voltages = SY8827N_NVOLTAGES;
938c2ecf20Sopenharmony_ci	rdesc->enable_reg = di->vsel_reg;
948c2ecf20Sopenharmony_ci	rdesc->enable_mask = SY8827N_BUCK_EN;
958c2ecf20Sopenharmony_ci	rdesc->min_uV = SY8827N_VSELMIN;
968c2ecf20Sopenharmony_ci	rdesc->uV_step = SY8827N_VSELSTEP;
978c2ecf20Sopenharmony_ci	rdesc->vsel_reg = di->vsel_reg;
988c2ecf20Sopenharmony_ci	rdesc->vsel_mask = rdesc->n_voltages - 1;
998c2ecf20Sopenharmony_ci	rdesc->owner = THIS_MODULE;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	rdev = devm_regulator_register(di->dev, &di->desc, config);
1028c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(rdev);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic const struct regmap_config sy8827n_regmap_config = {
1068c2ecf20Sopenharmony_ci	.reg_bits = 8,
1078c2ecf20Sopenharmony_ci	.val_bits = 8,
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int sy8827n_i2c_probe(struct i2c_client *client)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
1138c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
1148c2ecf20Sopenharmony_ci	struct sy8827n_device_info *di;
1158c2ecf20Sopenharmony_ci	struct regulator_config config = { };
1168c2ecf20Sopenharmony_ci	struct regmap *regmap;
1178c2ecf20Sopenharmony_ci	int ret;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	di = devm_kzalloc(dev, sizeof(struct sy8827n_device_info), GFP_KERNEL);
1208c2ecf20Sopenharmony_ci	if (!di)
1218c2ecf20Sopenharmony_ci		return -ENOMEM;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	di->regulator = of_get_regulator_init_data(dev, np, &di->desc);
1248c2ecf20Sopenharmony_ci	if (!di->regulator) {
1258c2ecf20Sopenharmony_ci		dev_err(dev, "Platform data not found!\n");
1268c2ecf20Sopenharmony_ci		return -EINVAL;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	di->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
1308c2ecf20Sopenharmony_ci	if (IS_ERR(di->en_gpio))
1318c2ecf20Sopenharmony_ci		return PTR_ERR(di->en_gpio);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "silergy,vsel-state-high"))
1348c2ecf20Sopenharmony_ci		di->vsel_reg = SY8827N_VSEL1;
1358c2ecf20Sopenharmony_ci	else
1368c2ecf20Sopenharmony_ci		di->vsel_reg = SY8827N_VSEL0;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	di->dev = dev;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(client, &sy8827n_regmap_config);
1418c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
1428c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to allocate regmap!\n");
1438c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, di);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	config.dev = di->dev;
1488c2ecf20Sopenharmony_ci	config.init_data = di->regulator;
1498c2ecf20Sopenharmony_ci	config.regmap = regmap;
1508c2ecf20Sopenharmony_ci	config.driver_data = di;
1518c2ecf20Sopenharmony_ci	config.of_node = np;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	ret = sy8827n_regulator_register(di, &config);
1548c2ecf20Sopenharmony_ci	if (ret < 0)
1558c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to register regulator!\n");
1568c2ecf20Sopenharmony_ci	return ret;
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#ifdef CONFIG_OF
1608c2ecf20Sopenharmony_cistatic const struct of_device_id sy8827n_dt_ids[] = {
1618c2ecf20Sopenharmony_ci	{
1628c2ecf20Sopenharmony_ci		.compatible = "silergy,sy8827n",
1638c2ecf20Sopenharmony_ci	},
1648c2ecf20Sopenharmony_ci	{ }
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sy8827n_dt_ids);
1678c2ecf20Sopenharmony_ci#endif
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic const struct i2c_device_id sy8827n_id[] = {
1708c2ecf20Sopenharmony_ci	{ "sy8827n", },
1718c2ecf20Sopenharmony_ci	{ },
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sy8827n_id);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct i2c_driver sy8827n_regulator_driver = {
1768c2ecf20Sopenharmony_ci	.driver = {
1778c2ecf20Sopenharmony_ci		.name = "sy8827n-regulator",
1788c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(sy8827n_dt_ids),
1798c2ecf20Sopenharmony_ci	},
1808c2ecf20Sopenharmony_ci	.probe_new = sy8827n_i2c_probe,
1818c2ecf20Sopenharmony_ci	.id_table = sy8827n_id,
1828c2ecf20Sopenharmony_ci};
1838c2ecf20Sopenharmony_cimodule_i2c_driver(sy8827n_regulator_driver);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>");
1868c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SY8827N regulator driver");
1878c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
188