Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // sy8106a-regulator.c - Regulator device driver for SY8106A |
| 4 | // |
| 5 | // Copyright (C) 2016 Ondřej Jirman <megous@megous.com> |
| 6 | // Copyright (c) 2017-2018 Icenowy Zheng <icenowy@aosc.io> |
| 7 | |
| 8 | #include <linux/err.h> |
| 9 | #include <linux/i2c.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/regmap.h> |
| 12 | #include <linux/regulator/driver.h> |
| 13 | #include <linux/regulator/of_regulator.h> |
| 14 | |
| 15 | #define SY8106A_REG_VOUT1_SEL 0x01 |
| 16 | #define SY8106A_REG_VOUT_COM 0x02 |
| 17 | #define SY8106A_REG_VOUT1_SEL_MASK 0x7f |
| 18 | #define SY8106A_DISABLE_REG BIT(0) |
| 19 | /* |
| 20 | * The I2C controlled voltage will only work when this bit is set; otherwise |
| 21 | * it will behave like a fixed regulator. |
| 22 | */ |
| 23 | #define SY8106A_GO_BIT BIT(7) |
| 24 | |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 25 | static const struct regmap_config sy8106a_regmap_config = { |
| 26 | .reg_bits = 8, |
| 27 | .val_bits = 8, |
| 28 | }; |
| 29 | |
| 30 | static const struct regulator_ops sy8106a_ops = { |
| 31 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 32 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
| 33 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 34 | .list_voltage = regulator_list_voltage_linear, |
| 35 | /* Enabling/disabling the regulator is not yet implemented */ |
| 36 | }; |
| 37 | |
| 38 | /* Default limits measured in millivolts */ |
| 39 | #define SY8106A_MIN_MV 680 |
| 40 | #define SY8106A_MAX_MV 1950 |
| 41 | #define SY8106A_STEP_MV 10 |
| 42 | |
| 43 | static const struct regulator_desc sy8106a_reg = { |
| 44 | .name = "SY8106A", |
| 45 | .id = 0, |
| 46 | .ops = &sy8106a_ops, |
| 47 | .type = REGULATOR_VOLTAGE, |
| 48 | .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1, |
| 49 | .min_uV = (SY8106A_MIN_MV * 1000), |
| 50 | .uV_step = (SY8106A_STEP_MV * 1000), |
| 51 | .vsel_reg = SY8106A_REG_VOUT1_SEL, |
| 52 | .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK, |
| 53 | /* |
| 54 | * This ramp_delay is a conservative default value which works on |
| 55 | * H3/H5 boards VDD-CPUX situations. |
| 56 | */ |
| 57 | .ramp_delay = 200, |
| 58 | .owner = THIS_MODULE, |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * I2C driver interface functions |
| 63 | */ |
Axel Lin | 77e2959 | 2020-01-09 23:58:08 +0800 | [diff] [blame] | 64 | static int sy8106a_i2c_probe(struct i2c_client *i2c) |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 65 | { |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 66 | struct device *dev = &i2c->dev; |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 67 | struct regulator_dev *rdev; |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 68 | struct regulator_config config = { }; |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 69 | struct regmap *regmap; |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 70 | unsigned int reg, vsel; |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 71 | u32 fixed_voltage; |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 72 | int error; |
| 73 | |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 74 | error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt", |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 75 | &fixed_voltage); |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 76 | if (error) |
| 77 | return error; |
| 78 | |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 79 | if (fixed_voltage < SY8106A_MIN_MV * 1000 || |
| 80 | fixed_voltage > SY8106A_MAX_MV * 1000) |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 81 | return -EINVAL; |
| 82 | |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 83 | regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config); |
| 84 | if (IS_ERR(regmap)) { |
| 85 | error = PTR_ERR(regmap); |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 86 | dev_err(dev, "Failed to allocate register map: %d\n", error); |
| 87 | return error; |
| 88 | } |
| 89 | |
| 90 | config.dev = &i2c->dev; |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 91 | config.regmap = regmap; |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 92 | |
| 93 | config.of_node = dev->of_node; |
| 94 | config.init_data = of_get_regulator_init_data(dev, dev->of_node, |
| 95 | &sy8106a_reg); |
| 96 | |
| 97 | if (!config.init_data) |
| 98 | return -ENOMEM; |
| 99 | |
| 100 | /* Ensure GO_BIT is enabled when probing */ |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 101 | error = regmap_read(regmap, SY8106A_REG_VOUT1_SEL, ®); |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 102 | if (error) |
| 103 | return error; |
| 104 | |
| 105 | if (!(reg & SY8106A_GO_BIT)) { |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 106 | vsel = (fixed_voltage / 1000 - SY8106A_MIN_MV) / |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 107 | SY8106A_STEP_MV; |
| 108 | |
Axel Lin | 5d7ebba | 2019-04-20 11:01:25 +0800 | [diff] [blame] | 109 | error = regmap_write(regmap, SY8106A_REG_VOUT1_SEL, |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 110 | vsel | SY8106A_GO_BIT); |
| 111 | if (error) |
| 112 | return error; |
| 113 | } |
| 114 | |
| 115 | /* Probe regulator */ |
| 116 | rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config); |
| 117 | if (IS_ERR(rdev)) { |
| 118 | error = PTR_ERR(rdev); |
| 119 | dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error); |
| 120 | return error; |
| 121 | } |
| 122 | |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static const struct of_device_id sy8106a_i2c_of_match[] = { |
| 127 | { .compatible = "silergy,sy8106a" }, |
| 128 | { }, |
| 129 | }; |
| 130 | MODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match); |
| 131 | |
| 132 | static const struct i2c_device_id sy8106a_i2c_id[] = { |
| 133 | { "sy8106a", 0 }, |
| 134 | { }, |
| 135 | }; |
| 136 | MODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id); |
| 137 | |
| 138 | static struct i2c_driver sy8106a_regulator_driver = { |
| 139 | .driver = { |
| 140 | .name = "sy8106a", |
| 141 | .of_match_table = of_match_ptr(sy8106a_i2c_of_match), |
| 142 | }, |
Axel Lin | 77e2959 | 2020-01-09 23:58:08 +0800 | [diff] [blame] | 143 | .probe_new = sy8106a_i2c_probe, |
Ondrej Jirman | 8878302 | 2018-05-07 20:29:41 +0800 | [diff] [blame] | 144 | .id_table = sy8106a_i2c_id, |
| 145 | }; |
| 146 | |
| 147 | module_i2c_driver(sy8106a_regulator_driver); |
| 148 | |
| 149 | MODULE_AUTHOR("Ondřej Jirman <megous@megous.com>"); |
| 150 | MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>"); |
| 151 | MODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A"); |
| 152 | MODULE_LICENSE("GPL"); |