Lines Matching +full:cros +full:- +full:ec +full:- +full:pwm
1 // SPDX-License-Identifier: GPL-2.0
3 * Expose a PWM controlled by the ChromeOS EC to the host processor.
13 #include <linux/pwm.h>
16 #include <dt-bindings/mfd/cros_ec.h>
19 * struct cros_ec_pwm_device - Driver data for EC PWM
22 * @ec: Pointer to EC device
23 * @chip: PWM controller chip
24 * @use_pwm_type: Use PWM types instead of generic channels
28 struct cros_ec_device *ec; member
34 * struct cros_ec_pwm - per-PWM driver data
46 static int cros_ec_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) in cros_ec_pwm_request() argument
52 return -ENOMEM; in cros_ec_pwm_request()
54 pwm_set_chip_data(pwm, channel); in cros_ec_pwm_request()
59 static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) in cros_ec_pwm_free() argument
61 struct cros_ec_pwm *channel = pwm_get_chip_data(pwm); in cros_ec_pwm_free()
76 return -EINVAL; in cros_ec_dt_type_to_pwm_type()
83 struct cros_ec_device *ec = ec_pwm->ec; in cros_ec_pwm_set_duty() local
94 msg->version = 0; in cros_ec_pwm_set_duty()
95 msg->command = EC_CMD_PWM_SET_DUTY; in cros_ec_pwm_set_duty()
96 msg->insize = 0; in cros_ec_pwm_set_duty()
97 msg->outsize = sizeof(*params); in cros_ec_pwm_set_duty()
99 params->duty = duty; in cros_ec_pwm_set_duty()
101 if (ec_pwm->use_pwm_type) { in cros_ec_pwm_set_duty()
102 ret = cros_ec_dt_type_to_pwm_type(index, ¶ms->pwm_type); in cros_ec_pwm_set_duty()
104 dev_err(ec->dev, "Invalid PWM type index: %d\n", index); in cros_ec_pwm_set_duty()
107 params->index = 0; in cros_ec_pwm_set_duty()
109 params->pwm_type = EC_PWM_TYPE_GENERIC; in cros_ec_pwm_set_duty()
110 params->index = index; in cros_ec_pwm_set_duty()
113 return cros_ec_cmd_xfer_status(ec, msg); in cros_ec_pwm_set_duty()
118 struct cros_ec_device *ec = ec_pwm->ec; in cros_ec_pwm_get_duty() local
133 msg->version = 0; in cros_ec_pwm_get_duty()
134 msg->command = EC_CMD_PWM_GET_DUTY; in cros_ec_pwm_get_duty()
135 msg->insize = sizeof(*resp); in cros_ec_pwm_get_duty()
136 msg->outsize = sizeof(*params); in cros_ec_pwm_get_duty()
138 if (ec_pwm->use_pwm_type) { in cros_ec_pwm_get_duty()
139 ret = cros_ec_dt_type_to_pwm_type(index, ¶ms->pwm_type); in cros_ec_pwm_get_duty()
141 dev_err(ec->dev, "Invalid PWM type index: %d\n", index); in cros_ec_pwm_get_duty()
144 params->index = 0; in cros_ec_pwm_get_duty()
146 params->pwm_type = EC_PWM_TYPE_GENERIC; in cros_ec_pwm_get_duty()
147 params->index = index; in cros_ec_pwm_get_duty()
150 ret = cros_ec_cmd_xfer_status(ec, msg); in cros_ec_pwm_get_duty()
154 return resp->duty; in cros_ec_pwm_get_duty()
157 static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, in cros_ec_pwm_apply() argument
161 struct cros_ec_pwm *channel = pwm_get_chip_data(pwm); in cros_ec_pwm_apply()
165 /* The EC won't let us change the period */ in cros_ec_pwm_apply()
166 if (state->period != EC_PWM_MAX_DUTY) in cros_ec_pwm_apply()
167 return -EINVAL; in cros_ec_pwm_apply()
169 if (state->polarity != PWM_POLARITY_NORMAL) in cros_ec_pwm_apply()
170 return -EINVAL; in cros_ec_pwm_apply()
173 * EC doesn't separate the concept of duty cycle and enabled, but in cros_ec_pwm_apply()
176 duty_cycle = state->enabled ? state->duty_cycle : 0; in cros_ec_pwm_apply()
178 ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle); in cros_ec_pwm_apply()
182 channel->duty_cycle = state->duty_cycle; in cros_ec_pwm_apply()
187 static int cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, in cros_ec_pwm_get_state() argument
191 struct cros_ec_pwm *channel = pwm_get_chip_data(pwm); in cros_ec_pwm_get_state()
194 ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm); in cros_ec_pwm_get_state()
196 dev_err(chip->dev, "error getting initial duty: %d\n", ret); in cros_ec_pwm_get_state()
200 state->enabled = (ret > 0); in cros_ec_pwm_get_state()
201 state->period = EC_PWM_MAX_DUTY; in cros_ec_pwm_get_state()
202 state->polarity = PWM_POLARITY_NORMAL; in cros_ec_pwm_get_state()
210 * For the case of the initial hardware readout, channel->duty_cycle in cros_ec_pwm_get_state()
211 * will be 0 and the actual duty cycle read from the EC is used. in cros_ec_pwm_get_state()
213 if (ret == 0 && channel->duty_cycle > 0) in cros_ec_pwm_get_state()
214 state->duty_cycle = channel->duty_cycle; in cros_ec_pwm_get_state()
216 state->duty_cycle = ret; in cros_ec_pwm_get_state()
224 struct pwm_device *pwm; in cros_ec_pwm_xlate() local
226 if (args->args[0] >= chip->npwm) in cros_ec_pwm_xlate()
227 return ERR_PTR(-EINVAL); in cros_ec_pwm_xlate()
229 pwm = pwm_request_from_chip(chip, args->args[0], NULL); in cros_ec_pwm_xlate()
230 if (IS_ERR(pwm)) in cros_ec_pwm_xlate()
231 return pwm; in cros_ec_pwm_xlate()
233 /* The EC won't let us change the period */ in cros_ec_pwm_xlate()
234 pwm->args.period = EC_PWM_MAX_DUTY; in cros_ec_pwm_xlate()
236 return pwm; in cros_ec_pwm_xlate()
248 * Determine the number of supported PWMs. The EC does not return the number
249 * of PWMs it supports directly, so we have to read the pwm duty cycle for
262 * The EC error codes map to -EOPNOTSUPP and -EINVAL, in cros_ec_num_pwms()
266 case -EOPNOTSUPP: /* invalid command */ in cros_ec_num_pwms()
267 return -ENODEV; in cros_ec_num_pwms()
268 case -EINVAL: /* invalid parameter */ in cros_ec_num_pwms()
282 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); in cros_ec_pwm_probe() local
283 struct device *dev = &pdev->dev; in cros_ec_pwm_probe()
284 struct device_node *np = pdev->dev.of_node; in cros_ec_pwm_probe()
289 if (!ec) { in cros_ec_pwm_probe()
290 dev_err(dev, "no parent EC device\n"); in cros_ec_pwm_probe()
291 return -EINVAL; in cros_ec_pwm_probe()
296 return -ENOMEM; in cros_ec_pwm_probe()
297 chip = &ec_pwm->chip; in cros_ec_pwm_probe()
298 ec_pwm->ec = ec; in cros_ec_pwm_probe()
300 if (of_device_is_compatible(np, "google,cros-ec-pwm-type")) in cros_ec_pwm_probe()
301 ec_pwm->use_pwm_type = true; in cros_ec_pwm_probe()
303 /* PWM chip */ in cros_ec_pwm_probe()
304 chip->dev = dev; in cros_ec_pwm_probe()
305 chip->ops = &cros_ec_pwm_ops; in cros_ec_pwm_probe()
306 chip->of_xlate = cros_ec_pwm_xlate; in cros_ec_pwm_probe()
307 chip->of_pwm_n_cells = 1; in cros_ec_pwm_probe()
309 if (ec_pwm->use_pwm_type) { in cros_ec_pwm_probe()
310 chip->npwm = CROS_EC_PWM_DT_COUNT; in cros_ec_pwm_probe()
317 chip->npwm = ret; in cros_ec_pwm_probe()
320 dev_dbg(dev, "Probed %u PWMs\n", chip->npwm); in cros_ec_pwm_probe()
324 dev_err(dev, "cannot register PWM: %d\n", ret); in cros_ec_pwm_probe()
336 struct pwm_chip *chip = &ec_pwm->chip; in cros_ec_pwm_remove()
343 { .compatible = "google,cros-ec-pwm" },
344 { .compatible = "google,cros-ec-pwm-type" },
354 .name = "cros-ec-pwm",
360 MODULE_ALIAS("platform:cros-ec-pwm");
361 MODULE_DESCRIPTION("ChromeOS EC PWM driver");