22// Copyright (c) 2017 Intel Corporation.
33
44#include <linux/acpi.h>
5+ #include <linux/delay.h>
56#include <linux/clk.h>
67#include <linux/i2c.h>
78#include <linux/module.h>
@@ -119,6 +120,14 @@ struct ov13858_mode {
119120 struct ov13858_reg_list reg_list ;
120121};
121122
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+
122131/* 4224x3136 needs 1080Mbps/lane, 4 lanes */
123132static const struct ov13858_reg mipi_data_rate_1080mbps [] = {
124133 /* PLL1 registers */
@@ -1046,12 +1055,126 @@ struct ov13858 {
10461055 /* Current mode */
10471056 const struct ov13858_mode * cur_mode ;
10481057
1058+ struct regulator_bulk_data supplies [OV13858_NUM_SUPPLIES ];
1059+ struct gpio_desc * reset ;
1060+ struct clk * xvclk ;
1061+
10491062 /* Mutex for serialized access */
10501063 struct mutex mutex ;
10511064};
10521065
10531066#define to_ov13858 (_sd ) container_of(_sd, struct ov13858, sd)
10541067
1068+
1069+ static int ov13858_get_regulators (struct ov13858 * ov13858 )
1070+ {
1071+ unsigned int i ;
1072+
1073+ for (i = 0 ; i < OV13858_NUM_SUPPLIES ; i ++ )
1074+ ov13858 -> supplies [i ].supply = ov13858_supply_names [i ];
1075+
1076+ return devm_regulator_bulk_get (ov13858 -> dev , OV13858_NUM_SUPPLIES ,
1077+ ov13858 -> supplies );
1078+ }
1079+
1080+ static int ov13858_sensor_powerup (struct ov13858 * ov13858 )
1081+ {
1082+ int ret ;
1083+
1084+ dev_info (ov13858 -> dev , "Powering up sensor\n" );
1085+
1086+ /* Assert reset */
1087+ gpiod_set_value_cansleep (ov13858 -> reset , 1 );
1088+
1089+ /* Enable clock FIRST - sensor needs clock to communicate */
1090+ ret = clk_prepare_enable (ov13858 -> xvclk );
1091+ if (ret ) {
1092+ dev_err (ov13858 -> dev , "Failed to enable clock: %d\n" , ret );
1093+ return ret ;
1094+ }
1095+ dev_info (ov13858 -> dev , "Clock enabled\n" );
1096+
1097+ /* Enable regulators */
1098+ ret = regulator_bulk_enable (OV13858_NUM_SUPPLIES , ov13858 -> supplies );
1099+ if (ret ) {
1100+ dev_err (ov13858 -> dev , "Failed to enable regulators: %d\n" , ret );
1101+ clk_disable_unprepare (ov13858 -> xvclk );
1102+ return ret ;
1103+ }
1104+
1105+ dev_info (ov13858 -> dev , "Regulators enabled\n" );
1106+
1107+ /* Wait for power to stabilize */
1108+ usleep_range (5000 , 10000 );
1109+
1110+ /* De-assert reset */
1111+ gpiod_set_value_cansleep (ov13858 -> reset , 0 );
1112+
1113+ /* Wait for sensor to boot */
1114+ msleep (20 );
1115+
1116+ dev_info (ov13858 -> dev , "Reset de-asserted, sensor should be ready\n" );
1117+
1118+ return 0 ;
1119+ }
1120+
1121+ static void ov13858_sensor_powerdown (struct ov13858 * ov13858 )
1122+ {
1123+ dev_info (ov13858 -> dev , "Powering down sensor\n" );
1124+
1125+ /* Assert reset to put sensor in reset state */
1126+ gpiod_set_value_cansleep (ov13858 -> reset , 1 );
1127+
1128+ /* Disable regulators */
1129+ regulator_bulk_disable (OV13858_NUM_SUPPLIES , ov13858 -> supplies );
1130+ dev_info (ov13858 -> dev , "Regulators disabled\n" );
1131+
1132+ /* Disable clock */
1133+ clk_disable_unprepare (ov13858 -> xvclk );
1134+ dev_info (ov13858 -> dev , "Clock disabled\n" );
1135+ }
1136+
1137+ static int __maybe_unused ov13858_suspend (struct device * dev )
1138+ {
1139+ struct i2c_client * client = to_i2c_client (dev );
1140+ struct v4l2_subdev * sd = i2c_get_clientdata (client );
1141+ struct ov13858 * ov13858 = to_ov13858 (sd );
1142+
1143+ ov13858_sensor_powerdown (ov13858 );
1144+
1145+ return 0 ;
1146+ }
1147+
1148+ static int __maybe_unused ov13858_resume (struct device * dev )
1149+ {
1150+ struct i2c_client * client = to_i2c_client (dev );
1151+ struct v4l2_subdev * sd = i2c_get_clientdata (client );
1152+ struct ov13858 * ov13858 = to_ov13858 (sd );
1153+ int ret ;
1154+
1155+ ret = ov13858_sensor_powerup (ov13858 );
1156+ if (ret )
1157+ return ret ;
1158+
1159+ return 0 ;
1160+ }
1161+
1162+ static const struct dev_pm_ops ov13858_pm_ops = {
1163+ SET_RUNTIME_PM_OPS (ov13858_suspend , ov13858_resume , NULL )
1164+ };
1165+
1166+ static int ov13858_get_gpios (struct ov13858 * ov13858 )
1167+ {
1168+ ov13858 -> reset = devm_gpiod_get_optional (ov13858 -> dev , "reset" ,
1169+ GPIOD_OUT_HIGH );
1170+ if (IS_ERR (ov13858 -> reset )) {
1171+ dev_err (ov13858 -> dev , "Error fetching reset GPIO\n" );
1172+ return PTR_ERR (ov13858 -> reset );
1173+ }
1174+
1175+ return 0 ;
1176+ }
1177+
10551178/* Read registers up to 4 at a time */
10561179static int ov13858_read_reg (struct ov13858 * ov13858 , u16 reg , u32 len ,
10571180 u32 * val )
@@ -1498,8 +1621,11 @@ static int ov13858_identify_module(struct ov13858 *ov13858)
14981621 int ret ;
14991622 u32 val ;
15001623
1624+ dev_info (ov13858 -> dev , "Attempting to read chip ID from register 0x300a\n" );
15011625 ret = ov13858_read_reg (ov13858 , OV13858_REG_CHIP_ID ,
15021626 OV13858_REG_VALUE_24BIT , & val );
1627+ dev_info (ov13858 -> dev , "Chip ID read result: ret=%d, val=0x%06x (expected 0x%06x)\n" ,
1628+ ret , val , OV13858_CHIP_ID );
15031629 if (ret )
15041630 return ret ;
15051631
@@ -1509,6 +1635,7 @@ static int ov13858_identify_module(struct ov13858 *ov13858)
15091635 return - EIO ;
15101636 }
15111637
1638+ dev_info (ov13858 -> dev , "Chip ID verified successfully!\n" );
15121639 return 0 ;
15131640}
15141641
@@ -1678,22 +1805,41 @@ static int ov13858_probe(struct i2c_client *client)
16781805 "external clock %lu is not supported\n" ,
16791806 freq );
16801807
1808+ /* Get the external clock */
1809+ ov13858 -> xvclk = devm_clk_get_optional (ov13858 -> dev , "xvclk" );
1810+ if (IS_ERR (ov13858 -> xvclk ))
1811+ return dev_err_probe (ov13858 -> dev , PTR_ERR (ov13858 -> xvclk ),
1812+ "failed to get xvclk\n" );
1813+
16811814 /* Initialize subdev */
16821815 v4l2_i2c_subdev_init (& ov13858 -> sd , client , & ov13858_subdev_ops );
16831816
1817+ ret = ov13858_get_regulators (ov13858 );
1818+ if (ret )
1819+ return dev_err_probe (ov13858 -> dev , ret ,
1820+ "Error fetching regulators\n" );
1821+
1822+ ret = ov13858_get_gpios (ov13858 );
1823+ if (ret )
1824+ return ret ;
1825+
1826+ ret = ov13858_sensor_powerup (ov13858 );
1827+ if (ret )
1828+ return ret ; // No cleanup needed yet, devm handles GPIOs/regulators
1829+
16841830 /* Check module identity */
16851831 ret = ov13858_identify_module (ov13858 );
16861832 if (ret ) {
16871833 dev_err (ov13858 -> dev , "failed to find sensor: %d\n" , ret );
1688- return ret ;
1834+ goto error_power_off ; // Now we need to power down
16891835 }
16901836
16911837 /* Set default mode to max resolution */
16921838 ov13858 -> cur_mode = & supported_modes [0 ];
16931839
16941840 ret = ov13858_init_controls (ov13858 );
16951841 if (ret )
1696- return ret ;
1842+ goto error_power_off ;
16971843
16981844 /* Initialize subdev */
16991845 ov13858 -> sd .internal_ops = & ov13858_internal_ops ;
@@ -1729,6 +1875,10 @@ static int ov13858_probe(struct i2c_client *client)
17291875
17301876error_handler_free :
17311877 ov13858_free_controls (ov13858 );
1878+
1879+ error_power_off :
1880+ ov13858_sensor_powerdown (ov13858 );
1881+
17321882 dev_err (ov13858 -> dev , "%s failed:%d\n" , __func__ , ret );
17331883
17341884 return ret ;
@@ -1744,6 +1894,9 @@ static void ov13858_remove(struct i2c_client *client)
17441894 ov13858_free_controls (ov13858 );
17451895
17461896 pm_runtime_disable (ov13858 -> dev );
1897+ if (!pm_runtime_status_suspended (ov13858 -> dev ))
1898+ ov13858_sensor_powerdown (ov13858 );
1899+ pm_runtime_set_suspended (ov13858 -> dev );
17471900}
17481901
17491902static const struct i2c_device_id ov13858_id_table [] = {
@@ -1765,6 +1918,7 @@ MODULE_DEVICE_TABLE(acpi, ov13858_acpi_ids);
17651918static struct i2c_driver ov13858_i2c_driver = {
17661919 .driver = {
17671920 .name = "ov13858" ,
1921+ .pm = & ov13858_pm_ops ,
17681922 .acpi_match_table = ACPI_PTR (ov13858_acpi_ids ),
17691923 },
17701924 .probe = ov13858_probe ,
0 commit comments