1010#include <linux/init.h>
1111#include <linux/i2c.h>
1212#include <linux/err.h>
13- #include <linux/gpio.h>
13+ #include <linux/bits.h>
14+ #include <linux/gpio/consumer.h>
1415#include <linux/slab.h>
1516#include <linux/interrupt.h>
1617#include <linux/mutex.h>
1718#include <linux/of.h>
18- #include <linux/of_gpio.h>
1919#include <linux/platform_device.h>
2020#include <linux/regulator/driver.h>
2121#include <linux/regulator/of_regulator.h>
@@ -31,6 +31,9 @@ struct max8998_data {
3131 unsigned int buck1_idx ; /* index to last changed voltage */
3232 /* value in a set */
3333 unsigned int buck2_idx ;
34+ struct gpio_desc * buck1_gpio1 ;
35+ struct gpio_desc * buck1_gpio2 ;
36+ struct gpio_desc * buck2_gpio ;
3437};
3538
3639static const unsigned int charger_current_table [] = {
@@ -227,15 +230,15 @@ static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
227230 return ret ;
228231}
229232
230- static inline void buck1_gpio_set (int gpio1 , int gpio2 , int v )
233+ static inline void buck1_gpio_set (struct gpio_desc * gpio1 , struct gpio_desc * gpio2 , int v )
231234{
232- gpio_set_value (gpio1 , v & 0x1 );
233- gpio_set_value (gpio2 , (v >> 1 ) & 0x1 );
235+ gpiod_set_value (gpio1 , v & 0x1 );
236+ gpiod_set_value (gpio2 , (v >> 1 ) & 0x1 );
234237}
235238
236- static inline void buck2_gpio_set (int gpio , int v )
239+ static inline void buck2_gpio_set (struct gpio_desc * gpio , int v )
237240{
238- gpio_set_value (gpio , v & 0x1 );
241+ gpiod_set_value (gpio , v & 0x1 );
239242}
240243
241244static int max8998_set_voltage_buck_sel (struct regulator_dev * rdev ,
@@ -260,16 +263,15 @@ static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
260263 selector , max8998 -> buck1_vol [0 ], max8998 -> buck1_vol [1 ],
261264 max8998 -> buck1_vol [2 ], max8998 -> buck1_vol [3 ]);
262265
263- if (gpio_is_valid (pdata -> buck1_set1 ) &&
264- gpio_is_valid (pdata -> buck1_set2 )) {
266+ if (max8998 -> buck1_gpio1 && max8998 -> buck1_gpio2 ) {
265267
266268 /* check if requested voltage */
267269 /* value is already defined */
268270 for (j = 0 ; j < ARRAY_SIZE (max8998 -> buck1_vol ); j ++ ) {
269271 if (max8998 -> buck1_vol [j ] == selector ) {
270272 max8998 -> buck1_idx = j ;
271- buck1_gpio_set (pdata -> buck1_set1 ,
272- pdata -> buck1_set2 , j );
273+ buck1_gpio_set (max8998 -> buck1_gpio1 ,
274+ max8998 -> buck1_gpio2 , j );
273275 goto buck1_exit ;
274276 }
275277 }
@@ -286,13 +288,13 @@ static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
286288 & shift ,
287289 & mask );
288290 ret = max8998_write_reg (i2c , reg , selector );
289- buck1_gpio_set (pdata -> buck1_set1 ,
290- pdata -> buck1_set2 , max8998 -> buck1_idx );
291+ buck1_gpio_set (max8998 -> buck1_gpio1 ,
292+ max8998 -> buck1_gpio2 , max8998 -> buck1_idx );
291293 buck1_last_val ++ ;
292294buck1_exit :
293295 dev_dbg (max8998 -> dev , "%s: SET1:%d, SET2:%d\n" ,
294- i2c -> name , gpio_get_value ( pdata -> buck1_set1 ),
295- gpio_get_value ( pdata -> buck1_set2 ));
296+ i2c -> name , gpiod_get_value ( max8998 -> buck1_gpio1 ),
297+ gpiod_get_value ( max8998 -> buck1_gpio2 ));
296298 break ;
297299 } else {
298300 ret = max8998_write_reg (i2c , reg , selector );
@@ -303,14 +305,13 @@ static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
303305 dev_dbg (max8998 -> dev ,
304306 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n" ,
305307 selector , max8998 -> buck2_vol [0 ], max8998 -> buck2_vol [1 ]);
306- if (gpio_is_valid (pdata -> buck2_set3 )) {
307-
308+ if (max8998 -> buck2_gpio ) {
308309 /* check if requested voltage */
309310 /* value is already defined */
310311 for (j = 0 ; j < ARRAY_SIZE (max8998 -> buck2_vol ); j ++ ) {
311312 if (max8998 -> buck2_vol [j ] == selector ) {
312313 max8998 -> buck2_idx = j ;
313- buck2_gpio_set (pdata -> buck2_set3 , j );
314+ buck2_gpio_set (max8998 -> buck2_gpio , j );
314315 goto buck2_exit ;
315316 }
316317 }
@@ -322,10 +323,10 @@ static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
322323 & reg , & shift , & mask );
323324 ret = max8998_write_reg (i2c , reg , selector );
324325 max8998 -> buck2_vol [max8998 -> buck2_idx ] = selector ;
325- buck2_gpio_set (pdata -> buck2_set3 , max8998 -> buck2_idx );
326+ buck2_gpio_set (max8998 -> buck2_gpio , max8998 -> buck2_idx );
326327buck2_exit :
327328 dev_dbg (max8998 -> dev , "%s: SET3:%d\n" , i2c -> name ,
328- gpio_get_value ( pdata -> buck2_set3 ));
329+ gpiod_get_value ( max8998 -> buck2_gpio ));
329330 } else {
330331 ret = max8998_write_reg (i2c , reg , selector );
331332 }
@@ -539,36 +540,6 @@ static const struct regulator_desc regulators[] = {
539540 charger_current_table , MAX8998_REG_CHGR1 , 0x7 ),
540541};
541542
542- static int max8998_pmic_dt_parse_dvs_gpio (struct max8998_dev * iodev ,
543- struct max8998_platform_data * pdata ,
544- struct device_node * pmic_np )
545- {
546- int gpio ;
547-
548- gpio = of_get_named_gpio (pmic_np , "max8998,pmic-buck1-dvs-gpios" , 0 );
549- if (!gpio_is_valid (gpio )) {
550- dev_err (iodev -> dev , "invalid buck1 gpio[0]: %d\n" , gpio );
551- return - EINVAL ;
552- }
553- pdata -> buck1_set1 = gpio ;
554-
555- gpio = of_get_named_gpio (pmic_np , "max8998,pmic-buck1-dvs-gpios" , 1 );
556- if (!gpio_is_valid (gpio )) {
557- dev_err (iodev -> dev , "invalid buck1 gpio[1]: %d\n" , gpio );
558- return - EINVAL ;
559- }
560- pdata -> buck1_set2 = gpio ;
561-
562- gpio = of_get_named_gpio (pmic_np , "max8998,pmic-buck2-dvs-gpio" , 0 );
563- if (!gpio_is_valid (gpio )) {
564- dev_err (iodev -> dev , "invalid buck 2 gpio: %d\n" , gpio );
565- return - EINVAL ;
566- }
567- pdata -> buck2_set3 = gpio ;
568-
569- return 0 ;
570- }
571-
572543static int max8998_pmic_dt_parse_pdata (struct max8998_dev * iodev ,
573544 struct max8998_platform_data * pdata )
574545{
@@ -614,10 +585,6 @@ static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
614585 of_node_put (reg_np );
615586 of_node_put (regulators_np );
616587
617- ret = max8998_pmic_dt_parse_dvs_gpio (iodev , pdata , pmic_np );
618- if (ret )
619- return - EINVAL ;
620-
621588 pdata -> buck_voltage_lock = of_property_read_bool (pmic_np , "max8998,pmic-buck-voltage-lock" );
622589
623590 ret = of_property_read_u32 (pmic_np ,
@@ -665,6 +632,7 @@ static int max8998_pmic_probe(struct platform_device *pdev)
665632 struct regulator_dev * rdev ;
666633 struct max8998_data * max8998 ;
667634 struct i2c_client * i2c ;
635+ enum gpiod_flags flags ;
668636 int i , ret ;
669637 unsigned int v ;
670638
@@ -693,37 +661,38 @@ static int max8998_pmic_probe(struct platform_device *pdev)
693661 max8998 -> buck1_idx = pdata -> buck1_default_idx ;
694662 max8998 -> buck2_idx = pdata -> buck2_default_idx ;
695663
696- /* NOTE: */
697- /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */
698- /* will be displayed */
699-
700664 /* Check if MAX8998 voltage selection GPIOs are defined */
701- if (gpio_is_valid (pdata -> buck1_set1 ) &&
702- gpio_is_valid (pdata -> buck1_set2 )) {
703- /* Check if SET1 is not equal to 0 */
704- if (!pdata -> buck1_set1 ) {
705- dev_err (& pdev -> dev ,
706- "MAX8998 SET1 GPIO defined as 0 !\n" );
707- WARN_ON (!pdata -> buck1_set1 );
708- return - EIO ;
709- }
710- /* Check if SET2 is not equal to 0 */
711- if (!pdata -> buck1_set2 ) {
712- dev_err (& pdev -> dev ,
713- "MAX8998 SET2 GPIO defined as 0 !\n" );
714- WARN_ON (!pdata -> buck1_set2 );
715- return - EIO ;
716- }
717-
718- gpio_request (pdata -> buck1_set1 , "MAX8998 BUCK1_SET1" );
719- gpio_direction_output (pdata -> buck1_set1 ,
720- max8998 -> buck1_idx & 0x1 );
721-
722-
723- gpio_request (pdata -> buck1_set2 , "MAX8998 BUCK1_SET2" );
724- gpio_direction_output (pdata -> buck1_set2 ,
725- (max8998 -> buck1_idx >> 1 ) & 0x1 );
726-
665+ flags = (max8998 -> buck1_idx & BIT (0 )) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW ;
666+ max8998 -> buck1_gpio1 = devm_gpiod_get_index_optional (iodev -> dev ,
667+ "max8998,pmic-buck1-dvs" ,
668+ 0 ,
669+ flags );
670+ if (IS_ERR (max8998 -> buck1_gpio1 ))
671+ return dev_err_probe (& pdev -> dev , PTR_ERR (max8998 -> buck1_gpio1 ),
672+ "could not get BUCK1 GPIO1\n" );
673+ gpiod_set_consumer_name (max8998 -> buck1_gpio1 , "MAX8998 BUCK1_SET1" );
674+
675+ flags = (max8998 -> buck1_idx & BIT (1 )) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW ;
676+ max8998 -> buck1_gpio2 = devm_gpiod_get_index_optional (iodev -> dev ,
677+ "max8998,pmic-buck1-dvs" ,
678+ 1 ,
679+ flags );
680+ if (IS_ERR (max8998 -> buck1_gpio2 ))
681+ return dev_err_probe (& pdev -> dev , PTR_ERR (max8998 -> buck1_gpio2 ),
682+ "could not get BUCK1 GPIO2\n" );
683+ gpiod_set_consumer_name (max8998 -> buck1_gpio1 , "MAX8998 BUCK1_SET2" );
684+
685+ flags = (max8998 -> buck2_idx & BIT (0 )) ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW ;
686+ max8998 -> buck2_gpio = devm_gpiod_get_index_optional (iodev -> dev ,
687+ "max8998,pmic-buck2-dvs" ,
688+ 0 ,
689+ flags );
690+ if (IS_ERR (max8998 -> buck2_gpio ))
691+ return dev_err_probe (& pdev -> dev , PTR_ERR (max8998 -> buck2_gpio ),
692+ "could not get BUCK2 GPIO\n" );
693+ gpiod_set_consumer_name (max8998 -> buck1_gpio1 , "MAX8998 BUCK2_SET3" );
694+
695+ if (max8998 -> buck1_gpio1 && max8998 -> buck1_gpio2 ) {
727696 /* Set predefined values for BUCK1 registers */
728697 for (v = 0 ; v < ARRAY_SIZE (pdata -> buck1_voltage ); ++ v ) {
729698 int index = MAX8998_BUCK1 - MAX8998_LDO2 ;
@@ -742,18 +711,7 @@ static int max8998_pmic_probe(struct platform_device *pdev)
742711 }
743712 }
744713
745- if (gpio_is_valid (pdata -> buck2_set3 )) {
746- /* Check if SET3 is not equal to 0 */
747- if (!pdata -> buck2_set3 ) {
748- dev_err (& pdev -> dev ,
749- "MAX8998 SET3 GPIO defined as 0 !\n" );
750- WARN_ON (!pdata -> buck2_set3 );
751- return - EIO ;
752- }
753- gpio_request (pdata -> buck2_set3 , "MAX8998 BUCK2_SET3" );
754- gpio_direction_output (pdata -> buck2_set3 ,
755- max8998 -> buck2_idx & 0x1 );
756-
714+ if (max8998 -> buck2_gpio ) {
757715 /* Set predefined values for BUCK2 registers */
758716 for (v = 0 ; v < ARRAY_SIZE (pdata -> buck2_voltage ); ++ v ) {
759717 int index = MAX8998_BUCK2 - MAX8998_LDO2 ;
0 commit comments