18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Copyright 2020 Google LLC.
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/of.h>
78c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
108c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
118c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistruct cros_ec_regulator_data {
158c2ecf20Sopenharmony_ci	struct regulator_desc desc;
168c2ecf20Sopenharmony_ci	struct regulator_dev *dev;
178c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	u32 index;
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	u16 *voltages_mV;
228c2ecf20Sopenharmony_ci	u16 num_voltages;
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int cros_ec_cmd(struct cros_ec_device *ec, u32 version, u32 command,
268c2ecf20Sopenharmony_ci		       void *outdata, u32 outsize, void *indata, u32 insize)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
298c2ecf20Sopenharmony_ci	int ret;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
328c2ecf20Sopenharmony_ci	if (!msg)
338c2ecf20Sopenharmony_ci		return -ENOMEM;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	msg->version = version;
368c2ecf20Sopenharmony_ci	msg->command = command;
378c2ecf20Sopenharmony_ci	msg->outsize = outsize;
388c2ecf20Sopenharmony_ci	msg->insize = insize;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	if (outdata && outsize > 0)
418c2ecf20Sopenharmony_ci		memcpy(msg->data, outdata, outsize);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	ret = cros_ec_cmd_xfer_status(ec, msg);
448c2ecf20Sopenharmony_ci	if (ret < 0)
458c2ecf20Sopenharmony_ci		goto cleanup;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (insize)
488c2ecf20Sopenharmony_ci		memcpy(indata, msg->data, insize);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cicleanup:
518c2ecf20Sopenharmony_ci	kfree(msg);
528c2ecf20Sopenharmony_ci	return ret;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic int cros_ec_regulator_enable(struct regulator_dev *dev)
568c2ecf20Sopenharmony_ci{
578c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
588c2ecf20Sopenharmony_ci	struct ec_params_regulator_enable cmd = {
598c2ecf20Sopenharmony_ci		.index = data->index,
608c2ecf20Sopenharmony_ci		.enable = 1,
618c2ecf20Sopenharmony_ci	};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_ENABLE, &cmd,
648c2ecf20Sopenharmony_ci			  sizeof(cmd), NULL, 0);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int cros_ec_regulator_disable(struct regulator_dev *dev)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
708c2ecf20Sopenharmony_ci	struct ec_params_regulator_enable cmd = {
718c2ecf20Sopenharmony_ci		.index = data->index,
728c2ecf20Sopenharmony_ci		.enable = 0,
738c2ecf20Sopenharmony_ci	};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_ENABLE, &cmd,
768c2ecf20Sopenharmony_ci			  sizeof(cmd), NULL, 0);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int cros_ec_regulator_is_enabled(struct regulator_dev *dev)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
828c2ecf20Sopenharmony_ci	struct ec_params_regulator_is_enabled cmd = {
838c2ecf20Sopenharmony_ci		.index = data->index,
848c2ecf20Sopenharmony_ci	};
858c2ecf20Sopenharmony_ci	struct ec_response_regulator_is_enabled resp;
868c2ecf20Sopenharmony_ci	int ret;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	ret = cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_IS_ENABLED, &cmd,
898c2ecf20Sopenharmony_ci			  sizeof(cmd), &resp, sizeof(resp));
908c2ecf20Sopenharmony_ci	if (ret < 0)
918c2ecf20Sopenharmony_ci		return ret;
928c2ecf20Sopenharmony_ci	return resp.enabled;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic int cros_ec_regulator_list_voltage(struct regulator_dev *dev,
968c2ecf20Sopenharmony_ci					  unsigned int selector)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (selector >= data->num_voltages)
1018c2ecf20Sopenharmony_ci		return -EINVAL;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	return data->voltages_mV[selector] * 1000;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int cros_ec_regulator_get_voltage(struct regulator_dev *dev)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
1098c2ecf20Sopenharmony_ci	struct ec_params_regulator_get_voltage cmd = {
1108c2ecf20Sopenharmony_ci		.index = data->index,
1118c2ecf20Sopenharmony_ci	};
1128c2ecf20Sopenharmony_ci	struct ec_response_regulator_get_voltage resp;
1138c2ecf20Sopenharmony_ci	int ret;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	ret = cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_GET_VOLTAGE, &cmd,
1168c2ecf20Sopenharmony_ci			  sizeof(cmd), &resp, sizeof(resp));
1178c2ecf20Sopenharmony_ci	if (ret < 0)
1188c2ecf20Sopenharmony_ci		return ret;
1198c2ecf20Sopenharmony_ci	return resp.voltage_mv * 1000;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int cros_ec_regulator_set_voltage(struct regulator_dev *dev, int min_uV,
1238c2ecf20Sopenharmony_ci					 int max_uV, unsigned int *selector)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *data = rdev_get_drvdata(dev);
1268c2ecf20Sopenharmony_ci	int min_mV = DIV_ROUND_UP(min_uV, 1000);
1278c2ecf20Sopenharmony_ci	int max_mV = max_uV / 1000;
1288c2ecf20Sopenharmony_ci	struct ec_params_regulator_set_voltage cmd = {
1298c2ecf20Sopenharmony_ci		.index = data->index,
1308c2ecf20Sopenharmony_ci		.min_mv = min_mV,
1318c2ecf20Sopenharmony_ci		.max_mv = max_mV,
1328c2ecf20Sopenharmony_ci	};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/*
1358c2ecf20Sopenharmony_ci	 * This can happen when the given range [min_uV, max_uV] doesn't
1368c2ecf20Sopenharmony_ci	 * contain any voltage that can be represented exactly in mV.
1378c2ecf20Sopenharmony_ci	 */
1388c2ecf20Sopenharmony_ci	if (min_mV > max_mV)
1398c2ecf20Sopenharmony_ci		return -EINVAL;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	return cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_SET_VOLTAGE, &cmd,
1428c2ecf20Sopenharmony_ci			   sizeof(cmd), NULL, 0);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic const struct regulator_ops cros_ec_regulator_voltage_ops = {
1468c2ecf20Sopenharmony_ci	.enable = cros_ec_regulator_enable,
1478c2ecf20Sopenharmony_ci	.disable = cros_ec_regulator_disable,
1488c2ecf20Sopenharmony_ci	.is_enabled = cros_ec_regulator_is_enabled,
1498c2ecf20Sopenharmony_ci	.list_voltage = cros_ec_regulator_list_voltage,
1508c2ecf20Sopenharmony_ci	.get_voltage = cros_ec_regulator_get_voltage,
1518c2ecf20Sopenharmony_ci	.set_voltage = cros_ec_regulator_set_voltage,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int cros_ec_regulator_init_info(struct device *dev,
1558c2ecf20Sopenharmony_ci				       struct cros_ec_regulator_data *data)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct ec_params_regulator_get_info cmd = {
1588c2ecf20Sopenharmony_ci		.index = data->index,
1598c2ecf20Sopenharmony_ci	};
1608c2ecf20Sopenharmony_ci	struct ec_response_regulator_get_info resp;
1618c2ecf20Sopenharmony_ci	int ret;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ret = cros_ec_cmd(data->ec_dev, 0, EC_CMD_REGULATOR_GET_INFO, &cmd,
1648c2ecf20Sopenharmony_ci			   sizeof(cmd), &resp, sizeof(resp));
1658c2ecf20Sopenharmony_ci	if (ret < 0)
1668c2ecf20Sopenharmony_ci		return ret;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	data->num_voltages =
1698c2ecf20Sopenharmony_ci		min_t(u16, ARRAY_SIZE(resp.voltages_mv), resp.num_voltages);
1708c2ecf20Sopenharmony_ci	data->voltages_mV =
1718c2ecf20Sopenharmony_ci		devm_kmemdup(dev, resp.voltages_mv,
1728c2ecf20Sopenharmony_ci			     sizeof(u16) * data->num_voltages, GFP_KERNEL);
1738c2ecf20Sopenharmony_ci	if (!data->voltages_mV)
1748c2ecf20Sopenharmony_ci		return -ENOMEM;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	data->desc.n_voltages = data->num_voltages;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/* Make sure the returned name is always a valid string */
1798c2ecf20Sopenharmony_ci	resp.name[ARRAY_SIZE(resp.name) - 1] = '\0';
1808c2ecf20Sopenharmony_ci	data->desc.name = devm_kstrdup(dev, resp.name, GFP_KERNEL);
1818c2ecf20Sopenharmony_ci	if (!data->desc.name)
1828c2ecf20Sopenharmony_ci		return -ENOMEM;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	return 0;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int cros_ec_regulator_probe(struct platform_device *pdev)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1908c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
1918c2ecf20Sopenharmony_ci	struct cros_ec_regulator_data *drvdata;
1928c2ecf20Sopenharmony_ci	struct regulator_init_data *init_data;
1938c2ecf20Sopenharmony_ci	struct regulator_config cfg = {};
1948c2ecf20Sopenharmony_ci	struct regulator_desc *desc;
1958c2ecf20Sopenharmony_ci	int ret;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	drvdata = devm_kzalloc(
1988c2ecf20Sopenharmony_ci		&pdev->dev, sizeof(struct cros_ec_regulator_data), GFP_KERNEL);
1998c2ecf20Sopenharmony_ci	if (!drvdata)
2008c2ecf20Sopenharmony_ci		return -ENOMEM;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	drvdata->ec_dev = dev_get_drvdata(dev->parent);
2038c2ecf20Sopenharmony_ci	desc = &drvdata->desc;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	init_data = of_get_regulator_init_data(dev, np, desc);
2068c2ecf20Sopenharmony_ci	if (!init_data)
2078c2ecf20Sopenharmony_ci		return -EINVAL;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "reg", &drvdata->index);
2108c2ecf20Sopenharmony_ci	if (ret < 0)
2118c2ecf20Sopenharmony_ci		return ret;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	desc->owner = THIS_MODULE;
2148c2ecf20Sopenharmony_ci	desc->type = REGULATOR_VOLTAGE;
2158c2ecf20Sopenharmony_ci	desc->ops = &cros_ec_regulator_voltage_ops;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	ret = cros_ec_regulator_init_info(dev, drvdata);
2188c2ecf20Sopenharmony_ci	if (ret < 0)
2198c2ecf20Sopenharmony_ci		return ret;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	cfg.dev = &pdev->dev;
2228c2ecf20Sopenharmony_ci	cfg.init_data = init_data;
2238c2ecf20Sopenharmony_ci	cfg.driver_data = drvdata;
2248c2ecf20Sopenharmony_ci	cfg.of_node = np;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	drvdata->dev = devm_regulator_register(dev, &drvdata->desc, &cfg);
2278c2ecf20Sopenharmony_ci	if (IS_ERR(drvdata->dev)) {
2288c2ecf20Sopenharmony_ci		ret = PTR_ERR(drvdata->dev);
2298c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
2308c2ecf20Sopenharmony_ci		return ret;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, drvdata);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return 0;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic const struct of_device_id regulator_cros_ec_of_match[] = {
2398c2ecf20Sopenharmony_ci	{ .compatible = "google,cros-ec-regulator", },
2408c2ecf20Sopenharmony_ci	{}
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, regulator_cros_ec_of_match);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic struct platform_driver cros_ec_regulator_driver = {
2458c2ecf20Sopenharmony_ci	.probe		= cros_ec_regulator_probe,
2468c2ecf20Sopenharmony_ci	.driver		= {
2478c2ecf20Sopenharmony_ci		.name		= "cros-ec-regulator",
2488c2ecf20Sopenharmony_ci		.of_match_table = regulator_cros_ec_of_match,
2498c2ecf20Sopenharmony_ci	},
2508c2ecf20Sopenharmony_ci};
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cimodule_platform_driver(cros_ec_regulator_driver);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ChromeOS EC controlled regulator");
2568c2ecf20Sopenharmony_ciMODULE_AUTHOR("Pi-Hsun Shih <pihsun@chromium.org>");
257