|
| 1 | +// max77675.c |
| 2 | +#include <linux/module.h> |
| 3 | +#include <linux/i2c.h> |
| 4 | +#include <linux/regulator/driver.h> |
| 5 | +#include <linux/of_device.h> |
| 6 | +#include <linux/regmap.h> |
| 7 | + |
| 8 | +#define MAX77675_NUM_REGULATORS 4 |
| 9 | + |
| 10 | +struct max77675_regulator { |
| 11 | + struct device *dev; |
| 12 | + struct regulator_dev *rdev[MAX77675_NUM_REGULATORS]; |
| 13 | + struct regmap *regmap; |
| 14 | +}; |
| 15 | + |
| 16 | +static const struct regulator_ops max77675_regulator_ops = { |
| 17 | + .enable = regulator_enable_regmap, |
| 18 | + .disable = regulator_disable_regmap, |
| 19 | + .is_enabled = regulator_is_enabled_regmap, |
| 20 | + .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 21 | + .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 22 | +}; |
| 23 | + |
| 24 | +static const struct regulator_desc max77675_regulators[] = { |
| 25 | + { |
| 26 | + .name = "simo0", |
| 27 | + .id = 0, |
| 28 | + .ops = &max77675_regulator_ops, |
| 29 | + .type = REGULATOR_VOLTAGE, |
| 30 | + .owner = THIS_MODULE, |
| 31 | + .n_voltages = 64, |
| 32 | + .min_uV = 500000, |
| 33 | + .uV_step = 50000, |
| 34 | + .vsel_reg = 0x10, // example value : SIMO0 VOUT |
| 35 | + .vsel_mask = 0x3F, |
| 36 | + .enable_reg = 0x20, // example value : SIMO0 ENABLE |
| 37 | + .enable_mask = 0x01, |
| 38 | + }, |
| 39 | + // simo1, simo2, simo3 |
| 40 | +}; |
| 41 | + |
| 42 | +static int max77675_probe(struct i2c_client *client, const struct i2c_device_id *id) |
| 43 | +{ |
| 44 | + struct max77675_regulator *priv; |
| 45 | + int i, ret; |
| 46 | + |
| 47 | + priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); |
| 48 | + if (!priv) |
| 49 | + return -ENOMEM; |
| 50 | + |
| 51 | + i2c_set_clientdata(client, priv); |
| 52 | + |
| 53 | + priv->regmap = devm_regmap_init_i2c(client, &some_regmap_config); |
| 54 | + if (IS_ERR(priv->regmap)) |
| 55 | + return dev_err_probe(&client->dev, PTR_ERR(priv->regmap), "regmap init failed"); |
| 56 | + |
| 57 | + for (i = 0; i < MAX77675_NUM_REGULATORS; i++) { |
| 58 | + struct regulator_config config = {}; |
| 59 | + config.dev = &client->dev; |
| 60 | + config.regmap = priv->regmap; |
| 61 | + |
| 62 | + priv->rdev[i] = devm_regulator_register(&client->dev, |
| 63 | + &max77675_regulators[i], &config); |
| 64 | + if (IS_ERR(priv->rdev[i])) { |
| 65 | + dev_err(&client->dev, "Failed to register regulator %d\n", i); |
| 66 | + return PTR_ERR(priv->rdev[i]); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + dev_info(&client->dev, "MAX77675 regulator driver loaded\n"); |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static const struct of_device_id max77675_of_match[] = { |
| 75 | + { .compatible = "maxim,max77675", }, |
| 76 | + { /* sentinel */ } |
| 77 | +}; |
| 78 | +MODULE_DEVICE_TABLE(of, max77675_of_match); |
| 79 | + |
| 80 | +static const struct i2c_device_id max77675_id[] = { |
| 81 | + { "max77675", 0 }, |
| 82 | + { } |
| 83 | +}; |
| 84 | +MODULE_DEVICE_TABLE(i2c, max77675_id); |
| 85 | + |
| 86 | +static struct i2c_driver max77675_driver = { |
| 87 | + .driver = { |
| 88 | + .name = "max77675", |
| 89 | + .of_match_table = max77675_of_match, |
| 90 | + }, |
| 91 | + .probe = max77675_probe, |
| 92 | + .id_table = max77675_id, |
| 93 | +}; |
| 94 | + |
| 95 | +module_i2c_driver(max77675_driver); |
| 96 | + |
| 97 | +MODULE_AUTHOR("joan.na@analog.com"); |
| 98 | +MODULE_DESCRIPTION("MAX77675 Regulator Driver"); |
| 99 | +MODULE_LICENSE("GPL"); |
| 100 | + |
0 commit comments