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 */
25 static inline struct gb_pwm_chip *pwm_chip_to_gb_pwm_chip(struct pwm_chip *chip)
27 return container_of(chip, struct gb_pwm_chip, chip);
30 static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
32 struct gb_pwm_count_response response;
35 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
36 NULL, 0, &response, sizeof(response));
39 pwmc->pwm_max = response.count;
43 static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
46 struct gb_pwm_activate_request request;
47 struct gbphy_device *gbphy_dev;
50 if (which > pwmc->pwm_max)
53 request.which = which;
55 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
56 ret = gbphy_runtime_get_sync(gbphy_dev);
60 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
61 &request, sizeof(request), NULL, 0);
63 gbphy_runtime_put_autosuspend(gbphy_dev);
68 static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
71 struct gb_pwm_deactivate_request request;
72 struct gbphy_device *gbphy_dev;
75 if (which > pwmc->pwm_max)
78 request.which = which;
80 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
81 ret = gbphy_runtime_get_sync(gbphy_dev);
85 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
86 &request, sizeof(request), NULL, 0);
88 gbphy_runtime_put_autosuspend(gbphy_dev);
93 static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
94 u8 which, u32 duty, u32 period)
96 struct gb_pwm_config_request request;
97 struct gbphy_device *gbphy_dev;
100 if (which > pwmc->pwm_max)
103 request.which = which;
104 request.duty = cpu_to_le32(duty);
105 request.period = cpu_to_le32(period);
107 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
108 ret = gbphy_runtime_get_sync(gbphy_dev);
112 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
113 &request, sizeof(request), NULL, 0);
115 gbphy_runtime_put_autosuspend(gbphy_dev);
120 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
121 u8 which, u8 polarity)
123 struct gb_pwm_polarity_request request;
124 struct gbphy_device *gbphy_dev;
127 if (which > pwmc->pwm_max)
130 request.which = which;
131 request.polarity = polarity;
133 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
134 ret = gbphy_runtime_get_sync(gbphy_dev);
138 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
139 &request, sizeof(request), NULL, 0);
141 gbphy_runtime_put_autosuspend(gbphy_dev);
146 static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
149 struct gb_pwm_enable_request request;
150 struct gbphy_device *gbphy_dev;
153 if (which > pwmc->pwm_max)
156 request.which = which;
158 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
159 ret = gbphy_runtime_get_sync(gbphy_dev);
163 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
164 &request, sizeof(request), NULL, 0);
166 gbphy_runtime_put_autosuspend(gbphy_dev);
171 static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
174 struct gb_pwm_disable_request request;
175 struct gbphy_device *gbphy_dev;
178 if (which > pwmc->pwm_max)
181 request.which = which;
183 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
184 &request, sizeof(request), NULL, 0);
186 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
187 gbphy_runtime_put_autosuspend(gbphy_dev);
192 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
194 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
196 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
199 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
201 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
203 if (pwm_is_enabled(pwm))
204 dev_warn(chip->dev, "freeing PWM device without disabling\n");
206 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
209 static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
210 const struct pwm_state *state)
213 bool enabled = pwm->state.enabled;
214 u64 period = state->period;
215 u64 duty_cycle = state->duty_cycle;
216 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
219 if (state->polarity != pwm->state.polarity) {
221 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
224 err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity);
229 if (!state->enabled) {
231 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
236 * Set period and duty cycle
238 * PWM privodes 64-bit period and duty_cycle, but greybus only accepts
239 * 32-bit, so their values have to be limited to U32_MAX.
241 if (period > U32_MAX)
244 if (duty_cycle > period)
247 err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period);
253 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
258 static const struct pwm_ops gb_pwm_ops = {
259 .request = gb_pwm_request,
261 .apply = gb_pwm_apply,
262 .owner = THIS_MODULE,
265 static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
266 const struct gbphy_device_id *id)
268 struct gb_connection *connection;
269 struct gb_pwm_chip *pwmc;
270 struct pwm_chip *pwm;
273 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
277 connection = gb_connection_create(gbphy_dev->bundle,
278 le16_to_cpu(gbphy_dev->cport_desc->id),
280 if (IS_ERR(connection)) {
281 ret = PTR_ERR(connection);
285 pwmc->connection = connection;
286 gb_connection_set_data(connection, pwmc);
287 gb_gbphy_set_data(gbphy_dev, pwmc);
289 ret = gb_connection_enable(connection);
291 goto exit_connection_destroy;
293 /* Query number of pwms present */
294 ret = gb_pwm_count_operation(pwmc);
296 goto exit_connection_disable;
300 pwm->dev = &gbphy_dev->dev;
301 pwm->ops = &gb_pwm_ops;
302 pwm->npwm = pwmc->pwm_max + 1;
304 ret = pwmchip_add(pwm);
306 dev_err(&gbphy_dev->dev,
307 "failed to register PWM: %d\n", ret);
308 goto exit_connection_disable;
311 gbphy_runtime_put_autosuspend(gbphy_dev);
314 exit_connection_disable:
315 gb_connection_disable(connection);
316 exit_connection_destroy:
317 gb_connection_destroy(connection);
323 static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
325 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
326 struct gb_connection *connection = pwmc->connection;
329 ret = gbphy_runtime_get_sync(gbphy_dev);
331 gbphy_runtime_get_noresume(gbphy_dev);
333 pwmchip_remove(&pwmc->chip);
334 gb_connection_disable(connection);
335 gb_connection_destroy(connection);
339 static const struct gbphy_device_id gb_pwm_id_table[] = {
340 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
343 MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
345 static struct gbphy_driver pwm_driver = {
347 .probe = gb_pwm_probe,
348 .remove = gb_pwm_remove,
349 .id_table = gb_pwm_id_table,
352 module_gbphy_driver(pwm_driver);
353 MODULE_LICENSE("GPL v2");