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 #define pwm_chip_to_gb_pwm_chip(chip) \
25 container_of(chip, struct gb_pwm_chip, chip)
28 static int gb_pwm_count_operation(struct gb_pwm_chip
*pwmc
)
30 struct gb_pwm_count_response response
;
33 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_PWM_COUNT
,
34 NULL
, 0, &response
, sizeof(response
));
37 pwmc
->pwm_max
= response
.count
;
41 static int gb_pwm_activate_operation(struct gb_pwm_chip
*pwmc
,
44 struct gb_pwm_activate_request request
;
45 struct gbphy_device
*gbphy_dev
;
48 if (which
> pwmc
->pwm_max
)
51 request
.which
= which
;
53 gbphy_dev
= to_gbphy_dev(pwmc
->chip
.dev
);
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 gb_pwm_chip
*pwmc
,
69 struct gb_pwm_deactivate_request request
;
70 struct gbphy_device
*gbphy_dev
;
73 if (which
> pwmc
->pwm_max
)
76 request
.which
= which
;
78 gbphy_dev
= to_gbphy_dev(pwmc
->chip
.dev
);
79 ret
= gbphy_runtime_get_sync(gbphy_dev
);
83 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_DEACTIVATE
,
84 &request
, sizeof(request
), NULL
, 0);
86 gbphy_runtime_put_autosuspend(gbphy_dev
);
91 static int gb_pwm_config_operation(struct gb_pwm_chip
*pwmc
,
92 u8 which
, u32 duty
, u32 period
)
94 struct gb_pwm_config_request request
;
95 struct gbphy_device
*gbphy_dev
;
98 if (which
> pwmc
->pwm_max
)
101 request
.which
= which
;
102 request
.duty
= cpu_to_le32(duty
);
103 request
.period
= cpu_to_le32(period
);
105 gbphy_dev
= to_gbphy_dev(pwmc
->chip
.dev
);
106 ret
= gbphy_runtime_get_sync(gbphy_dev
);
110 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_CONFIG
,
111 &request
, sizeof(request
), NULL
, 0);
113 gbphy_runtime_put_autosuspend(gbphy_dev
);
118 static int gb_pwm_set_polarity_operation(struct gb_pwm_chip
*pwmc
,
119 u8 which
, u8 polarity
)
121 struct gb_pwm_polarity_request request
;
122 struct gbphy_device
*gbphy_dev
;
125 if (which
> pwmc
->pwm_max
)
128 request
.which
= which
;
129 request
.polarity
= polarity
;
131 gbphy_dev
= to_gbphy_dev(pwmc
->chip
.dev
);
132 ret
= gbphy_runtime_get_sync(gbphy_dev
);
136 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_POLARITY
,
137 &request
, sizeof(request
), NULL
, 0);
139 gbphy_runtime_put_autosuspend(gbphy_dev
);
144 static int gb_pwm_enable_operation(struct gb_pwm_chip
*pwmc
,
147 struct gb_pwm_enable_request request
;
148 struct gbphy_device
*gbphy_dev
;
151 if (which
> pwmc
->pwm_max
)
154 request
.which
= which
;
156 gbphy_dev
= to_gbphy_dev(pwmc
->chip
.dev
);
157 ret
= gbphy_runtime_get_sync(gbphy_dev
);
161 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_ENABLE
,
162 &request
, sizeof(request
), NULL
, 0);
164 gbphy_runtime_put_autosuspend(gbphy_dev
);
169 static int gb_pwm_disable_operation(struct gb_pwm_chip
*pwmc
,
172 struct gb_pwm_disable_request request
;
173 struct gbphy_device
*gbphy_dev
;
176 if (which
> pwmc
->pwm_max
)
179 request
.which
= which
;
181 ret
= gb_operation_sync(pwmc
->connection
, GB_PWM_TYPE_DISABLE
,
182 &request
, sizeof(request
), NULL
, 0);
184 gbphy_dev
= to_gbphy_dev(pwmc
->chip
.dev
);
185 gbphy_runtime_put_autosuspend(gbphy_dev
);
190 static int gb_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
192 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
194 return gb_pwm_activate_operation(pwmc
, pwm
->hwpwm
);
197 static void gb_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
199 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
201 if (pwm_is_enabled(pwm
))
202 dev_warn(chip
->dev
, "freeing PWM device without disabling\n");
204 gb_pwm_deactivate_operation(pwmc
, pwm
->hwpwm
);
207 static int gb_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
208 int duty_ns
, int period_ns
)
210 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
212 return gb_pwm_config_operation(pwmc
, pwm
->hwpwm
, duty_ns
, period_ns
);
215 static int gb_pwm_set_polarity(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
216 enum pwm_polarity polarity
)
218 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
220 return gb_pwm_set_polarity_operation(pwmc
, pwm
->hwpwm
, polarity
);
223 static int gb_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
225 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
227 return gb_pwm_enable_operation(pwmc
, pwm
->hwpwm
);
230 static void gb_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
232 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
234 gb_pwm_disable_operation(pwmc
, pwm
->hwpwm
);
237 static const struct pwm_ops gb_pwm_ops
= {
238 .request
= gb_pwm_request
,
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
,
247 static int gb_pwm_probe(struct gbphy_device
*gbphy_dev
,
248 const struct gbphy_device_id
*id
)
250 struct gb_connection
*connection
;
251 struct gb_pwm_chip
*pwmc
;
252 struct pwm_chip
*pwm
;
255 pwmc
= kzalloc(sizeof(*pwmc
), GFP_KERNEL
);
259 connection
= gb_connection_create(gbphy_dev
->bundle
,
260 le16_to_cpu(gbphy_dev
->cport_desc
->id
),
262 if (IS_ERR(connection
)) {
263 ret
= PTR_ERR(connection
);
267 pwmc
->connection
= connection
;
268 gb_connection_set_data(connection
, pwmc
);
269 gb_gbphy_set_data(gbphy_dev
, pwmc
);
271 ret
= gb_connection_enable(connection
);
273 goto exit_connection_destroy
;
275 /* Query number of pwms present */
276 ret
= gb_pwm_count_operation(pwmc
);
278 goto exit_connection_disable
;
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;
287 ret
= pwmchip_add(pwm
);
289 dev_err(&gbphy_dev
->dev
,
290 "failed to register PWM: %d\n", ret
);
291 goto exit_connection_disable
;
294 gbphy_runtime_put_autosuspend(gbphy_dev
);
297 exit_connection_disable
:
298 gb_connection_disable(connection
);
299 exit_connection_destroy
:
300 gb_connection_destroy(connection
);
306 static void gb_pwm_remove(struct gbphy_device
*gbphy_dev
)
308 struct gb_pwm_chip
*pwmc
= gb_gbphy_get_data(gbphy_dev
);
309 struct gb_connection
*connection
= pwmc
->connection
;
312 ret
= gbphy_runtime_get_sync(gbphy_dev
);
314 gbphy_runtime_get_noresume(gbphy_dev
);
316 pwmchip_remove(&pwmc
->chip
);
317 gb_connection_disable(connection
);
318 gb_connection_destroy(connection
);
322 static const struct gbphy_device_id gb_pwm_id_table
[] = {
323 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM
) },
326 MODULE_DEVICE_TABLE(gbphy
, gb_pwm_id_table
);
328 static struct gbphy_driver pwm_driver
= {
330 .probe
= gb_pwm_probe
,
331 .remove
= gb_pwm_remove
,
332 .id_table
= gb_pwm_id_table
,
335 module_gbphy_driver(pwm_driver
);
336 MODULE_LICENSE("GPL v2");