1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright 2020 Google LLC.
5 #include <linux/module.h>
7 #include <linux/platform_data/cros_ec_proto.h>
8 #include <linux/platform_device.h>
9 #include <linux/regulator/driver.h>
10 #include <linux/regulator/machine.h>
11 #include <linux/regulator/of_regulator.h>
12 #include <linux/slab.h>
14 struct cros_ec_regulator_data {
15 struct regulator_desc desc;
16 struct regulator_dev *dev;
17 struct cros_ec_device *ec_dev;
25 static int cros_ec_regulator_enable(struct regulator_dev *dev)
27 struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
28 struct ec_params_regulator_enable cmd = {
33 return cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_ENABLE, &cmd,
34 sizeof(cmd), NULL, 0);
37 static int cros_ec_regulator_disable(struct regulator_dev *dev)
39 struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
40 struct ec_params_regulator_enable cmd = {
45 return cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_ENABLE, &cmd,
46 sizeof(cmd), NULL, 0);
49 static int cros_ec_regulator_is_enabled(struct regulator_dev *dev)
51 struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
52 struct ec_params_regulator_is_enabled cmd = {
55 struct ec_response_regulator_is_enabled resp;
58 ret = cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_IS_ENABLED, &cmd,
59 sizeof(cmd), &resp, sizeof(resp));
65 static int cros_ec_regulator_list_voltage(struct regulator_dev *dev,
66 unsigned int selector)
68 struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
70 if (selector >= data->num_voltages)
73 return data->voltages_mV[selector] * 1000;
76 static int cros_ec_regulator_get_voltage(struct regulator_dev *dev)
78 struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
79 struct ec_params_regulator_get_voltage cmd = {
82 struct ec_response_regulator_get_voltage resp;
85 ret = cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_GET_VOLTAGE, &cmd,
86 sizeof(cmd), &resp, sizeof(resp));
89 return resp.voltage_mv * 1000;
92 static int cros_ec_regulator_set_voltage(struct regulator_dev *dev, int min_uV,
93 int max_uV, unsigned int *selector)
95 struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
96 int min_mV = DIV_ROUND_UP(min_uV, 1000);
97 int max_mV = max_uV / 1000;
98 struct ec_params_regulator_set_voltage cmd = {
105 * This can happen when the given range [min_uV, max_uV] doesn't
106 * contain any voltage that can be represented exactly in mV.
111 return cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_SET_VOLTAGE, &cmd,
112 sizeof(cmd), NULL, 0);
115 static const struct regulator_ops cros_ec_regulator_voltage_ops = {
116 .enable = cros_ec_regulator_enable,
117 .disable = cros_ec_regulator_disable,
118 .is_enabled = cros_ec_regulator_is_enabled,
119 .list_voltage = cros_ec_regulator_list_voltage,
120 .get_voltage = cros_ec_regulator_get_voltage,
121 .set_voltage = cros_ec_regulator_set_voltage,
124 static int cros_ec_regulator_init_info(struct device *dev,
125 struct cros_ec_regulator_data *data)
127 struct ec_params_regulator_get_info cmd = {
128 .index = data->index,
130 struct ec_response_regulator_get_info resp;
133 ret = cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_GET_INFO, &cmd,
134 sizeof(cmd), &resp, sizeof(resp));
139 min_t(u16, ARRAY_SIZE(resp.voltages_mv), resp.num_voltages);
141 devm_kmemdup(dev, resp.voltages_mv,
142 sizeof(u16) * data->num_voltages, GFP_KERNEL);
143 if (!data->voltages_mV)
146 data->desc.n_voltages = data->num_voltages;
148 /* Make sure the returned name is always a valid string */
149 resp.name[ARRAY_SIZE(resp.name) - 1] = '\0';
150 data->desc.name = devm_kstrdup(dev, resp.name, GFP_KERNEL);
151 if (!data->desc.name)
157 static int cros_ec_regulator_probe(struct platform_device *pdev)
159 struct device *dev = &pdev->dev;
160 struct device_node *np = dev->of_node;
161 struct cros_ec_regulator_data *drvdata;
162 struct regulator_init_data *init_data;
163 struct regulator_config cfg = {};
164 struct regulator_desc *desc;
167 drvdata = devm_kzalloc(
168 &pdev->dev, sizeof(struct cros_ec_regulator_data), GFP_KERNEL);
172 drvdata->ec_dev = dev_get_drvdata(dev->parent);
173 desc = &drvdata->desc;
175 init_data = of_get_regulator_init_data(dev, np, desc);
179 ret = of_property_read_u32(np, "reg", &drvdata->index);
183 desc->owner = THIS_MODULE;
184 desc->type = REGULATOR_VOLTAGE;
185 desc->ops = &cros_ec_regulator_voltage_ops;
187 ret = cros_ec_regulator_init_info(dev, drvdata);
191 cfg.dev = &pdev->dev;
192 cfg.init_data = init_data;
193 cfg.driver_data = drvdata;
196 drvdata->dev = devm_regulator_register(dev, &drvdata->desc, &cfg);
197 if (IS_ERR(drvdata->dev)) {
198 ret = PTR_ERR(drvdata->dev);
199 dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
203 platform_set_drvdata(pdev, drvdata);
208 static const struct of_device_id regulator_cros_ec_of_match[] = {
209 { .compatible = "google,cros-ec-regulator", },
212 MODULE_DEVICE_TABLE(of, regulator_cros_ec_of_match);
214 static struct platform_driver cros_ec_regulator_driver = {
215 .probe = cros_ec_regulator_probe,
217 .name = "cros-ec-regulator",
218 .of_match_table = regulator_cros_ec_of_match,
222 module_platform_driver(cros_ec_regulator_driver);
224 MODULE_LICENSE("GPL v2");
225 MODULE_DESCRIPTION("ChromeOS EC controlled regulator");
226 MODULE_AUTHOR("Pi-Hsun Shih <pihsun@chromium.org>");