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
;
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
;
32 ret
= gb_operation_sync(connection
, GB_PWM_TYPE_PWM_COUNT
,
33 NULL
, 0, &response
, sizeof(response
));
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
;
51 request
.which
= which
;
53 gbphy_dev
= to_gbphy_dev(pwmchip_parent(chip
));
54 ret
= gbphy_runtime_get_sync(gbphy_dev
);
58 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_ACTIVATE
,
59 &request
, sizeof(request
), NULL
, 0);
61 gbphy_runtime_put_autosuspend(gbphy_dev
);
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
;
73 request
.which
= which
;
75 gbphy_dev
= to_gbphy_dev(pwmchip_parent(chip
));
76 ret
= gbphy_runtime_get_sync(gbphy_dev
);
80 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_DEACTIVATE
,
81 &request
, sizeof(request
), NULL
, 0);
83 gbphy_runtime_put_autosuspend(gbphy_dev
);
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
;
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
);
105 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_CONFIG
,
106 &request
, sizeof(request
), NULL
, 0);
108 gbphy_runtime_put_autosuspend(gbphy_dev
);
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
;
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
);
129 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_POLARITY
,
130 &request
, sizeof(request
), NULL
, 0);
132 gbphy_runtime_put_autosuspend(gbphy_dev
);
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
;
144 request
.which
= which
;
146 gbphy_dev
= to_gbphy_dev(pwmchip_parent(chip
));
147 ret
= gbphy_runtime_get_sync(gbphy_dev
);
151 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_ENABLE
,
152 &request
, sizeof(request
), NULL
, 0);
154 gbphy_runtime_put_autosuspend(gbphy_dev
);
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
;
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
);
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
)
194 bool enabled
= pwm
->state
.enabled
;
195 u64 period
= state
->period
;
196 u64 duty_cycle
= state
->duty_cycle
;
199 if (state
->polarity
!= pwm
->state
.polarity
) {
201 gb_pwm_disable_operation(chip
, pwm
->hwpwm
);
204 err
= gb_pwm_set_polarity_operation(chip
, pwm
->hwpwm
, state
->polarity
);
209 if (!state
->enabled
) {
211 gb_pwm_disable_operation(chip
, pwm
->hwpwm
);
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
)
224 if (duty_cycle
> period
)
227 err
= gb_pwm_config_operation(chip
, pwm
->hwpwm
, duty_cycle
, period
);
233 return gb_pwm_enable_operation(chip
, pwm
->hwpwm
);
238 static const struct pwm_ops gb_pwm_ops
= {
239 .request
= gb_pwm_request
,
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
;
252 connection
= gb_connection_create(gbphy_dev
->bundle
,
253 le16_to_cpu(gbphy_dev
->cport_desc
->id
),
255 if (IS_ERR(connection
))
256 return PTR_ERR(connection
);
258 ret
= gb_connection_enable(connection
);
260 goto exit_connection_destroy
;
262 /* Query number of pwms present */
263 ret
= gb_pwm_get_npwm(connection
);
265 goto exit_connection_disable
;
268 chip
= pwmchip_alloc(&gbphy_dev
->dev
, npwm
, sizeof(*pwmc
));
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
);
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
);
292 exit_connection_disable
:
293 gb_connection_disable(connection
);
294 exit_connection_destroy
:
295 gb_connection_destroy(connection
);
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
;
306 ret
= gbphy_runtime_get_sync(gbphy_dev
);
308 gbphy_runtime_get_noresume(gbphy_dev
);
310 pwmchip_remove(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
) },
320 MODULE_DEVICE_TABLE(gbphy
, gb_pwm_id_table
);
322 static struct gbphy_driver pwm_driver
= {
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");