0001
0002
0003
0004
0005
0006
0007 #include <linux/module.h>
0008 #include <linux/err.h>
0009 #include <linux/i2c.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/regulator/driver.h>
0012 #include <linux/slab.h>
0013 #include <linux/regulator/max1586.h>
0014 #include <linux/of_device.h>
0015 #include <linux/regulator/of_regulator.h>
0016
0017 #define MAX1586_V3_MAX_VSEL 31
0018 #define MAX1586_V6_MAX_VSEL 3
0019
0020 #define MAX1586_V3_MIN_UV 700000
0021 #define MAX1586_V3_MAX_UV 1475000
0022
0023 #define MAX1586_V6_MIN_UV 0
0024 #define MAX1586_V6_MAX_UV 3000000
0025
0026 #define I2C_V3_SELECT (0 << 5)
0027 #define I2C_V6_SELECT (1 << 5)
0028
0029 struct max1586_data {
0030 struct i2c_client *client;
0031
0032
0033 unsigned int min_uV;
0034 unsigned int max_uV;
0035
0036 unsigned int v3_curr_sel;
0037 unsigned int v6_curr_sel;
0038 };
0039
0040
0041
0042
0043
0044
0045
0046 static const unsigned int v6_voltages_uv[] = { 1, 1800000, 2500000, 3000000 };
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 static int max1586_v3_get_voltage_sel(struct regulator_dev *rdev)
0057 {
0058 struct max1586_data *max1586 = rdev_get_drvdata(rdev);
0059
0060 return max1586->v3_curr_sel;
0061 }
0062
0063 static int max1586_v3_set_voltage_sel(struct regulator_dev *rdev,
0064 unsigned selector)
0065 {
0066 struct max1586_data *max1586 = rdev_get_drvdata(rdev);
0067 struct i2c_client *client = max1586->client;
0068 int ret;
0069 u8 v3_prog;
0070
0071 dev_dbg(&client->dev, "changing voltage v3 to %dmv\n",
0072 regulator_list_voltage_linear(rdev, selector) / 1000);
0073
0074 v3_prog = I2C_V3_SELECT | (u8) selector;
0075 ret = i2c_smbus_write_byte(client, v3_prog);
0076 if (ret)
0077 return ret;
0078
0079 max1586->v3_curr_sel = selector;
0080
0081 return 0;
0082 }
0083
0084 static int max1586_v6_get_voltage_sel(struct regulator_dev *rdev)
0085 {
0086 struct max1586_data *max1586 = rdev_get_drvdata(rdev);
0087
0088 return max1586->v6_curr_sel;
0089 }
0090
0091 static int max1586_v6_set_voltage_sel(struct regulator_dev *rdev,
0092 unsigned int selector)
0093 {
0094 struct max1586_data *max1586 = rdev_get_drvdata(rdev);
0095 struct i2c_client *client = max1586->client;
0096 u8 v6_prog;
0097 int ret;
0098
0099 dev_dbg(&client->dev, "changing voltage v6 to %dmv\n",
0100 rdev->desc->volt_table[selector] / 1000);
0101
0102 v6_prog = I2C_V6_SELECT | (u8) selector;
0103 ret = i2c_smbus_write_byte(client, v6_prog);
0104 if (ret)
0105 return ret;
0106
0107 max1586->v6_curr_sel = selector;
0108
0109 return 0;
0110 }
0111
0112
0113
0114
0115
0116 static const struct regulator_ops max1586_v3_ops = {
0117 .get_voltage_sel = max1586_v3_get_voltage_sel,
0118 .set_voltage_sel = max1586_v3_set_voltage_sel,
0119 .list_voltage = regulator_list_voltage_linear,
0120 .map_voltage = regulator_map_voltage_linear,
0121 };
0122
0123 static const struct regulator_ops max1586_v6_ops = {
0124 .get_voltage_sel = max1586_v6_get_voltage_sel,
0125 .set_voltage_sel = max1586_v6_set_voltage_sel,
0126 .list_voltage = regulator_list_voltage_table,
0127 };
0128
0129 static struct regulator_desc max1586_reg[] = {
0130 {
0131 .name = "Output_V3",
0132 .id = MAX1586_V3,
0133 .ops = &max1586_v3_ops,
0134 .type = REGULATOR_VOLTAGE,
0135 .n_voltages = MAX1586_V3_MAX_VSEL + 1,
0136 .owner = THIS_MODULE,
0137 },
0138 {
0139 .name = "Output_V6",
0140 .id = MAX1586_V6,
0141 .ops = &max1586_v6_ops,
0142 .type = REGULATOR_VOLTAGE,
0143 .n_voltages = MAX1586_V6_MAX_VSEL + 1,
0144 .volt_table = v6_voltages_uv,
0145 .owner = THIS_MODULE,
0146 },
0147 };
0148
0149 static int of_get_max1586_platform_data(struct device *dev,
0150 struct max1586_platform_data *pdata)
0151 {
0152 struct max1586_subdev_data *sub;
0153 struct of_regulator_match rmatch[ARRAY_SIZE(max1586_reg)] = { };
0154 struct device_node *np = dev->of_node;
0155 int i, matched;
0156
0157 if (of_property_read_u32(np, "v3-gain",
0158 &pdata->v3_gain) < 0) {
0159 dev_err(dev, "%pOF has no 'v3-gain' property\n", np);
0160 return -EINVAL;
0161 }
0162
0163 np = of_get_child_by_name(np, "regulators");
0164 if (!np) {
0165 dev_err(dev, "missing 'regulators' subnode in DT\n");
0166 return -EINVAL;
0167 }
0168
0169 for (i = 0; i < ARRAY_SIZE(rmatch); i++)
0170 rmatch[i].name = max1586_reg[i].name;
0171
0172 matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
0173 of_node_put(np);
0174
0175
0176
0177
0178
0179
0180
0181 if (matched <= 0)
0182 return matched;
0183
0184 pdata->subdevs = devm_kcalloc(dev,
0185 matched,
0186 sizeof(struct max1586_subdev_data),
0187 GFP_KERNEL);
0188 if (!pdata->subdevs)
0189 return -ENOMEM;
0190
0191 pdata->num_subdevs = matched;
0192 sub = pdata->subdevs;
0193
0194 for (i = 0; i < matched; i++) {
0195 sub->id = i;
0196 sub->name = rmatch[i].of_node->name;
0197 sub->platform_data = rmatch[i].init_data;
0198 sub++;
0199 }
0200
0201 return 0;
0202 }
0203
0204 static const struct of_device_id __maybe_unused max1586_of_match[] = {
0205 { .compatible = "maxim,max1586", },
0206 {},
0207 };
0208 MODULE_DEVICE_TABLE(of, max1586_of_match);
0209
0210 static int max1586_pmic_probe(struct i2c_client *client,
0211 const struct i2c_device_id *i2c_id)
0212 {
0213 struct max1586_platform_data *pdata, pdata_of;
0214 struct regulator_config config = { };
0215 struct max1586_data *max1586;
0216 int i, id, ret;
0217 const struct of_device_id *match;
0218
0219 pdata = dev_get_platdata(&client->dev);
0220 if (client->dev.of_node && !pdata) {
0221 match = of_match_device(of_match_ptr(max1586_of_match),
0222 &client->dev);
0223 if (!match) {
0224 dev_err(&client->dev, "Error: No device match found\n");
0225 return -ENODEV;
0226 }
0227 ret = of_get_max1586_platform_data(&client->dev, &pdata_of);
0228 if (ret < 0)
0229 return ret;
0230 pdata = &pdata_of;
0231 }
0232
0233 max1586 = devm_kzalloc(&client->dev, sizeof(struct max1586_data),
0234 GFP_KERNEL);
0235 if (!max1586)
0236 return -ENOMEM;
0237
0238 max1586->client = client;
0239
0240 if (!pdata->v3_gain)
0241 return -EINVAL;
0242
0243 max1586->min_uV = MAX1586_V3_MIN_UV / 1000 * pdata->v3_gain / 1000;
0244 max1586->max_uV = MAX1586_V3_MAX_UV / 1000 * pdata->v3_gain / 1000;
0245
0246
0247 max1586->v3_curr_sel = 24;
0248 max1586->v6_curr_sel = 0;
0249
0250 for (i = 0; i < pdata->num_subdevs && i <= MAX1586_V6; i++) {
0251 struct regulator_dev *rdev;
0252
0253 id = pdata->subdevs[i].id;
0254 if (!pdata->subdevs[i].platform_data)
0255 continue;
0256 if (id < MAX1586_V3 || id > MAX1586_V6) {
0257 dev_err(&client->dev, "invalid regulator id %d\n", id);
0258 return -EINVAL;
0259 }
0260
0261 if (id == MAX1586_V3) {
0262 max1586_reg[id].min_uV = max1586->min_uV;
0263 max1586_reg[id].uV_step =
0264 (max1586->max_uV - max1586->min_uV) /
0265 MAX1586_V3_MAX_VSEL;
0266 }
0267
0268 config.dev = &client->dev;
0269 config.init_data = pdata->subdevs[i].platform_data;
0270 config.driver_data = max1586;
0271
0272 rdev = devm_regulator_register(&client->dev,
0273 &max1586_reg[id], &config);
0274 if (IS_ERR(rdev)) {
0275 dev_err(&client->dev, "failed to register %s\n",
0276 max1586_reg[id].name);
0277 return PTR_ERR(rdev);
0278 }
0279 }
0280
0281 i2c_set_clientdata(client, max1586);
0282 dev_info(&client->dev, "Maxim 1586 regulator driver loaded\n");
0283 return 0;
0284 }
0285
0286 static const struct i2c_device_id max1586_id[] = {
0287 { "max1586", 0 },
0288 { }
0289 };
0290 MODULE_DEVICE_TABLE(i2c, max1586_id);
0291
0292 static struct i2c_driver max1586_pmic_driver = {
0293 .probe = max1586_pmic_probe,
0294 .driver = {
0295 .name = "max1586",
0296 .of_match_table = of_match_ptr(max1586_of_match),
0297 },
0298 .id_table = max1586_id,
0299 };
0300
0301 static int __init max1586_pmic_init(void)
0302 {
0303 return i2c_add_driver(&max1586_pmic_driver);
0304 }
0305 subsys_initcall(max1586_pmic_init);
0306
0307 static void __exit max1586_pmic_exit(void)
0308 {
0309 i2c_del_driver(&max1586_pmic_driver);
0310 }
0311 module_exit(max1586_pmic_exit);
0312
0313
0314 MODULE_DESCRIPTION("MAXIM 1586 voltage regulator driver");
0315 MODULE_AUTHOR("Robert Jarzmik");
0316 MODULE_LICENSE("GPL");