blob: 7b725235857531d539c2022a628d3a40f8ce474a [file] [log] [blame]
Matt Porter34c65072014-11-13 09:14:13 -05001/*
2 * PWM Greybus driver.
3 *
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/pwm.h>
14#include "greybus.h"
15
16struct gb_pwm_chip {
17 struct gb_connection *connection;
18 u8 version_major;
19 u8 version_minor;
20 u8 pwm_max; /* max pwm number */
21
22 struct pwm_chip chip;
23 struct pwm_chip *pwm;
24};
25#define pwm_chip_to_gb_pwm_chip(chip) \
26 container_of(chip, struct gb_pwm_chip, chip)
27
28/* Version of the Greybus PWM protocol we support */
29#define GB_PWM_VERSION_MAJOR 0x00
30#define GB_PWM_VERSION_MINOR 0x01
31
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 */
43
Matt Porter34c65072014-11-13 09:14:13 -050044/* version request has no payload */
45struct gb_pwm_proto_version_response {
Matt Porter34c65072014-11-13 09:14:13 -050046 __u8 major;
47 __u8 minor;
48};
49
50/* pwm count request has no payload */
51struct gb_pwm_count_response {
Matt Porter34c65072014-11-13 09:14:13 -050052 __u8 count;
53};
54
55struct gb_pwm_activate_request {
56 __u8 which;
57};
58
59struct gb_pwm_deactivate_request {
60 __u8 which;
61};
62
63struct gb_pwm_config_request {
64 __u8 which;
Alex Elder583d2332014-12-02 15:48:09 -060065 __le32 duty;
66 __le32 period;
Matt Porter34c65072014-11-13 09:14:13 -050067};
68
69struct gb_pwm_polarity_request {
70 __u8 which;
71 __u8 polarity;
72};
73
74struct gb_pwm_enable_request {
75 __u8 which;
76};
77
78struct gb_pwm_disable_request {
79 __u8 which;
80};
81
Viresh Kumar36e79de2015-01-21 18:12:36 +053082/* Define get_version() routine */
83define_get_version(gb_pwm_chip, PWM);
Matt Porter34c65072014-11-13 09:14:13 -050084
85static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
86{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080087 struct gb_pwm_count_response response;
Matt Porter34c65072014-11-13 09:14:13 -050088 int ret;
89
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080090 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
91 NULL, 0, &response, sizeof(response));
92 if (ret)
93 return ret;
94 pwmc->pwm_max = response.count;
95 return 0;
Matt Porter34c65072014-11-13 09:14:13 -050096}
97
98static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
99 u8 which)
100{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800101 struct gb_pwm_activate_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500102
103 if (which > pwmc->pwm_max)
104 return -EINVAL;
105
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800106 request.which = which;
107 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
108 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500109}
110
111static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
112 u8 which)
113{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800114 struct gb_pwm_deactivate_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500115
116 if (which > pwmc->pwm_max)
117 return -EINVAL;
118
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800119 request.which = which;
120 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
121 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500122}
123
124static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
125 u8 which, u32 duty, u32 period)
126{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800127 struct gb_pwm_config_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500128
129 if (which > pwmc->pwm_max)
130 return -EINVAL;
131
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800132 request.which = which;
Alex Elder583d2332014-12-02 15:48:09 -0600133 request.duty = cpu_to_le32(duty);
134 request.period = cpu_to_le32(period);
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800135 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
136 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500137}
138
139
140static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
141 u8 which, u8 polarity)
142{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800143 struct gb_pwm_polarity_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500144
145 if (which > pwmc->pwm_max)
146 return -EINVAL;
147
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800148 request.which = which;
149 request.polarity = polarity;
150 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
151 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500152}
153
154static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
155 u8 which)
156{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800157 struct gb_pwm_enable_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500158
159 if (which > pwmc->pwm_max)
160 return -EINVAL;
161
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800162 request.which = which;
163 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
164 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500165}
166
167static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
168 u8 which)
169{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800170 struct gb_pwm_disable_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500171
172 if (which > pwmc->pwm_max)
173 return -EINVAL;
174
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800175 request.which = which;
176 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
177 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500178}
179
180static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
181{
182 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
183
184 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
185};
186
187static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
188{
189 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
190
191 if (test_bit(PWMF_ENABLED, &pwm->flags))
192 dev_warn(chip->dev, "freeing PWM device without disabling\n");
193
194 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
195}
196
197static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
198 int duty_ns, int period_ns)
199{
200 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
201
202 return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
203};
204
205static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
206 enum pwm_polarity polarity)
207{
208 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
209
210 return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
211};
212
213static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
214{
215 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
216
217 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
218};
219
220static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
221{
222 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
223
224 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
225};
226
227static const struct pwm_ops gb_pwm_ops = {
228 .request = gb_pwm_request,
229 .free = gb_pwm_free,
230 .config = gb_pwm_config,
231 .set_polarity = gb_pwm_set_polarity,
232 .enable = gb_pwm_enable,
233 .disable = gb_pwm_disable,
234 .owner = THIS_MODULE,
235};
236
237static int gb_pwm_connection_init(struct gb_connection *connection)
238{
239 struct gb_pwm_chip *pwmc;
240 struct pwm_chip *pwm;
241 int ret;
242
243 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
244 if (!pwmc)
245 return -ENOMEM;
246 pwmc->connection = connection;
Alex Elder93bbe852014-12-03 12:27:42 -0600247 connection->private = pwmc;
Matt Porter34c65072014-11-13 09:14:13 -0500248
249 /* Check for compatible protocol version */
Viresh Kumar36e79de2015-01-21 18:12:36 +0530250 ret = get_version(pwmc);
Matt Porter34c65072014-11-13 09:14:13 -0500251 if (ret)
252 goto out_err;
253
254 /* Query number of pwms present */
255 ret = gb_pwm_count_operation(pwmc);
256 if (ret)
257 goto out_err;
258
259 pwm = &pwmc->chip;
260
261 pwm->dev = &connection->dev;
262 pwm->ops = &gb_pwm_ops;
263 pwm->base = -1; /* Allocate base dynamically */
264 pwm->npwm = pwmc->pwm_max + 1;
265 pwm->can_sleep = true; /* FIXME */
266
267 ret = pwmchip_add(pwm);
268 if (ret) {
269 pr_err("Failed to register PWM\n");
270 return ret;
271 }
Matt Porter34c65072014-11-13 09:14:13 -0500272
273 return 0;
274out_err:
275 kfree(pwmc);
276 return ret;
277}
278
279static void gb_pwm_connection_exit(struct gb_connection *connection)
280{
281 struct gb_pwm_chip *pwmc = connection->private;
282
283 if (!pwmc)
284 return;
285
286 pwmchip_remove(&pwmc->chip);
287 /* kref_put(pwmc->connection) */
288 kfree(pwmc);
289}
290
291static struct gb_protocol pwm_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800292 .name = "pwm",
Matt Porter34c65072014-11-13 09:14:13 -0500293 .id = GREYBUS_PROTOCOL_PWM,
294 .major = 0,
295 .minor = 1,
296 .connection_init = gb_pwm_connection_init,
297 .connection_exit = gb_pwm_connection_exit,
298 .request_recv = NULL, /* no incoming requests */
299};
300
Greg Kroah-Hartman7c7d5b92014-12-23 15:16:51 -0800301int gb_pwm_protocol_init(void)
Matt Porter34c65072014-11-13 09:14:13 -0500302{
303 return gb_protocol_register(&pwm_protocol);
304}
305
306void gb_pwm_protocol_exit(void)
307{
308 gb_protocol_deregister(&pwm_protocol);
309}