gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / greybus / control.c
blob359a2584197397dc5351fa0f01b1c3ac5a79444f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus CPort control protocol.
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
7 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/greybus.h>
14 /* Highest control-protocol version supported */
15 #define GB_CONTROL_VERSION_MAJOR 0
16 #define GB_CONTROL_VERSION_MINOR 1
18 static int gb_control_get_version(struct gb_control *control)
20 struct gb_interface *intf = control->connection->intf;
21 struct gb_control_version_request request;
22 struct gb_control_version_response response;
23 int ret;
25 request.major = GB_CONTROL_VERSION_MAJOR;
26 request.minor = GB_CONTROL_VERSION_MINOR;
28 ret = gb_operation_sync(control->connection,
29 GB_CONTROL_TYPE_VERSION,
30 &request, sizeof(request), &response,
31 sizeof(response));
32 if (ret) {
33 dev_err(&intf->dev,
34 "failed to get control-protocol version: %d\n",
35 ret);
36 return ret;
39 if (response.major > request.major) {
40 dev_err(&intf->dev,
41 "unsupported major control-protocol version (%u > %u)\n",
42 response.major, request.major);
43 return -ENOTSUPP;
46 control->protocol_major = response.major;
47 control->protocol_minor = response.minor;
49 dev_dbg(&intf->dev, "%s - %u.%u\n", __func__, response.major,
50 response.minor);
52 return 0;
55 static int gb_control_get_bundle_version(struct gb_control *control,
56 struct gb_bundle *bundle)
58 struct gb_interface *intf = control->connection->intf;
59 struct gb_control_bundle_version_request request;
60 struct gb_control_bundle_version_response response;
61 int ret;
63 request.bundle_id = bundle->id;
65 ret = gb_operation_sync(control->connection,
66 GB_CONTROL_TYPE_BUNDLE_VERSION,
67 &request, sizeof(request),
68 &response, sizeof(response));
69 if (ret) {
70 dev_err(&intf->dev,
71 "failed to get bundle %u class version: %d\n",
72 bundle->id, ret);
73 return ret;
76 bundle->class_major = response.major;
77 bundle->class_minor = response.minor;
79 dev_dbg(&intf->dev, "%s - %u: %u.%u\n", __func__, bundle->id,
80 response.major, response.minor);
82 return 0;
85 int gb_control_get_bundle_versions(struct gb_control *control)
87 struct gb_interface *intf = control->connection->intf;
88 struct gb_bundle *bundle;
89 int ret;
91 if (!control->has_bundle_version)
92 return 0;
94 list_for_each_entry(bundle, &intf->bundles, links) {
95 ret = gb_control_get_bundle_version(control, bundle);
96 if (ret)
97 return ret;
100 return 0;
103 /* Get Manifest's size from the interface */
104 int gb_control_get_manifest_size_operation(struct gb_interface *intf)
106 struct gb_control_get_manifest_size_response response;
107 struct gb_connection *connection = intf->control->connection;
108 int ret;
110 ret = gb_operation_sync(connection, GB_CONTROL_TYPE_GET_MANIFEST_SIZE,
111 NULL, 0, &response, sizeof(response));
112 if (ret) {
113 dev_err(&connection->intf->dev,
114 "failed to get manifest size: %d\n", ret);
115 return ret;
118 return le16_to_cpu(response.size);
121 /* Reads Manifest from the interface */
122 int gb_control_get_manifest_operation(struct gb_interface *intf, void *manifest,
123 size_t size)
125 struct gb_connection *connection = intf->control->connection;
127 return gb_operation_sync(connection, GB_CONTROL_TYPE_GET_MANIFEST,
128 NULL, 0, manifest, size);
131 int gb_control_connected_operation(struct gb_control *control, u16 cport_id)
133 struct gb_control_connected_request request;
135 request.cport_id = cpu_to_le16(cport_id);
136 return gb_operation_sync(control->connection, GB_CONTROL_TYPE_CONNECTED,
137 &request, sizeof(request), NULL, 0);
140 int gb_control_disconnected_operation(struct gb_control *control, u16 cport_id)
142 struct gb_control_disconnected_request request;
144 request.cport_id = cpu_to_le16(cport_id);
145 return gb_operation_sync(control->connection,
146 GB_CONTROL_TYPE_DISCONNECTED, &request,
147 sizeof(request), NULL, 0);
150 int gb_control_disconnecting_operation(struct gb_control *control,
151 u16 cport_id)
153 struct gb_control_disconnecting_request *request;
154 struct gb_operation *operation;
155 int ret;
157 operation = gb_operation_create_core(control->connection,
158 GB_CONTROL_TYPE_DISCONNECTING,
159 sizeof(*request), 0, 0,
160 GFP_KERNEL);
161 if (!operation)
162 return -ENOMEM;
164 request = operation->request->payload;
165 request->cport_id = cpu_to_le16(cport_id);
167 ret = gb_operation_request_send_sync(operation);
168 if (ret) {
169 dev_err(&control->dev, "failed to send disconnecting: %d\n",
170 ret);
173 gb_operation_put(operation);
175 return ret;
178 int gb_control_mode_switch_operation(struct gb_control *control)
180 struct gb_operation *operation;
181 int ret;
183 operation = gb_operation_create_core(control->connection,
184 GB_CONTROL_TYPE_MODE_SWITCH,
185 0, 0,
186 GB_OPERATION_FLAG_UNIDIRECTIONAL,
187 GFP_KERNEL);
188 if (!operation)
189 return -ENOMEM;
191 ret = gb_operation_request_send_sync(operation);
192 if (ret)
193 dev_err(&control->dev, "failed to send mode switch: %d\n", ret);
195 gb_operation_put(operation);
197 return ret;
200 static int gb_control_bundle_pm_status_map(u8 status)
202 switch (status) {
203 case GB_CONTROL_BUNDLE_PM_INVAL:
204 return -EINVAL;
205 case GB_CONTROL_BUNDLE_PM_BUSY:
206 return -EBUSY;
207 case GB_CONTROL_BUNDLE_PM_NA:
208 return -ENOMSG;
209 case GB_CONTROL_BUNDLE_PM_FAIL:
210 default:
211 return -EREMOTEIO;
215 int gb_control_bundle_suspend(struct gb_control *control, u8 bundle_id)
217 struct gb_control_bundle_pm_request request;
218 struct gb_control_bundle_pm_response response;
219 int ret;
221 request.bundle_id = bundle_id;
222 ret = gb_operation_sync(control->connection,
223 GB_CONTROL_TYPE_BUNDLE_SUSPEND, &request,
224 sizeof(request), &response, sizeof(response));
225 if (ret) {
226 dev_err(&control->dev, "failed to send bundle %u suspend: %d\n",
227 bundle_id, ret);
228 return ret;
231 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
232 dev_err(&control->dev, "failed to suspend bundle %u: %d\n",
233 bundle_id, response.status);
234 return gb_control_bundle_pm_status_map(response.status);
237 return 0;
240 int gb_control_bundle_resume(struct gb_control *control, u8 bundle_id)
242 struct gb_control_bundle_pm_request request;
243 struct gb_control_bundle_pm_response response;
244 int ret;
246 request.bundle_id = bundle_id;
247 ret = gb_operation_sync(control->connection,
248 GB_CONTROL_TYPE_BUNDLE_RESUME, &request,
249 sizeof(request), &response, sizeof(response));
250 if (ret) {
251 dev_err(&control->dev, "failed to send bundle %u resume: %d\n",
252 bundle_id, ret);
253 return ret;
256 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
257 dev_err(&control->dev, "failed to resume bundle %u: %d\n",
258 bundle_id, response.status);
259 return gb_control_bundle_pm_status_map(response.status);
262 return 0;
265 int gb_control_bundle_deactivate(struct gb_control *control, u8 bundle_id)
267 struct gb_control_bundle_pm_request request;
268 struct gb_control_bundle_pm_response response;
269 int ret;
271 request.bundle_id = bundle_id;
272 ret = gb_operation_sync(control->connection,
273 GB_CONTROL_TYPE_BUNDLE_DEACTIVATE, &request,
274 sizeof(request), &response, sizeof(response));
275 if (ret) {
276 dev_err(&control->dev,
277 "failed to send bundle %u deactivate: %d\n", bundle_id,
278 ret);
279 return ret;
282 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
283 dev_err(&control->dev, "failed to deactivate bundle %u: %d\n",
284 bundle_id, response.status);
285 return gb_control_bundle_pm_status_map(response.status);
288 return 0;
291 int gb_control_bundle_activate(struct gb_control *control, u8 bundle_id)
293 struct gb_control_bundle_pm_request request;
294 struct gb_control_bundle_pm_response response;
295 int ret;
297 if (!control->has_bundle_activate)
298 return 0;
300 request.bundle_id = bundle_id;
301 ret = gb_operation_sync(control->connection,
302 GB_CONTROL_TYPE_BUNDLE_ACTIVATE, &request,
303 sizeof(request), &response, sizeof(response));
304 if (ret) {
305 dev_err(&control->dev,
306 "failed to send bundle %u activate: %d\n", bundle_id,
307 ret);
308 return ret;
311 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
312 dev_err(&control->dev, "failed to activate bundle %u: %d\n",
313 bundle_id, response.status);
314 return gb_control_bundle_pm_status_map(response.status);
317 return 0;
320 static int gb_control_interface_pm_status_map(u8 status)
322 switch (status) {
323 case GB_CONTROL_INTF_PM_BUSY:
324 return -EBUSY;
325 case GB_CONTROL_INTF_PM_NA:
326 return -ENOMSG;
327 default:
328 return -EREMOTEIO;
332 int gb_control_interface_suspend_prepare(struct gb_control *control)
334 struct gb_control_intf_pm_response response;
335 int ret;
337 ret = gb_operation_sync(control->connection,
338 GB_CONTROL_TYPE_INTF_SUSPEND_PREPARE, NULL, 0,
339 &response, sizeof(response));
340 if (ret) {
341 dev_err(&control->dev,
342 "failed to send interface suspend prepare: %d\n", ret);
343 return ret;
346 if (response.status != GB_CONTROL_INTF_PM_OK) {
347 dev_err(&control->dev, "interface error while preparing suspend: %d\n",
348 response.status);
349 return gb_control_interface_pm_status_map(response.status);
352 return 0;
355 int gb_control_interface_deactivate_prepare(struct gb_control *control)
357 struct gb_control_intf_pm_response response;
358 int ret;
360 ret = gb_operation_sync(control->connection,
361 GB_CONTROL_TYPE_INTF_DEACTIVATE_PREPARE, NULL,
362 0, &response, sizeof(response));
363 if (ret) {
364 dev_err(&control->dev, "failed to send interface deactivate prepare: %d\n",
365 ret);
366 return ret;
369 if (response.status != GB_CONTROL_INTF_PM_OK) {
370 dev_err(&control->dev, "interface error while preparing deactivate: %d\n",
371 response.status);
372 return gb_control_interface_pm_status_map(response.status);
375 return 0;
378 int gb_control_interface_hibernate_abort(struct gb_control *control)
380 struct gb_control_intf_pm_response response;
381 int ret;
383 ret = gb_operation_sync(control->connection,
384 GB_CONTROL_TYPE_INTF_HIBERNATE_ABORT, NULL, 0,
385 &response, sizeof(response));
386 if (ret) {
387 dev_err(&control->dev,
388 "failed to send interface aborting hibernate: %d\n",
389 ret);
390 return ret;
393 if (response.status != GB_CONTROL_INTF_PM_OK) {
394 dev_err(&control->dev, "interface error while aborting hibernate: %d\n",
395 response.status);
396 return gb_control_interface_pm_status_map(response.status);
399 return 0;
402 static ssize_t vendor_string_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
405 struct gb_control *control = to_gb_control(dev);
407 return scnprintf(buf, PAGE_SIZE, "%s\n", control->vendor_string);
409 static DEVICE_ATTR_RO(vendor_string);
411 static ssize_t product_string_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct gb_control *control = to_gb_control(dev);
416 return scnprintf(buf, PAGE_SIZE, "%s\n", control->product_string);
418 static DEVICE_ATTR_RO(product_string);
420 static struct attribute *control_attrs[] = {
421 &dev_attr_vendor_string.attr,
422 &dev_attr_product_string.attr,
423 NULL,
425 ATTRIBUTE_GROUPS(control);
427 static void gb_control_release(struct device *dev)
429 struct gb_control *control = to_gb_control(dev);
431 gb_connection_destroy(control->connection);
433 kfree(control->vendor_string);
434 kfree(control->product_string);
436 kfree(control);
439 struct device_type greybus_control_type = {
440 .name = "greybus_control",
441 .release = gb_control_release,
444 struct gb_control *gb_control_create(struct gb_interface *intf)
446 struct gb_connection *connection;
447 struct gb_control *control;
449 control = kzalloc(sizeof(*control), GFP_KERNEL);
450 if (!control)
451 return ERR_PTR(-ENOMEM);
453 control->intf = intf;
455 connection = gb_connection_create_control(intf);
456 if (IS_ERR(connection)) {
457 dev_err(&intf->dev,
458 "failed to create control connection: %ld\n",
459 PTR_ERR(connection));
460 kfree(control);
461 return ERR_CAST(connection);
464 control->connection = connection;
466 control->dev.parent = &intf->dev;
467 control->dev.bus = &greybus_bus_type;
468 control->dev.type = &greybus_control_type;
469 control->dev.groups = control_groups;
470 control->dev.dma_mask = intf->dev.dma_mask;
471 device_initialize(&control->dev);
472 dev_set_name(&control->dev, "%s.ctrl", dev_name(&intf->dev));
474 gb_connection_set_data(control->connection, control);
476 return control;
479 int gb_control_enable(struct gb_control *control)
481 int ret;
483 dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
485 ret = gb_connection_enable_tx(control->connection);
486 if (ret) {
487 dev_err(&control->connection->intf->dev,
488 "failed to enable control connection: %d\n",
489 ret);
490 return ret;
493 ret = gb_control_get_version(control);
494 if (ret)
495 goto err_disable_connection;
497 if (control->protocol_major > 0 || control->protocol_minor > 1)
498 control->has_bundle_version = true;
500 /* FIXME: use protocol version instead */
501 if (!(control->intf->quirks & GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE))
502 control->has_bundle_activate = true;
504 return 0;
506 err_disable_connection:
507 gb_connection_disable(control->connection);
509 return ret;
512 void gb_control_disable(struct gb_control *control)
514 dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
516 if (control->intf->disconnected)
517 gb_connection_disable_forced(control->connection);
518 else
519 gb_connection_disable(control->connection);
522 int gb_control_suspend(struct gb_control *control)
524 gb_connection_disable(control->connection);
526 return 0;
529 int gb_control_resume(struct gb_control *control)
531 int ret;
533 ret = gb_connection_enable_tx(control->connection);
534 if (ret) {
535 dev_err(&control->connection->intf->dev,
536 "failed to enable control connection: %d\n", ret);
537 return ret;
540 return 0;
543 int gb_control_add(struct gb_control *control)
545 int ret;
547 ret = device_add(&control->dev);
548 if (ret) {
549 dev_err(&control->dev,
550 "failed to register control device: %d\n",
551 ret);
552 return ret;
555 return 0;
558 void gb_control_del(struct gb_control *control)
560 if (device_is_registered(&control->dev))
561 device_del(&control->dev);
564 struct gb_control *gb_control_get(struct gb_control *control)
566 get_device(&control->dev);
568 return control;
571 void gb_control_put(struct gb_control *control)
573 put_device(&control->dev);
576 void gb_control_mode_switch_prepare(struct gb_control *control)
578 gb_connection_mode_switch_prepare(control->connection);
581 void gb_control_mode_switch_complete(struct gb_control *control)
583 gb_connection_mode_switch_complete(control->connection);