|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (c) 2021, The Linux Foundation. All rights reserved. |
| 4 | + * Copyright (c) 2022, Linaro Ltd. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <linux/gpio/consumer.h> |
| 8 | +#include <linux/i2c.h> |
| 9 | +#include <linux/module.h> |
| 10 | +#include <linux/of_platform.h> |
| 11 | +#include <linux/regmap.h> |
| 12 | +#include <linux/regulator/driver.h> |
| 13 | +#include <linux/regulator/machine.h> |
| 14 | +#include <linux/regulator/of_regulator.h> |
| 15 | + |
| 16 | +#define MAX20411_UV_STEP 6250 |
| 17 | +#define MAX20411_BASE_UV 243750 |
| 18 | +#define MAX20411_MIN_SEL 41 /* 0.5V */ |
| 19 | +#define MAX20411_MAX_SEL 165 /* 1.275V */ |
| 20 | +#define MAX20411_VID_OFFSET 0x7 |
| 21 | +#define MAX20411_VID_MASK 0xff |
| 22 | +#define MAX20411_SLEW_OFFSET 0x6 |
| 23 | +#define MAX20411_SLEW_DVS_MASK 0xc |
| 24 | +#define MAX20411_SLEW_SR_MASK 0x3 |
| 25 | + |
| 26 | +struct max20411 { |
| 27 | + struct device *dev; |
| 28 | + struct device_node *of_node; |
| 29 | + struct regulator_desc desc; |
| 30 | + struct regulator_dev *rdev; |
| 31 | + struct regmap *regmap; |
| 32 | +}; |
| 33 | + |
| 34 | +static const unsigned int max20411_slew_rates[] = { 13100, 6600, 3300, 1600 }; |
| 35 | + |
| 36 | +static int max20411_enable_time(struct regulator_dev *rdev) |
| 37 | +{ |
| 38 | + int voltage, rate, ret; |
| 39 | + unsigned int val; |
| 40 | + |
| 41 | + /* get voltage */ |
| 42 | + ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); |
| 43 | + if (ret) |
| 44 | + return ret; |
| 45 | + |
| 46 | + val &= rdev->desc->vsel_mask; |
| 47 | + voltage = regulator_list_voltage_linear(rdev, val); |
| 48 | + |
| 49 | + /* get rate */ |
| 50 | + ret = regmap_read(rdev->regmap, MAX20411_SLEW_OFFSET, &val); |
| 51 | + if (ret) |
| 52 | + return ret; |
| 53 | + |
| 54 | + val = FIELD_GET(MAX20411_SLEW_SR_MASK, val); |
| 55 | + rate = max20411_slew_rates[val]; |
| 56 | + |
| 57 | + return DIV_ROUND_UP(voltage, rate); |
| 58 | +} |
| 59 | + |
| 60 | +static const struct regmap_config max20411_regmap_config = { |
| 61 | + .reg_bits = 8, |
| 62 | + .val_bits = 8, |
| 63 | + .max_register = 0xe, |
| 64 | +}; |
| 65 | + |
| 66 | +static const struct regulator_ops max20411_ops = { |
| 67 | + .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 68 | + .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 69 | + .list_voltage = regulator_list_voltage_linear, |
| 70 | + .enable_time = max20411_enable_time, |
| 71 | +}; |
| 72 | + |
| 73 | +static const struct regulator_desc max20411_desc = { |
| 74 | + .ops = &max20411_ops, |
| 75 | + .owner = THIS_MODULE, |
| 76 | + .type = REGULATOR_VOLTAGE, |
| 77 | + .supply_name = "vin", |
| 78 | + .name = "max20411", |
| 79 | + |
| 80 | + /* |
| 81 | + * voltage = 0.24375V + selector * 6.25mV |
| 82 | + * with valid selector between 41 to 165 (0.5V to 1.275V) |
| 83 | + */ |
| 84 | + .min_uV = MAX20411_BASE_UV, |
| 85 | + .uV_step = MAX20411_UV_STEP, |
| 86 | + .linear_min_sel = MAX20411_MIN_SEL, |
| 87 | + .n_voltages = MAX20411_MAX_SEL, |
| 88 | + |
| 89 | + .vsel_reg = MAX20411_VID_OFFSET, |
| 90 | + .vsel_mask = MAX20411_VID_MASK, |
| 91 | + |
| 92 | + .ramp_reg = MAX20411_SLEW_OFFSET, |
| 93 | + .ramp_mask = MAX20411_SLEW_DVS_MASK, |
| 94 | + .ramp_delay_table = max20411_slew_rates, |
| 95 | + .n_ramp_values = ARRAY_SIZE(max20411_slew_rates), |
| 96 | +}; |
| 97 | + |
| 98 | +static int max20411_probe(struct i2c_client *client, |
| 99 | + const struct i2c_device_id *id) |
| 100 | +{ |
| 101 | + struct regulator_init_data *init_data; |
| 102 | + struct device *dev = &client->dev; |
| 103 | + struct regulator_config cfg = {}; |
| 104 | + struct max20411 *max20411; |
| 105 | + |
| 106 | + max20411 = devm_kzalloc(dev, sizeof(*max20411), GFP_KERNEL); |
| 107 | + if (!max20411) |
| 108 | + return -ENOMEM; |
| 109 | + |
| 110 | + max20411->regmap = devm_regmap_init_i2c(client, &max20411_regmap_config); |
| 111 | + if (IS_ERR(max20411->regmap)) { |
| 112 | + dev_err(dev, "Failed to allocate regmap!\n"); |
| 113 | + return PTR_ERR(max20411->regmap); |
| 114 | + } |
| 115 | + |
| 116 | + max20411->dev = dev; |
| 117 | + max20411->of_node = dev->of_node; |
| 118 | + |
| 119 | + max20411->desc = max20411_desc; |
| 120 | + init_data = of_get_regulator_init_data(max20411->dev, max20411->of_node, &max20411->desc); |
| 121 | + if (!init_data) |
| 122 | + return -ENODATA; |
| 123 | + |
| 124 | + cfg.dev = max20411->dev; |
| 125 | + cfg.init_data = init_data; |
| 126 | + cfg.of_node = max20411->of_node; |
| 127 | + cfg.driver_data = max20411; |
| 128 | + |
| 129 | + cfg.ena_gpiod = gpiod_get(max20411->dev, "enable", GPIOD_ASIS); |
| 130 | + if (IS_ERR(cfg.ena_gpiod)) |
| 131 | + return dev_err_probe(dev, PTR_ERR(cfg.ena_gpiod), |
| 132 | + "unable to acquire enable gpio\n"); |
| 133 | + |
| 134 | + max20411->rdev = devm_regulator_register(max20411->dev, &max20411->desc, &cfg); |
| 135 | + if (IS_ERR(max20411->rdev)) |
| 136 | + dev_err(max20411->dev, "Failed to register regulator\n"); |
| 137 | + |
| 138 | + return PTR_ERR_OR_ZERO(max20411->rdev); |
| 139 | +} |
| 140 | + |
| 141 | +static const struct of_device_id of_max20411_match_tbl[] = { |
| 142 | + { .compatible = "maxim,max20411", }, |
| 143 | + { }, |
| 144 | +}; |
| 145 | +MODULE_DEVICE_TABLE(of, of_max20411_match_tbl); |
| 146 | + |
| 147 | +static const struct i2c_device_id max20411_id[] = { |
| 148 | + { "max20411", 0 }, |
| 149 | + { }, |
| 150 | +}; |
| 151 | +MODULE_DEVICE_TABLE(i2c, max20411_id); |
| 152 | + |
| 153 | +static struct i2c_driver max20411_i2c_driver = { |
| 154 | + .driver = { |
| 155 | + .name = "max20411", |
| 156 | + .of_match_table = of_max20411_match_tbl, |
| 157 | + }, |
| 158 | + .probe = max20411_probe, |
| 159 | + .id_table = max20411_id, |
| 160 | +}; |
| 161 | +module_i2c_driver(max20411_i2c_driver); |
| 162 | + |
| 163 | +MODULE_LICENSE("GPL"); |
0 commit comments