4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/pwm.h>
19 struct gb_connection
*connection
;
20 u8 pwm_max
; /* max pwm number */
25 #define pwm_chip_to_gb_pwm_chip(chip) \
26 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_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
209 int duty_ns
, int period_ns
)
211 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
213 return gb_pwm_config_operation(pwmc
, pwm
->hwpwm
, duty_ns
, period_ns
);
216 static int gb_pwm_set_polarity(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
217 enum pwm_polarity polarity
)
219 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
221 return gb_pwm_set_polarity_operation(pwmc
, pwm
->hwpwm
, polarity
);
224 static int gb_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
226 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
228 return gb_pwm_enable_operation(pwmc
, pwm
->hwpwm
);
231 static void gb_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
233 struct gb_pwm_chip
*pwmc
= pwm_chip_to_gb_pwm_chip(chip
);
235 gb_pwm_disable_operation(pwmc
, pwm
->hwpwm
);
238 static const struct pwm_ops gb_pwm_ops
= {
239 .request
= gb_pwm_request
,
241 .config
= gb_pwm_config
,
242 .set_polarity
= gb_pwm_set_polarity
,
243 .enable
= gb_pwm_enable
,
244 .disable
= gb_pwm_disable
,
245 .owner
= THIS_MODULE
,
248 static int gb_pwm_probe(struct gbphy_device
*gbphy_dev
,
249 const struct gbphy_device_id
*id
)
251 struct gb_connection
*connection
;
252 struct gb_pwm_chip
*pwmc
;
253 struct pwm_chip
*pwm
;
256 pwmc
= kzalloc(sizeof(*pwmc
), GFP_KERNEL
);
260 connection
= gb_connection_create(gbphy_dev
->bundle
,
261 le16_to_cpu(gbphy_dev
->cport_desc
->id
),
263 if (IS_ERR(connection
)) {
264 ret
= PTR_ERR(connection
);
268 pwmc
->connection
= connection
;
269 gb_connection_set_data(connection
, pwmc
);
270 gb_gbphy_set_data(gbphy_dev
, pwmc
);
272 ret
= gb_connection_enable(connection
);
274 goto exit_connection_destroy
;
276 /* Query number of pwms present */
277 ret
= gb_pwm_count_operation(pwmc
);
279 goto exit_connection_disable
;
283 pwm
->dev
= &gbphy_dev
->dev
;
284 pwm
->ops
= &gb_pwm_ops
;
285 pwm
->base
= -1; /* Allocate base dynamically */
286 pwm
->npwm
= pwmc
->pwm_max
+ 1;
287 pwm
->can_sleep
= true; /* FIXME */
289 ret
= pwmchip_add(pwm
);
291 dev_err(&gbphy_dev
->dev
,
292 "failed to register PWM: %d\n", ret
);
293 goto exit_connection_disable
;
296 gbphy_runtime_put_autosuspend(gbphy_dev
);
299 exit_connection_disable
:
300 gb_connection_disable(connection
);
301 exit_connection_destroy
:
302 gb_connection_destroy(connection
);
308 static void gb_pwm_remove(struct gbphy_device
*gbphy_dev
)
310 struct gb_pwm_chip
*pwmc
= gb_gbphy_get_data(gbphy_dev
);
311 struct gb_connection
*connection
= pwmc
->connection
;
314 ret
= gbphy_runtime_get_sync(gbphy_dev
);
316 gbphy_runtime_get_noresume(gbphy_dev
);
318 pwmchip_remove(&pwmc
->chip
);
319 gb_connection_disable(connection
);
320 gb_connection_destroy(connection
);
324 static const struct gbphy_device_id gb_pwm_id_table
[] = {
325 { GBPHY_PROTOCOL(GREYBUS_PROTOCOL_PWM
) },
328 MODULE_DEVICE_TABLE(gbphy
, gb_pwm_id_table
);
330 static struct gbphy_driver pwm_driver
= {
332 .probe
= gb_pwm_probe
,
333 .remove
= gb_pwm_remove
,
334 .id_table
= gb_pwm_id_table
,
337 module_gbphy_driver(pwm_driver
);
338 MODULE_LICENSE("GPL v2");