1+ // SPDX-License-Identifier: GPL-2.0
12/*
23 * ov772x Camera Driver
34 *
5+ * Copyright (C) 2017 Jacopo Mondi <jacopo+renesas@jmondi.org>
6+ *
47 * Copyright (C) 2008 Renesas Solutions Corp.
58 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
69 *
912 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
1013 * Copyright (C) 2008 Magnus Damm
1114 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
12- *
13- * This program is free software; you can redistribute it and/or modify
14- * it under the terms of the GNU General Public License version 2 as
15- * published by the Free Software Foundation.
1615 */
1716
17+ #include <linux/clk.h>
18+ #include <linux/delay.h>
19+ #include <linux/gpio/consumer.h>
20+ #include <linux/i2c.h>
1821#include <linux/init.h>
1922#include <linux/kernel.h>
2023#include <linux/module.h>
21- #include <linux/i2c.h>
2224#include <linux/slab.h>
23- #include <linux/delay.h>
2425#include <linux/v4l2-mediabus.h>
2526#include <linux/videodev2.h>
2627
2728#include <media/i2c/ov772x.h>
28- #include <media/soc_camera.h>
29- #include <media/v4l2-clk.h>
29+
3030#include <media/v4l2-ctrls.h>
31- #include <media/v4l2-subdev .h>
31+ #include <media/v4l2-device .h>
3232#include <media/v4l2-image-sizes.h>
33+ #include <media/v4l2-subdev.h>
3334
3435/*
3536 * register offset
@@ -393,8 +394,10 @@ struct ov772x_win_size {
393394struct ov772x_priv {
394395 struct v4l2_subdev subdev ;
395396 struct v4l2_ctrl_handler hdl ;
396- struct v4l2_clk * clk ;
397+ struct clk * clk ;
397398 struct ov772x_camera_info * info ;
399+ struct gpio_desc * pwdn_gpio ;
400+ struct gpio_desc * rstb_gpio ;
398401 const struct ov772x_color_format * cfmt ;
399402 const struct ov772x_win_size * win ;
400403 unsigned short flag_vflip :1 ;
@@ -409,23 +412,23 @@ struct ov772x_priv {
409412static const struct ov772x_color_format ov772x_cfmts [] = {
410413 {
411414 .code = MEDIA_BUS_FMT_YUYV8_2X8 ,
412- .colorspace = V4L2_COLORSPACE_JPEG ,
415+ .colorspace = V4L2_COLORSPACE_SRGB ,
413416 .dsp3 = 0x0 ,
414417 .dsp4 = DSP_OFMT_YUV ,
415418 .com3 = SWAP_YUV ,
416419 .com7 = OFMT_YUV ,
417420 },
418421 {
419422 .code = MEDIA_BUS_FMT_YVYU8_2X8 ,
420- .colorspace = V4L2_COLORSPACE_JPEG ,
423+ .colorspace = V4L2_COLORSPACE_SRGB ,
421424 .dsp3 = UV_ON ,
422425 .dsp4 = DSP_OFMT_YUV ,
423426 .com3 = SWAP_YUV ,
424427 .com7 = OFMT_YUV ,
425428 },
426429 {
427430 .code = MEDIA_BUS_FMT_UYVY8_2X8 ,
428- .colorspace = V4L2_COLORSPACE_JPEG ,
431+ .colorspace = V4L2_COLORSPACE_SRGB ,
429432 .dsp3 = 0x0 ,
430433 .dsp4 = DSP_OFMT_YUV ,
431434 .com3 = 0x0 ,
@@ -550,7 +553,7 @@ static int ov772x_reset(struct i2c_client *client)
550553}
551554
552555/*
553- * soc_camera_ops function
556+ * subdev ops
554557 */
555558
556559static int ov772x_s_stream (struct v4l2_subdev * sd , int enable )
@@ -650,13 +653,65 @@ static int ov772x_s_register(struct v4l2_subdev *sd,
650653}
651654#endif
652655
656+ static int ov772x_power_on (struct ov772x_priv * priv )
657+ {
658+ struct i2c_client * client = v4l2_get_subdevdata (& priv -> subdev );
659+ int ret ;
660+
661+ if (priv -> clk ) {
662+ ret = clk_prepare_enable (priv -> clk );
663+ if (ret )
664+ return ret ;
665+ }
666+
667+ if (priv -> pwdn_gpio ) {
668+ gpiod_set_value (priv -> pwdn_gpio , 1 );
669+ usleep_range (500 , 1000 );
670+ }
671+
672+ /*
673+ * FIXME: The reset signal is connected to a shared GPIO on some
674+ * platforms (namely the SuperH Migo-R). Until a framework becomes
675+ * available to handle this cleanly, request the GPIO temporarily
676+ * to avoid conflicts.
677+ */
678+ priv -> rstb_gpio = gpiod_get_optional (& client -> dev , "rstb" ,
679+ GPIOD_OUT_LOW );
680+ if (IS_ERR (priv -> rstb_gpio )) {
681+ dev_info (& client -> dev , "Unable to get GPIO \"rstb\"" );
682+ return PTR_ERR (priv -> rstb_gpio );
683+ }
684+
685+ if (priv -> rstb_gpio ) {
686+ gpiod_set_value (priv -> rstb_gpio , 1 );
687+ usleep_range (500 , 1000 );
688+ gpiod_set_value (priv -> rstb_gpio , 0 );
689+ usleep_range (500 , 1000 );
690+
691+ gpiod_put (priv -> rstb_gpio );
692+ }
693+
694+ return 0 ;
695+ }
696+
697+ static int ov772x_power_off (struct ov772x_priv * priv )
698+ {
699+ clk_disable_unprepare (priv -> clk );
700+
701+ if (priv -> pwdn_gpio ) {
702+ gpiod_set_value (priv -> pwdn_gpio , 0 );
703+ usleep_range (500 , 1000 );
704+ }
705+
706+ return 0 ;
707+ }
708+
653709static int ov772x_s_power (struct v4l2_subdev * sd , int on )
654710{
655- struct i2c_client * client = v4l2_get_subdevdata (sd );
656- struct soc_camera_subdev_desc * ssdd = soc_camera_i2c_to_desc (client );
657711 struct ov772x_priv * priv = to_ov772x (sd );
658712
659- return soc_camera_set_power (& client -> dev , ssdd , priv -> clk , on );
713+ return on ? ov772x_power_on (priv ) :
714+ ov772x_power_off (priv );
660715}
661716
662717static const struct ov772x_win_size * ov772x_select_win (u32 width , u32 height )
@@ -855,6 +910,8 @@ static int ov772x_get_selection(struct v4l2_subdev *sd,
855910 struct v4l2_subdev_pad_config * cfg ,
856911 struct v4l2_subdev_selection * sel )
857912{
913+ struct ov772x_priv * priv = to_ov772x (sd );
914+
858915 if (sel -> which != V4L2_SUBDEV_FORMAT_ACTIVE )
859916 return - EINVAL ;
860917
@@ -863,12 +920,9 @@ static int ov772x_get_selection(struct v4l2_subdev *sd,
863920 switch (sel -> target ) {
864921 case V4L2_SEL_TGT_CROP_BOUNDS :
865922 case V4L2_SEL_TGT_CROP_DEFAULT :
866- sel -> r .width = OV772X_MAX_WIDTH ;
867- sel -> r .height = OV772X_MAX_HEIGHT ;
868- return 0 ;
869923 case V4L2_SEL_TGT_CROP :
870- sel -> r .width = VGA_WIDTH ;
871- sel -> r .height = VGA_HEIGHT ;
924+ sel -> r .width = priv -> win -> rect . width ;
925+ sel -> r .height = priv -> win -> rect . height ;
872926 return 0 ;
873927 default :
874928 return - EINVAL ;
@@ -914,6 +968,9 @@ static int ov772x_set_fmt(struct v4l2_subdev *sd,
914968 mf -> height = win -> rect .height ;
915969 mf -> field = V4L2_FIELD_NONE ;
916970 mf -> colorspace = cfmt -> colorspace ;
971+ mf -> ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT ;
972+ mf -> quantization = V4L2_QUANTIZATION_DEFAULT ;
973+ mf -> xfer_func = V4L2_XFER_FUNC_DEFAULT ;
917974
918975 if (format -> which == V4L2_SUBDEV_FORMAT_TRY ) {
919976 cfg -> try_fmt = * mf ;
@@ -997,24 +1054,8 @@ static int ov772x_enum_mbus_code(struct v4l2_subdev *sd,
9971054 return 0 ;
9981055}
9991056
1000- static int ov772x_g_mbus_config (struct v4l2_subdev * sd ,
1001- struct v4l2_mbus_config * cfg )
1002- {
1003- struct i2c_client * client = v4l2_get_subdevdata (sd );
1004- struct soc_camera_subdev_desc * ssdd = soc_camera_i2c_to_desc (client );
1005-
1006- cfg -> flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
1007- V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
1008- V4L2_MBUS_DATA_ACTIVE_HIGH ;
1009- cfg -> type = V4L2_MBUS_PARALLEL ;
1010- cfg -> flags = soc_camera_apply_board_flags (ssdd , cfg );
1011-
1012- return 0 ;
1013- }
1014-
10151057static const struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
10161058 .s_stream = ov772x_s_stream ,
1017- .g_mbus_config = ov772x_g_mbus_config ,
10181059};
10191060
10201061static const struct v4l2_subdev_pad_ops ov772x_subdev_pad_ops = {
@@ -1038,12 +1079,11 @@ static int ov772x_probe(struct i2c_client *client,
10381079 const struct i2c_device_id * did )
10391080{
10401081 struct ov772x_priv * priv ;
1041- struct soc_camera_subdev_desc * ssdd = soc_camera_i2c_to_desc (client );
1042- struct i2c_adapter * adapter = to_i2c_adapter (client -> dev .parent );
1082+ struct i2c_adapter * adapter = client -> adapter ;
10431083 int ret ;
10441084
1045- if (!ssdd || ! ssdd -> drv_priv ) {
1046- dev_err (& client -> dev , "OV772X: missing platform data! \n" );
1085+ if (!client -> dev . platform_data ) {
1086+ dev_err (& client -> dev , "Missing ov772x platform data\n" );
10471087 return - EINVAL ;
10481088 }
10491089
@@ -1059,7 +1099,7 @@ static int ov772x_probe(struct i2c_client *client,
10591099 if (!priv )
10601100 return - ENOMEM ;
10611101
1062- priv -> info = ssdd -> drv_priv ;
1102+ priv -> info = client -> dev . platform_data ;
10631103
10641104 v4l2_i2c_subdev_init (& priv -> subdev , client , & ov772x_subdev_ops );
10651105 v4l2_ctrl_handler_init (& priv -> hdl , 3 );
@@ -1073,30 +1113,52 @@ static int ov772x_probe(struct i2c_client *client,
10731113 if (priv -> hdl .error )
10741114 return priv -> hdl .error ;
10751115
1076- priv -> clk = v4l2_clk_get (& client -> dev , "mclk " );
1116+ priv -> clk = clk_get (& client -> dev , "xclk " );
10771117 if (IS_ERR (priv -> clk )) {
1118+ dev_err (& client -> dev , "Unable to get xclk clock\n" );
10781119 ret = PTR_ERR (priv -> clk );
1079- goto eclkget ;
1120+ goto error_ctrl_free ;
10801121 }
10811122
1082- ret = ov772x_video_probe (priv );
1083- if (ret < 0 ) {
1084- v4l2_clk_put (priv -> clk );
1085- eclkget :
1086- v4l2_ctrl_handler_free (& priv -> hdl );
1087- } else {
1088- priv -> cfmt = & ov772x_cfmts [0 ];
1089- priv -> win = & ov772x_win_sizes [0 ];
1123+ priv -> pwdn_gpio = gpiod_get_optional (& client -> dev , "pwdn" ,
1124+ GPIOD_OUT_LOW );
1125+ if (IS_ERR (priv -> pwdn_gpio )) {
1126+ dev_info (& client -> dev , "Unable to get GPIO \"pwdn\"" );
1127+ ret = PTR_ERR (priv -> pwdn_gpio );
1128+ goto error_clk_put ;
10901129 }
10911130
1131+ ret = ov772x_video_probe (priv );
1132+ if (ret < 0 )
1133+ goto error_gpio_put ;
1134+
1135+ priv -> cfmt = & ov772x_cfmts [0 ];
1136+ priv -> win = & ov772x_win_sizes [0 ];
1137+
1138+ ret = v4l2_async_register_subdev (& priv -> subdev );
1139+ if (ret )
1140+ goto error_gpio_put ;
1141+
1142+ return 0 ;
1143+
1144+ error_gpio_put :
1145+ if (priv -> pwdn_gpio )
1146+ gpiod_put (priv -> pwdn_gpio );
1147+ error_clk_put :
1148+ clk_put (priv -> clk );
1149+ error_ctrl_free :
1150+ v4l2_ctrl_handler_free (& priv -> hdl );
1151+
10921152 return ret ;
10931153}
10941154
10951155static int ov772x_remove (struct i2c_client * client )
10961156{
10971157 struct ov772x_priv * priv = to_ov772x (i2c_get_clientdata (client ));
10981158
1099- v4l2_clk_put (priv -> clk );
1159+ clk_put (priv -> clk );
1160+ if (priv -> pwdn_gpio )
1161+ gpiod_put (priv -> pwdn_gpio );
11001162 v4l2_device_unregister_subdev (& priv -> subdev );
11011163 v4l2_ctrl_handler_free (& priv -> hdl );
11021164 return 0 ;
@@ -1119,6 +1181,6 @@ static struct i2c_driver ov772x_i2c_driver = {
11191181
11201182module_i2c_driver (ov772x_i2c_driver );
11211183
1122- MODULE_DESCRIPTION ("SoC Camera driver for ov772x " );
1184+ MODULE_DESCRIPTION ("V4L2 driver for OV772x image sensor " );
11231185MODULE_AUTHOR ("Kuninori Morimoto" );
11241186MODULE_LICENSE ("GPL v2" );
0 commit comments