1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
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>
18 struct gb_connection *connection;
19 u8 pwm_max; /* max pwm number */
24 static inline struct gb_pwm_chip *pwm_chip_to_gb_pwm_chip(struct pwm_chip *chip)
26 return container_of(chip, struct gb_pwm_chip, chip);
29 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
31 struct gb_pwm_count_response response;
34 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
35 NULL, 0, &response, sizeof(response));
38 pwmc->pwm_max = response.count;
42 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
45 struct gb_pwm_activate_request request;
46 struct gbphy_device *gbphy_dev;
49 if (which > pwmc->pwm_max)
52 request.which = which;
54 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
55 ret = gbphy_runtime_get_sync(gbphy_dev);
59 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
60 &request, sizeof(request), NULL, 0);
62 gbphy_runtime_put_autosuspend(gbphy_dev);
67 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
70 struct gb_pwm_deactivate_request request;
71 struct gbphy_device *gbphy_dev;
74 if (which > pwmc->pwm_max)
77 request.which = which;
79 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
80 ret = gbphy_runtime_get_sync(gbphy_dev);
84 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
85 &request, sizeof(request), NULL, 0);
87 gbphy_runtime_put_autosuspend(gbphy_dev);
92 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
93 u8 which, u32 duty, u32 period)
95 struct gb_pwm_config_request request;
96 struct gbphy_device *gbphy_dev;
99 if (which > pwmc->pwm_max)
102 request.which = which;
103 request.duty = cpu_to_le32(duty);
104 request.period = cpu_to_le32(period);
106 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
107 ret = gbphy_runtime_get_sync(gbphy_dev);
111 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
112 &request, sizeof(request), NULL, 0);
114 gbphy_runtime_put_autosuspend(gbphy_dev);
119 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
120 u8 which, u8 polarity)
122 struct gb_pwm_polarity_request request;
123 struct gbphy_device *gbphy_dev;
126 if (which > pwmc->pwm_max)
129 request.which = which;
130 request.polarity = polarity;
132 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
133 ret = gbphy_runtime_get_sync(gbphy_dev);
137 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
138 &request, sizeof(request), NULL, 0);
140 gbphy_runtime_put_autosuspend(gbphy_dev);
145 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
148 struct gb_pwm_enable_request request;
149 struct gbphy_device *gbphy_dev;
152 if (which > pwmc->pwm_max)
155 request.which = which;
157 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
158 ret = gbphy_runtime_get_sync(gbphy_dev);
162 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
163 &request, sizeof(request), NULL, 0);
165 gbphy_runtime_put_autosuspend(gbphy_dev);
170 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
173 struct gb_pwm_disable_request request;
174 struct gbphy_device *gbphy_dev;
177 if (which > pwmc->pwm_max)
180 request.which = which;
182 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
183 &request, sizeof(request), NULL, 0);
185 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
186 gbphy_runtime_put_autosuspend(gbphy_dev);
191 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
193 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
195 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
198 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
200 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
202 if (pwm_is_enabled(pwm))
203 dev_warn(chip->dev, "freeing PWM device without disabling\n");
205 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
208 static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
209 const struct pwm_state *state)
212 bool enabled = pwm->state.enabled;
213 u64 period = state->period;
214 u64 duty_cycle = state->duty_cycle;
215 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
218 if (state->polarity != pwm->state.polarity) {
220 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
223 err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity);
228 if (!state->enabled) {
230 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
235 * Set period and duty cycle
237 * PWM privodes 64-bit period and duty_cycle, but greybus only accepts
238 * 32-bit, so their values have to be limited to U32_MAX.
240 if (period > U32_MAX)
243 if (duty_cycle > period)
246 err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period);
252 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
257 static const struct pwm_ops gb_pwm_ops = {
258 .request = gb_pwm_request,
260 .apply = gb_pwm_apply,
263 static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
264 const struct gbphy_device_id *id)
266 struct gb_connection *connection;
267 struct gb_pwm_chip *pwmc;
268 struct pwm_chip *chip;
271 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
275 connection = gb_connection_create(gbphy_dev->bundle,
276 le16_to_cpu(gbphy_dev->cport_desc->id),
278 if (IS_ERR(connection)) {
279 ret = PTR_ERR(connection);
283 pwmc->connection = connection;
284 gb_connection_set_data(connection, pwmc);
285 gb_gbphy_set_data(gbphy_dev, pwmc);
287 ret = gb_connection_enable(connection);
289 goto exit_connection_destroy;
291 /* Query number of pwms present */
292 ret = gb_pwm_count_operation(pwmc);
294 goto exit_connection_disable;
298 chip->dev = &gbphy_dev->dev;
299 chip->ops = &gb_pwm_ops;
300 chip->npwm = pwmc->pwm_max + 1;
302 ret = pwmchip_add(chip);
304 dev_err(&gbphy_dev->dev,
305 "failed to register PWM: %d\n", ret);
306 goto exit_connection_disable;
309 gbphy_runtime_put_autosuspend(gbphy_dev);
312 exit_connection_disable:
313 gb_connection_disable(connection);
314 exit_connection_destroy:
315 gb_connection_destroy(connection);
321 static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
323 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
324 struct gb_connection *connection = pwmc->connection;
327 ret = gbphy_runtime_get_sync(gbphy_dev);
329 gbphy_runtime_get_noresume(gbphy_dev);
331 pwmchip_remove(&pwmc->chip);
332 gb_connection_disable(connection);
333 gb_connection_destroy(connection);
337 static const struct gbphy_device_id gb_pwm_id_table[] = {
338 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
341 MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
343 static struct gbphy_driver pwm_driver = {
345 .probe = gb_pwm_probe,
346 .remove = gb_pwm_remove,
347 .id_table = gb_pwm_id_table,
350 module_gbphy_driver(pwm_driver);
351 MODULE_LICENSE("GPL v2");