1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus Camera protocol driver.
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
9 #include <linux/debugfs.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/uaccess.h>
16 #include <linux/vmalloc.h>
17 #include <linux/greybus.h>
19 #include "gb-camera.h"
20 #include "greybus_protocols.h"
22 enum gb_camera_debugs_buffer_id
{
23 GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES
,
24 GB_CAMERA_DEBUGFS_BUFFER_STREAMS
,
25 GB_CAMERA_DEBUGFS_BUFFER_CAPTURE
,
26 GB_CAMERA_DEBUGFS_BUFFER_FLUSH
,
27 GB_CAMERA_DEBUGFS_BUFFER_MAX
,
30 struct gb_camera_debugfs_buffer
{
35 enum gb_camera_state
{
36 GB_CAMERA_STATE_UNCONFIGURED
,
37 GB_CAMERA_STATE_CONFIGURED
,
41 * struct gb_camera - A Greybus Camera Device
42 * @connection: the greybus connection for camera management
43 * @data_connection: the greybus connection for camera data
44 * @data_cport_id: the data CPort ID on the module side
45 * @mutex: protects the connection and state fields
46 * @state: the current module state
47 * @debugfs: debugfs entries for camera protocol operations testing
48 * @module: Greybus camera module registered to HOST processor.
51 struct gb_bundle
*bundle
;
52 struct gb_connection
*connection
;
53 struct gb_connection
*data_connection
;
57 enum gb_camera_state state
;
61 struct gb_camera_debugfs_buffer
*buffers
;
64 struct gb_camera_module module
;
67 struct gb_camera_stream_config
{
73 unsigned int max_size
;
76 struct gb_camera_fmt_info
{
77 enum v4l2_mbus_pixelcode mbus_code
;
78 unsigned int gb_format
;
82 /* GB format to media code map */
83 static const struct gb_camera_fmt_info gb_fmt_info
[] = {
85 .mbus_code
= V4L2_MBUS_FMT_UYVY8_1X16
,
90 .mbus_code
= V4L2_MBUS_FMT_NV12_1x8
,
95 .mbus_code
= V4L2_MBUS_FMT_NV21_1x8
,
100 .mbus_code
= V4L2_MBUS_FMT_YU12_1x8
,
105 .mbus_code
= V4L2_MBUS_FMT_YV12_1x8
,
110 .mbus_code
= V4L2_MBUS_FMT_JPEG_1X8
,
115 .mbus_code
= V4L2_MBUS_FMT_GB_CAM_METADATA_1X8
,
120 .mbus_code
= V4L2_MBUS_FMT_GB_CAM_DEBUG_DATA_1X8
,
125 .mbus_code
= V4L2_MBUS_FMT_SBGGR10_1X10
,
130 .mbus_code
= V4L2_MBUS_FMT_SGBRG10_1X10
,
135 .mbus_code
= V4L2_MBUS_FMT_SGRBG10_1X10
,
140 .mbus_code
= V4L2_MBUS_FMT_SRGGB10_1X10
,
145 .mbus_code
= V4L2_MBUS_FMT_SBGGR12_1X12
,
150 .mbus_code
= V4L2_MBUS_FMT_SGBRG12_1X12
,
155 .mbus_code
= V4L2_MBUS_FMT_SGRBG12_1X12
,
160 .mbus_code
= V4L2_MBUS_FMT_SRGGB12_1X12
,
166 static const struct gb_camera_fmt_info
*gb_camera_get_format_info(u16 gb_fmt
)
170 for (i
= 0; i
< ARRAY_SIZE(gb_fmt_info
); i
++) {
171 if (gb_fmt_info
[i
].gb_format
== gb_fmt
)
172 return &gb_fmt_info
[i
];
178 #define ES2_APB_CDSI0_CPORT 16
179 #define ES2_APB_CDSI1_CPORT 17
181 #define GB_CAMERA_MAX_SETTINGS_SIZE 8192
183 static int gb_camera_operation_sync_flags(struct gb_connection
*connection
,
184 int type
, unsigned int flags
,
185 void *request
, size_t request_size
,
186 void *response
, size_t *response_size
)
188 struct gb_operation
*operation
;
191 operation
= gb_operation_create_flags(connection
, type
, request_size
,
192 *response_size
, flags
,
198 memcpy(operation
->request
->payload
, request
, request_size
);
200 ret
= gb_operation_request_send_sync(operation
);
202 dev_err(&connection
->hd
->dev
,
203 "%s: synchronous operation of type 0x%02x failed: %d\n",
204 connection
->name
, type
, ret
);
206 *response_size
= operation
->response
->payload_size
;
208 if (operation
->response
->payload_size
)
209 memcpy(response
, operation
->response
->payload
,
210 operation
->response
->payload_size
);
213 gb_operation_put(operation
);
218 static int gb_camera_get_max_pkt_size(struct gb_camera
*gcam
,
219 struct gb_camera_configure_streams_response
*resp
)
221 unsigned int max_pkt_size
= 0;
224 for (i
= 0; i
< resp
->num_streams
; i
++) {
225 struct gb_camera_stream_config_response
*cfg
= &resp
->config
[i
];
226 const struct gb_camera_fmt_info
*fmt_info
;
227 unsigned int pkt_size
;
229 fmt_info
= gb_camera_get_format_info(cfg
->format
);
231 dev_err(&gcam
->bundle
->dev
, "unsupported greybus image format: %d\n",
236 if (fmt_info
->bpp
== 0) {
237 pkt_size
= le32_to_cpu(cfg
->max_pkt_size
);
240 dev_err(&gcam
->bundle
->dev
,
241 "Stream %u: invalid zero maximum packet size\n",
246 pkt_size
= le16_to_cpu(cfg
->width
) * fmt_info
->bpp
/ 8;
248 if (pkt_size
!= le32_to_cpu(cfg
->max_pkt_size
)) {
249 dev_err(&gcam
->bundle
->dev
,
250 "Stream %u: maximum packet size mismatch (%u/%u)\n",
251 i
, pkt_size
, cfg
->max_pkt_size
);
256 max_pkt_size
= max(pkt_size
, max_pkt_size
);
263 * Validate the stream configuration response verifying padding is correctly
264 * set and the returned number of streams is supported
266 static const int gb_camera_configure_streams_validate_response(struct gb_camera
*gcam
,
267 struct gb_camera_configure_streams_response
*resp
,
268 unsigned int nstreams
)
272 /* Validate the returned response structure */
273 if (resp
->padding
[0] || resp
->padding
[1]) {
274 dev_err(&gcam
->bundle
->dev
, "response padding != 0\n");
278 if (resp
->num_streams
> nstreams
) {
279 dev_err(&gcam
->bundle
->dev
, "got #streams %u > request %u\n",
280 resp
->num_streams
, nstreams
);
284 for (i
= 0; i
< resp
->num_streams
; i
++) {
285 struct gb_camera_stream_config_response
*cfg
= &resp
->config
[i
];
288 dev_err(&gcam
->bundle
->dev
, "stream #%u padding != 0\n", i
);
296 /* -----------------------------------------------------------------------------
297 * Hardware Configuration
300 static int gb_camera_set_intf_power_mode(struct gb_camera
*gcam
, u8 intf_id
,
303 struct gb_svc
*svc
= gcam
->connection
->hd
->svc
;
307 ret
= gb_svc_intf_set_power_mode(svc
, intf_id
,
308 GB_SVC_UNIPRO_HS_SERIES_A
,
309 GB_SVC_UNIPRO_FAST_MODE
, 2, 2,
310 GB_SVC_SMALL_AMPLITUDE
,
311 GB_SVC_NO_DE_EMPHASIS
,
312 GB_SVC_UNIPRO_FAST_MODE
, 2, 2,
313 GB_SVC_PWRM_RXTERMINATION
|
314 GB_SVC_PWRM_TXTERMINATION
, 0,
317 ret
= gb_svc_intf_set_power_mode(svc
, intf_id
,
318 GB_SVC_UNIPRO_HS_SERIES_A
,
319 GB_SVC_UNIPRO_SLOW_AUTO_MODE
,
321 GB_SVC_SMALL_AMPLITUDE
,
322 GB_SVC_NO_DE_EMPHASIS
,
323 GB_SVC_UNIPRO_SLOW_AUTO_MODE
,
331 static int gb_camera_set_power_mode(struct gb_camera
*gcam
, bool hs
)
333 struct gb_interface
*intf
= gcam
->connection
->intf
;
334 struct gb_svc
*svc
= gcam
->connection
->hd
->svc
;
337 ret
= gb_camera_set_intf_power_mode(gcam
, intf
->interface_id
, hs
);
339 dev_err(&gcam
->bundle
->dev
, "failed to set module interface to %s (%d)\n",
340 hs
? "HS" : "PWM", ret
);
344 ret
= gb_camera_set_intf_power_mode(gcam
, svc
->ap_intf_id
, hs
);
346 gb_camera_set_intf_power_mode(gcam
, intf
->interface_id
, !hs
);
347 dev_err(&gcam
->bundle
->dev
, "failed to set AP interface to %s (%d)\n",
348 hs
? "HS" : "PWM", ret
);
355 struct ap_csi_config_request
{
358 #define GB_CAMERA_CSI_FLAG_CLOCK_CONTINUOUS 0x01
366 * TODO: Compute the number of lanes dynamically based on bandwidth
369 #define GB_CAMERA_CSI_NUM_DATA_LANES 4
371 #define GB_CAMERA_CSI_CLK_FREQ_MAX 999000000U
372 #define GB_CAMERA_CSI_CLK_FREQ_MIN 100000000U
373 #define GB_CAMERA_CSI_CLK_FREQ_MARGIN 150000000U
375 static int gb_camera_setup_data_connection(struct gb_camera
*gcam
,
376 struct gb_camera_configure_streams_response
*resp
,
377 struct gb_camera_csi_params
*csi_params
)
379 struct ap_csi_config_request csi_cfg
;
380 struct gb_connection
*conn
;
381 unsigned int clk_freq
;
385 * Create the data connection between the camera module data CPort and
386 * APB CDSI1. The CDSI1 CPort ID is hardcoded by the ES2 bridge.
388 conn
= gb_connection_create_offloaded(gcam
->bundle
, gcam
->data_cport_id
,
389 GB_CONNECTION_FLAG_NO_FLOWCTRL
|
390 GB_CONNECTION_FLAG_CDSI1
);
392 return PTR_ERR(conn
);
394 gcam
->data_connection
= conn
;
395 gb_connection_set_data(conn
, gcam
);
397 ret
= gb_connection_enable(conn
);
399 goto error_conn_destroy
;
401 /* Set the UniPro link to high speed mode. */
402 ret
= gb_camera_set_power_mode(gcam
, true);
404 goto error_conn_disable
;
407 * Configure the APB-A CSI-2 transmitter.
409 * Hardcode the number of lanes to 4 and compute the bus clock frequency
410 * based on the module bandwidth requirements with a safety margin.
412 memset(&csi_cfg
, 0, sizeof(csi_cfg
));
415 csi_cfg
.num_lanes
= GB_CAMERA_CSI_NUM_DATA_LANES
;
417 clk_freq
= resp
->data_rate
/ 2 / GB_CAMERA_CSI_NUM_DATA_LANES
;
418 clk_freq
= clamp(clk_freq
+ GB_CAMERA_CSI_CLK_FREQ_MARGIN
,
419 GB_CAMERA_CSI_CLK_FREQ_MIN
,
420 GB_CAMERA_CSI_CLK_FREQ_MAX
);
421 csi_cfg
.csi_clk_freq
= clk_freq
;
423 ret
= gb_camera_get_max_pkt_size(gcam
, resp
);
428 csi_cfg
.max_pkt_size
= ret
;
430 ret
= gb_hd_output(gcam
->connection
->hd
, &csi_cfg
,
432 GB_APB_REQUEST_CSI_TX_CONTROL
, false);
434 dev_err(&gcam
->bundle
->dev
, "failed to start the CSI transmitter\n");
439 csi_params
->clk_freq
= csi_cfg
.csi_clk_freq
;
440 csi_params
->num_lanes
= csi_cfg
.num_lanes
;
446 gb_camera_set_power_mode(gcam
, false);
448 gb_connection_disable(gcam
->data_connection
);
450 gb_connection_destroy(gcam
->data_connection
);
451 gcam
->data_connection
= NULL
;
455 static void gb_camera_teardown_data_connection(struct gb_camera
*gcam
)
457 struct ap_csi_config_request csi_cfg
;
460 /* Stop the APB1 CSI transmitter. */
461 memset(&csi_cfg
, 0, sizeof(csi_cfg
));
464 ret
= gb_hd_output(gcam
->connection
->hd
, &csi_cfg
,
466 GB_APB_REQUEST_CSI_TX_CONTROL
, false);
469 dev_err(&gcam
->bundle
->dev
, "failed to stop the CSI transmitter\n");
471 /* Set the UniPro link to low speed mode. */
472 gb_camera_set_power_mode(gcam
, false);
474 /* Destroy the data connection. */
475 gb_connection_disable(gcam
->data_connection
);
476 gb_connection_destroy(gcam
->data_connection
);
477 gcam
->data_connection
= NULL
;
480 /* -----------------------------------------------------------------------------
481 * Camera Protocol Operations
484 static int gb_camera_capabilities(struct gb_camera
*gcam
,
485 u8
*capabilities
, size_t *size
)
489 ret
= gb_pm_runtime_get_sync(gcam
->bundle
);
493 mutex_lock(&gcam
->mutex
);
495 if (!gcam
->connection
) {
500 ret
= gb_camera_operation_sync_flags(gcam
->connection
,
501 GB_CAMERA_TYPE_CAPABILITIES
,
502 GB_OPERATION_FLAG_SHORT_RESPONSE
,
504 (void *)capabilities
, size
);
506 dev_err(&gcam
->bundle
->dev
, "failed to retrieve capabilities: %d\n", ret
);
509 mutex_unlock(&gcam
->mutex
);
511 gb_pm_runtime_put_autosuspend(gcam
->bundle
);
516 static int gb_camera_configure_streams(struct gb_camera
*gcam
,
517 unsigned int *num_streams
,
519 struct gb_camera_stream_config
*streams
,
520 struct gb_camera_csi_params
*csi_params
)
522 struct gb_camera_configure_streams_request
*req
;
523 struct gb_camera_configure_streams_response
*resp
;
524 unsigned int nstreams
= *num_streams
;
530 if (nstreams
> GB_CAMERA_MAX_STREAMS
)
533 req_size
= sizeof(*req
) + nstreams
* sizeof(req
->config
[0]);
534 resp_size
= sizeof(*resp
) + nstreams
* sizeof(resp
->config
[0]);
536 req
= kmalloc(req_size
, GFP_KERNEL
);
537 resp
= kmalloc(resp_size
, GFP_KERNEL
);
544 req
->num_streams
= nstreams
;
548 for (i
= 0; i
< nstreams
; ++i
) {
549 struct gb_camera_stream_config_request
*cfg
= &req
->config
[i
];
551 cfg
->width
= cpu_to_le16(streams
[i
].width
);
552 cfg
->height
= cpu_to_le16(streams
[i
].height
);
553 cfg
->format
= cpu_to_le16(streams
[i
].format
);
557 mutex_lock(&gcam
->mutex
);
559 ret
= gb_pm_runtime_get_sync(gcam
->bundle
);
561 goto done_skip_pm_put
;
563 if (!gcam
->connection
) {
568 ret
= gb_camera_operation_sync_flags(gcam
->connection
,
569 GB_CAMERA_TYPE_CONFIGURE_STREAMS
,
570 GB_OPERATION_FLAG_SHORT_RESPONSE
,
576 ret
= gb_camera_configure_streams_validate_response(gcam
, resp
,
581 *flags
= resp
->flags
;
582 *num_streams
= resp
->num_streams
;
584 for (i
= 0; i
< resp
->num_streams
; ++i
) {
585 struct gb_camera_stream_config_response
*cfg
= &resp
->config
[i
];
587 streams
[i
].width
= le16_to_cpu(cfg
->width
);
588 streams
[i
].height
= le16_to_cpu(cfg
->height
);
589 streams
[i
].format
= le16_to_cpu(cfg
->format
);
590 streams
[i
].vc
= cfg
->virtual_channel
;
591 streams
[i
].dt
[0] = cfg
->data_type
[0];
592 streams
[i
].dt
[1] = cfg
->data_type
[1];
593 streams
[i
].max_size
= le32_to_cpu(cfg
->max_size
);
596 if ((resp
->flags
& GB_CAMERA_CONFIGURE_STREAMS_ADJUSTED
) ||
597 (req
->flags
& GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY
))
600 if (gcam
->state
== GB_CAMERA_STATE_CONFIGURED
) {
601 gb_camera_teardown_data_connection(gcam
);
602 gcam
->state
= GB_CAMERA_STATE_UNCONFIGURED
;
605 * When unconfiguring streams release the PM runtime reference
606 * that was acquired when streams were configured. The bundle
607 * won't be suspended until the PM runtime reference acquired at
608 * the beginning of this function gets released right before
611 gb_pm_runtime_put_noidle(gcam
->bundle
);
614 if (resp
->num_streams
== 0)
618 * Make sure the bundle won't be suspended until streams get
619 * unconfigured after the stream is configured successfully
621 gb_pm_runtime_get_noresume(gcam
->bundle
);
623 /* Setup CSI-2 connection from APB-A to AP */
624 ret
= gb_camera_setup_data_connection(gcam
, resp
, csi_params
);
626 memset(req
, 0, sizeof(*req
));
627 gb_operation_sync(gcam
->connection
,
628 GB_CAMERA_TYPE_CONFIGURE_STREAMS
,
630 resp
, sizeof(*resp
));
633 gb_pm_runtime_put_noidle(gcam
->bundle
);
637 gcam
->state
= GB_CAMERA_STATE_CONFIGURED
;
640 gb_pm_runtime_put_autosuspend(gcam
->bundle
);
643 mutex_unlock(&gcam
->mutex
);
649 static int gb_camera_capture(struct gb_camera
*gcam
, u32 request_id
,
650 unsigned int streams
, unsigned int num_frames
,
651 size_t settings_size
, const void *settings
)
653 struct gb_camera_capture_request
*req
;
657 if (settings_size
> GB_CAMERA_MAX_SETTINGS_SIZE
)
660 req_size
= sizeof(*req
) + settings_size
;
661 req
= kmalloc(req_size
, GFP_KERNEL
);
665 req
->request_id
= cpu_to_le32(request_id
);
666 req
->streams
= streams
;
668 req
->num_frames
= cpu_to_le16(num_frames
);
669 memcpy(req
->settings
, settings
, settings_size
);
671 mutex_lock(&gcam
->mutex
);
673 if (!gcam
->connection
) {
678 ret
= gb_operation_sync(gcam
->connection
, GB_CAMERA_TYPE_CAPTURE
,
679 req
, req_size
, NULL
, 0);
681 mutex_unlock(&gcam
->mutex
);
688 static int gb_camera_flush(struct gb_camera
*gcam
, u32
*request_id
)
690 struct gb_camera_flush_response resp
;
693 mutex_lock(&gcam
->mutex
);
695 if (!gcam
->connection
) {
700 ret
= gb_operation_sync(gcam
->connection
, GB_CAMERA_TYPE_FLUSH
, NULL
, 0,
701 &resp
, sizeof(resp
));
707 *request_id
= le32_to_cpu(resp
.request_id
);
710 mutex_unlock(&gcam
->mutex
);
715 static int gb_camera_request_handler(struct gb_operation
*op
)
717 struct gb_camera
*gcam
= gb_connection_get_data(op
->connection
);
718 struct gb_camera_metadata_request
*payload
;
719 struct gb_message
*request
;
721 if (op
->type
!= GB_CAMERA_TYPE_METADATA
) {
722 dev_err(&gcam
->bundle
->dev
, "Unsupported unsolicited event: %u\n", op
->type
);
726 request
= op
->request
;
728 if (request
->payload_size
< sizeof(*payload
)) {
729 dev_err(&gcam
->bundle
->dev
, "Wrong event size received (%zu < %zu)\n",
730 request
->payload_size
, sizeof(*payload
));
734 payload
= request
->payload
;
736 dev_dbg(&gcam
->bundle
->dev
, "received metadata for request %u, frame %u, stream %u\n",
737 payload
->request_id
, payload
->frame_number
, payload
->stream
);
742 /* -----------------------------------------------------------------------------
743 * Interface with HOST gmp camera.
745 static unsigned int gb_camera_mbus_to_gb(enum v4l2_mbus_pixelcode mbus_code
)
749 for (i
= 0; i
< ARRAY_SIZE(gb_fmt_info
); i
++) {
750 if (gb_fmt_info
[i
].mbus_code
== mbus_code
)
751 return gb_fmt_info
[i
].gb_format
;
753 return gb_fmt_info
[0].gb_format
;
756 static enum v4l2_mbus_pixelcode
gb_camera_gb_to_mbus(u16 gb_fmt
)
760 for (i
= 0; i
< ARRAY_SIZE(gb_fmt_info
); i
++) {
761 if (gb_fmt_info
[i
].gb_format
== gb_fmt
)
762 return gb_fmt_info
[i
].mbus_code
;
764 return gb_fmt_info
[0].mbus_code
;
767 static ssize_t
gb_camera_op_capabilities(void *priv
, char *data
, size_t len
)
769 struct gb_camera
*gcam
= priv
;
770 size_t capabilities_len
= len
;
773 ret
= gb_camera_capabilities(gcam
, data
, &capabilities_len
);
777 return capabilities_len
;
780 static int gb_camera_op_configure_streams(void *priv
, unsigned int *nstreams
,
781 unsigned int *flags
, struct gb_camera_stream
*streams
,
782 struct gb_camera_csi_params
*csi_params
)
784 struct gb_camera
*gcam
= priv
;
785 struct gb_camera_stream_config
*gb_streams
;
786 unsigned int gb_flags
= 0;
787 unsigned int gb_nstreams
= *nstreams
;
791 if (gb_nstreams
> GB_CAMERA_MAX_STREAMS
)
794 gb_streams
= kcalloc(gb_nstreams
, sizeof(*gb_streams
), GFP_KERNEL
);
798 for (i
= 0; i
< gb_nstreams
; i
++) {
799 gb_streams
[i
].width
= streams
[i
].width
;
800 gb_streams
[i
].height
= streams
[i
].height
;
801 gb_streams
[i
].format
=
802 gb_camera_mbus_to_gb(streams
[i
].pixel_code
);
805 if (*flags
& GB_CAMERA_IN_FLAG_TEST
)
806 gb_flags
|= GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY
;
808 ret
= gb_camera_configure_streams(gcam
, &gb_nstreams
,
809 &gb_flags
, gb_streams
, csi_params
);
812 if (gb_nstreams
> *nstreams
) {
818 if (gb_flags
& GB_CAMERA_CONFIGURE_STREAMS_ADJUSTED
)
819 *flags
|= GB_CAMERA_OUT_FLAG_ADJUSTED
;
821 for (i
= 0; i
< gb_nstreams
; i
++) {
822 streams
[i
].width
= gb_streams
[i
].width
;
823 streams
[i
].height
= gb_streams
[i
].height
;
824 streams
[i
].vc
= gb_streams
[i
].vc
;
825 streams
[i
].dt
[0] = gb_streams
[i
].dt
[0];
826 streams
[i
].dt
[1] = gb_streams
[i
].dt
[1];
827 streams
[i
].max_size
= gb_streams
[i
].max_size
;
828 streams
[i
].pixel_code
=
829 gb_camera_gb_to_mbus(gb_streams
[i
].format
);
831 *nstreams
= gb_nstreams
;
838 static int gb_camera_op_capture(void *priv
, u32 request_id
,
839 unsigned int streams
, unsigned int num_frames
,
840 size_t settings_size
, const void *settings
)
842 struct gb_camera
*gcam
= priv
;
844 return gb_camera_capture(gcam
, request_id
, streams
, num_frames
,
845 settings_size
, settings
);
848 static int gb_camera_op_flush(void *priv
, u32
*request_id
)
850 struct gb_camera
*gcam
= priv
;
852 return gb_camera_flush(gcam
, request_id
);
855 static const struct gb_camera_ops gb_cam_ops
= {
856 .capabilities
= gb_camera_op_capabilities
,
857 .configure_streams
= gb_camera_op_configure_streams
,
858 .capture
= gb_camera_op_capture
,
859 .flush
= gb_camera_op_flush
,
862 /* -----------------------------------------------------------------------------
866 static ssize_t
gb_camera_debugfs_capabilities(struct gb_camera
*gcam
,
867 char *buf
, size_t len
)
869 struct gb_camera_debugfs_buffer
*buffer
=
870 &gcam
->debugfs
.buffers
[GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES
];
876 caps
= kmalloc(size
, GFP_KERNEL
);
880 ret
= gb_camera_capabilities(gcam
, caps
, &size
);
885 * hex_dump_to_buffer() doesn't return the number of bytes dumped prior
886 * to v4.0, we need our own implementation :-(
890 for (i
= 0; i
< size
; i
+= 16) {
891 unsigned int nbytes
= min_t(unsigned int, size
- i
, 16);
893 buffer
->length
+= sprintf(buffer
->data
+ buffer
->length
,
894 "%*ph\n", nbytes
, caps
+ i
);
902 static ssize_t
gb_camera_debugfs_configure_streams(struct gb_camera
*gcam
,
903 char *buf
, size_t len
)
905 struct gb_camera_debugfs_buffer
*buffer
=
906 &gcam
->debugfs
.buffers
[GB_CAMERA_DEBUGFS_BUFFER_STREAMS
];
907 struct gb_camera_stream_config
*streams
;
908 unsigned int nstreams
;
914 /* Retrieve number of streams to configure */
915 token
= strsep(&buf
, ";");
919 ret
= kstrtouint(token
, 10, &nstreams
);
923 if (nstreams
> GB_CAMERA_MAX_STREAMS
)
926 token
= strsep(&buf
, ";");
930 ret
= kstrtouint(token
, 10, &flags
);
934 /* For each stream to configure parse width, height and format */
935 streams
= kcalloc(nstreams
, sizeof(*streams
), GFP_KERNEL
);
939 for (i
= 0; i
< nstreams
; ++i
) {
940 struct gb_camera_stream_config
*stream
= &streams
[i
];
943 token
= strsep(&buf
, ";");
948 ret
= kstrtouint(token
, 10, &stream
->width
);
953 token
= strsep(&buf
, ";");
957 ret
= kstrtouint(token
, 10, &stream
->height
);
961 /* Image format code */
962 token
= strsep(&buf
, ";");
966 ret
= kstrtouint(token
, 16, &stream
->format
);
971 ret
= gb_camera_configure_streams(gcam
, &nstreams
, &flags
, streams
,
976 buffer
->length
= sprintf(buffer
->data
, "%u;%u;", nstreams
, flags
);
978 for (i
= 0; i
< nstreams
; ++i
) {
979 struct gb_camera_stream_config
*stream
= &streams
[i
];
981 buffer
->length
+= sprintf(buffer
->data
+ buffer
->length
,
982 "%u;%u;%u;%u;%u;%u;%u;",
983 stream
->width
, stream
->height
,
984 stream
->format
, stream
->vc
,
985 stream
->dt
[0], stream
->dt
[1],
996 static ssize_t
gb_camera_debugfs_capture(struct gb_camera
*gcam
,
997 char *buf
, size_t len
)
999 unsigned int request_id
;
1000 unsigned int streams_mask
;
1001 unsigned int num_frames
;
1006 token
= strsep(&buf
, ";");
1009 ret
= kstrtouint(token
, 10, &request_id
);
1014 token
= strsep(&buf
, ";");
1017 ret
= kstrtouint(token
, 16, &streams_mask
);
1021 /* number of frames */
1022 token
= strsep(&buf
, ";");
1025 ret
= kstrtouint(token
, 10, &num_frames
);
1029 ret
= gb_camera_capture(gcam
, request_id
, streams_mask
, num_frames
, 0,
1037 static ssize_t
gb_camera_debugfs_flush(struct gb_camera
*gcam
,
1038 char *buf
, size_t len
)
1040 struct gb_camera_debugfs_buffer
*buffer
=
1041 &gcam
->debugfs
.buffers
[GB_CAMERA_DEBUGFS_BUFFER_FLUSH
];
1042 unsigned int req_id
;
1045 ret
= gb_camera_flush(gcam
, &req_id
);
1049 buffer
->length
= sprintf(buffer
->data
, "%u", req_id
);
1054 struct gb_camera_debugfs_entry
{
1057 unsigned int buffer
;
1058 ssize_t (*execute
)(struct gb_camera
*gcam
, char *buf
, size_t len
);
1061 static const struct gb_camera_debugfs_entry gb_camera_debugfs_entries
[] = {
1063 .name
= "capabilities",
1064 .mask
= S_IFREG
| 0444,
1065 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES
,
1066 .execute
= gb_camera_debugfs_capabilities
,
1068 .name
= "configure_streams",
1069 .mask
= S_IFREG
| 0666,
1070 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_STREAMS
,
1071 .execute
= gb_camera_debugfs_configure_streams
,
1074 .mask
= S_IFREG
| 0666,
1075 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_CAPTURE
,
1076 .execute
= gb_camera_debugfs_capture
,
1079 .mask
= S_IFREG
| 0666,
1080 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_FLUSH
,
1081 .execute
= gb_camera_debugfs_flush
,
1085 static ssize_t
gb_camera_debugfs_read(struct file
*file
, char __user
*buf
,
1086 size_t len
, loff_t
*offset
)
1088 const struct gb_camera_debugfs_entry
*op
= file
->private_data
;
1089 struct gb_camera
*gcam
= file_inode(file
)->i_private
;
1090 struct gb_camera_debugfs_buffer
*buffer
;
1093 /* For read-only entries the operation is triggered by a read. */
1094 if (!(op
->mask
& 0222)) {
1095 ret
= op
->execute(gcam
, NULL
, 0);
1100 buffer
= &gcam
->debugfs
.buffers
[op
->buffer
];
1102 return simple_read_from_buffer(buf
, len
, offset
, buffer
->data
,
1106 static ssize_t
gb_camera_debugfs_write(struct file
*file
,
1107 const char __user
*buf
, size_t len
,
1110 const struct gb_camera_debugfs_entry
*op
= file
->private_data
;
1111 struct gb_camera
*gcam
= file_inode(file
)->i_private
;
1118 kbuf
= memdup_user_nul(buf
, len
);
1120 return PTR_ERR(kbuf
);
1122 ret
= op
->execute(gcam
, kbuf
, len
);
1129 static int gb_camera_debugfs_open(struct inode
*inode
, struct file
*file
)
1133 for (i
= 0; i
< ARRAY_SIZE(gb_camera_debugfs_entries
); ++i
) {
1134 const struct gb_camera_debugfs_entry
*entry
=
1135 &gb_camera_debugfs_entries
[i
];
1137 if (!strcmp(file
->f_path
.dentry
->d_iname
, entry
->name
)) {
1138 file
->private_data
= (void *)entry
;
1146 static const struct file_operations gb_camera_debugfs_ops
= {
1147 .open
= gb_camera_debugfs_open
,
1148 .read
= gb_camera_debugfs_read
,
1149 .write
= gb_camera_debugfs_write
,
1152 static int gb_camera_debugfs_init(struct gb_camera
*gcam
)
1154 struct gb_connection
*connection
= gcam
->connection
;
1159 * Create root debugfs entry and a file entry for each camera operation.
1161 snprintf(dirname
, 27, "camera-%u.%u", connection
->intf
->interface_id
,
1164 gcam
->debugfs
.root
= debugfs_create_dir(dirname
, gb_debugfs_get());
1166 gcam
->debugfs
.buffers
=
1167 vmalloc(array_size(GB_CAMERA_DEBUGFS_BUFFER_MAX
,
1168 sizeof(*gcam
->debugfs
.buffers
)));
1169 if (!gcam
->debugfs
.buffers
)
1172 for (i
= 0; i
< ARRAY_SIZE(gb_camera_debugfs_entries
); ++i
) {
1173 const struct gb_camera_debugfs_entry
*entry
=
1174 &gb_camera_debugfs_entries
[i
];
1176 gcam
->debugfs
.buffers
[i
].length
= 0;
1178 debugfs_create_file(entry
->name
, entry
->mask
,
1179 gcam
->debugfs
.root
, gcam
,
1180 &gb_camera_debugfs_ops
);
1186 static void gb_camera_debugfs_cleanup(struct gb_camera
*gcam
)
1188 debugfs_remove_recursive(gcam
->debugfs
.root
);
1190 vfree(gcam
->debugfs
.buffers
);
1193 /* -----------------------------------------------------------------------------
1197 static void gb_camera_cleanup(struct gb_camera
*gcam
)
1199 gb_camera_debugfs_cleanup(gcam
);
1201 mutex_lock(&gcam
->mutex
);
1202 if (gcam
->data_connection
) {
1203 gb_connection_disable(gcam
->data_connection
);
1204 gb_connection_destroy(gcam
->data_connection
);
1205 gcam
->data_connection
= NULL
;
1208 if (gcam
->connection
) {
1209 gb_connection_disable(gcam
->connection
);
1210 gb_connection_destroy(gcam
->connection
);
1211 gcam
->connection
= NULL
;
1213 mutex_unlock(&gcam
->mutex
);
1216 static void gb_camera_release_module(struct kref
*ref
)
1218 struct gb_camera_module
*cam_mod
=
1219 container_of(ref
, struct gb_camera_module
, refcount
);
1220 kfree(cam_mod
->priv
);
1223 static int gb_camera_probe(struct gb_bundle
*bundle
,
1224 const struct greybus_bundle_id
*id
)
1226 struct gb_connection
*conn
;
1227 struct gb_camera
*gcam
;
1228 u16 mgmt_cport_id
= 0;
1229 u16 data_cport_id
= 0;
1234 * The camera bundle must contain exactly two CPorts, one for the
1235 * camera management protocol and one for the camera data protocol.
1237 if (bundle
->num_cports
!= 2)
1240 for (i
= 0; i
< bundle
->num_cports
; ++i
) {
1241 struct greybus_descriptor_cport
*desc
= &bundle
->cport_desc
[i
];
1243 switch (desc
->protocol_id
) {
1244 case GREYBUS_PROTOCOL_CAMERA_MGMT
:
1245 mgmt_cport_id
= le16_to_cpu(desc
->id
);
1247 case GREYBUS_PROTOCOL_CAMERA_DATA
:
1248 data_cport_id
= le16_to_cpu(desc
->id
);
1255 if (!mgmt_cport_id
|| !data_cport_id
)
1258 gcam
= kzalloc(sizeof(*gcam
), GFP_KERNEL
);
1262 mutex_init(&gcam
->mutex
);
1264 gcam
->bundle
= bundle
;
1265 gcam
->state
= GB_CAMERA_STATE_UNCONFIGURED
;
1266 gcam
->data_cport_id
= data_cport_id
;
1268 conn
= gb_connection_create(bundle
, mgmt_cport_id
,
1269 gb_camera_request_handler
);
1271 ret
= PTR_ERR(conn
);
1275 gcam
->connection
= conn
;
1276 gb_connection_set_data(conn
, gcam
);
1278 ret
= gb_connection_enable(conn
);
1282 ret
= gb_camera_debugfs_init(gcam
);
1286 gcam
->module
.priv
= gcam
;
1287 gcam
->module
.ops
= &gb_cam_ops
;
1288 gcam
->module
.interface_id
= gcam
->connection
->intf
->interface_id
;
1289 gcam
->module
.release
= gb_camera_release_module
;
1290 ret
= gb_camera_register(&gcam
->module
);
1294 greybus_set_drvdata(bundle
, gcam
);
1296 gb_pm_runtime_put_autosuspend(gcam
->bundle
);
1301 gb_camera_cleanup(gcam
);
1306 static void gb_camera_disconnect(struct gb_bundle
*bundle
)
1308 struct gb_camera
*gcam
= greybus_get_drvdata(bundle
);
1311 ret
= gb_pm_runtime_get_sync(bundle
);
1313 gb_pm_runtime_get_noresume(bundle
);
1315 gb_camera_cleanup(gcam
);
1316 gb_camera_unregister(&gcam
->module
);
1319 static const struct greybus_bundle_id gb_camera_id_table
[] = {
1320 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_CAMERA
) },
1325 static int gb_camera_suspend(struct device
*dev
)
1327 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
1328 struct gb_camera
*gcam
= greybus_get_drvdata(bundle
);
1330 if (gcam
->data_connection
)
1331 gb_connection_disable(gcam
->data_connection
);
1333 gb_connection_disable(gcam
->connection
);
1338 static int gb_camera_resume(struct device
*dev
)
1340 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
1341 struct gb_camera
*gcam
= greybus_get_drvdata(bundle
);
1344 ret
= gb_connection_enable(gcam
->connection
);
1346 dev_err(&gcam
->bundle
->dev
, "failed to enable connection: %d\n", ret
);
1350 if (gcam
->data_connection
) {
1351 ret
= gb_connection_enable(gcam
->data_connection
);
1353 dev_err(&gcam
->bundle
->dev
,
1354 "failed to enable data connection: %d\n", ret
);
1363 static const struct dev_pm_ops gb_camera_pm_ops
= {
1364 SET_RUNTIME_PM_OPS(gb_camera_suspend
, gb_camera_resume
, NULL
)
1367 static struct greybus_driver gb_camera_driver
= {
1369 .probe
= gb_camera_probe
,
1370 .disconnect
= gb_camera_disconnect
,
1371 .id_table
= gb_camera_id_table
,
1372 .driver
.pm
= &gb_camera_pm_ops
,
1375 module_greybus_driver(gb_camera_driver
);
1377 MODULE_DESCRIPTION("Greybus Camera protocol driver.");
1378 MODULE_LICENSE("GPL v2");