22// Copyright (c) 2017 Intel Corporation.
33
44#include <linux/acpi.h>
5+ #include <linux/delay.h>
6+ #include <linux/clk.h>
57#include <linux/i2c.h>
68#include <linux/module.h>
79#include <linux/pm_runtime.h>
@@ -118,6 +120,14 @@ struct ov13858_mode {
118120 struct ov13858_reg_list reg_list ;
119121};
120122
123+ // Or use standard names that might be more correct:
124+ static const char * const ov13858_supply_names [] = {
125+ "avdd" , // Analog voltage
126+ "pwr1" , // Digital core voltage
127+ };
128+
129+ #define OV13858_NUM_SUPPLIES ARRAY_SIZE(ov13858_supply_names)
130+
121131/* 4224x3136 needs 1080Mbps/lane, 4 lanes */
122132static const struct ov13858_reg mipi_data_rate_1080mbps [] = {
123133 /* PLL1 registers */
@@ -1041,13 +1051,131 @@ struct ov13858 {
10411051
10421052 /* Current mode */
10431053 const struct ov13858_mode * cur_mode ;
1044-
1054+ struct regulator_bulk_data supplies [OV13858_NUM_SUPPLIES ];
1055+ struct gpio_desc * reset ;
1056+ struct clk * xvclk ;
10451057 /* Mutex for serialized access */
10461058 struct mutex mutex ;
10471059};
10481060
10491061#define to_ov13858 (_sd ) container_of(_sd, struct ov13858, sd)
10501062
1063+
1064+ static int ov13858_get_regulators (struct ov13858 * ov13858 )
1065+ {
1066+ struct i2c_client * client = v4l2_get_subdevdata (& ov13858 -> sd );
1067+ unsigned int i ;
1068+
1069+ for (i = 0 ; i < OV13858_NUM_SUPPLIES ; i ++ )
1070+ ov13858 -> supplies [i ].supply = ov13858_supply_names [i ];
1071+
1072+ return devm_regulator_bulk_get (& client -> dev , OV13858_NUM_SUPPLIES ,
1073+ ov13858 -> supplies );
1074+ }
1075+
1076+ static int ov13858_sensor_powerup (struct ov13858 * ov13858 )
1077+ {
1078+ struct i2c_client * client = v4l2_get_subdevdata (& ov13858 -> sd );
1079+ int ret ;
1080+
1081+ dev_info (& client -> dev , "Powering up sensor\n" );
1082+
1083+ /* Assert reset */
1084+ gpiod_set_value_cansleep (ov13858 -> reset , 1 );
1085+
1086+ /* Enable clock FIRST - sensor needs clock to communicate */
1087+ ret = clk_prepare_enable (ov13858 -> xvclk );
1088+ if (ret ) {
1089+ dev_err (& client -> dev , "Failed to enable clock: %d\n" , ret );
1090+ return ret ;
1091+ }
1092+ dev_info (& client -> dev , "Clock enabled\n" );
1093+
1094+ /* Enable regulators */
1095+ ret = regulator_bulk_enable (OV13858_NUM_SUPPLIES , ov13858 -> supplies );
1096+ if (ret ) {
1097+ dev_err (& client -> dev , "Failed to enable regulators: %d\n" , ret );
1098+ clk_disable_unprepare (ov13858 -> xvclk );
1099+ return ret ;
1100+ }
1101+
1102+ dev_info (& client -> dev , "Regulators enabled\n" );
1103+
1104+ /* Wait for power to stabilize */
1105+ usleep_range (5000 , 10000 );
1106+
1107+ /* De-assert reset */
1108+ gpiod_set_value_cansleep (ov13858 -> reset , 0 );
1109+
1110+ /* Wait for sensor to boot */
1111+ msleep (20 );
1112+
1113+ dev_info (& client -> dev , "Reset de-asserted, sensor should be ready\n" );
1114+
1115+ return 0 ;
1116+ }
1117+
1118+ static void ov13858_sensor_powerdown (struct ov13858 * ov13858 )
1119+ {
1120+ struct i2c_client * client = v4l2_get_subdevdata (& ov13858 -> sd );
1121+
1122+ dev_info (& client -> dev , "Powering down sensor\n" );
1123+
1124+ /* Assert reset to put sensor in reset state */
1125+ gpiod_set_value_cansleep (ov13858 -> reset , 1 );
1126+
1127+ /* Disable regulators */
1128+ regulator_bulk_disable (OV13858_NUM_SUPPLIES , ov13858 -> supplies );
1129+ dev_info (& client -> dev , "Regulators disabled\n" );
1130+
1131+ /* Disable clock */
1132+ clk_disable_unprepare (ov13858 -> xvclk );
1133+ dev_info (& client -> dev , "Clock disabled\n" );
1134+ }
1135+
1136+ static int __maybe_unused ov13858_suspend (struct device * dev )
1137+ {
1138+ struct i2c_client * client = to_i2c_client (dev );
1139+ struct v4l2_subdev * sd = i2c_get_clientdata (client );
1140+ struct ov13858 * ov13858 = to_ov13858 (sd );
1141+
1142+ ov13858_sensor_powerdown (ov13858 );
1143+
1144+ return 0 ;
1145+ }
1146+
1147+ static int __maybe_unused ov13858_resume (struct device * dev )
1148+ {
1149+ struct i2c_client * client = to_i2c_client (dev );
1150+ struct v4l2_subdev * sd = i2c_get_clientdata (client );
1151+ struct ov13858 * ov13858 = to_ov13858 (sd );
1152+ int ret ;
1153+
1154+ ret = ov13858_sensor_powerup (ov13858 );
1155+ if (ret )
1156+ return ret ;
1157+
1158+ return 0 ;
1159+ }
1160+
1161+ static const struct dev_pm_ops ov13858_pm_ops = {
1162+ SET_RUNTIME_PM_OPS (ov13858_suspend , ov13858_resume , NULL )
1163+ };
1164+
1165+ static int ov13858_get_gpios (struct ov13858 * ov13858 )
1166+ {
1167+ struct i2c_client * client = v4l2_get_subdevdata (& ov13858 -> sd );
1168+
1169+ ov13858 -> reset = devm_gpiod_get_optional (& client -> dev , "reset" ,
1170+ GPIOD_OUT_HIGH );
1171+ if (IS_ERR (ov13858 -> reset )) {
1172+ dev_err (& client -> dev , "Error fetching reset GPIO\n" );
1173+ return PTR_ERR (ov13858 -> reset );
1174+ }
1175+
1176+ return 0 ;
1177+ }
1178+
10511179/* Read registers up to 4 at a time */
10521180static int ov13858_read_reg (struct ov13858 * ov13858 , u16 reg , u32 len ,
10531181 u32 * val )
@@ -1499,8 +1627,11 @@ static int ov13858_identify_module(struct ov13858 *ov13858)
14991627 int ret ;
15001628 u32 val ;
15011629
1630+ dev_info (& client -> dev , "Attempting to read chip ID from register 0x300a\n" );
15021631 ret = ov13858_read_reg (ov13858 , OV13858_REG_CHIP_ID ,
15031632 OV13858_REG_VALUE_24BIT , & val );
1633+ dev_info (& client -> dev , "Chip ID read result: ret=%d, val=0x%06x (expected 0x%06x)\n" ,
1634+ ret , val , OV13858_CHIP_ID );
15041635 if (ret )
15051636 return ret ;
15061637
@@ -1509,7 +1640,7 @@ static int ov13858_identify_module(struct ov13858 *ov13858)
15091640 OV13858_CHIP_ID , val );
15101641 return - EIO ;
15111642 }
1512-
1643+ dev_info ( & client -> dev , "Chip ID verified successfully!\n" );
15131644 return 0 ;
15141645}
15151646
@@ -1664,29 +1795,49 @@ static int ov13858_probe(struct i2c_client *client)
16641795 u32 val = 0 ;
16651796
16661797 device_property_read_u32 (& client -> dev , "clock-frequency" , & val );
1798+ dev_info (& client -> dev , "Clock frequency read: %u (expected 19200000)\n" , val );
16671799 if (val != 19200000 )
16681800 return - EINVAL ;
16691801
16701802 ov13858 = devm_kzalloc (& client -> dev , sizeof (* ov13858 ), GFP_KERNEL );
16711803 if (!ov13858 )
16721804 return - ENOMEM ;
16731805
1806+ /* Get the external clock */
1807+ ov13858 -> xvclk = devm_clk_get_optional (& client -> dev , "xvclk" );
1808+ if (IS_ERR (ov13858 -> xvclk ))
1809+ return dev_err_probe (& client -> dev , PTR_ERR (ov13858 -> xvclk ),
1810+ "failed to get xvclk\n" );
1811+
16741812 /* Initialize subdev */
16751813 v4l2_i2c_subdev_init (& ov13858 -> sd , client , & ov13858_subdev_ops );
16761814
1815+ ret = ov13858_get_regulators (ov13858 );
1816+ if (ret )
1817+ return dev_err_probe (& client -> dev , ret ,
1818+ "Error fetching regulators\n" );
1819+
1820+ ret = ov13858_get_gpios (ov13858 );
1821+ if (ret )
1822+ return ret ;
1823+
1824+ ret = ov13858_sensor_powerup (ov13858 );
1825+ if (ret )
1826+ return ret ; // No cleanup needed yet, devm handles GPIOs/regulators
1827+
16771828 /* Check module identity */
16781829 ret = ov13858_identify_module (ov13858 );
16791830 if (ret ) {
16801831 dev_err (& client -> dev , "failed to find sensor: %d\n" , ret );
1681- return ret ;
1832+ goto error_power_off ; // Now we need to power down
16821833 }
16831834
16841835 /* Set default mode to max resolution */
16851836 ov13858 -> cur_mode = & supported_modes [0 ];
16861837
16871838 ret = ov13858_init_controls (ov13858 );
16881839 if (ret )
1689- return ret ;
1840+ goto error_power_off ;
16901841
16911842 /* Initialize subdev */
16921843 ov13858 -> sd .internal_ops = & ov13858_internal_ops ;
@@ -1722,6 +1873,9 @@ static int ov13858_probe(struct i2c_client *client)
17221873
17231874error_handler_free :
17241875 ov13858_free_controls (ov13858 );
1876+
1877+ error_power_off :
1878+ ov13858_sensor_powerdown (ov13858 );
17251879 dev_err (& client -> dev , "%s failed:%d\n" , __func__ , ret );
17261880
17271881 return ret ;
@@ -1737,6 +1891,9 @@ static void ov13858_remove(struct i2c_client *client)
17371891 ov13858_free_controls (ov13858 );
17381892
17391893 pm_runtime_disable (& client -> dev );
1894+ if (!pm_runtime_status_suspended (& client -> dev ))
1895+ ov13858_sensor_powerdown (ov13858 );
1896+ pm_runtime_set_suspended (& client -> dev );
17401897}
17411898
17421899static const struct i2c_device_id ov13858_id_table [] = {
@@ -1758,6 +1915,7 @@ MODULE_DEVICE_TABLE(acpi, ov13858_acpi_ids);
17581915static struct i2c_driver ov13858_i2c_driver = {
17591916 .driver = {
17601917 .name = "ov13858" ,
1918+ .pm = & ov13858_pm_ops ,
17611919 .acpi_match_table = ACPI_PTR (ov13858_acpi_ids ),
17621920 },
17631921 .probe = ov13858_probe ,
0 commit comments