@@ -2412,9 +2412,11 @@ static int imx500_state_transition(struct imx500 *imx500, const u8 *fw,
2412
2412
}
2413
2413
2414
2414
/* Do SPI transfer */
2415
- gpiod_set_value_cansleep (imx500 -> led_gpio , 1 );
2415
+ if (imx500 -> led_gpio )
2416
+ gpiod_set_value_cansleep (imx500 -> led_gpio , 1 );
2416
2417
ret = imx500_spi_write (imx500 , data , size );
2417
- gpiod_set_value_cansleep (imx500 -> led_gpio , 0 );
2418
+ if (imx500 -> led_gpio )
2419
+ gpiod_set_value_cansleep (imx500 -> led_gpio , 0 );
2418
2420
2419
2421
imx500 -> fw_progress += size ;
2420
2422
@@ -2748,11 +2750,28 @@ static int imx500_power_on(struct device *dev)
2748
2750
struct imx500 * imx500 = to_imx500 (sd );
2749
2751
int ret ;
2750
2752
2753
+ /* Acquire GPIOs first to ensure reset is asserted before power is applied */
2754
+ imx500 -> led_gpio = devm_gpiod_get_optional (dev , "led" , GPIOD_OUT_LOW );
2755
+ if (IS_ERR (imx500 -> led_gpio )) {
2756
+ ret = PTR_ERR (imx500 -> led_gpio );
2757
+ dev_err (& client -> dev , "%s: failed to get led gpio\n" , __func__ );
2758
+ return ret ;
2759
+ }
2760
+
2761
+ imx500 -> reset_gpio =
2762
+ devm_gpiod_get_optional (dev , "reset" , GPIOD_OUT_HIGH );
2763
+ if (IS_ERR (imx500 -> reset_gpio )) {
2764
+ ret = PTR_ERR (imx500 -> reset_gpio );
2765
+ dev_err (& client -> dev , "%s: failed to get reset gpio\n" ,
2766
+ __func__ );
2767
+ goto gpio_led_put ;
2768
+ }
2769
+
2751
2770
ret = regulator_bulk_enable (IMX500_NUM_SUPPLIES , imx500 -> supplies );
2752
2771
if (ret ) {
2753
2772
dev_err (& client -> dev , "%s: failed to enable regulators\n" ,
2754
2773
__func__ );
2755
- return ret ;
2774
+ goto gpio_reset_put ;
2756
2775
}
2757
2776
2758
2777
/* T4 - 1us
@@ -2772,7 +2791,8 @@ static int imx500_power_on(struct device *dev)
2772
2791
* as 0ms but also "XCLR pin should be set to 'High' after INCK supplied.".
2773
2792
* T4 and T5 are shown as overlapping.
2774
2793
*/
2775
- gpiod_set_value_cansleep (imx500 -> reset_gpio , 1 );
2794
+ if (imx500 -> reset_gpio )
2795
+ gpiod_set_value_cansleep (imx500 -> reset_gpio , 1 );
2776
2796
2777
2797
/* T7 - 9ms
2778
2798
* "INCK start and CXLR rising till Send Streaming Command wait time"
@@ -2783,6 +2803,16 @@ static int imx500_power_on(struct device *dev)
2783
2803
2784
2804
reg_off :
2785
2805
regulator_bulk_disable (IMX500_NUM_SUPPLIES , imx500 -> supplies );
2806
+ gpio_reset_put :
2807
+ if (imx500 -> reset_gpio ) {
2808
+ devm_gpiod_put (dev , imx500 -> reset_gpio );
2809
+ imx500 -> reset_gpio = NULL ;
2810
+ }
2811
+ gpio_led_put :
2812
+ if (imx500 -> led_gpio ) {
2813
+ devm_gpiod_put (dev , imx500 -> led_gpio );
2814
+ imx500 -> led_gpio = NULL ;
2815
+ }
2786
2816
return ret ;
2787
2817
}
2788
2818
@@ -2799,7 +2829,19 @@ static int imx500_power_off(struct device *dev)
2799
2829
* Note, this is not the reverse order of power up.
2800
2830
*/
2801
2831
clk_disable_unprepare (imx500 -> xclk );
2802
- gpiod_set_value_cansleep (imx500 -> reset_gpio , 0 );
2832
+ if (imx500 -> reset_gpio )
2833
+ gpiod_set_value_cansleep (imx500 -> reset_gpio , 0 );
2834
+
2835
+ /* Release GPIOs before disabling regulators */
2836
+ if (imx500 -> reset_gpio ) {
2837
+ devm_gpiod_put (& client -> dev , imx500 -> reset_gpio );
2838
+ imx500 -> reset_gpio = NULL ;
2839
+ }
2840
+ if (imx500 -> led_gpio ) {
2841
+ devm_gpiod_put (& client -> dev , imx500 -> led_gpio );
2842
+ imx500 -> led_gpio = NULL ;
2843
+ }
2844
+
2803
2845
regulator_bulk_disable (IMX500_NUM_SUPPLIES , imx500 -> supplies );
2804
2846
2805
2847
/* Force reprogramming of the common registers when powered up again. */
@@ -3135,14 +3177,14 @@ static int imx500_probe(struct i2c_client *client)
3135
3177
return ret ;
3136
3178
}
3137
3179
3138
- imx500 -> led_gpio = devm_gpiod_get_optional (dev , "led" , GPIOD_OUT_LOW );
3139
-
3140
- imx500 -> reset_gpio =
3141
- devm_gpiod_get_optional (dev , "reset" , GPIOD_OUT_HIGH );
3180
+ /* GPIOs are acquired in imx500_power_on() to avoid preventing
3181
+ * regulator power down when shared with other drivers.
3182
+ */
3142
3183
3143
3184
/*
3144
3185
* The sensor must be powered for imx500_identify_module()
3145
- * to be able to read the CHIP_ID register
3186
+ * to be able to read the CHIP_ID register. This also ensures
3187
+ * GPIOs are available.
3146
3188
*/
3147
3189
ret = imx500_power_on (dev );
3148
3190
if (ret )
0 commit comments