Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PWM Greybus driver.
4 *
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
7 */
8
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>
14
15#include "gbphy.h"
16
17struct gb_pwm_chip {
18 struct gb_connection *connection;
19 u8 pwm_max; /* max pwm number */
20
21 struct pwm_chip chip;
22};
23
24static inline struct gb_pwm_chip *pwm_chip_to_gb_pwm_chip(struct pwm_chip *chip)
25{
26 return container_of(chip, struct gb_pwm_chip, chip);
27}
28
29static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
30{
31 struct gb_pwm_count_response response;
32 int ret;
33
34 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT,
35 NULL, 0, &response, sizeof(response));
36 if (ret)
37 return ret;
38 pwmc->pwm_max = response.count;
39 return 0;
40}
41
42static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
43 u8 which)
44{
45 struct gb_pwm_activate_request request;
46 struct gbphy_device *gbphy_dev;
47 int ret;
48
49 if (which > pwmc->pwm_max)
50 return -EINVAL;
51
52 request.which = which;
53
54 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
55 ret = gbphy_runtime_get_sync(gbphy_dev);
56 if (ret)
57 return ret;
58
59 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
60 &request, sizeof(request), NULL, 0);
61
62 gbphy_runtime_put_autosuspend(gbphy_dev);
63
64 return ret;
65}
66
67static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
68 u8 which)
69{
70 struct gb_pwm_deactivate_request request;
71 struct gbphy_device *gbphy_dev;
72 int ret;
73
74 if (which > pwmc->pwm_max)
75 return -EINVAL;
76
77 request.which = which;
78
79 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
80 ret = gbphy_runtime_get_sync(gbphy_dev);
81 if (ret)
82 return ret;
83
84 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
85 &request, sizeof(request), NULL, 0);
86
87 gbphy_runtime_put_autosuspend(gbphy_dev);
88
89 return ret;
90}
91
92static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
93 u8 which, u32 duty, u32 period)
94{
95 struct gb_pwm_config_request request;
96 struct gbphy_device *gbphy_dev;
97 int ret;
98
99 if (which > pwmc->pwm_max)
100 return -EINVAL;
101
102 request.which = which;
103 request.duty = cpu_to_le32(duty);
104 request.period = cpu_to_le32(period);
105
106 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
107 ret = gbphy_runtime_get_sync(gbphy_dev);
108 if (ret)
109 return ret;
110
111 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
112 &request, sizeof(request), NULL, 0);
113
114 gbphy_runtime_put_autosuspend(gbphy_dev);
115
116 return ret;
117}
118
119static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
120 u8 which, u8 polarity)
121{
122 struct gb_pwm_polarity_request request;
123 struct gbphy_device *gbphy_dev;
124 int ret;
125
126 if (which > pwmc->pwm_max)
127 return -EINVAL;
128
129 request.which = which;
130 request.polarity = polarity;
131
132 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
133 ret = gbphy_runtime_get_sync(gbphy_dev);
134 if (ret)
135 return ret;
136
137 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
138 &request, sizeof(request), NULL, 0);
139
140 gbphy_runtime_put_autosuspend(gbphy_dev);
141
142 return ret;
143}
144
145static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
146 u8 which)
147{
148 struct gb_pwm_enable_request request;
149 struct gbphy_device *gbphy_dev;
150 int ret;
151
152 if (which > pwmc->pwm_max)
153 return -EINVAL;
154
155 request.which = which;
156
157 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
158 ret = gbphy_runtime_get_sync(gbphy_dev);
159 if (ret)
160 return ret;
161
162 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
163 &request, sizeof(request), NULL, 0);
164 if (ret)
165 gbphy_runtime_put_autosuspend(gbphy_dev);
166
167 return ret;
168}
169
170static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
171 u8 which)
172{
173 struct gb_pwm_disable_request request;
174 struct gbphy_device *gbphy_dev;
175 int ret;
176
177 if (which > pwmc->pwm_max)
178 return -EINVAL;
179
180 request.which = which;
181
182 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
183 &request, sizeof(request), NULL, 0);
184
185 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
186 gbphy_runtime_put_autosuspend(gbphy_dev);
187
188 return ret;
189}
190
191static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
192{
193 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
194
195 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
196};
197
198static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
199{
200 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
201
202 if (pwm_is_enabled(pwm))
203 dev_warn(chip->dev, "freeing PWM device without disabling\n");
204
205 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
206}
207
208static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
209 const struct pwm_state *state)
210{
211 int err;
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);
216
217 /* Set polarity */
218 if (state->polarity != pwm->state.polarity) {
219 if (enabled) {
220 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
221 enabled = false;
222 }
223 err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity);
224 if (err)
225 return err;
226 }
227
228 if (!state->enabled) {
229 if (enabled)
230 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
231 return 0;
232 }
233
234 /*
235 * Set period and duty cycle
236 *
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.
239 */
240 if (period > U32_MAX)
241 period = U32_MAX;
242
243 if (duty_cycle > period)
244 duty_cycle = period;
245
246 err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period);
247 if (err)
248 return err;
249
250 /* enable/disable */
251 if (!enabled)
252 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
253
254 return 0;
255}
256
257static const struct pwm_ops gb_pwm_ops = {
258 .request = gb_pwm_request,
259 .free = gb_pwm_free,
260 .apply = gb_pwm_apply,
261};
262
263static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
264 const struct gbphy_device_id *id)
265{
266 struct gb_connection *connection;
267 struct gb_pwm_chip *pwmc;
268 struct pwm_chip *chip;
269 int ret;
270
271 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
272 if (!pwmc)
273 return -ENOMEM;
274
275 connection = gb_connection_create(gbphy_dev->bundle,
276 le16_to_cpu(gbphy_dev->cport_desc->id),
277 NULL);
278 if (IS_ERR(connection)) {
279 ret = PTR_ERR(connection);
280 goto exit_pwmc_free;
281 }
282
283 pwmc->connection = connection;
284 gb_connection_set_data(connection, pwmc);
285 gb_gbphy_set_data(gbphy_dev, pwmc);
286
287 ret = gb_connection_enable(connection);
288 if (ret)
289 goto exit_connection_destroy;
290
291 /* Query number of pwms present */
292 ret = gb_pwm_count_operation(pwmc);
293 if (ret)
294 goto exit_connection_disable;
295
296 chip = &pwmc->chip;
297
298 chip->dev = &gbphy_dev->dev;
299 chip->ops = &gb_pwm_ops;
300 chip->npwm = pwmc->pwm_max + 1;
301
302 ret = pwmchip_add(chip);
303 if (ret) {
304 dev_err(&gbphy_dev->dev,
305 "failed to register PWM: %d\n", ret);
306 goto exit_connection_disable;
307 }
308
309 gbphy_runtime_put_autosuspend(gbphy_dev);
310 return 0;
311
312exit_connection_disable:
313 gb_connection_disable(connection);
314exit_connection_destroy:
315 gb_connection_destroy(connection);
316exit_pwmc_free:
317 kfree(pwmc);
318 return ret;
319}
320
321static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
322{
323 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
324 struct gb_connection *connection = pwmc->connection;
325 int ret;
326
327 ret = gbphy_runtime_get_sync(gbphy_dev);
328 if (ret)
329 gbphy_runtime_get_noresume(gbphy_dev);
330
331 pwmchip_remove(&pwmc->chip);
332 gb_connection_disable(connection);
333 gb_connection_destroy(connection);
334 kfree(pwmc);
335}
336
337static const struct gbphy_device_id gb_pwm_id_table[] = {
338 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
339 { },
340};
341MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
342
343static struct gbphy_driver pwm_driver = {
344 .name = "pwm",
345 .probe = gb_pwm_probe,
346 .remove = gb_pwm_remove,
347 .id_table = gb_pwm_id_table,
348};
349
350module_gbphy_driver(pwm_driver);
351MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PWM Greybus driver.
4 *
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
7 */
8
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>
14
15#include "gbphy.h"
16
17struct gb_pwm_chip {
18 struct gb_connection *connection;
19 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
27
28static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc)
29{
30 struct gb_pwm_count_response response;
31 int ret;
32
33 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;
39}
40
41static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc,
42 u8 which)
43{
44 struct gb_pwm_activate_request request;
45 struct gbphy_device *gbphy_dev;
46 int ret;
47
48 if (which > pwmc->pwm_max)
49 return -EINVAL;
50
51 request.which = which;
52
53 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
54 ret = gbphy_runtime_get_sync(gbphy_dev);
55 if (ret)
56 return ret;
57
58 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ACTIVATE,
59 &request, sizeof(request), NULL, 0);
60
61 gbphy_runtime_put_autosuspend(gbphy_dev);
62
63 return ret;
64}
65
66static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc,
67 u8 which)
68{
69 struct gb_pwm_deactivate_request request;
70 struct gbphy_device *gbphy_dev;
71 int ret;
72
73 if (which > pwmc->pwm_max)
74 return -EINVAL;
75
76 request.which = which;
77
78 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
79 ret = gbphy_runtime_get_sync(gbphy_dev);
80 if (ret)
81 return ret;
82
83 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DEACTIVATE,
84 &request, sizeof(request), NULL, 0);
85
86 gbphy_runtime_put_autosuspend(gbphy_dev);
87
88 return ret;
89}
90
91static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc,
92 u8 which, u32 duty, u32 period)
93{
94 struct gb_pwm_config_request request;
95 struct gbphy_device *gbphy_dev;
96 int ret;
97
98 if (which > pwmc->pwm_max)
99 return -EINVAL;
100
101 request.which = which;
102 request.duty = cpu_to_le32(duty);
103 request.period = cpu_to_le32(period);
104
105 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
106 ret = gbphy_runtime_get_sync(gbphy_dev);
107 if (ret)
108 return ret;
109
110 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_CONFIG,
111 &request, sizeof(request), NULL, 0);
112
113 gbphy_runtime_put_autosuspend(gbphy_dev);
114
115 return ret;
116}
117
118static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc,
119 u8 which, u8 polarity)
120{
121 struct gb_pwm_polarity_request request;
122 struct gbphy_device *gbphy_dev;
123 int ret;
124
125 if (which > pwmc->pwm_max)
126 return -EINVAL;
127
128 request.which = which;
129 request.polarity = polarity;
130
131 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
132 ret = gbphy_runtime_get_sync(gbphy_dev);
133 if (ret)
134 return ret;
135
136 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_POLARITY,
137 &request, sizeof(request), NULL, 0);
138
139 gbphy_runtime_put_autosuspend(gbphy_dev);
140
141 return ret;
142}
143
144static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc,
145 u8 which)
146{
147 struct gb_pwm_enable_request request;
148 struct gbphy_device *gbphy_dev;
149 int ret;
150
151 if (which > pwmc->pwm_max)
152 return -EINVAL;
153
154 request.which = which;
155
156 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
157 ret = gbphy_runtime_get_sync(gbphy_dev);
158 if (ret)
159 return ret;
160
161 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_ENABLE,
162 &request, sizeof(request), NULL, 0);
163 if (ret)
164 gbphy_runtime_put_autosuspend(gbphy_dev);
165
166 return ret;
167}
168
169static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc,
170 u8 which)
171{
172 struct gb_pwm_disable_request request;
173 struct gbphy_device *gbphy_dev;
174 int ret;
175
176 if (which > pwmc->pwm_max)
177 return -EINVAL;
178
179 request.which = which;
180
181 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE,
182 &request, sizeof(request), NULL, 0);
183
184 gbphy_dev = to_gbphy_dev(pwmc->chip.dev);
185 gbphy_runtime_put_autosuspend(gbphy_dev);
186
187 return ret;
188}
189
190static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
191{
192 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
193
194 return gb_pwm_activate_operation(pwmc, pwm->hwpwm);
195};
196
197static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
198{
199 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
200
201 if (pwm_is_enabled(pwm))
202 dev_warn(chip->dev, "freeing PWM device without disabling\n");
203
204 gb_pwm_deactivate_operation(pwmc, pwm->hwpwm);
205}
206
207static int gb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
208 int duty_ns, int period_ns)
209{
210 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
211
212 return gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_ns, period_ns);
213};
214
215static int gb_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
216 enum pwm_polarity polarity)
217{
218 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
219
220 return gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, polarity);
221};
222
223static int gb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
224{
225 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
226
227 return gb_pwm_enable_operation(pwmc, pwm->hwpwm);
228};
229
230static void gb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
231{
232 struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip);
233
234 gb_pwm_disable_operation(pwmc, pwm->hwpwm);
235};
236
237static const struct pwm_ops gb_pwm_ops = {
238 .request = gb_pwm_request,
239 .free = gb_pwm_free,
240 .config = gb_pwm_config,
241 .set_polarity = gb_pwm_set_polarity,
242 .enable = gb_pwm_enable,
243 .disable = gb_pwm_disable,
244 .owner = THIS_MODULE,
245};
246
247static int gb_pwm_probe(struct gbphy_device *gbphy_dev,
248 const struct gbphy_device_id *id)
249{
250 struct gb_connection *connection;
251 struct gb_pwm_chip *pwmc;
252 struct pwm_chip *pwm;
253 int ret;
254
255 pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL);
256 if (!pwmc)
257 return -ENOMEM;
258
259 connection = gb_connection_create(gbphy_dev->bundle,
260 le16_to_cpu(gbphy_dev->cport_desc->id),
261 NULL);
262 if (IS_ERR(connection)) {
263 ret = PTR_ERR(connection);
264 goto exit_pwmc_free;
265 }
266
267 pwmc->connection = connection;
268 gb_connection_set_data(connection, pwmc);
269 gb_gbphy_set_data(gbphy_dev, pwmc);
270
271 ret = gb_connection_enable(connection);
272 if (ret)
273 goto exit_connection_destroy;
274
275 /* Query number of pwms present */
276 ret = gb_pwm_count_operation(pwmc);
277 if (ret)
278 goto exit_connection_disable;
279
280 pwm = &pwmc->chip;
281
282 pwm->dev = &gbphy_dev->dev;
283 pwm->ops = &gb_pwm_ops;
284 pwm->base = -1; /* Allocate base dynamically */
285 pwm->npwm = pwmc->pwm_max + 1;
286
287 ret = pwmchip_add(pwm);
288 if (ret) {
289 dev_err(&gbphy_dev->dev,
290 "failed to register PWM: %d\n", ret);
291 goto exit_connection_disable;
292 }
293
294 gbphy_runtime_put_autosuspend(gbphy_dev);
295 return 0;
296
297exit_connection_disable:
298 gb_connection_disable(connection);
299exit_connection_destroy:
300 gb_connection_destroy(connection);
301exit_pwmc_free:
302 kfree(pwmc);
303 return ret;
304}
305
306static void gb_pwm_remove(struct gbphy_device *gbphy_dev)
307{
308 struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev);
309 struct gb_connection *connection = pwmc->connection;
310 int ret;
311
312 ret = gbphy_runtime_get_sync(gbphy_dev);
313 if (ret)
314 gbphy_runtime_get_noresume(gbphy_dev);
315
316 pwmchip_remove(&pwmc->chip);
317 gb_connection_disable(connection);
318 gb_connection_destroy(connection);
319 kfree(pwmc);
320}
321
322static const struct gbphy_device_id gb_pwm_id_table[] = {
323 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM) },
324 { },
325};
326MODULE_DEVICE_TABLE(gbphy, gb_pwm_id_table);
327
328static struct gbphy_driver pwm_driver = {
329 .name = "pwm",
330 .probe = gb_pwm_probe,
331 .remove = gb_pwm_remove,
332 .id_table = gb_pwm_id_table,
333};
334
335module_gbphy_driver(pwm_driver);
336MODULE_LICENSE("GPL v2");