162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci// 362306a36Sopenharmony_ci// sy8106a-regulator.c - Regulator device driver for SY8106A 462306a36Sopenharmony_ci// 562306a36Sopenharmony_ci// Copyright (C) 2016 Ondřej Jirman <megous@megous.com> 662306a36Sopenharmony_ci// Copyright (c) 2017-2018 Icenowy Zheng <icenowy@aosc.io> 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/err.h> 962306a36Sopenharmony_ci#include <linux/i2c.h> 1062306a36Sopenharmony_ci#include <linux/module.h> 1162306a36Sopenharmony_ci#include <linux/regmap.h> 1262306a36Sopenharmony_ci#include <linux/regulator/driver.h> 1362306a36Sopenharmony_ci#include <linux/regulator/of_regulator.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#define SY8106A_REG_VOUT1_SEL 0x01 1662306a36Sopenharmony_ci#define SY8106A_REG_VOUT_COM 0x02 1762306a36Sopenharmony_ci#define SY8106A_REG_VOUT1_SEL_MASK 0x7f 1862306a36Sopenharmony_ci#define SY8106A_DISABLE_REG BIT(0) 1962306a36Sopenharmony_ci/* 2062306a36Sopenharmony_ci * The I2C controlled voltage will only work when this bit is set; otherwise 2162306a36Sopenharmony_ci * it will behave like a fixed regulator. 2262306a36Sopenharmony_ci */ 2362306a36Sopenharmony_ci#define SY8106A_GO_BIT BIT(7) 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_cistatic const struct regmap_config sy8106a_regmap_config = { 2662306a36Sopenharmony_ci .reg_bits = 8, 2762306a36Sopenharmony_ci .val_bits = 8, 2862306a36Sopenharmony_ci}; 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_cistatic const struct regulator_ops sy8106a_ops = { 3162306a36Sopenharmony_ci .set_voltage_sel = regulator_set_voltage_sel_regmap, 3262306a36Sopenharmony_ci .set_voltage_time_sel = regulator_set_voltage_time_sel, 3362306a36Sopenharmony_ci .get_voltage_sel = regulator_get_voltage_sel_regmap, 3462306a36Sopenharmony_ci .list_voltage = regulator_list_voltage_linear, 3562306a36Sopenharmony_ci /* Enabling/disabling the regulator is not yet implemented */ 3662306a36Sopenharmony_ci}; 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci/* Default limits measured in millivolts */ 3962306a36Sopenharmony_ci#define SY8106A_MIN_MV 680 4062306a36Sopenharmony_ci#define SY8106A_MAX_MV 1950 4162306a36Sopenharmony_ci#define SY8106A_STEP_MV 10 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistatic const struct regulator_desc sy8106a_reg = { 4462306a36Sopenharmony_ci .name = "SY8106A", 4562306a36Sopenharmony_ci .id = 0, 4662306a36Sopenharmony_ci .ops = &sy8106a_ops, 4762306a36Sopenharmony_ci .type = REGULATOR_VOLTAGE, 4862306a36Sopenharmony_ci .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1, 4962306a36Sopenharmony_ci .min_uV = (SY8106A_MIN_MV * 1000), 5062306a36Sopenharmony_ci .uV_step = (SY8106A_STEP_MV * 1000), 5162306a36Sopenharmony_ci .vsel_reg = SY8106A_REG_VOUT1_SEL, 5262306a36Sopenharmony_ci .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK, 5362306a36Sopenharmony_ci /* 5462306a36Sopenharmony_ci * This ramp_delay is a conservative default value which works on 5562306a36Sopenharmony_ci * H3/H5 boards VDD-CPUX situations. 5662306a36Sopenharmony_ci */ 5762306a36Sopenharmony_ci .ramp_delay = 200, 5862306a36Sopenharmony_ci .owner = THIS_MODULE, 5962306a36Sopenharmony_ci}; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci/* 6262306a36Sopenharmony_ci * I2C driver interface functions 6362306a36Sopenharmony_ci */ 6462306a36Sopenharmony_cistatic int sy8106a_i2c_probe(struct i2c_client *i2c) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci struct device *dev = &i2c->dev; 6762306a36Sopenharmony_ci struct regulator_dev *rdev; 6862306a36Sopenharmony_ci struct regulator_config config = { }; 6962306a36Sopenharmony_ci struct regmap *regmap; 7062306a36Sopenharmony_ci unsigned int reg, vsel; 7162306a36Sopenharmony_ci u32 fixed_voltage; 7262306a36Sopenharmony_ci int error; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt", 7562306a36Sopenharmony_ci &fixed_voltage); 7662306a36Sopenharmony_ci if (error) 7762306a36Sopenharmony_ci return error; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci if (fixed_voltage < SY8106A_MIN_MV * 1000 || 8062306a36Sopenharmony_ci fixed_voltage > SY8106A_MAX_MV * 1000) 8162306a36Sopenharmony_ci return -EINVAL; 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config); 8462306a36Sopenharmony_ci if (IS_ERR(regmap)) { 8562306a36Sopenharmony_ci error = PTR_ERR(regmap); 8662306a36Sopenharmony_ci dev_err(dev, "Failed to allocate register map: %d\n", error); 8762306a36Sopenharmony_ci return error; 8862306a36Sopenharmony_ci } 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci config.dev = &i2c->dev; 9162306a36Sopenharmony_ci config.regmap = regmap; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci config.of_node = dev->of_node; 9462306a36Sopenharmony_ci config.init_data = of_get_regulator_init_data(dev, dev->of_node, 9562306a36Sopenharmony_ci &sy8106a_reg); 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci if (!config.init_data) 9862306a36Sopenharmony_ci return -ENOMEM; 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci /* Ensure GO_BIT is enabled when probing */ 10162306a36Sopenharmony_ci error = regmap_read(regmap, SY8106A_REG_VOUT1_SEL, ®); 10262306a36Sopenharmony_ci if (error) 10362306a36Sopenharmony_ci return error; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci if (!(reg & SY8106A_GO_BIT)) { 10662306a36Sopenharmony_ci vsel = (fixed_voltage / 1000 - SY8106A_MIN_MV) / 10762306a36Sopenharmony_ci SY8106A_STEP_MV; 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci error = regmap_write(regmap, SY8106A_REG_VOUT1_SEL, 11062306a36Sopenharmony_ci vsel | SY8106A_GO_BIT); 11162306a36Sopenharmony_ci if (error) 11262306a36Sopenharmony_ci return error; 11362306a36Sopenharmony_ci } 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci /* Probe regulator */ 11662306a36Sopenharmony_ci rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config); 11762306a36Sopenharmony_ci if (IS_ERR(rdev)) { 11862306a36Sopenharmony_ci error = PTR_ERR(rdev); 11962306a36Sopenharmony_ci dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error); 12062306a36Sopenharmony_ci return error; 12162306a36Sopenharmony_ci } 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci return 0; 12462306a36Sopenharmony_ci} 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cistatic const struct of_device_id sy8106a_i2c_of_match[] = { 12762306a36Sopenharmony_ci { .compatible = "silergy,sy8106a" }, 12862306a36Sopenharmony_ci { }, 12962306a36Sopenharmony_ci}; 13062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match); 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_cistatic const struct i2c_device_id sy8106a_i2c_id[] = { 13362306a36Sopenharmony_ci { "sy8106a", 0 }, 13462306a36Sopenharmony_ci { }, 13562306a36Sopenharmony_ci}; 13662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id); 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_cistatic struct i2c_driver sy8106a_regulator_driver = { 13962306a36Sopenharmony_ci .driver = { 14062306a36Sopenharmony_ci .name = "sy8106a", 14162306a36Sopenharmony_ci .probe_type = PROBE_PREFER_ASYNCHRONOUS, 14262306a36Sopenharmony_ci .of_match_table = sy8106a_i2c_of_match, 14362306a36Sopenharmony_ci }, 14462306a36Sopenharmony_ci .probe = sy8106a_i2c_probe, 14562306a36Sopenharmony_ci .id_table = sy8106a_i2c_id, 14662306a36Sopenharmony_ci}; 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_cimodule_i2c_driver(sy8106a_regulator_driver); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ciMODULE_AUTHOR("Ondřej Jirman <megous@megous.com>"); 15162306a36Sopenharmony_ciMODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>"); 15262306a36Sopenharmony_ciMODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A"); 15362306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 154