11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
255f4fa4eSRobert Jarzmik /*
355f4fa4eSRobert Jarzmik * max1586.c -- Voltage and current regulation for the Maxim 1586
455f4fa4eSRobert Jarzmik *
555f4fa4eSRobert Jarzmik * Copyright (C) 2008 Robert Jarzmik
655f4fa4eSRobert Jarzmik */
755f4fa4eSRobert Jarzmik #include <linux/module.h>
855f4fa4eSRobert Jarzmik #include <linux/err.h>
955f4fa4eSRobert Jarzmik #include <linux/i2c.h>
1055f4fa4eSRobert Jarzmik #include <linux/platform_device.h>
1155f4fa4eSRobert Jarzmik #include <linux/regulator/driver.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
1355f4fa4eSRobert Jarzmik #include <linux/regulator/max1586.h>
144e005179SRobert Jarzmik #include <linux/of_device.h>
154e005179SRobert Jarzmik #include <linux/regulator/of_regulator.h>
1655f4fa4eSRobert Jarzmik
1755f4fa4eSRobert Jarzmik #define MAX1586_V3_MAX_VSEL 31
1855f4fa4eSRobert Jarzmik #define MAX1586_V6_MAX_VSEL 3
1955f4fa4eSRobert Jarzmik
2055f4fa4eSRobert Jarzmik #define MAX1586_V3_MIN_UV 700000
2155f4fa4eSRobert Jarzmik #define MAX1586_V3_MAX_UV 1475000
2255f4fa4eSRobert Jarzmik
2355f4fa4eSRobert Jarzmik #define MAX1586_V6_MIN_UV 0
2455f4fa4eSRobert Jarzmik #define MAX1586_V6_MAX_UV 3000000
2555f4fa4eSRobert Jarzmik
2655f4fa4eSRobert Jarzmik #define I2C_V3_SELECT (0 << 5)
2755f4fa4eSRobert Jarzmik #define I2C_V6_SELECT (1 << 5)
2855f4fa4eSRobert Jarzmik
29b110a8fbSPhilipp Zabel struct max1586_data {
30b110a8fbSPhilipp Zabel struct i2c_client *client;
31b110a8fbSPhilipp Zabel
32b110a8fbSPhilipp Zabel /* min/max V3 voltage */
33c8f1e502SPhilipp Zabel unsigned int min_uV;
34c8f1e502SPhilipp Zabel unsigned int max_uV;
35b110a8fbSPhilipp Zabel
364efd9dfeSAxel Lin unsigned int v3_curr_sel;
374efd9dfeSAxel Lin unsigned int v6_curr_sel;
38b110a8fbSPhilipp Zabel };
39b110a8fbSPhilipp Zabel
4055f4fa4eSRobert Jarzmik /*
4196d25221SAxel Lin * V6 voltage
4296d25221SAxel Lin * On I2C bus, sending a "x" byte to the max1586 means :
4396d25221SAxel Lin * set V6 to either 0V, 1.8V, 2.5V, 3V depending on (x & 0x3)
4496d25221SAxel Lin * As regulator framework doesn't accept voltages to be 0V, we use 1uV.
4596d25221SAxel Lin */
46d67c42ccSAxel Lin static const unsigned int v6_voltages_uv[] = { 1, 1800000, 2500000, 3000000 };
4796d25221SAxel Lin
4896d25221SAxel Lin /*
4955f4fa4eSRobert Jarzmik * V3 voltage
5055f4fa4eSRobert Jarzmik * On I2C bus, sending a "x" byte to the max1586 means :
5155f4fa4eSRobert Jarzmik * set V3 to 0.700V + (x & 0x1f) * 0.025V
52b110a8fbSPhilipp Zabel * This voltage can be increased by external resistors
53b110a8fbSPhilipp Zabel * R24 and R25=100kOhm as described in the data sheet.
54b110a8fbSPhilipp Zabel * The gain is approximately: 1 + R24/R25 + R24/185.5kOhm
5555f4fa4eSRobert Jarzmik */
max1586_v3_get_voltage_sel(struct regulator_dev * rdev)564efd9dfeSAxel Lin static int max1586_v3_get_voltage_sel(struct regulator_dev *rdev)
574efd9dfeSAxel Lin {
584efd9dfeSAxel Lin struct max1586_data *max1586 = rdev_get_drvdata(rdev);
594efd9dfeSAxel Lin
604efd9dfeSAxel Lin return max1586->v3_curr_sel;
614efd9dfeSAxel Lin }
624efd9dfeSAxel Lin
max1586_v3_set_voltage_sel(struct regulator_dev * rdev,unsigned selector)639b558950SAxel Lin static int max1586_v3_set_voltage_sel(struct regulator_dev *rdev,
649b558950SAxel Lin unsigned selector)
6555f4fa4eSRobert Jarzmik {
66b110a8fbSPhilipp Zabel struct max1586_data *max1586 = rdev_get_drvdata(rdev);
67b110a8fbSPhilipp Zabel struct i2c_client *client = max1586->client;
684efd9dfeSAxel Lin int ret;
6955f4fa4eSRobert Jarzmik u8 v3_prog;
7055f4fa4eSRobert Jarzmik
7155f4fa4eSRobert Jarzmik dev_dbg(&client->dev, "changing voltage v3 to %dmv\n",
729b558950SAxel Lin regulator_list_voltage_linear(rdev, selector) / 1000);
7355f4fa4eSRobert Jarzmik
749b558950SAxel Lin v3_prog = I2C_V3_SELECT | (u8) selector;
754efd9dfeSAxel Lin ret = i2c_smbus_write_byte(client, v3_prog);
764efd9dfeSAxel Lin if (ret)
774efd9dfeSAxel Lin return ret;
784efd9dfeSAxel Lin
794efd9dfeSAxel Lin max1586->v3_curr_sel = selector;
804efd9dfeSAxel Lin
814efd9dfeSAxel Lin return 0;
824efd9dfeSAxel Lin }
834efd9dfeSAxel Lin
max1586_v6_get_voltage_sel(struct regulator_dev * rdev)844efd9dfeSAxel Lin static int max1586_v6_get_voltage_sel(struct regulator_dev *rdev)
854efd9dfeSAxel Lin {
864efd9dfeSAxel Lin struct max1586_data *max1586 = rdev_get_drvdata(rdev);
874efd9dfeSAxel Lin
884efd9dfeSAxel Lin return max1586->v6_curr_sel;
8955f4fa4eSRobert Jarzmik }
9055f4fa4eSRobert Jarzmik
max1586_v6_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)910d032731SAxel Lin static int max1586_v6_set_voltage_sel(struct regulator_dev *rdev,
920d032731SAxel Lin unsigned int selector)
9355f4fa4eSRobert Jarzmik {
944efd9dfeSAxel Lin struct max1586_data *max1586 = rdev_get_drvdata(rdev);
954efd9dfeSAxel Lin struct i2c_client *client = max1586->client;
9655f4fa4eSRobert Jarzmik u8 v6_prog;
974efd9dfeSAxel Lin int ret;
9855f4fa4eSRobert Jarzmik
9955f4fa4eSRobert Jarzmik dev_dbg(&client->dev, "changing voltage v6 to %dmv\n",
1000d032731SAxel Lin rdev->desc->volt_table[selector] / 1000);
10155f4fa4eSRobert Jarzmik
1020d032731SAxel Lin v6_prog = I2C_V6_SELECT | (u8) selector;
1034efd9dfeSAxel Lin ret = i2c_smbus_write_byte(client, v6_prog);
1044efd9dfeSAxel Lin if (ret)
1054efd9dfeSAxel Lin return ret;
1064efd9dfeSAxel Lin
1074efd9dfeSAxel Lin max1586->v6_curr_sel = selector;
1084efd9dfeSAxel Lin
1094efd9dfeSAxel Lin return 0;
11055f4fa4eSRobert Jarzmik }
11155f4fa4eSRobert Jarzmik
11255f4fa4eSRobert Jarzmik /*
11355f4fa4eSRobert Jarzmik * The Maxim 1586 controls V3 and V6 voltages, but offers no way of reading back
11455f4fa4eSRobert Jarzmik * the set up value.
11555f4fa4eSRobert Jarzmik */
1160f6ce809SKrzysztof Kozlowski static const struct regulator_ops max1586_v3_ops = {
1174efd9dfeSAxel Lin .get_voltage_sel = max1586_v3_get_voltage_sel,
1189b558950SAxel Lin .set_voltage_sel = max1586_v3_set_voltage_sel,
11993f5de5cSAxel Lin .list_voltage = regulator_list_voltage_linear,
1209b558950SAxel Lin .map_voltage = regulator_map_voltage_linear,
12155f4fa4eSRobert Jarzmik };
12255f4fa4eSRobert Jarzmik
1230f6ce809SKrzysztof Kozlowski static const struct regulator_ops max1586_v6_ops = {
1244efd9dfeSAxel Lin .get_voltage_sel = max1586_v6_get_voltage_sel,
1250d032731SAxel Lin .set_voltage_sel = max1586_v6_set_voltage_sel,
12696d25221SAxel Lin .list_voltage = regulator_list_voltage_table,
12755f4fa4eSRobert Jarzmik };
12855f4fa4eSRobert Jarzmik
12993f5de5cSAxel Lin static struct regulator_desc max1586_reg[] = {
13055f4fa4eSRobert Jarzmik {
13155f4fa4eSRobert Jarzmik .name = "Output_V3",
13255f4fa4eSRobert Jarzmik .id = MAX1586_V3,
13355f4fa4eSRobert Jarzmik .ops = &max1586_v3_ops,
13455f4fa4eSRobert Jarzmik .type = REGULATOR_VOLTAGE,
13555f4fa4eSRobert Jarzmik .n_voltages = MAX1586_V3_MAX_VSEL + 1,
13655f4fa4eSRobert Jarzmik .owner = THIS_MODULE,
13755f4fa4eSRobert Jarzmik },
13855f4fa4eSRobert Jarzmik {
13955f4fa4eSRobert Jarzmik .name = "Output_V6",
14055f4fa4eSRobert Jarzmik .id = MAX1586_V6,
14155f4fa4eSRobert Jarzmik .ops = &max1586_v6_ops,
14255f4fa4eSRobert Jarzmik .type = REGULATOR_VOLTAGE,
14355f4fa4eSRobert Jarzmik .n_voltages = MAX1586_V6_MAX_VSEL + 1,
14496d25221SAxel Lin .volt_table = v6_voltages_uv,
14555f4fa4eSRobert Jarzmik .owner = THIS_MODULE,
14655f4fa4eSRobert Jarzmik },
14755f4fa4eSRobert Jarzmik };
14855f4fa4eSRobert Jarzmik
of_get_max1586_platform_data(struct device * dev,struct max1586_platform_data * pdata)1495ccedf03SFengguang Wu static int of_get_max1586_platform_data(struct device *dev,
1504e005179SRobert Jarzmik struct max1586_platform_data *pdata)
1514e005179SRobert Jarzmik {
1524e005179SRobert Jarzmik struct max1586_subdev_data *sub;
153d83aef13SJavier Martinez Canillas struct of_regulator_match rmatch[ARRAY_SIZE(max1586_reg)] = { };
1544e005179SRobert Jarzmik struct device_node *np = dev->of_node;
1554e005179SRobert Jarzmik int i, matched;
1564e005179SRobert Jarzmik
1574e005179SRobert Jarzmik if (of_property_read_u32(np, "v3-gain",
1584e005179SRobert Jarzmik &pdata->v3_gain) < 0) {
1597799167bSRob Herring dev_err(dev, "%pOF has no 'v3-gain' property\n", np);
1604e005179SRobert Jarzmik return -EINVAL;
1614e005179SRobert Jarzmik }
1624e005179SRobert Jarzmik
1634e005179SRobert Jarzmik np = of_get_child_by_name(np, "regulators");
1644e005179SRobert Jarzmik if (!np) {
1654e005179SRobert Jarzmik dev_err(dev, "missing 'regulators' subnode in DT\n");
1664e005179SRobert Jarzmik return -EINVAL;
1674e005179SRobert Jarzmik }
1684e005179SRobert Jarzmik
1694e005179SRobert Jarzmik for (i = 0; i < ARRAY_SIZE(rmatch); i++)
1704e005179SRobert Jarzmik rmatch[i].name = max1586_reg[i].name;
1714e005179SRobert Jarzmik
1724e005179SRobert Jarzmik matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch));
1734e005179SRobert Jarzmik of_node_put(np);
1744e005179SRobert Jarzmik /*
1754e005179SRobert Jarzmik * If matched is 0, ie. neither Output_V3 nor Output_V6 have been found,
1764e005179SRobert Jarzmik * return 0, which signals the normal situation where no subregulator is
1774e005179SRobert Jarzmik * available. This is normal because the max1586 doesn't provide any
1784e005179SRobert Jarzmik * readback support, so the subregulators can't report any status
1794e005179SRobert Jarzmik * anyway. If matched < 0, return the error.
1804e005179SRobert Jarzmik */
1814e005179SRobert Jarzmik if (matched <= 0)
1824e005179SRobert Jarzmik return matched;
1834e005179SRobert Jarzmik
184a86854d0SKees Cook pdata->subdevs = devm_kcalloc(dev,
185a86854d0SKees Cook matched,
186a86854d0SKees Cook sizeof(struct max1586_subdev_data),
187a86854d0SKees Cook GFP_KERNEL);
1884e005179SRobert Jarzmik if (!pdata->subdevs)
1894e005179SRobert Jarzmik return -ENOMEM;
1904e005179SRobert Jarzmik
1914e005179SRobert Jarzmik pdata->num_subdevs = matched;
1924e005179SRobert Jarzmik sub = pdata->subdevs;
1934e005179SRobert Jarzmik
1944e005179SRobert Jarzmik for (i = 0; i < matched; i++) {
1954e005179SRobert Jarzmik sub->id = i;
1964e005179SRobert Jarzmik sub->name = rmatch[i].of_node->name;
1974e005179SRobert Jarzmik sub->platform_data = rmatch[i].init_data;
1984e005179SRobert Jarzmik sub++;
1994e005179SRobert Jarzmik }
2004e005179SRobert Jarzmik
2014e005179SRobert Jarzmik return 0;
2024e005179SRobert Jarzmik }
2034e005179SRobert Jarzmik
20444bc5d16SJisheng Zhang static const struct of_device_id __maybe_unused max1586_of_match[] = {
2054e005179SRobert Jarzmik { .compatible = "maxim,max1586", },
2064e005179SRobert Jarzmik {},
2074e005179SRobert Jarzmik };
2084e005179SRobert Jarzmik MODULE_DEVICE_TABLE(of, max1586_of_match);
2094e005179SRobert Jarzmik
max1586_pmic_probe(struct i2c_client * client)2103d54f7baSUwe Kleine-König static int max1586_pmic_probe(struct i2c_client *client)
21155f4fa4eSRobert Jarzmik {
2124e005179SRobert Jarzmik struct max1586_platform_data *pdata, pdata_of;
213c172708dSMark Brown struct regulator_config config = { };
214b110a8fbSPhilipp Zabel struct max1586_data *max1586;
2154e005179SRobert Jarzmik int i, id, ret;
2164e005179SRobert Jarzmik const struct of_device_id *match;
2174e005179SRobert Jarzmik
2184e005179SRobert Jarzmik pdata = dev_get_platdata(&client->dev);
2194e005179SRobert Jarzmik if (client->dev.of_node && !pdata) {
2204e005179SRobert Jarzmik match = of_match_device(of_match_ptr(max1586_of_match),
2214e005179SRobert Jarzmik &client->dev);
2224e005179SRobert Jarzmik if (!match) {
2234e005179SRobert Jarzmik dev_err(&client->dev, "Error: No device match found\n");
2244e005179SRobert Jarzmik return -ENODEV;
2254e005179SRobert Jarzmik }
2264e005179SRobert Jarzmik ret = of_get_max1586_platform_data(&client->dev, &pdata_of);
2274e005179SRobert Jarzmik if (ret < 0)
2284e005179SRobert Jarzmik return ret;
2294e005179SRobert Jarzmik pdata = &pdata_of;
2304e005179SRobert Jarzmik }
23155f4fa4eSRobert Jarzmik
2322d45e78aSKrzysztof Kozlowski max1586 = devm_kzalloc(&client->dev, sizeof(struct max1586_data),
23355f4fa4eSRobert Jarzmik GFP_KERNEL);
234b110a8fbSPhilipp Zabel if (!max1586)
235b7bd05b8SAxel Lin return -ENOMEM;
23655f4fa4eSRobert Jarzmik
237b110a8fbSPhilipp Zabel max1586->client = client;
238b110a8fbSPhilipp Zabel
239b7bd05b8SAxel Lin if (!pdata->v3_gain)
240b7bd05b8SAxel Lin return -EINVAL;
241b7bd05b8SAxel Lin
242c8f1e502SPhilipp Zabel max1586->min_uV = MAX1586_V3_MIN_UV / 1000 * pdata->v3_gain / 1000;
243c8f1e502SPhilipp Zabel max1586->max_uV = MAX1586_V3_MAX_UV / 1000 * pdata->v3_gain / 1000;
244b110a8fbSPhilipp Zabel
2454efd9dfeSAxel Lin /* Set curr_sel to default voltage on power-up */
2464efd9dfeSAxel Lin max1586->v3_curr_sel = 24; /* 1.3V */
2474efd9dfeSAxel Lin max1586->v6_curr_sel = 0;
2484efd9dfeSAxel Lin
24955f4fa4eSRobert Jarzmik for (i = 0; i < pdata->num_subdevs && i <= MAX1586_V6; i++) {
250aaa46b4bSKrzysztof Kozlowski struct regulator_dev *rdev;
251aaa46b4bSKrzysztof Kozlowski
25255f4fa4eSRobert Jarzmik id = pdata->subdevs[i].id;
25355f4fa4eSRobert Jarzmik if (!pdata->subdevs[i].platform_data)
25455f4fa4eSRobert Jarzmik continue;
25555f4fa4eSRobert Jarzmik if (id < MAX1586_V3 || id > MAX1586_V6) {
25655f4fa4eSRobert Jarzmik dev_err(&client->dev, "invalid regulator id %d\n", id);
257249cc1f2SSachin Kamat return -EINVAL;
25855f4fa4eSRobert Jarzmik }
259c172708dSMark Brown
26093f5de5cSAxel Lin if (id == MAX1586_V3) {
26193f5de5cSAxel Lin max1586_reg[id].min_uV = max1586->min_uV;
26293f5de5cSAxel Lin max1586_reg[id].uV_step =
26393f5de5cSAxel Lin (max1586->max_uV - max1586->min_uV) /
26493f5de5cSAxel Lin MAX1586_V3_MAX_VSEL;
26593f5de5cSAxel Lin }
26693f5de5cSAxel Lin
267c172708dSMark Brown config.dev = &client->dev;
268c172708dSMark Brown config.init_data = pdata->subdevs[i].platform_data;
269c172708dSMark Brown config.driver_data = max1586;
270c172708dSMark Brown
271aaa46b4bSKrzysztof Kozlowski rdev = devm_regulator_register(&client->dev,
272249cc1f2SSachin Kamat &max1586_reg[id], &config);
273aaa46b4bSKrzysztof Kozlowski if (IS_ERR(rdev)) {
27455f4fa4eSRobert Jarzmik dev_err(&client->dev, "failed to register %s\n",
27555f4fa4eSRobert Jarzmik max1586_reg[id].name);
276aaa46b4bSKrzysztof Kozlowski return PTR_ERR(rdev);
27755f4fa4eSRobert Jarzmik }
27855f4fa4eSRobert Jarzmik }
27955f4fa4eSRobert Jarzmik
2807c4c25e4SAxel Lin i2c_set_clientdata(client, max1586);
28155f4fa4eSRobert Jarzmik dev_info(&client->dev, "Maxim 1586 regulator driver loaded\n");
28255f4fa4eSRobert Jarzmik return 0;
28355f4fa4eSRobert Jarzmik }
28455f4fa4eSRobert Jarzmik
28555f4fa4eSRobert Jarzmik static const struct i2c_device_id max1586_id[] = {
28655f4fa4eSRobert Jarzmik { "max1586", 0 },
28755f4fa4eSRobert Jarzmik { }
28855f4fa4eSRobert Jarzmik };
28955f4fa4eSRobert Jarzmik MODULE_DEVICE_TABLE(i2c, max1586_id);
29055f4fa4eSRobert Jarzmik
29155f4fa4eSRobert Jarzmik static struct i2c_driver max1586_pmic_driver = {
2923d54f7baSUwe Kleine-König .probe = max1586_pmic_probe,
29355f4fa4eSRobert Jarzmik .driver = {
29455f4fa4eSRobert Jarzmik .name = "max1586",
295*259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
2964e005179SRobert Jarzmik .of_match_table = of_match_ptr(max1586_of_match),
29755f4fa4eSRobert Jarzmik },
29855f4fa4eSRobert Jarzmik .id_table = max1586_id,
29955f4fa4eSRobert Jarzmik };
30055f4fa4eSRobert Jarzmik
max1586_pmic_init(void)30155f4fa4eSRobert Jarzmik static int __init max1586_pmic_init(void)
30255f4fa4eSRobert Jarzmik {
30355f4fa4eSRobert Jarzmik return i2c_add_driver(&max1586_pmic_driver);
30455f4fa4eSRobert Jarzmik }
30555f4fa4eSRobert Jarzmik subsys_initcall(max1586_pmic_init);
30655f4fa4eSRobert Jarzmik
max1586_pmic_exit(void)30755f4fa4eSRobert Jarzmik static void __exit max1586_pmic_exit(void)
30855f4fa4eSRobert Jarzmik {
30955f4fa4eSRobert Jarzmik i2c_del_driver(&max1586_pmic_driver);
31055f4fa4eSRobert Jarzmik }
31155f4fa4eSRobert Jarzmik module_exit(max1586_pmic_exit);
31255f4fa4eSRobert Jarzmik
31355f4fa4eSRobert Jarzmik /* Module information */
31455f4fa4eSRobert Jarzmik MODULE_DESCRIPTION("MAXIM 1586 voltage regulator driver");
31555f4fa4eSRobert Jarzmik MODULE_AUTHOR("Robert Jarzmik");
31655f4fa4eSRobert Jarzmik MODULE_LICENSE("GPL");
317