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 #define gcam_dbg(gcam, format...) dev_dbg(&gcam->bundle->dev, format)
184 #define gcam_info(gcam, format...) dev_info(&gcam->bundle->dev, format)
185 #define gcam_err(gcam, format...) dev_err(&gcam->bundle->dev, format)
187 static int gb_camera_operation_sync_flags(struct gb_connection
*connection
,
188 int type
, unsigned int flags
,
189 void *request
, size_t request_size
,
190 void *response
, size_t *response_size
)
192 struct gb_operation
*operation
;
195 operation
= gb_operation_create_flags(connection
, type
, request_size
,
196 *response_size
, flags
,
202 memcpy(operation
->request
->payload
, request
, request_size
);
204 ret
= gb_operation_request_send_sync(operation
);
206 dev_err(&connection
->hd
->dev
,
207 "%s: synchronous operation of type 0x%02x failed: %d\n",
208 connection
->name
, type
, ret
);
210 *response_size
= operation
->response
->payload_size
;
212 if (operation
->response
->payload_size
)
213 memcpy(response
, operation
->response
->payload
,
214 operation
->response
->payload_size
);
217 gb_operation_put(operation
);
222 static int gb_camera_get_max_pkt_size(struct gb_camera
*gcam
,
223 struct gb_camera_configure_streams_response
*resp
)
225 unsigned int max_pkt_size
= 0;
228 for (i
= 0; i
< resp
->num_streams
; i
++) {
229 struct gb_camera_stream_config_response
*cfg
= &resp
->config
[i
];
230 const struct gb_camera_fmt_info
*fmt_info
;
231 unsigned int pkt_size
;
233 fmt_info
= gb_camera_get_format_info(cfg
->format
);
235 gcam_err(gcam
, "unsupported greybus image format: %d\n",
240 if (fmt_info
->bpp
== 0) {
241 pkt_size
= le32_to_cpu(cfg
->max_pkt_size
);
245 "Stream %u: invalid zero maximum packet size\n",
250 pkt_size
= le16_to_cpu(cfg
->width
) * fmt_info
->bpp
/ 8;
252 if (pkt_size
!= le32_to_cpu(cfg
->max_pkt_size
)) {
254 "Stream %u: maximum packet size mismatch (%u/%u)\n",
255 i
, pkt_size
, cfg
->max_pkt_size
);
260 max_pkt_size
= max(pkt_size
, max_pkt_size
);
267 * Validate the stream configuration response verifying padding is correctly
268 * set and the returned number of streams is supported
270 static const int gb_camera_configure_streams_validate_response(
271 struct gb_camera
*gcam
,
272 struct gb_camera_configure_streams_response
*resp
,
273 unsigned int nstreams
)
277 /* Validate the returned response structure */
278 if (resp
->padding
[0] || resp
->padding
[1]) {
279 gcam_err(gcam
, "response padding != 0\n");
283 if (resp
->num_streams
> nstreams
) {
284 gcam_err(gcam
, "got #streams %u > request %u\n",
285 resp
->num_streams
, nstreams
);
289 for (i
= 0; i
< resp
->num_streams
; i
++) {
290 struct gb_camera_stream_config_response
*cfg
= &resp
->config
[i
];
293 gcam_err(gcam
, "stream #%u padding != 0\n", i
);
301 /* -----------------------------------------------------------------------------
302 * Hardware Configuration
305 static int gb_camera_set_intf_power_mode(struct gb_camera
*gcam
, u8 intf_id
,
308 struct gb_svc
*svc
= gcam
->connection
->hd
->svc
;
312 ret
= gb_svc_intf_set_power_mode(svc
, intf_id
,
313 GB_SVC_UNIPRO_HS_SERIES_A
,
314 GB_SVC_UNIPRO_FAST_MODE
, 2, 2,
315 GB_SVC_SMALL_AMPLITUDE
,
316 GB_SVC_NO_DE_EMPHASIS
,
317 GB_SVC_UNIPRO_FAST_MODE
, 2, 2,
318 GB_SVC_PWRM_RXTERMINATION
|
319 GB_SVC_PWRM_TXTERMINATION
, 0,
322 ret
= gb_svc_intf_set_power_mode(svc
, intf_id
,
323 GB_SVC_UNIPRO_HS_SERIES_A
,
324 GB_SVC_UNIPRO_SLOW_AUTO_MODE
,
326 GB_SVC_SMALL_AMPLITUDE
,
327 GB_SVC_NO_DE_EMPHASIS
,
328 GB_SVC_UNIPRO_SLOW_AUTO_MODE
,
336 static int gb_camera_set_power_mode(struct gb_camera
*gcam
, bool hs
)
338 struct gb_interface
*intf
= gcam
->connection
->intf
;
339 struct gb_svc
*svc
= gcam
->connection
->hd
->svc
;
342 ret
= gb_camera_set_intf_power_mode(gcam
, intf
->interface_id
, hs
);
344 gcam_err(gcam
, "failed to set module interface to %s (%d)\n",
345 hs
? "HS" : "PWM", ret
);
349 ret
= gb_camera_set_intf_power_mode(gcam
, svc
->ap_intf_id
, hs
);
351 gb_camera_set_intf_power_mode(gcam
, intf
->interface_id
, !hs
);
352 gcam_err(gcam
, "failed to set AP interface to %s (%d)\n",
353 hs
? "HS" : "PWM", ret
);
360 struct ap_csi_config_request
{
363 #define GB_CAMERA_CSI_FLAG_CLOCK_CONTINUOUS 0x01
371 * TODO: Compute the number of lanes dynamically based on bandwidth
374 #define GB_CAMERA_CSI_NUM_DATA_LANES 4
376 #define GB_CAMERA_CSI_CLK_FREQ_MAX 999000000U
377 #define GB_CAMERA_CSI_CLK_FREQ_MIN 100000000U
378 #define GB_CAMERA_CSI_CLK_FREQ_MARGIN 150000000U
380 static int gb_camera_setup_data_connection(struct gb_camera
*gcam
,
381 struct gb_camera_configure_streams_response
*resp
,
382 struct gb_camera_csi_params
*csi_params
)
384 struct ap_csi_config_request csi_cfg
;
385 struct gb_connection
*conn
;
386 unsigned int clk_freq
;
390 * Create the data connection between the camera module data CPort and
391 * APB CDSI1. The CDSI1 CPort ID is hardcoded by the ES2 bridge.
393 conn
= gb_connection_create_offloaded(gcam
->bundle
, gcam
->data_cport_id
,
394 GB_CONNECTION_FLAG_NO_FLOWCTRL
|
395 GB_CONNECTION_FLAG_CDSI1
);
397 return PTR_ERR(conn
);
399 gcam
->data_connection
= conn
;
400 gb_connection_set_data(conn
, gcam
);
402 ret
= gb_connection_enable(conn
);
404 goto error_conn_destroy
;
406 /* Set the UniPro link to high speed mode. */
407 ret
= gb_camera_set_power_mode(gcam
, true);
409 goto error_conn_disable
;
412 * Configure the APB-A CSI-2 transmitter.
414 * Hardcode the number of lanes to 4 and compute the bus clock frequency
415 * based on the module bandwidth requirements with a safety margin.
417 memset(&csi_cfg
, 0, sizeof(csi_cfg
));
420 csi_cfg
.num_lanes
= GB_CAMERA_CSI_NUM_DATA_LANES
;
422 clk_freq
= resp
->data_rate
/ 2 / GB_CAMERA_CSI_NUM_DATA_LANES
;
423 clk_freq
= clamp(clk_freq
+ GB_CAMERA_CSI_CLK_FREQ_MARGIN
,
424 GB_CAMERA_CSI_CLK_FREQ_MIN
,
425 GB_CAMERA_CSI_CLK_FREQ_MAX
);
426 csi_cfg
.csi_clk_freq
= clk_freq
;
428 ret
= gb_camera_get_max_pkt_size(gcam
, resp
);
433 csi_cfg
.max_pkt_size
= ret
;
435 ret
= gb_hd_output(gcam
->connection
->hd
, &csi_cfg
,
437 GB_APB_REQUEST_CSI_TX_CONTROL
, false);
439 gcam_err(gcam
, "failed to start the CSI transmitter\n");
444 csi_params
->clk_freq
= csi_cfg
.csi_clk_freq
;
445 csi_params
->num_lanes
= csi_cfg
.num_lanes
;
451 gb_camera_set_power_mode(gcam
, false);
453 gb_connection_disable(gcam
->data_connection
);
455 gb_connection_destroy(gcam
->data_connection
);
456 gcam
->data_connection
= NULL
;
460 static void gb_camera_teardown_data_connection(struct gb_camera
*gcam
)
462 struct ap_csi_config_request csi_cfg
;
465 /* Stop the APB1 CSI transmitter. */
466 memset(&csi_cfg
, 0, sizeof(csi_cfg
));
469 ret
= gb_hd_output(gcam
->connection
->hd
, &csi_cfg
,
471 GB_APB_REQUEST_CSI_TX_CONTROL
, false);
474 gcam_err(gcam
, "failed to stop the CSI transmitter\n");
476 /* Set the UniPro link to low speed mode. */
477 gb_camera_set_power_mode(gcam
, false);
479 /* Destroy the data connection. */
480 gb_connection_disable(gcam
->data_connection
);
481 gb_connection_destroy(gcam
->data_connection
);
482 gcam
->data_connection
= NULL
;
485 /* -----------------------------------------------------------------------------
486 * Camera Protocol Operations
489 static int gb_camera_capabilities(struct gb_camera
*gcam
,
490 u8
*capabilities
, size_t *size
)
494 ret
= gb_pm_runtime_get_sync(gcam
->bundle
);
498 mutex_lock(&gcam
->mutex
);
500 if (!gcam
->connection
) {
505 ret
= gb_camera_operation_sync_flags(gcam
->connection
,
506 GB_CAMERA_TYPE_CAPABILITIES
,
507 GB_OPERATION_FLAG_SHORT_RESPONSE
,
509 (void *)capabilities
, size
);
511 gcam_err(gcam
, "failed to retrieve capabilities: %d\n", ret
);
514 mutex_unlock(&gcam
->mutex
);
516 gb_pm_runtime_put_autosuspend(gcam
->bundle
);
521 static int gb_camera_configure_streams(struct gb_camera
*gcam
,
522 unsigned int *num_streams
,
524 struct gb_camera_stream_config
*streams
,
525 struct gb_camera_csi_params
*csi_params
)
527 struct gb_camera_configure_streams_request
*req
;
528 struct gb_camera_configure_streams_response
*resp
;
529 unsigned int nstreams
= *num_streams
;
535 if (nstreams
> GB_CAMERA_MAX_STREAMS
)
538 req_size
= sizeof(*req
) + nstreams
* sizeof(req
->config
[0]);
539 resp_size
= sizeof(*resp
) + nstreams
* sizeof(resp
->config
[0]);
541 req
= kmalloc(req_size
, GFP_KERNEL
);
542 resp
= kmalloc(resp_size
, GFP_KERNEL
);
549 req
->num_streams
= nstreams
;
553 for (i
= 0; i
< nstreams
; ++i
) {
554 struct gb_camera_stream_config_request
*cfg
= &req
->config
[i
];
556 cfg
->width
= cpu_to_le16(streams
[i
].width
);
557 cfg
->height
= cpu_to_le16(streams
[i
].height
);
558 cfg
->format
= cpu_to_le16(streams
[i
].format
);
562 mutex_lock(&gcam
->mutex
);
564 ret
= gb_pm_runtime_get_sync(gcam
->bundle
);
566 goto done_skip_pm_put
;
568 if (!gcam
->connection
) {
573 ret
= gb_camera_operation_sync_flags(gcam
->connection
,
574 GB_CAMERA_TYPE_CONFIGURE_STREAMS
,
575 GB_OPERATION_FLAG_SHORT_RESPONSE
,
581 ret
= gb_camera_configure_streams_validate_response(gcam
, resp
,
586 *flags
= resp
->flags
;
587 *num_streams
= resp
->num_streams
;
589 for (i
= 0; i
< resp
->num_streams
; ++i
) {
590 struct gb_camera_stream_config_response
*cfg
= &resp
->config
[i
];
592 streams
[i
].width
= le16_to_cpu(cfg
->width
);
593 streams
[i
].height
= le16_to_cpu(cfg
->height
);
594 streams
[i
].format
= le16_to_cpu(cfg
->format
);
595 streams
[i
].vc
= cfg
->virtual_channel
;
596 streams
[i
].dt
[0] = cfg
->data_type
[0];
597 streams
[i
].dt
[1] = cfg
->data_type
[1];
598 streams
[i
].max_size
= le32_to_cpu(cfg
->max_size
);
601 if ((resp
->flags
& GB_CAMERA_CONFIGURE_STREAMS_ADJUSTED
) ||
602 (req
->flags
& GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY
))
605 if (gcam
->state
== GB_CAMERA_STATE_CONFIGURED
) {
606 gb_camera_teardown_data_connection(gcam
);
607 gcam
->state
= GB_CAMERA_STATE_UNCONFIGURED
;
610 * When unconfiguring streams release the PM runtime reference
611 * that was acquired when streams were configured. The bundle
612 * won't be suspended until the PM runtime reference acquired at
613 * the beginning of this function gets released right before
616 gb_pm_runtime_put_noidle(gcam
->bundle
);
619 if (resp
->num_streams
== 0)
623 * Make sure the bundle won't be suspended until streams get
624 * unconfigured after the stream is configured successfully
626 gb_pm_runtime_get_noresume(gcam
->bundle
);
628 /* Setup CSI-2 connection from APB-A to AP */
629 ret
= gb_camera_setup_data_connection(gcam
, resp
, csi_params
);
631 memset(req
, 0, sizeof(*req
));
632 gb_operation_sync(gcam
->connection
,
633 GB_CAMERA_TYPE_CONFIGURE_STREAMS
,
635 resp
, sizeof(*resp
));
638 gb_pm_runtime_put_noidle(gcam
->bundle
);
642 gcam
->state
= GB_CAMERA_STATE_CONFIGURED
;
645 gb_pm_runtime_put_autosuspend(gcam
->bundle
);
648 mutex_unlock(&gcam
->mutex
);
654 static int gb_camera_capture(struct gb_camera
*gcam
, u32 request_id
,
655 unsigned int streams
, unsigned int num_frames
,
656 size_t settings_size
, const void *settings
)
658 struct gb_camera_capture_request
*req
;
662 if (settings_size
> GB_CAMERA_MAX_SETTINGS_SIZE
)
665 req_size
= sizeof(*req
) + settings_size
;
666 req
= kmalloc(req_size
, GFP_KERNEL
);
670 req
->request_id
= cpu_to_le32(request_id
);
671 req
->streams
= streams
;
673 req
->num_frames
= cpu_to_le16(num_frames
);
674 memcpy(req
->settings
, settings
, settings_size
);
676 mutex_lock(&gcam
->mutex
);
678 if (!gcam
->connection
) {
683 ret
= gb_operation_sync(gcam
->connection
, GB_CAMERA_TYPE_CAPTURE
,
684 req
, req_size
, NULL
, 0);
686 mutex_unlock(&gcam
->mutex
);
693 static int gb_camera_flush(struct gb_camera
*gcam
, u32
*request_id
)
695 struct gb_camera_flush_response resp
;
698 mutex_lock(&gcam
->mutex
);
700 if (!gcam
->connection
) {
705 ret
= gb_operation_sync(gcam
->connection
, GB_CAMERA_TYPE_FLUSH
, NULL
, 0,
706 &resp
, sizeof(resp
));
712 *request_id
= le32_to_cpu(resp
.request_id
);
715 mutex_unlock(&gcam
->mutex
);
720 static int gb_camera_request_handler(struct gb_operation
*op
)
722 struct gb_camera
*gcam
= gb_connection_get_data(op
->connection
);
723 struct gb_camera_metadata_request
*payload
;
724 struct gb_message
*request
;
726 if (op
->type
!= GB_CAMERA_TYPE_METADATA
) {
727 gcam_err(gcam
, "Unsupported unsolicited event: %u\n", op
->type
);
731 request
= op
->request
;
733 if (request
->payload_size
< sizeof(*payload
)) {
734 gcam_err(gcam
, "Wrong event size received (%zu < %zu)\n",
735 request
->payload_size
, sizeof(*payload
));
739 payload
= request
->payload
;
741 gcam_dbg(gcam
, "received metadata for request %u, frame %u, stream %u\n",
742 payload
->request_id
, payload
->frame_number
, payload
->stream
);
747 /* -----------------------------------------------------------------------------
748 * Interface with HOST gmp camera.
750 static unsigned int gb_camera_mbus_to_gb(enum v4l2_mbus_pixelcode mbus_code
)
754 for (i
= 0; i
< ARRAY_SIZE(gb_fmt_info
); i
++) {
755 if (gb_fmt_info
[i
].mbus_code
== mbus_code
)
756 return gb_fmt_info
[i
].gb_format
;
758 return gb_fmt_info
[0].gb_format
;
761 static enum v4l2_mbus_pixelcode
gb_camera_gb_to_mbus(u16 gb_fmt
)
765 for (i
= 0; i
< ARRAY_SIZE(gb_fmt_info
); i
++) {
766 if (gb_fmt_info
[i
].gb_format
== gb_fmt
)
767 return gb_fmt_info
[i
].mbus_code
;
769 return gb_fmt_info
[0].mbus_code
;
772 static ssize_t
gb_camera_op_capabilities(void *priv
, char *data
, size_t len
)
774 struct gb_camera
*gcam
= priv
;
775 size_t capabilities_len
= len
;
778 ret
= gb_camera_capabilities(gcam
, data
, &capabilities_len
);
782 return capabilities_len
;
785 static int gb_camera_op_configure_streams(void *priv
, unsigned int *nstreams
,
786 unsigned int *flags
, struct gb_camera_stream
*streams
,
787 struct gb_camera_csi_params
*csi_params
)
789 struct gb_camera
*gcam
= priv
;
790 struct gb_camera_stream_config
*gb_streams
;
791 unsigned int gb_flags
= 0;
792 unsigned int gb_nstreams
= *nstreams
;
796 if (gb_nstreams
> GB_CAMERA_MAX_STREAMS
)
799 gb_streams
= kcalloc(gb_nstreams
, sizeof(*gb_streams
), GFP_KERNEL
);
803 for (i
= 0; i
< gb_nstreams
; i
++) {
804 gb_streams
[i
].width
= streams
[i
].width
;
805 gb_streams
[i
].height
= streams
[i
].height
;
806 gb_streams
[i
].format
=
807 gb_camera_mbus_to_gb(streams
[i
].pixel_code
);
810 if (*flags
& GB_CAMERA_IN_FLAG_TEST
)
811 gb_flags
|= GB_CAMERA_CONFIGURE_STREAMS_TEST_ONLY
;
813 ret
= gb_camera_configure_streams(gcam
, &gb_nstreams
,
814 &gb_flags
, gb_streams
, csi_params
);
817 if (gb_nstreams
> *nstreams
) {
823 if (gb_flags
& GB_CAMERA_CONFIGURE_STREAMS_ADJUSTED
)
824 *flags
|= GB_CAMERA_OUT_FLAG_ADJUSTED
;
826 for (i
= 0; i
< gb_nstreams
; i
++) {
827 streams
[i
].width
= gb_streams
[i
].width
;
828 streams
[i
].height
= gb_streams
[i
].height
;
829 streams
[i
].vc
= gb_streams
[i
].vc
;
830 streams
[i
].dt
[0] = gb_streams
[i
].dt
[0];
831 streams
[i
].dt
[1] = gb_streams
[i
].dt
[1];
832 streams
[i
].max_size
= gb_streams
[i
].max_size
;
833 streams
[i
].pixel_code
=
834 gb_camera_gb_to_mbus(gb_streams
[i
].format
);
836 *nstreams
= gb_nstreams
;
843 static int gb_camera_op_capture(void *priv
, u32 request_id
,
844 unsigned int streams
, unsigned int num_frames
,
845 size_t settings_size
, const void *settings
)
847 struct gb_camera
*gcam
= priv
;
849 return gb_camera_capture(gcam
, request_id
, streams
, num_frames
,
850 settings_size
, settings
);
853 static int gb_camera_op_flush(void *priv
, u32
*request_id
)
855 struct gb_camera
*gcam
= priv
;
857 return gb_camera_flush(gcam
, request_id
);
860 static const struct gb_camera_ops gb_cam_ops
= {
861 .capabilities
= gb_camera_op_capabilities
,
862 .configure_streams
= gb_camera_op_configure_streams
,
863 .capture
= gb_camera_op_capture
,
864 .flush
= gb_camera_op_flush
,
867 /* -----------------------------------------------------------------------------
871 static ssize_t
gb_camera_debugfs_capabilities(struct gb_camera
*gcam
,
872 char *buf
, size_t len
)
874 struct gb_camera_debugfs_buffer
*buffer
=
875 &gcam
->debugfs
.buffers
[GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES
];
881 caps
= kmalloc(size
, GFP_KERNEL
);
885 ret
= gb_camera_capabilities(gcam
, caps
, &size
);
890 * hex_dump_to_buffer() doesn't return the number of bytes dumped prior
891 * to v4.0, we need our own implementation :-(
895 for (i
= 0; i
< size
; i
+= 16) {
896 unsigned int nbytes
= min_t(unsigned int, size
- i
, 16);
898 buffer
->length
+= sprintf(buffer
->data
+ buffer
->length
,
899 "%*ph\n", nbytes
, caps
+ i
);
907 static ssize_t
gb_camera_debugfs_configure_streams(struct gb_camera
*gcam
,
908 char *buf
, size_t len
)
910 struct gb_camera_debugfs_buffer
*buffer
=
911 &gcam
->debugfs
.buffers
[GB_CAMERA_DEBUGFS_BUFFER_STREAMS
];
912 struct gb_camera_stream_config
*streams
;
913 unsigned int nstreams
;
919 /* Retrieve number of streams to configure */
920 token
= strsep(&buf
, ";");
924 ret
= kstrtouint(token
, 10, &nstreams
);
928 if (nstreams
> GB_CAMERA_MAX_STREAMS
)
931 token
= strsep(&buf
, ";");
935 ret
= kstrtouint(token
, 10, &flags
);
939 /* For each stream to configure parse width, height and format */
940 streams
= kcalloc(nstreams
, sizeof(*streams
), GFP_KERNEL
);
944 for (i
= 0; i
< nstreams
; ++i
) {
945 struct gb_camera_stream_config
*stream
= &streams
[i
];
948 token
= strsep(&buf
, ";");
953 ret
= kstrtouint(token
, 10, &stream
->width
);
958 token
= strsep(&buf
, ";");
962 ret
= kstrtouint(token
, 10, &stream
->height
);
966 /* Image format code */
967 token
= strsep(&buf
, ";");
971 ret
= kstrtouint(token
, 16, &stream
->format
);
976 ret
= gb_camera_configure_streams(gcam
, &nstreams
, &flags
, streams
,
981 buffer
->length
= sprintf(buffer
->data
, "%u;%u;", nstreams
, flags
);
983 for (i
= 0; i
< nstreams
; ++i
) {
984 struct gb_camera_stream_config
*stream
= &streams
[i
];
986 buffer
->length
+= sprintf(buffer
->data
+ buffer
->length
,
987 "%u;%u;%u;%u;%u;%u;%u;",
988 stream
->width
, stream
->height
,
989 stream
->format
, stream
->vc
,
990 stream
->dt
[0], stream
->dt
[1],
1001 static ssize_t
gb_camera_debugfs_capture(struct gb_camera
*gcam
,
1002 char *buf
, size_t len
)
1004 unsigned int request_id
;
1005 unsigned int streams_mask
;
1006 unsigned int num_frames
;
1011 token
= strsep(&buf
, ";");
1014 ret
= kstrtouint(token
, 10, &request_id
);
1019 token
= strsep(&buf
, ";");
1022 ret
= kstrtouint(token
, 16, &streams_mask
);
1026 /* number of frames */
1027 token
= strsep(&buf
, ";");
1030 ret
= kstrtouint(token
, 10, &num_frames
);
1034 ret
= gb_camera_capture(gcam
, request_id
, streams_mask
, num_frames
, 0,
1042 static ssize_t
gb_camera_debugfs_flush(struct gb_camera
*gcam
,
1043 char *buf
, size_t len
)
1045 struct gb_camera_debugfs_buffer
*buffer
=
1046 &gcam
->debugfs
.buffers
[GB_CAMERA_DEBUGFS_BUFFER_FLUSH
];
1047 unsigned int req_id
;
1050 ret
= gb_camera_flush(gcam
, &req_id
);
1054 buffer
->length
= sprintf(buffer
->data
, "%u", req_id
);
1059 struct gb_camera_debugfs_entry
{
1062 unsigned int buffer
;
1063 ssize_t (*execute
)(struct gb_camera
*gcam
, char *buf
, size_t len
);
1066 static const struct gb_camera_debugfs_entry gb_camera_debugfs_entries
[] = {
1068 .name
= "capabilities",
1069 .mask
= S_IFREG
| 0444,
1070 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_CAPABILITIES
,
1071 .execute
= gb_camera_debugfs_capabilities
,
1073 .name
= "configure_streams",
1074 .mask
= S_IFREG
| 0666,
1075 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_STREAMS
,
1076 .execute
= gb_camera_debugfs_configure_streams
,
1079 .mask
= S_IFREG
| 0666,
1080 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_CAPTURE
,
1081 .execute
= gb_camera_debugfs_capture
,
1084 .mask
= S_IFREG
| 0666,
1085 .buffer
= GB_CAMERA_DEBUGFS_BUFFER_FLUSH
,
1086 .execute
= gb_camera_debugfs_flush
,
1090 static ssize_t
gb_camera_debugfs_read(struct file
*file
, char __user
*buf
,
1091 size_t len
, loff_t
*offset
)
1093 const struct gb_camera_debugfs_entry
*op
= file
->private_data
;
1094 struct gb_camera
*gcam
= file_inode(file
)->i_private
;
1095 struct gb_camera_debugfs_buffer
*buffer
;
1098 /* For read-only entries the operation is triggered by a read. */
1099 if (!(op
->mask
& 0222)) {
1100 ret
= op
->execute(gcam
, NULL
, 0);
1105 buffer
= &gcam
->debugfs
.buffers
[op
->buffer
];
1107 return simple_read_from_buffer(buf
, len
, offset
, buffer
->data
,
1111 static ssize_t
gb_camera_debugfs_write(struct file
*file
,
1112 const char __user
*buf
, size_t len
,
1115 const struct gb_camera_debugfs_entry
*op
= file
->private_data
;
1116 struct gb_camera
*gcam
= file_inode(file
)->i_private
;
1123 kbuf
= kmalloc(len
+ 1, GFP_KERNEL
);
1127 if (copy_from_user(kbuf
, buf
, len
)) {
1134 ret
= op
->execute(gcam
, kbuf
, len
);
1141 static int gb_camera_debugfs_open(struct inode
*inode
, struct file
*file
)
1145 for (i
= 0; i
< ARRAY_SIZE(gb_camera_debugfs_entries
); ++i
) {
1146 const struct gb_camera_debugfs_entry
*entry
=
1147 &gb_camera_debugfs_entries
[i
];
1149 if (!strcmp(file
->f_path
.dentry
->d_iname
, entry
->name
)) {
1150 file
->private_data
= (void *)entry
;
1158 static const struct file_operations gb_camera_debugfs_ops
= {
1159 .open
= gb_camera_debugfs_open
,
1160 .read
= gb_camera_debugfs_read
,
1161 .write
= gb_camera_debugfs_write
,
1164 static int gb_camera_debugfs_init(struct gb_camera
*gcam
)
1166 struct gb_connection
*connection
= gcam
->connection
;
1171 * Create root debugfs entry and a file entry for each camera operation.
1173 snprintf(dirname
, 27, "camera-%u.%u", connection
->intf
->interface_id
,
1176 gcam
->debugfs
.root
= debugfs_create_dir(dirname
, gb_debugfs_get());
1178 gcam
->debugfs
.buffers
=
1179 vmalloc(array_size(GB_CAMERA_DEBUGFS_BUFFER_MAX
,
1180 sizeof(*gcam
->debugfs
.buffers
)));
1181 if (!gcam
->debugfs
.buffers
)
1184 for (i
= 0; i
< ARRAY_SIZE(gb_camera_debugfs_entries
); ++i
) {
1185 const struct gb_camera_debugfs_entry
*entry
=
1186 &gb_camera_debugfs_entries
[i
];
1188 gcam
->debugfs
.buffers
[i
].length
= 0;
1190 debugfs_create_file(entry
->name
, entry
->mask
,
1191 gcam
->debugfs
.root
, gcam
,
1192 &gb_camera_debugfs_ops
);
1198 static void gb_camera_debugfs_cleanup(struct gb_camera
*gcam
)
1200 debugfs_remove_recursive(gcam
->debugfs
.root
);
1202 vfree(gcam
->debugfs
.buffers
);
1205 /* -----------------------------------------------------------------------------
1209 static void gb_camera_cleanup(struct gb_camera
*gcam
)
1211 gb_camera_debugfs_cleanup(gcam
);
1213 mutex_lock(&gcam
->mutex
);
1214 if (gcam
->data_connection
) {
1215 gb_connection_disable(gcam
->data_connection
);
1216 gb_connection_destroy(gcam
->data_connection
);
1217 gcam
->data_connection
= NULL
;
1220 if (gcam
->connection
) {
1221 gb_connection_disable(gcam
->connection
);
1222 gb_connection_destroy(gcam
->connection
);
1223 gcam
->connection
= NULL
;
1225 mutex_unlock(&gcam
->mutex
);
1228 static void gb_camera_release_module(struct kref
*ref
)
1230 struct gb_camera_module
*cam_mod
=
1231 container_of(ref
, struct gb_camera_module
, refcount
);
1232 kfree(cam_mod
->priv
);
1235 static int gb_camera_probe(struct gb_bundle
*bundle
,
1236 const struct greybus_bundle_id
*id
)
1238 struct gb_connection
*conn
;
1239 struct gb_camera
*gcam
;
1240 u16 mgmt_cport_id
= 0;
1241 u16 data_cport_id
= 0;
1246 * The camera bundle must contain exactly two CPorts, one for the
1247 * camera management protocol and one for the camera data protocol.
1249 if (bundle
->num_cports
!= 2)
1252 for (i
= 0; i
< bundle
->num_cports
; ++i
) {
1253 struct greybus_descriptor_cport
*desc
= &bundle
->cport_desc
[i
];
1255 switch (desc
->protocol_id
) {
1256 case GREYBUS_PROTOCOL_CAMERA_MGMT
:
1257 mgmt_cport_id
= le16_to_cpu(desc
->id
);
1259 case GREYBUS_PROTOCOL_CAMERA_DATA
:
1260 data_cport_id
= le16_to_cpu(desc
->id
);
1267 if (!mgmt_cport_id
|| !data_cport_id
)
1270 gcam
= kzalloc(sizeof(*gcam
), GFP_KERNEL
);
1274 mutex_init(&gcam
->mutex
);
1276 gcam
->bundle
= bundle
;
1277 gcam
->state
= GB_CAMERA_STATE_UNCONFIGURED
;
1278 gcam
->data_cport_id
= data_cport_id
;
1280 conn
= gb_connection_create(bundle
, mgmt_cport_id
,
1281 gb_camera_request_handler
);
1283 ret
= PTR_ERR(conn
);
1287 gcam
->connection
= conn
;
1288 gb_connection_set_data(conn
, gcam
);
1290 ret
= gb_connection_enable(conn
);
1294 ret
= gb_camera_debugfs_init(gcam
);
1298 gcam
->module
.priv
= gcam
;
1299 gcam
->module
.ops
= &gb_cam_ops
;
1300 gcam
->module
.interface_id
= gcam
->connection
->intf
->interface_id
;
1301 gcam
->module
.release
= gb_camera_release_module
;
1302 ret
= gb_camera_register(&gcam
->module
);
1306 greybus_set_drvdata(bundle
, gcam
);
1308 gb_pm_runtime_put_autosuspend(gcam
->bundle
);
1313 gb_camera_cleanup(gcam
);
1318 static void gb_camera_disconnect(struct gb_bundle
*bundle
)
1320 struct gb_camera
*gcam
= greybus_get_drvdata(bundle
);
1323 ret
= gb_pm_runtime_get_sync(bundle
);
1325 gb_pm_runtime_get_noresume(bundle
);
1327 gb_camera_cleanup(gcam
);
1328 gb_camera_unregister(&gcam
->module
);
1331 static const struct greybus_bundle_id gb_camera_id_table
[] = {
1332 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_CAMERA
) },
1337 static int gb_camera_suspend(struct device
*dev
)
1339 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
1340 struct gb_camera
*gcam
= greybus_get_drvdata(bundle
);
1342 if (gcam
->data_connection
)
1343 gb_connection_disable(gcam
->data_connection
);
1345 gb_connection_disable(gcam
->connection
);
1350 static int gb_camera_resume(struct device
*dev
)
1352 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
1353 struct gb_camera
*gcam
= greybus_get_drvdata(bundle
);
1356 ret
= gb_connection_enable(gcam
->connection
);
1358 gcam_err(gcam
, "failed to enable connection: %d\n", ret
);
1362 if (gcam
->data_connection
) {
1363 ret
= gb_connection_enable(gcam
->data_connection
);
1366 "failed to enable data connection: %d\n", ret
);
1375 static const struct dev_pm_ops gb_camera_pm_ops
= {
1376 SET_RUNTIME_PM_OPS(gb_camera_suspend
, gb_camera_resume
, NULL
)
1379 static struct greybus_driver gb_camera_driver
= {
1381 .probe
= gb_camera_probe
,
1382 .disconnect
= gb_camera_disconnect
,
1383 .id_table
= gb_camera_id_table
,
1384 .driver
.pm
= &gb_camera_pm_ops
,
1387 module_greybus_driver(gb_camera_driver
);
1389 MODULE_LICENSE("GPL v2");