0001
0002 #include <linux/module.h>
0003 #include <linux/i2c.h>
0004 #include <linux/of.h>
0005 #include <linux/regulator/driver.h>
0006 #include <linux/regmap.h>
0007
0008 static const struct regulator_ops pg86x_ops = {
0009 .set_voltage_sel = regulator_set_voltage_sel_regmap,
0010 .get_voltage_sel = regulator_get_voltage_sel_regmap,
0011 .list_voltage = regulator_list_voltage_linear_range,
0012 };
0013
0014 static const struct linear_range pg86x_buck1_ranges[] = {
0015 REGULATOR_LINEAR_RANGE( 0, 0, 10, 0),
0016 REGULATOR_LINEAR_RANGE(1000000, 11, 34, 25000),
0017 REGULATOR_LINEAR_RANGE(1600000, 35, 47, 50000),
0018 };
0019
0020 static const struct linear_range pg86x_buck2_ranges[] = {
0021 REGULATOR_LINEAR_RANGE( 0, 0, 15, 0),
0022 REGULATOR_LINEAR_RANGE(1000000, 16, 39, 25000),
0023 REGULATOR_LINEAR_RANGE(1600000, 40, 52, 50000),
0024 };
0025
0026 static const struct regulator_desc pg86x_regulators[] = {
0027 {
0028 .id = 0,
0029 .type = REGULATOR_VOLTAGE,
0030 .name = "buck1",
0031 .of_match = of_match_ptr("buck1"),
0032 .n_voltages = 11 + 24 + 13,
0033 .linear_ranges = pg86x_buck1_ranges,
0034 .n_linear_ranges = 3,
0035 .vsel_reg = 0x24,
0036 .vsel_mask = 0xff,
0037 .ops = &pg86x_ops,
0038 .owner = THIS_MODULE
0039 },
0040 {
0041 .id = 1,
0042 .type = REGULATOR_VOLTAGE,
0043 .name = "buck2",
0044 .of_match = of_match_ptr("buck2"),
0045 .n_voltages = 16 + 24 + 13,
0046 .linear_ranges = pg86x_buck2_ranges,
0047 .n_linear_ranges = 3,
0048 .vsel_reg = 0x13,
0049 .vsel_mask = 0xff,
0050 .ops = &pg86x_ops,
0051 .owner = THIS_MODULE
0052 },
0053 };
0054
0055 static const struct regmap_config pg86x_regmap = {
0056 .reg_bits = 8,
0057 .val_bits = 8,
0058 };
0059
0060 static int pg86x_i2c_probe(struct i2c_client *i2c)
0061 {
0062 int id, ret;
0063 struct regulator_config config = {.dev = &i2c->dev};
0064 struct regmap *regmap = devm_regmap_init_i2c(i2c, &pg86x_regmap);
0065
0066 if (IS_ERR(regmap)) {
0067 ret = PTR_ERR(regmap);
0068 dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
0069 return ret;
0070 }
0071
0072 for (id = 0; id < ARRAY_SIZE(pg86x_regulators); id++) {
0073 struct regulator_dev *rdev;
0074 rdev = devm_regulator_register(&i2c->dev,
0075 &pg86x_regulators[id],
0076 &config);
0077 if (IS_ERR(rdev)) {
0078 ret = PTR_ERR(rdev);
0079 dev_err(&i2c->dev, "failed to register %s: %d\n",
0080 pg86x_regulators[id].name, ret);
0081 return ret;
0082 }
0083 }
0084 return 0;
0085 }
0086
0087 static const struct of_device_id __maybe_unused pg86x_dt_ids[] = {
0088 { .compatible = "marvell,88pg867" },
0089 { .compatible = "marvell,88pg868" },
0090 { }
0091 };
0092 MODULE_DEVICE_TABLE(of, pg86x_dt_ids);
0093
0094 static const struct i2c_device_id pg86x_i2c_id[] = {
0095 { "88pg867", },
0096 { "88pg868", },
0097 { }
0098 };
0099 MODULE_DEVICE_TABLE(i2c, pg86x_i2c_id);
0100
0101 static struct i2c_driver pg86x_regulator_driver = {
0102 .driver = {
0103 .name = "88pg86x",
0104 .of_match_table = of_match_ptr(pg86x_dt_ids),
0105 },
0106 .probe_new = pg86x_i2c_probe,
0107 .id_table = pg86x_i2c_id,
0108 };
0109
0110 module_i2c_driver(pg86x_regulator_driver);
0111
0112 MODULE_DESCRIPTION("Marvell 88PG86X voltage regulator");
0113 MODULE_AUTHOR("Alexander Monakov <amonakov@gmail.com>");
0114 MODULE_LICENSE("GPL");