Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / staging / greybus / pwm.c
blob1cb7b9089ead188700c144d966ea8e40da3f1493
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PWM Greybus driver.
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/pwm.h>
13 #include <linux/greybus.h>
15 #include "gbphy.h"
17 struct gb_pwm_chip {
18 struct gb_connection *connection;
19 struct pwm_chip chip;
22 static inline struct gb_pwm_chip *pwm_chip_to_gb_pwm_chip(struct pwm_chip *chip)
24 return container_of(chip, struct gb_pwm_chip, chip);
27 static int gb_pwm_get_npwm(struct gb_connection *connection)
29 struct gb_pwm_count_response response;
30 int ret;
32 ret = gb_operation_sync(connection, GB_PWM_TYPE_PWM_COUNT,
33 NULL, 0, &response, sizeof(response));
34 if (ret)
35 return ret;
38 * The request returns the highest allowed PWM id parameter. So add one
39 * to get the number of PWMs.
41 return response.count + 1;
44 static int gb_pwm_activate_operation(struct pwm_chip *chip, u8 which)
46 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
47 struct gb_pwm_activate_request request;
48 struct gbphy_device *gbphy_dev;
49 int ret;
51 request.which = which;
53 gbphy_dev = to_gbphy_dev(pwmchip_parent(chip));
54 ret = gbphy_runtime_get_sync(gbphy_dev);
55 if (ret)
56 return ret;
58 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
59 &request, sizeof(request), NULL, 0);
61 gbphy_runtime_put_autosuspend(gbphy_dev);
63 return ret;
66 static int gb_pwm_deactivate_operation(struct pwm_chip *chip, u8 which)
68 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
69 struct gb_pwm_deactivate_request request;
70 struct gbphy_device *gbphy_dev;
71 int ret;
73 request.which = which;
75 gbphy_dev = to_gbphy_dev(pwmchip_parent(chip));
76 ret = gbphy_runtime_get_sync(gbphy_dev);
77 if (ret)
78 return ret;
80 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
81 &request, sizeof(request), NULL, 0);
83 gbphy_runtime_put_autosuspend(gbphy_dev);
85 return ret;
88 static int gb_pwm_config_operation(struct pwm_chip *chip,
89 u8 which, u32 duty, u32 period)
91 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
92 struct gb_pwm_config_request request;
93 struct gbphy_device *gbphy_dev;
94 int ret;
96 request.which = which;
97 request.duty = cpu_to_le32(duty);
98 request.period = cpu_to_le32(period);
100 gbphy_dev = to_gbphy_dev(pwmchip_parent(chip));
101 ret = gbphy_runtime_get_sync(gbphy_dev);
102 if (ret)
103 return ret;
105 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
106 &request, sizeof(request), NULL, 0);
108 gbphy_runtime_put_autosuspend(gbphy_dev);
110 return ret;
113 static int gb_pwm_set_polarity_operation(struct pwm_chip *chip,
114 u8 which, u8 polarity)
116 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
117 struct gb_pwm_polarity_request request;
118 struct gbphy_device *gbphy_dev;
119 int ret;
121 request.which = which;
122 request.polarity = polarity;
124 gbphy_dev = to_gbphy_dev(pwmchip_parent(chip));
125 ret = gbphy_runtime_get_sync(gbphy_dev);
126 if (ret)
127 return ret;
129 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
130 &request, sizeof(request), NULL, 0);
132 gbphy_runtime_put_autosuspend(gbphy_dev);
134 return ret;
137 static int gb_pwm_enable_operation(struct pwm_chip *chip, u8 which)
139 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
140 struct gb_pwm_enable_request request;
141 struct gbphy_device *gbphy_dev;
142 int ret;
144 request.which = which;
146 gbphy_dev = to_gbphy_dev(pwmchip_parent(chip));
147 ret = gbphy_runtime_get_sync(gbphy_dev);
148 if (ret)
149 return ret;
151 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
152 &request, sizeof(request), NULL, 0);
153 if (ret)
154 gbphy_runtime_put_autosuspend(gbphy_dev);
156 return ret;
159 static int gb_pwm_disable_operation(struct pwm_chip *chip, u8 which)
161 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
162 struct gb_pwm_disable_request request;
163 struct gbphy_device *gbphy_dev;
164 int ret;
166 request.which = which;
168 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
169 &request, sizeof(request), NULL, 0);
171 gbphy_dev = to_gbphy_dev(pwmchip_parent(chip));
172 gbphy_runtime_put_autosuspend(gbphy_dev);
174 return ret;
177 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
179 return gb_pwm_activate_operation(chip, pwm->hwpwm);
182 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
184 if (pwm_is_enabled(pwm))
185 dev_warn(pwmchip_parent(chip), "freeing PWM device without disabling\n");
187 gb_pwm_deactivate_operation(chip, pwm->hwpwm);
190 static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
191 const struct pwm_state *state)
193 int err;
194 bool enabled = pwm->state.enabled;
195 u64 period = state->period;
196 u64 duty_cycle = state->duty_cycle;
198 /* Set polarity */
199 if (state->polarity != pwm->state.polarity) {
200 if (enabled) {
201 gb_pwm_disable_operation(chip, pwm->hwpwm);
202 enabled = false;
204 err = gb_pwm_set_polarity_operation(chip, pwm->hwpwm, state->polarity);
205 if (err)
206 return err;
209 if (!state->enabled) {
210 if (enabled)
211 gb_pwm_disable_operation(chip, pwm->hwpwm);
212 return 0;
216 * Set period and duty cycle
218 * PWM privodes 64-bit period and duty_cycle, but greybus only accepts
219 * 32-bit, so their values have to be limited to U32_MAX.
221 if (period > U32_MAX)
222 period = U32_MAX;
224 if (duty_cycle > period)
225 duty_cycle = period;
227 err = gb_pwm_config_operation(chip, pwm->hwpwm, duty_cycle, period);
228 if (err)
229 return err;
231 /* enable/disable */
232 if (!enabled)
233 return gb_pwm_enable_operation(chip, pwm->hwpwm);
235 return 0;
238 static const struct pwm_ops gb_pwm_ops = {
239 .request = gb_pwm_request,
240 .free = gb_pwm_free,
241 .apply = gb_pwm_apply,
244 static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
245 const struct gbphy_device_id *id)
247 struct gb_connection *connection;
248 struct gb_pwm_chip *pwmc;
249 struct pwm_chip *chip;
250 int ret, npwm;
252 connection = gb_connection_create(gbphy_dev->bundle,
253 le16_to_cpu(gbphy_dev->cport_desc->id),
254 NULL);
255 if (IS_ERR(connection))
256 return PTR_ERR(connection);
258 ret = gb_connection_enable(connection);
259 if (ret)
260 goto exit_connection_destroy;
262 /* Query number of pwms present */
263 ret = gb_pwm_get_npwm(connection);
264 if (ret < 0)
265 goto exit_connection_disable;
266 npwm = ret;
268 chip = pwmchip_alloc(&gbphy_dev->dev, npwm, sizeof(*pwmc));
269 if (IS_ERR(chip)) {
270 ret = PTR_ERR(chip);
271 goto exit_connection_disable;
273 gb_gbphy_set_data(gbphy_dev, chip);
275 pwmc = pwm_chip_to_gb_pwm_chip(chip);
276 pwmc->connection = connection;
278 chip->ops = &gb_pwm_ops;
280 ret = pwmchip_add(chip);
281 if (ret) {
282 dev_err(&gbphy_dev->dev,
283 "failed to register PWM: %d\n", ret);
284 goto exit_pwmchip_put;
287 gbphy_runtime_put_autosuspend(gbphy_dev);
288 return 0;
290 exit_pwmchip_put:
291 pwmchip_put(chip);
292 exit_connection_disable:
293 gb_connection_disable(connection);
294 exit_connection_destroy:
295 gb_connection_destroy(connection);
296 return ret;
299 static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
301 struct pwm_chip *chip = gb_gbphy_get_data(gbphy_dev);
302 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
303 struct gb_connection *connection = pwmc->connection;
304 int ret;
306 ret = gbphy_runtime_get_sync(gbphy_dev);
307 if (ret)
308 gbphy_runtime_get_noresume(gbphy_dev);
310 pwmchip_remove(chip);
311 pwmchip_put(chip);
312 gb_connection_disable(connection);
313 gb_connection_destroy(connection);
316 static const struct gbphy_device_id gb_pwm_id_table[] = {
317 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
318 { },
320 MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
322 static struct gbphy_driver pwm_driver = {
323 .name = "pwm",
324 .probe = gb_pwm_probe,
325 .remove = gb_pwm_remove,
326 .id_table = gb_pwm_id_table,
329 module_gbphy_driver(pwm_driver);
330 MODULE_DESCRIPTION("PWM Greybus driver");
331 MODULE_LICENSE("GPL v2");