| .. | .. |
|---|
| 6 | 6 | */ |
|---|
| 7 | 7 | |
|---|
| 8 | 8 | #include <linux/module.h> |
|---|
| 9 | | -#include <linux/mfd/cros_ec.h> |
|---|
| 10 | | -#include <linux/mfd/cros_ec_commands.h> |
|---|
| 9 | +#include <linux/platform_data/cros_ec_commands.h> |
|---|
| 10 | +#include <linux/platform_data/cros_ec_proto.h> |
|---|
| 11 | 11 | #include <linux/platform_device.h> |
|---|
| 12 | 12 | #include <linux/pwm.h> |
|---|
| 13 | 13 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 25 | 25 | struct pwm_chip chip; |
|---|
| 26 | 26 | }; |
|---|
| 27 | 27 | |
|---|
| 28 | +/** |
|---|
| 29 | + * struct cros_ec_pwm - per-PWM driver data |
|---|
| 30 | + * @duty_cycle: cached duty cycle |
|---|
| 31 | + */ |
|---|
| 32 | +struct cros_ec_pwm { |
|---|
| 33 | + u16 duty_cycle; |
|---|
| 34 | +}; |
|---|
| 35 | + |
|---|
| 28 | 36 | static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *c) |
|---|
| 29 | 37 | { |
|---|
| 30 | 38 | return container_of(c, struct cros_ec_pwm_device, chip); |
|---|
| 39 | +} |
|---|
| 40 | + |
|---|
| 41 | +static int cros_ec_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) |
|---|
| 42 | +{ |
|---|
| 43 | + struct cros_ec_pwm *channel; |
|---|
| 44 | + |
|---|
| 45 | + channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
|---|
| 46 | + if (!channel) |
|---|
| 47 | + return -ENOMEM; |
|---|
| 48 | + |
|---|
| 49 | + pwm_set_chip_data(pwm, channel); |
|---|
| 50 | + |
|---|
| 51 | + return 0; |
|---|
| 52 | +} |
|---|
| 53 | + |
|---|
| 54 | +static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
|---|
| 55 | +{ |
|---|
| 56 | + struct cros_ec_pwm *channel = pwm_get_chip_data(pwm); |
|---|
| 57 | + |
|---|
| 58 | + kfree(channel); |
|---|
| 31 | 59 | } |
|---|
| 32 | 60 | |
|---|
| 33 | 61 | static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty) |
|---|
| .. | .. |
|---|
| 53 | 81 | return cros_ec_cmd_xfer_status(ec, msg); |
|---|
| 54 | 82 | } |
|---|
| 55 | 83 | |
|---|
| 56 | | -static int __cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index, |
|---|
| 57 | | - u32 *result) |
|---|
| 84 | +static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index) |
|---|
| 58 | 85 | { |
|---|
| 59 | 86 | struct { |
|---|
| 60 | 87 | struct cros_ec_command msg; |
|---|
| .. | .. |
|---|
| 79 | 106 | params->index = index; |
|---|
| 80 | 107 | |
|---|
| 81 | 108 | ret = cros_ec_cmd_xfer_status(ec, msg); |
|---|
| 82 | | - if (result) |
|---|
| 83 | | - *result = msg->result; |
|---|
| 84 | 109 | if (ret < 0) |
|---|
| 85 | 110 | return ret; |
|---|
| 86 | 111 | |
|---|
| 87 | 112 | return resp->duty; |
|---|
| 88 | 113 | } |
|---|
| 89 | 114 | |
|---|
| 90 | | -static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index) |
|---|
| 91 | | -{ |
|---|
| 92 | | - return __cros_ec_pwm_get_duty(ec, index, NULL); |
|---|
| 93 | | -} |
|---|
| 94 | | - |
|---|
| 95 | 115 | static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
|---|
| 96 | | - struct pwm_state *state) |
|---|
| 116 | + const struct pwm_state *state) |
|---|
| 97 | 117 | { |
|---|
| 98 | 118 | struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip); |
|---|
| 99 | | - int duty_cycle; |
|---|
| 119 | + struct cros_ec_pwm *channel = pwm_get_chip_data(pwm); |
|---|
| 120 | + u16 duty_cycle; |
|---|
| 121 | + int ret; |
|---|
| 100 | 122 | |
|---|
| 101 | 123 | /* The EC won't let us change the period */ |
|---|
| 102 | 124 | if (state->period != EC_PWM_MAX_DUTY) |
|---|
| .. | .. |
|---|
| 108 | 130 | */ |
|---|
| 109 | 131 | duty_cycle = state->enabled ? state->duty_cycle : 0; |
|---|
| 110 | 132 | |
|---|
| 111 | | - return cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle); |
|---|
| 133 | + ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle); |
|---|
| 134 | + if (ret < 0) |
|---|
| 135 | + return ret; |
|---|
| 136 | + |
|---|
| 137 | + channel->duty_cycle = state->duty_cycle; |
|---|
| 138 | + |
|---|
| 139 | + return 0; |
|---|
| 112 | 140 | } |
|---|
| 113 | 141 | |
|---|
| 114 | 142 | static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, |
|---|
| 115 | 143 | struct pwm_state *state) |
|---|
| 116 | 144 | { |
|---|
| 117 | 145 | struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip); |
|---|
| 146 | + struct cros_ec_pwm *channel = pwm_get_chip_data(pwm); |
|---|
| 118 | 147 | int ret; |
|---|
| 119 | 148 | |
|---|
| 120 | 149 | ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm); |
|---|
| .. | .. |
|---|
| 125 | 154 | |
|---|
| 126 | 155 | state->enabled = (ret > 0); |
|---|
| 127 | 156 | state->period = EC_PWM_MAX_DUTY; |
|---|
| 157 | + state->polarity = PWM_POLARITY_NORMAL; |
|---|
| 128 | 158 | |
|---|
| 129 | | - /* Note that "disabled" and "duty cycle == 0" are treated the same */ |
|---|
| 130 | | - state->duty_cycle = ret; |
|---|
| 159 | + /* |
|---|
| 160 | + * Note that "disabled" and "duty cycle == 0" are treated the same. If |
|---|
| 161 | + * the cached duty cycle is not zero, used the cached duty cycle. This |
|---|
| 162 | + * ensures that the configured duty cycle is kept across a disable and |
|---|
| 163 | + * enable operation and avoids potentially confusing consumers. |
|---|
| 164 | + * |
|---|
| 165 | + * For the case of the initial hardware readout, channel->duty_cycle |
|---|
| 166 | + * will be 0 and the actual duty cycle read from the EC is used. |
|---|
| 167 | + */ |
|---|
| 168 | + if (ret == 0 && channel->duty_cycle > 0) |
|---|
| 169 | + state->duty_cycle = channel->duty_cycle; |
|---|
| 170 | + else |
|---|
| 171 | + state->duty_cycle = ret; |
|---|
| 131 | 172 | } |
|---|
| 132 | 173 | |
|---|
| 133 | 174 | static struct pwm_device * |
|---|
| .. | .. |
|---|
| 149 | 190 | } |
|---|
| 150 | 191 | |
|---|
| 151 | 192 | static const struct pwm_ops cros_ec_pwm_ops = { |
|---|
| 193 | + .request = cros_ec_pwm_request, |
|---|
| 194 | + .free = cros_ec_pwm_free, |
|---|
| 152 | 195 | .get_state = cros_ec_pwm_get_state, |
|---|
| 153 | 196 | .apply = cros_ec_pwm_apply, |
|---|
| 154 | 197 | .owner = THIS_MODULE, |
|---|
| 155 | 198 | }; |
|---|
| 156 | 199 | |
|---|
| 200 | +/* |
|---|
| 201 | + * Determine the number of supported PWMs. The EC does not return the number |
|---|
| 202 | + * of PWMs it supports directly, so we have to read the pwm duty cycle for |
|---|
| 203 | + * subsequent channels until we get an error. |
|---|
| 204 | + */ |
|---|
| 157 | 205 | static int cros_ec_num_pwms(struct cros_ec_device *ec) |
|---|
| 158 | 206 | { |
|---|
| 159 | 207 | int i, ret; |
|---|
| 160 | 208 | |
|---|
| 161 | 209 | /* The index field is only 8 bits */ |
|---|
| 162 | 210 | for (i = 0; i <= U8_MAX; i++) { |
|---|
| 163 | | - u32 result = 0; |
|---|
| 164 | | - |
|---|
| 165 | | - ret = __cros_ec_pwm_get_duty(ec, i, &result); |
|---|
| 166 | | - /* We want to parse EC protocol errors */ |
|---|
| 167 | | - if (ret < 0 && !(ret == -EPROTO && result)) |
|---|
| 168 | | - return ret; |
|---|
| 169 | | - |
|---|
| 211 | + ret = cros_ec_pwm_get_duty(ec, i); |
|---|
| 170 | 212 | /* |
|---|
| 171 | 213 | * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM |
|---|
| 172 | 214 | * responses; everything else is treated as an error. |
|---|
| 215 | + * The EC error codes map to -EOPNOTSUPP and -EINVAL, |
|---|
| 216 | + * so check for those. |
|---|
| 173 | 217 | */ |
|---|
| 174 | | - if (result == EC_RES_INVALID_COMMAND) |
|---|
| 218 | + switch (ret) { |
|---|
| 219 | + case -EOPNOTSUPP: /* invalid command */ |
|---|
| 175 | 220 | return -ENODEV; |
|---|
| 176 | | - else if (result == EC_RES_INVALID_PARAM) |
|---|
| 221 | + case -EINVAL: /* invalid parameter */ |
|---|
| 177 | 222 | return i; |
|---|
| 178 | | - else if (result) |
|---|
| 179 | | - return -EPROTO; |
|---|
| 223 | + default: |
|---|
| 224 | + if (ret < 0) |
|---|
| 225 | + return ret; |
|---|
| 226 | + break; |
|---|
| 227 | + } |
|---|
| 180 | 228 | } |
|---|
| 181 | 229 | |
|---|
| 182 | 230 | return U8_MAX; |
|---|