1 // SPDX-License-Identifier: GPL-2.0
3 * Expose a PWM controlled by the ChromeOS EC to the host processor.
5 * Copyright (C) 2016 Google, Inc.
8 #include <linux/module.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 #include <linux/platform_device.h>
13 #include <linux/pwm.h>
14 #include <linux/slab.h>
16 #include <dt-bindings/mfd/cros_ec.h>
19 * struct cros_ec_pwm_device - Driver data for EC PWM
21 * @ec: Pointer to EC device
22 * @use_pwm_type: Use PWM types instead of generic channels
24 struct cros_ec_pwm_device {
25 struct cros_ec_device *ec;
29 static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *chip)
31 return pwmchip_get_drvdata(chip);
34 static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
37 case CROS_EC_PWM_DT_KB_LIGHT:
38 *pwm_type = EC_PWM_TYPE_KB_LIGHT;
40 case CROS_EC_PWM_DT_DISPLAY_LIGHT:
41 *pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
48 static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
51 struct cros_ec_device *ec = ec_pwm->ec;
53 struct cros_ec_command msg;
54 struct ec_params_pwm_set_duty params;
56 struct ec_params_pwm_set_duty *params = &buf.params;
57 struct cros_ec_command *msg = &buf.msg;
60 memset(&buf, 0, sizeof(buf));
63 msg->command = EC_CMD_PWM_SET_DUTY;
65 msg->outsize = sizeof(*params);
69 if (ec_pwm->use_pwm_type) {
70 ret = cros_ec_dt_type_to_pwm_type(index, ¶ms->pwm_type);
72 dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
77 params->pwm_type = EC_PWM_TYPE_GENERIC;
78 params->index = index;
81 return cros_ec_cmd_xfer_status(ec, msg);
84 static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, bool use_pwm_type, u8 index)
87 struct cros_ec_command msg;
89 struct ec_params_pwm_get_duty params;
90 struct ec_response_pwm_get_duty resp;
93 struct ec_params_pwm_get_duty *params = &buf.params;
94 struct ec_response_pwm_get_duty *resp = &buf.resp;
95 struct cros_ec_command *msg = &buf.msg;
98 memset(&buf, 0, sizeof(buf));
101 msg->command = EC_CMD_PWM_GET_DUTY;
102 msg->insize = sizeof(*resp);
103 msg->outsize = sizeof(*params);
106 ret = cros_ec_dt_type_to_pwm_type(index, ¶ms->pwm_type);
108 dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
113 params->pwm_type = EC_PWM_TYPE_GENERIC;
114 params->index = index;
117 ret = cros_ec_cmd_xfer_status(ec, msg);
124 static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
125 const struct pwm_state *state)
127 struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
131 /* The EC won't let us change the period */
132 if (state->period != EC_PWM_MAX_DUTY)
135 if (state->polarity != PWM_POLARITY_NORMAL)
139 * EC doesn't separate the concept of duty cycle and enabled, but
140 * kernel does. Translate.
142 duty_cycle = state->enabled ? state->duty_cycle : 0;
144 ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
151 static int cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
152 struct pwm_state *state)
154 struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
157 ret = cros_ec_pwm_get_duty(ec_pwm->ec, ec_pwm->use_pwm_type, pwm->hwpwm);
159 dev_err(pwmchip_parent(chip), "error getting initial duty: %d\n", ret);
163 state->enabled = (ret > 0);
164 state->duty_cycle = ret;
165 state->period = EC_PWM_MAX_DUTY;
166 state->polarity = PWM_POLARITY_NORMAL;
171 static const struct pwm_ops cros_ec_pwm_ops = {
172 .get_state = cros_ec_pwm_get_state,
173 .apply = cros_ec_pwm_apply,
177 * Determine the number of supported PWMs. The EC does not return the number
178 * of PWMs it supports directly, so we have to read the pwm duty cycle for
179 * subsequent channels until we get an error.
181 static int cros_ec_num_pwms(struct cros_ec_device *ec)
185 /* The index field is only 8 bits */
186 for (i = 0; i <= U8_MAX; i++) {
188 * Note that this function is only called when use_pwm_type is
189 * false. With use_pwm_type == true the number of PWMs is fixed.
191 ret = cros_ec_pwm_get_duty(ec, false, i);
193 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
194 * responses; everything else is treated as an error.
195 * The EC error codes map to -EOPNOTSUPP and -EINVAL,
196 * so check for those.
199 case -EOPNOTSUPP: /* invalid command */
201 case -EINVAL: /* invalid parameter */
213 static int cros_ec_pwm_probe(struct platform_device *pdev)
215 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
216 struct device *dev = &pdev->dev;
217 struct device_node *np = pdev->dev.of_node;
218 struct cros_ec_pwm_device *ec_pwm;
219 struct pwm_chip *chip;
220 bool use_pwm_type = false;
221 unsigned int i, npwm;
225 return dev_err_probe(dev, -EINVAL, "no parent EC device\n");
227 if (of_device_is_compatible(np, "google,cros-ec-pwm-type")) {
229 npwm = CROS_EC_PWM_DT_COUNT;
231 ret = cros_ec_num_pwms(ec);
233 return dev_err_probe(dev, ret, "Couldn't find PWMs\n");
237 chip = devm_pwmchip_alloc(dev, npwm, sizeof(*ec_pwm));
239 return PTR_ERR(chip);
241 ec_pwm = pwm_to_cros_ec_pwm(chip);
242 ec_pwm->use_pwm_type = use_pwm_type;
246 chip->ops = &cros_ec_pwm_ops;
249 * The device tree binding for this device is special as it only uses a
250 * single cell (for the hwid) and so doesn't provide a default period.
251 * This isn't a big problem though as the hardware only supports a
252 * single period length, it's just a bit ugly to make this fit into the
253 * pwm core abstractions. So initialize the period here, as
254 * of_pwm_xlate_with_flags() won't do that for us.
256 for (i = 0; i < npwm; ++i)
257 chip->pwms[i].args.period = EC_PWM_MAX_DUTY;
259 dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
261 ret = devm_pwmchip_add(dev, chip);
263 return dev_err_probe(dev, ret, "cannot register PWM\n");
269 static const struct of_device_id cros_ec_pwm_of_match[] = {
270 { .compatible = "google,cros-ec-pwm" },
271 { .compatible = "google,cros-ec-pwm-type" },
274 MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
277 static struct platform_driver cros_ec_pwm_driver = {
278 .probe = cros_ec_pwm_probe,
280 .name = "cros-ec-pwm",
281 .of_match_table = of_match_ptr(cros_ec_pwm_of_match),
284 module_platform_driver(cros_ec_pwm_driver);
286 MODULE_ALIAS("platform:cros-ec-pwm");
287 MODULE_DESCRIPTION("ChromeOS EC PWM driver");
288 MODULE_LICENSE("GPL v2");