18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// SY8824C/SY8824E regulator driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (C) 2019 Synaptics Incorporated
68c2ecf20Sopenharmony_ci//
78c2ecf20Sopenharmony_ci// Author: Jisheng Zhang <jszhang@kernel.org>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/i2c.h>
118c2ecf20Sopenharmony_ci#include <linux/of_device.h>
128c2ecf20Sopenharmony_ci#include <linux/regmap.h>
138c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
148c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define SY8824C_BUCK_EN		(1 << 7)
178c2ecf20Sopenharmony_ci#define SY8824C_MODE		(1 << 6)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct sy8824_config {
208c2ecf20Sopenharmony_ci	/* registers */
218c2ecf20Sopenharmony_ci	unsigned int vol_reg;
228c2ecf20Sopenharmony_ci	unsigned int mode_reg;
238c2ecf20Sopenharmony_ci	unsigned int enable_reg;
248c2ecf20Sopenharmony_ci	/* Voltage range and step(linear) */
258c2ecf20Sopenharmony_ci	unsigned int vsel_min;
268c2ecf20Sopenharmony_ci	unsigned int vsel_step;
278c2ecf20Sopenharmony_ci	unsigned int vsel_count;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct sy8824_device_info {
318c2ecf20Sopenharmony_ci	struct device *dev;
328c2ecf20Sopenharmony_ci	struct regulator_desc desc;
338c2ecf20Sopenharmony_ci	struct regulator_init_data *regulator;
348c2ecf20Sopenharmony_ci	const struct sy8824_config *cfg;
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int sy8824_set_mode(struct regulator_dev *rdev, unsigned int mode)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct sy8824_device_info *di = rdev_get_drvdata(rdev);
408c2ecf20Sopenharmony_ci	const struct sy8824_config *cfg = di->cfg;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	switch (mode) {
438c2ecf20Sopenharmony_ci	case REGULATOR_MODE_FAST:
448c2ecf20Sopenharmony_ci		regmap_update_bits(rdev->regmap, cfg->mode_reg,
458c2ecf20Sopenharmony_ci				   SY8824C_MODE, SY8824C_MODE);
468c2ecf20Sopenharmony_ci		break;
478c2ecf20Sopenharmony_ci	case REGULATOR_MODE_NORMAL:
488c2ecf20Sopenharmony_ci		regmap_update_bits(rdev->regmap, cfg->mode_reg,
498c2ecf20Sopenharmony_ci				   SY8824C_MODE, 0);
508c2ecf20Sopenharmony_ci		break;
518c2ecf20Sopenharmony_ci	default:
528c2ecf20Sopenharmony_ci		return -EINVAL;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	return 0;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic unsigned int sy8824_get_mode(struct regulator_dev *rdev)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct sy8824_device_info *di = rdev_get_drvdata(rdev);
608c2ecf20Sopenharmony_ci	const struct sy8824_config *cfg = di->cfg;
618c2ecf20Sopenharmony_ci	u32 val;
628c2ecf20Sopenharmony_ci	int ret = 0;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ret = regmap_read(rdev->regmap, cfg->mode_reg, &val);
658c2ecf20Sopenharmony_ci	if (ret < 0)
668c2ecf20Sopenharmony_ci		return ret;
678c2ecf20Sopenharmony_ci	if (val & SY8824C_MODE)
688c2ecf20Sopenharmony_ci		return REGULATOR_MODE_FAST;
698c2ecf20Sopenharmony_ci	else
708c2ecf20Sopenharmony_ci		return REGULATOR_MODE_NORMAL;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic const struct regulator_ops sy8824_regulator_ops = {
748c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
758c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
768c2ecf20Sopenharmony_ci	.set_voltage_time_sel = regulator_set_voltage_time_sel,
778c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear,
788c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear,
798c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
808c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
818c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
828c2ecf20Sopenharmony_ci	.set_mode = sy8824_set_mode,
838c2ecf20Sopenharmony_ci	.get_mode = sy8824_get_mode,
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int sy8824_regulator_register(struct sy8824_device_info *di,
878c2ecf20Sopenharmony_ci			struct regulator_config *config)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct regulator_desc *rdesc = &di->desc;
908c2ecf20Sopenharmony_ci	const struct sy8824_config *cfg = di->cfg;
918c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	rdesc->name = "sy8824-reg";
948c2ecf20Sopenharmony_ci	rdesc->supply_name = "vin";
958c2ecf20Sopenharmony_ci	rdesc->ops = &sy8824_regulator_ops;
968c2ecf20Sopenharmony_ci	rdesc->type = REGULATOR_VOLTAGE;
978c2ecf20Sopenharmony_ci	rdesc->n_voltages = cfg->vsel_count;
988c2ecf20Sopenharmony_ci	rdesc->enable_reg = cfg->enable_reg;
998c2ecf20Sopenharmony_ci	rdesc->enable_mask = SY8824C_BUCK_EN;
1008c2ecf20Sopenharmony_ci	rdesc->min_uV = cfg->vsel_min;
1018c2ecf20Sopenharmony_ci	rdesc->uV_step = cfg->vsel_step;
1028c2ecf20Sopenharmony_ci	rdesc->vsel_reg = cfg->vol_reg;
1038c2ecf20Sopenharmony_ci	rdesc->vsel_mask = cfg->vsel_count - 1;
1048c2ecf20Sopenharmony_ci	rdesc->owner = THIS_MODULE;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	rdev = devm_regulator_register(di->dev, &di->desc, config);
1078c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(rdev);
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic const struct regmap_config sy8824_regmap_config = {
1118c2ecf20Sopenharmony_ci	.reg_bits = 8,
1128c2ecf20Sopenharmony_ci	.val_bits = 8,
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int sy8824_i2c_probe(struct i2c_client *client)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
1188c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
1198c2ecf20Sopenharmony_ci	struct sy8824_device_info *di;
1208c2ecf20Sopenharmony_ci	struct regulator_config config = { };
1218c2ecf20Sopenharmony_ci	struct regmap *regmap;
1228c2ecf20Sopenharmony_ci	int ret;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	di = devm_kzalloc(dev, sizeof(struct sy8824_device_info), GFP_KERNEL);
1258c2ecf20Sopenharmony_ci	if (!di)
1268c2ecf20Sopenharmony_ci		return -ENOMEM;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	di->regulator = of_get_regulator_init_data(dev, np, &di->desc);
1298c2ecf20Sopenharmony_ci	if (!di->regulator) {
1308c2ecf20Sopenharmony_ci		dev_err(dev, "Platform data not found!\n");
1318c2ecf20Sopenharmony_ci		return -EINVAL;
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	di->dev = dev;
1358c2ecf20Sopenharmony_ci	di->cfg = of_device_get_match_data(dev);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_i2c(client, &sy8824_regmap_config);
1388c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
1398c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to allocate regmap!\n");
1408c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, di);
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	config.dev = di->dev;
1458c2ecf20Sopenharmony_ci	config.init_data = di->regulator;
1468c2ecf20Sopenharmony_ci	config.regmap = regmap;
1478c2ecf20Sopenharmony_ci	config.driver_data = di;
1488c2ecf20Sopenharmony_ci	config.of_node = np;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	ret = sy8824_regulator_register(di, &config);
1518c2ecf20Sopenharmony_ci	if (ret < 0)
1528c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to register regulator!\n");
1538c2ecf20Sopenharmony_ci	return ret;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic const struct sy8824_config sy8824c_cfg = {
1578c2ecf20Sopenharmony_ci	.vol_reg = 0x00,
1588c2ecf20Sopenharmony_ci	.mode_reg = 0x00,
1598c2ecf20Sopenharmony_ci	.enable_reg = 0x00,
1608c2ecf20Sopenharmony_ci	.vsel_min = 762500,
1618c2ecf20Sopenharmony_ci	.vsel_step = 12500,
1628c2ecf20Sopenharmony_ci	.vsel_count = 64,
1638c2ecf20Sopenharmony_ci};
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic const struct sy8824_config sy8824e_cfg = {
1668c2ecf20Sopenharmony_ci	.vol_reg = 0x00,
1678c2ecf20Sopenharmony_ci	.mode_reg = 0x00,
1688c2ecf20Sopenharmony_ci	.enable_reg = 0x00,
1698c2ecf20Sopenharmony_ci	.vsel_min = 700000,
1708c2ecf20Sopenharmony_ci	.vsel_step = 12500,
1718c2ecf20Sopenharmony_ci	.vsel_count = 64,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic const struct sy8824_config sy20276_cfg = {
1758c2ecf20Sopenharmony_ci	.vol_reg = 0x00,
1768c2ecf20Sopenharmony_ci	.mode_reg = 0x01,
1778c2ecf20Sopenharmony_ci	.enable_reg = 0x01,
1788c2ecf20Sopenharmony_ci	.vsel_min = 600000,
1798c2ecf20Sopenharmony_ci	.vsel_step = 10000,
1808c2ecf20Sopenharmony_ci	.vsel_count = 128,
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic const struct sy8824_config sy20278_cfg = {
1848c2ecf20Sopenharmony_ci	.vol_reg = 0x00,
1858c2ecf20Sopenharmony_ci	.mode_reg = 0x01,
1868c2ecf20Sopenharmony_ci	.enable_reg = 0x01,
1878c2ecf20Sopenharmony_ci	.vsel_min = 762500,
1888c2ecf20Sopenharmony_ci	.vsel_step = 12500,
1898c2ecf20Sopenharmony_ci	.vsel_count = 64,
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic const struct of_device_id sy8824_dt_ids[] = {
1938c2ecf20Sopenharmony_ci	{
1948c2ecf20Sopenharmony_ci		.compatible = "silergy,sy8824c",
1958c2ecf20Sopenharmony_ci		.data = &sy8824c_cfg
1968c2ecf20Sopenharmony_ci	},
1978c2ecf20Sopenharmony_ci	{
1988c2ecf20Sopenharmony_ci		.compatible = "silergy,sy8824e",
1998c2ecf20Sopenharmony_ci		.data = &sy8824e_cfg
2008c2ecf20Sopenharmony_ci	},
2018c2ecf20Sopenharmony_ci	{
2028c2ecf20Sopenharmony_ci		.compatible = "silergy,sy20276",
2038c2ecf20Sopenharmony_ci		.data = &sy20276_cfg
2048c2ecf20Sopenharmony_ci	},
2058c2ecf20Sopenharmony_ci	{
2068c2ecf20Sopenharmony_ci		.compatible = "silergy,sy20278",
2078c2ecf20Sopenharmony_ci		.data = &sy20278_cfg
2088c2ecf20Sopenharmony_ci	},
2098c2ecf20Sopenharmony_ci	{ }
2108c2ecf20Sopenharmony_ci};
2118c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sy8824_dt_ids);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic const struct i2c_device_id sy8824_id[] = {
2148c2ecf20Sopenharmony_ci	{ "sy8824", },
2158c2ecf20Sopenharmony_ci	{ },
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sy8824_id);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic struct i2c_driver sy8824_regulator_driver = {
2208c2ecf20Sopenharmony_ci	.driver = {
2218c2ecf20Sopenharmony_ci		.name = "sy8824-regulator",
2228c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(sy8824_dt_ids),
2238c2ecf20Sopenharmony_ci	},
2248c2ecf20Sopenharmony_ci	.probe_new = sy8824_i2c_probe,
2258c2ecf20Sopenharmony_ci	.id_table = sy8824_id,
2268c2ecf20Sopenharmony_ci};
2278c2ecf20Sopenharmony_cimodule_i2c_driver(sy8824_regulator_driver);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>");
2308c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SY8824C/SY8824E regulator driver");
2318c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
232