blob: c3a3a9dfc6b4bba905538ad355bef4fb8709f9c5 [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>
Alex Eldere9385e52015-05-22 12:35:31 -050014
Matt Porter34c65072014-11-13 09:14:13 -050015#include "greybus.h"
16
17struct gb_pwm_chip {
18 struct gb_connection *connection;
Matt Porter34c65072014-11-13 09:14:13 -050019 u8 pwm_max; /* max pwm number */
20
21 struct pwm_chip chip;
22 struct pwm_chip *pwm;
23};
24#define pwm_chip_to_gb_pwm_chip(chip) \
25 container_of(chip, struct gb_pwm_chip, chip)
26
Matt Porter34c65072014-11-13 09:14:13 -050027
Matt Porter34c65072014-11-13 09:14:13 -050028static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
29{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080030 struct gb_pwm_count_response response;
Matt Porter34c65072014-11-13 09:14:13 -050031 int ret;
32
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080033 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
34 NULL, 0, &response, sizeof(response));
35 if (ret)
36 return ret;
37 pwmc->pwm_max = response.count;
38 return 0;
Matt Porter34c65072014-11-13 09:14:13 -050039}
40
41static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
42 u8 which)
43{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080044 struct gb_pwm_activate_request request;
Matt Porter34c65072014-11-13 09:14:13 -050045
46 if (which > pwmc->pwm_max)
47 return -EINVAL;
48
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080049 request.which = which;
50 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
51 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -050052}
53
54static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
55 u8 which)
56{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080057 struct gb_pwm_deactivate_request request;
Matt Porter34c65072014-11-13 09:14:13 -050058
59 if (which > pwmc->pwm_max)
60 return -EINVAL;
61
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080062 request.which = which;
63 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
64 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -050065}
66
67static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
68 u8 which, u32 duty, u32 period)
69{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080070 struct gb_pwm_config_request request;
Matt Porter34c65072014-11-13 09:14:13 -050071
72 if (which > pwmc->pwm_max)
73 return -EINVAL;
74
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080075 request.which = which;
Alex Elder583d2332014-12-02 15:48:09 -060076 request.duty = cpu_to_le32(duty);
77 request.period = cpu_to_le32(period);
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080078 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
79 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -050080}
81
82
83static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
84 u8 which, u8 polarity)
85{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080086 struct gb_pwm_polarity_request request;
Matt Porter34c65072014-11-13 09:14:13 -050087
88 if (which > pwmc->pwm_max)
89 return -EINVAL;
90
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -080091 request.which = which;
92 request.polarity = polarity;
93 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
94 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -050095}
96
97static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
98 u8 which)
99{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800100 struct gb_pwm_enable_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500101
102 if (which > pwmc->pwm_max)
103 return -EINVAL;
104
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800105 request.which = which;
106 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
107 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500108}
109
110static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
111 u8 which)
112{
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800113 struct gb_pwm_disable_request request;
Matt Porter34c65072014-11-13 09:14:13 -0500114
115 if (which > pwmc->pwm_max)
116 return -EINVAL;
117
Greg Kroah-Hartmanbf2329f2014-11-23 17:45:24 -0800118 request.which = which;
119 return gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
120 &request, sizeof(request), NULL, 0);
Matt Porter34c65072014-11-13 09:14:13 -0500121}
122
123static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
124{
125 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
126
127 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
128};
129
130static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
131{
132 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
133
134 if (test_bit(PWMF_ENABLED, &pwm->flags))
135 dev_warn(chip->dev, "freeing PWM device without disabling\n");
136
137 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
138}
139
140static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
141 int duty_ns, int period_ns)
142{
143 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
144
145 return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
146};
147
148static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
149 enum pwm_polarity polarity)
150{
151 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
152
153 return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
154};
155
156static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
157{
158 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
159
160 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
161};
162
163static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
164{
165 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
166
167 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
168};
169
170static const struct pwm_ops gb_pwm_ops = {
171 .request = gb_pwm_request,
172 .free = gb_pwm_free,
173 .config = gb_pwm_config,
174 .set_polarity = gb_pwm_set_polarity,
175 .enable = gb_pwm_enable,
176 .disable = gb_pwm_disable,
177 .owner = THIS_MODULE,
178};
179
180static int gb_pwm_connection_init(struct gb_connection *connection)
181{
182 struct gb_pwm_chip *pwmc;
183 struct pwm_chip *pwm;
184 int ret;
185
186 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
187 if (!pwmc)
188 return -ENOMEM;
189 pwmc->connection = connection;
Alex Elder93bbe852014-12-03 12:27:42 -0600190 connection->private = pwmc;
Matt Porter34c65072014-11-13 09:14:13 -0500191
Matt Porter34c65072014-11-13 09:14:13 -0500192 /* Query number of pwms present */
193 ret = gb_pwm_count_operation(pwmc);
194 if (ret)
195 goto out_err;
196
197 pwm = &pwmc->chip;
198
Greg Kroah-Hartman8d5732f2015-10-14 11:18:50 -0700199 pwm->dev = &connection->bundle->dev;
Matt Porter34c65072014-11-13 09:14:13 -0500200 pwm->ops = &gb_pwm_ops;
201 pwm->base = -1; /* Allocate base dynamically */
202 pwm->npwm = pwmc->pwm_max + 1;
203 pwm->can_sleep = true; /* FIXME */
204
205 ret = pwmchip_add(pwm);
206 if (ret) {
Greg Kroah-Hartman8d5732f2015-10-14 11:18:50 -0700207 dev_err(&connection->bundle->dev,
208 "failed to register PWM: %d\n", ret);
Johan Hovold6a80ed42015-03-17 10:55:50 +0100209 goto out_err;
Matt Porter34c65072014-11-13 09:14:13 -0500210 }
Matt Porter34c65072014-11-13 09:14:13 -0500211
212 return 0;
213out_err:
214 kfree(pwmc);
215 return ret;
216}
217
218static void gb_pwm_connection_exit(struct gb_connection *connection)
219{
220 struct gb_pwm_chip *pwmc = connection->private;
221
222 if (!pwmc)
223 return;
224
225 pwmchip_remove(&pwmc->chip);
226 /* kref_put(pwmc->connection) */
227 kfree(pwmc);
228}
229
230static struct gb_protocol pwm_protocol = {
Greg Kroah-Hartman7422a1e2014-12-24 13:01:45 -0800231 .name = "pwm",
Matt Porter34c65072014-11-13 09:14:13 -0500232 .id = GREYBUS_PROTOCOL_PWM,
Viresh Kumar3fb97e42015-08-08 10:25:37 +0530233 .major = GB_PWM_VERSION_MAJOR,
234 .minor = GB_PWM_VERSION_MINOR,
Matt Porter34c65072014-11-13 09:14:13 -0500235 .connection_init = gb_pwm_connection_init,
236 .connection_exit = gb_pwm_connection_exit,
237 .request_recv = NULL, /* no incoming requests */
238};
239
Viresh Kumare18822e2015-07-01 12:13:52 +0530240gb_builtin_protocol_driver(pwm_protocol);