hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/pwm/pwm-cros-ec.c
....@@ -6,8 +6,8 @@
66 */
77
88 #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>
1111 #include <linux/platform_device.h>
1212 #include <linux/pwm.h>
1313 #include <linux/slab.h>
....@@ -25,9 +25,37 @@
2525 struct pwm_chip chip;
2626 };
2727
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
+
2836 static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *c)
2937 {
3038 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);
3159 }
3260
3361 static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
....@@ -53,8 +81,7 @@
5381 return cros_ec_cmd_xfer_status(ec, msg);
5482 }
5583
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)
5885 {
5986 struct {
6087 struct cros_ec_command msg;
....@@ -79,24 +106,19 @@
79106 params->index = index;
80107
81108 ret = cros_ec_cmd_xfer_status(ec, msg);
82
- if (result)
83
- *result = msg->result;
84109 if (ret < 0)
85110 return ret;
86111
87112 return resp->duty;
88113 }
89114
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
-
95115 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)
97117 {
98118 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;
100122
101123 /* The EC won't let us change the period */
102124 if (state->period != EC_PWM_MAX_DUTY)
....@@ -108,13 +130,20 @@
108130 */
109131 duty_cycle = state->enabled ? state->duty_cycle : 0;
110132
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;
112140 }
113141
114142 static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
115143 struct pwm_state *state)
116144 {
117145 struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
146
+ struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
118147 int ret;
119148
120149 ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
....@@ -125,9 +154,21 @@
125154
126155 state->enabled = (ret > 0);
127156 state->period = EC_PWM_MAX_DUTY;
157
+ state->polarity = PWM_POLARITY_NORMAL;
128158
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;
131172 }
132173
133174 static struct pwm_device *
....@@ -149,34 +190,41 @@
149190 }
150191
151192 static const struct pwm_ops cros_ec_pwm_ops = {
193
+ .request = cros_ec_pwm_request,
194
+ .free = cros_ec_pwm_free,
152195 .get_state = cros_ec_pwm_get_state,
153196 .apply = cros_ec_pwm_apply,
154197 .owner = THIS_MODULE,
155198 };
156199
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
+ */
157205 static int cros_ec_num_pwms(struct cros_ec_device *ec)
158206 {
159207 int i, ret;
160208
161209 /* The index field is only 8 bits */
162210 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);
170212 /*
171213 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
172214 * responses; everything else is treated as an error.
215
+ * The EC error codes map to -EOPNOTSUPP and -EINVAL,
216
+ * so check for those.
173217 */
174
- if (result == EC_RES_INVALID_COMMAND)
218
+ switch (ret) {
219
+ case -EOPNOTSUPP: /* invalid command */
175220 return -ENODEV;
176
- else if (result == EC_RES_INVALID_PARAM)
221
+ case -EINVAL: /* invalid parameter */
177222 return i;
178
- else if (result)
179
- return -EPROTO;
223
+ default:
224
+ if (ret < 0)
225
+ return ret;
226
+ break;
227
+ }
180228 }
181229
182230 return U8_MAX;