4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/pwm.h>
17 struct gb_connection *connection;
20 u8 pwm_max; /* max pwm number */
25 #define pwm_chip_to_gb_pwm_chip(chip) \
26 container_of(chip, struct gb_pwm_chip, chip)
28 /* Version of the Greybus PWM protocol we support */
29 #define GB_PWM_VERSION_MAJOR 0x00
30 #define GB_PWM_VERSION_MINOR 0x01
32 /* Greybus PWM request types */
33 #define GB_PWM_TYPE_INVALID 0x00
34 #define GB_PWM_TYPE_PROTOCOL_VERSION 0x01
35 #define GB_PWM_TYPE_PWM_COUNT 0x02
36 #define GB_PWM_TYPE_ACTIVATE 0x03
37 #define GB_PWM_TYPE_DEACTIVATE 0x04
38 #define GB_PWM_TYPE_CONFIG 0x05
39 #define GB_PWM_TYPE_POLARITY 0x06
40 #define GB_PWM_TYPE_ENABLE 0x07
41 #define GB_PWM_TYPE_DISABLE 0x08
42 #define GB_PWM_TYPE_RESPONSE 0x80 /* OR'd with rest */
44 /* version request has no payload */
45 struct gb_pwm_proto_version_response {
50 /* pwm count request has no payload */
51 struct gb_pwm_count_response {
55 struct gb_pwm_activate_request {
59 struct gb_pwm_deactivate_request {
63 struct gb_pwm_config_request {
69 struct gb_pwm_polarity_request {
74 struct gb_pwm_enable_request {
78 struct gb_pwm_disable_request {
83 * This request only uses the connection field, and if successful,
84 * fills in the major and minor protocol version of the target.
86 static int gb_pwm_proto_version_operation(struct gb_pwm_chip *pwmc)
88 struct gb_pwm_proto_version_response response;
91 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PROTOCOL_VERSION,
92 NULL, 0, &response, sizeof(response));
97 if (response.major > GB_PWM_VERSION_MAJOR) {
98 pr_err("unsupported major version (%hhu > %hhu)\n",
99 response.major, GB_PWM_VERSION_MAJOR);
102 pwmc->version_major = response.major;
103 pwmc->version_minor = response.minor;
107 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
109 struct gb_pwm_count_response response;
112 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
113 NULL, 0, &response, sizeof(response));
116 pwmc->pwm_max = response.count;
120 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
123 struct gb_pwm_activate_request request;
125 if (which > pwmc->pwm_max)
128 request.which = which;
129 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
130 &request, sizeof(request), NULL, 0);
133 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
136 struct gb_pwm_deactivate_request request;
138 if (which > pwmc->pwm_max)
141 request.which = which;
142 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
143 &request, sizeof(request), NULL, 0);
146 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
147 u8 which, u32 duty, u32 period)
149 struct gb_pwm_config_request request;
151 if (which > pwmc->pwm_max)
154 request.which = which;
155 request.duty = cpu_to_le32(duty);
156 request.period = cpu_to_le32(period);
157 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
158 &request, sizeof(request), NULL, 0);
162 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
163 u8 which, u8 polarity)
165 struct gb_pwm_polarity_request request;
167 if (which > pwmc->pwm_max)
170 request.which = which;
171 request.polarity = polarity;
172 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
173 &request, sizeof(request), NULL, 0);
176 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
179 struct gb_pwm_enable_request request;
181 if (which > pwmc->pwm_max)
184 request.which = which;
185 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
186 &request, sizeof(request), NULL, 0);
189 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
192 struct gb_pwm_disable_request request;
194 if (which > pwmc->pwm_max)
197 request.which = which;
198 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
199 &request, sizeof(request), NULL, 0);
202 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
204 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
206 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
209 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
211 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
213 if (test_bit(PWMF_ENABLED, &pwm->flags))
214 dev_warn(chip->dev, "freeing PWM device without disabling\n");
216 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
219 static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
220 int duty_ns, int period_ns)
222 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
224 return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
227 static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
228 enum pwm_polarity polarity)
230 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
232 return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
235 static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
237 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
239 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
242 static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
244 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
246 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
249 static const struct pwm_ops gb_pwm_ops = {
250 .request = gb_pwm_request,
252 .config = gb_pwm_config,
253 .set_polarity = gb_pwm_set_polarity,
254 .enable = gb_pwm_enable,
255 .disable = gb_pwm_disable,
256 .owner = THIS_MODULE,
259 static int gb_pwm_connection_init(struct gb_connection *connection)
261 struct gb_pwm_chip *pwmc;
262 struct pwm_chip *pwm;
265 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
268 pwmc->connection = connection;
269 connection->private = pwmc;
271 /* Check for compatible protocol version */
272 ret = gb_pwm_proto_version_operation(pwmc);
276 /* Query number of pwms present */
277 ret = gb_pwm_count_operation(pwmc);
283 pwm->dev = &connection->dev;
284 pwm->ops = &gb_pwm_ops;
285 pwm->base = -1; /* Allocate base dynamically */
286 pwm->npwm = pwmc->pwm_max + 1;
287 pwm->can_sleep = true; /* FIXME */
289 ret = pwmchip_add(pwm);
291 pr_err("Failed to register PWM\n");
301 static void gb_pwm_connection_exit(struct gb_connection *connection)
303 struct gb_pwm_chip *pwmc = connection->private;
308 pwmchip_remove(&pwmc->chip);
309 /* kref_put(pwmc->connection) */
313 static struct gb_protocol pwm_protocol = {
315 .id = GREYBUS_PROTOCOL_PWM,
318 .connection_init = gb_pwm_connection_init,
319 .connection_exit = gb_pwm_connection_exit,
320 .request_recv = NULL, /* no incoming requests */
323 int gb_pwm_protocol_init(void)
325 return gb_protocol_register(&pwm_protocol);
328 void gb_pwm_protocol_exit(void)
330 gb_protocol_deregister(&pwm_protocol);