7575#define T_CONVERT_NS 190 /* conversion time */
7676#define T_CONVERT_0_NS 10 /* 1st conversion start time (oversampling) */
7777#define T_CONVERT_X_NS 500 /* xth conversion start time (oversampling) */
78+ #define T_POWERUP_US 5000 /* Power up */
7879
7980struct ad7380_timing_specs {
8081 const unsigned int t_csh_ns ; /* CS minimum high time */
@@ -86,6 +87,9 @@ struct ad7380_chip_info {
8687 unsigned int num_channels ;
8788 unsigned int num_simult_channels ;
8889 bool has_mux ;
90+ const char * const * supplies ;
91+ unsigned int num_supplies ;
92+ bool external_ref_only ;
8993 const char * const * vcm_supplies ;
9094 unsigned int num_vcm_supplies ;
9195 const unsigned long * available_scan_masks ;
@@ -243,6 +247,10 @@ DEFINE_AD7380_8_CHANNEL(ad7386_4_channels, 16, 0, u);
243247DEFINE_AD7380_8_CHANNEL (ad7387_4_channels , 14 , 0 , u );
244248DEFINE_AD7380_8_CHANNEL (ad7388_4_channels , 12 , 0 , u );
245249
250+ static const char * const ad7380_supplies [] = {
251+ "vcc" , "vlogic" ,
252+ };
253+
246254static const char * const ad7380_2_channel_vcm_supplies [] = {
247255 "aina" , "ainb" ,
248256};
@@ -338,6 +346,8 @@ static const struct ad7380_chip_info ad7380_chip_info = {
338346 .channels = ad7380_channels ,
339347 .num_channels = ARRAY_SIZE (ad7380_channels ),
340348 .num_simult_channels = 2 ,
349+ .supplies = ad7380_supplies ,
350+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
341351 .available_scan_masks = ad7380_2_channel_scan_masks ,
342352 .timing_specs = & ad7380_timing ,
343353};
@@ -347,6 +357,8 @@ static const struct ad7380_chip_info ad7381_chip_info = {
347357 .channels = ad7381_channels ,
348358 .num_channels = ARRAY_SIZE (ad7381_channels ),
349359 .num_simult_channels = 2 ,
360+ .supplies = ad7380_supplies ,
361+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
350362 .available_scan_masks = ad7380_2_channel_scan_masks ,
351363 .timing_specs = & ad7380_timing ,
352364};
@@ -356,6 +368,8 @@ static const struct ad7380_chip_info ad7383_chip_info = {
356368 .channels = ad7383_channels ,
357369 .num_channels = ARRAY_SIZE (ad7383_channels ),
358370 .num_simult_channels = 2 ,
371+ .supplies = ad7380_supplies ,
372+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
359373 .vcm_supplies = ad7380_2_channel_vcm_supplies ,
360374 .num_vcm_supplies = ARRAY_SIZE (ad7380_2_channel_vcm_supplies ),
361375 .available_scan_masks = ad7380_2_channel_scan_masks ,
@@ -367,6 +381,8 @@ static const struct ad7380_chip_info ad7384_chip_info = {
367381 .channels = ad7384_channels ,
368382 .num_channels = ARRAY_SIZE (ad7384_channels ),
369383 .num_simult_channels = 2 ,
384+ .supplies = ad7380_supplies ,
385+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
370386 .vcm_supplies = ad7380_2_channel_vcm_supplies ,
371387 .num_vcm_supplies = ARRAY_SIZE (ad7380_2_channel_vcm_supplies ),
372388 .available_scan_masks = ad7380_2_channel_scan_masks ,
@@ -378,6 +394,8 @@ static const struct ad7380_chip_info ad7386_chip_info = {
378394 .channels = ad7386_channels ,
379395 .num_channels = ARRAY_SIZE (ad7386_channels ),
380396 .num_simult_channels = 2 ,
397+ .supplies = ad7380_supplies ,
398+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
381399 .has_mux = true,
382400 .available_scan_masks = ad7380_2x2_channel_scan_masks ,
383401 .timing_specs = & ad7380_timing ,
@@ -388,6 +406,8 @@ static const struct ad7380_chip_info ad7387_chip_info = {
388406 .channels = ad7387_channels ,
389407 .num_channels = ARRAY_SIZE (ad7387_channels ),
390408 .num_simult_channels = 2 ,
409+ .supplies = ad7380_supplies ,
410+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
391411 .has_mux = true,
392412 .available_scan_masks = ad7380_2x2_channel_scan_masks ,
393413 .timing_specs = & ad7380_timing ,
@@ -398,6 +418,8 @@ static const struct ad7380_chip_info ad7388_chip_info = {
398418 .channels = ad7388_channels ,
399419 .num_channels = ARRAY_SIZE (ad7388_channels ),
400420 .num_simult_channels = 2 ,
421+ .supplies = ad7380_supplies ,
422+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
401423 .has_mux = true,
402424 .available_scan_masks = ad7380_2x2_channel_scan_masks ,
403425 .timing_specs = & ad7380_timing ,
@@ -408,6 +430,9 @@ static const struct ad7380_chip_info ad7380_4_chip_info = {
408430 .channels = ad7380_4_channels ,
409431 .num_channels = ARRAY_SIZE (ad7380_4_channels ),
410432 .num_simult_channels = 4 ,
433+ .supplies = ad7380_supplies ,
434+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
435+ .external_ref_only = true,
411436 .available_scan_masks = ad7380_4_channel_scan_masks ,
412437 .timing_specs = & ad7380_4_timing ,
413438};
@@ -417,6 +442,8 @@ static const struct ad7380_chip_info ad7381_4_chip_info = {
417442 .channels = ad7381_4_channels ,
418443 .num_channels = ARRAY_SIZE (ad7381_4_channels ),
419444 .num_simult_channels = 4 ,
445+ .supplies = ad7380_supplies ,
446+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
420447 .available_scan_masks = ad7380_4_channel_scan_masks ,
421448 .timing_specs = & ad7380_4_timing ,
422449};
@@ -426,6 +453,8 @@ static const struct ad7380_chip_info ad7383_4_chip_info = {
426453 .channels = ad7383_4_channels ,
427454 .num_channels = ARRAY_SIZE (ad7383_4_channels ),
428455 .num_simult_channels = 4 ,
456+ .supplies = ad7380_supplies ,
457+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
429458 .vcm_supplies = ad7380_4_channel_vcm_supplies ,
430459 .num_vcm_supplies = ARRAY_SIZE (ad7380_4_channel_vcm_supplies ),
431460 .available_scan_masks = ad7380_4_channel_scan_masks ,
@@ -437,6 +466,8 @@ static const struct ad7380_chip_info ad7384_4_chip_info = {
437466 .channels = ad7384_4_channels ,
438467 .num_channels = ARRAY_SIZE (ad7384_4_channels ),
439468 .num_simult_channels = 4 ,
469+ .supplies = ad7380_supplies ,
470+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
440471 .vcm_supplies = ad7380_4_channel_vcm_supplies ,
441472 .num_vcm_supplies = ARRAY_SIZE (ad7380_4_channel_vcm_supplies ),
442473 .available_scan_masks = ad7380_4_channel_scan_masks ,
@@ -448,6 +479,8 @@ static const struct ad7380_chip_info ad7386_4_chip_info = {
448479 .channels = ad7386_4_channels ,
449480 .num_channels = ARRAY_SIZE (ad7386_4_channels ),
450481 .num_simult_channels = 4 ,
482+ .supplies = ad7380_supplies ,
483+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
451484 .has_mux = true,
452485 .available_scan_masks = ad7380_2x4_channel_scan_masks ,
453486 .timing_specs = & ad7380_4_timing ,
@@ -458,6 +491,8 @@ static const struct ad7380_chip_info ad7387_4_chip_info = {
458491 .channels = ad7387_4_channels ,
459492 .num_channels = ARRAY_SIZE (ad7387_4_channels ),
460493 .num_simult_channels = 4 ,
494+ .supplies = ad7380_supplies ,
495+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
461496 .has_mux = true,
462497 .available_scan_masks = ad7380_2x4_channel_scan_masks ,
463498 .timing_specs = & ad7380_4_timing ,
@@ -468,6 +503,8 @@ static const struct ad7380_chip_info ad7388_4_chip_info = {
468503 .channels = ad7388_4_channels ,
469504 .num_channels = ARRAY_SIZE (ad7388_4_channels ),
470505 .num_simult_channels = 4 ,
506+ .supplies = ad7380_supplies ,
507+ .num_supplies = ARRAY_SIZE (ad7380_supplies ),
471508 .has_mux = true,
472509 .available_scan_masks = ad7380_2x4_channel_scan_masks ,
473510 .timing_specs = & ad7380_4_timing ,
@@ -956,7 +993,7 @@ static const struct iio_info ad7380_info = {
956993 .debugfs_reg_access = & ad7380_debugfs_reg_access ,
957994};
958995
959- static int ad7380_init (struct ad7380_state * st , struct regulator * vref )
996+ static int ad7380_init (struct ad7380_state * st , bool external_ref_en )
960997{
961998 int ret ;
962999
@@ -968,13 +1005,13 @@ static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
9681005 if (ret < 0 )
9691006 return ret ;
9701007
971- /* select internal or external reference voltage */
972- ret = regmap_update_bits ( st -> regmap , AD7380_REG_ADDR_CONFIG1 ,
973- AD7380_CONFIG1_REFSEL ,
974- FIELD_PREP ( AD7380_CONFIG1_REFSEL ,
975- vref ? 1 : 0 ));
976- if ( ret < 0 )
977- return ret ;
1008+ if ( external_ref_en ) {
1009+ /* select external reference voltage */
1010+ ret = regmap_set_bits ( st -> regmap , AD7380_REG_ADDR_CONFIG1 ,
1011+ AD7380_CONFIG1_REFSEL );
1012+ if ( ret < 0 )
1013+ return ret ;
1014+ }
9781015
9791016 /* This is the default value after reset. */
9801017 st -> oversampling_ratio = 1 ;
@@ -987,16 +1024,11 @@ static int ad7380_init(struct ad7380_state *st, struct regulator *vref)
9871024 FIELD_PREP (AD7380_CONFIG2_SDO , 1 ));
9881025}
9891026
990- static void ad7380_regulator_disable (void * p )
991- {
992- regulator_disable (p );
993- }
994-
9951027static int ad7380_probe (struct spi_device * spi )
9961028{
9971029 struct iio_dev * indio_dev ;
9981030 struct ad7380_state * st ;
999- struct regulator * vref ;
1031+ bool external_ref_en ;
10001032 int ret , i ;
10011033
10021034 indio_dev = devm_iio_device_alloc (& spi -> dev , sizeof (* st ));
@@ -1009,36 +1041,38 @@ static int ad7380_probe(struct spi_device *spi)
10091041 if (!st -> chip_info )
10101042 return dev_err_probe (& spi -> dev , - EINVAL , "missing match data\n" );
10111043
1012- vref = devm_regulator_get_optional (& spi -> dev , "refio" );
1013- if (IS_ERR (vref )) {
1014- if (PTR_ERR (vref ) != - ENODEV )
1015- return dev_err_probe (& spi -> dev , PTR_ERR (vref ),
1016- "Failed to get refio regulator\n" );
1017-
1018- vref = NULL ;
1019- }
1044+ ret = devm_regulator_bulk_get_enable (& spi -> dev , st -> chip_info -> num_supplies ,
1045+ st -> chip_info -> supplies );
10201046
1021- /*
1022- * If there is no REFIO supply, then it means that we are using
1023- * the internal 2.5V reference, otherwise REFIO is reference voltage.
1024- */
1025- if (vref ) {
1026- ret = regulator_enable (vref );
1027- if (ret )
1028- return ret ;
1029-
1030- ret = devm_add_action_or_reset (& spi -> dev ,
1031- ad7380_regulator_disable , vref );
1032- if (ret )
1033- return ret ;
1047+ if (ret )
1048+ return dev_err_probe (& spi -> dev , ret ,
1049+ "Failed to enable power supplies\n" );
1050+ fsleep (T_POWERUP_US );
10341051
1035- ret = regulator_get_voltage (vref );
1052+ if (st -> chip_info -> external_ref_only ) {
1053+ ret = devm_regulator_get_enable_read_voltage (& spi -> dev ,
1054+ "refin" );
10361055 if (ret < 0 )
1037- return ret ;
1056+ return dev_err_probe (& spi -> dev , ret ,
1057+ "Failed to get refin regulator\n" );
10381058
10391059 st -> vref_mv = ret / 1000 ;
1060+
1061+ /* these chips don't have a register bit for this */
1062+ external_ref_en = false;
10401063 } else {
1041- st -> vref_mv = AD7380_INTERNAL_REF_MV ;
1064+ /*
1065+ * If there is no REFIO supply, then it means that we are using
1066+ * the internal reference, otherwise REFIO is reference voltage.
1067+ */
1068+ ret = devm_regulator_get_enable_read_voltage (& spi -> dev ,
1069+ "refio" );
1070+ if (ret < 0 && ret != - ENODEV )
1071+ return dev_err_probe (& spi -> dev , ret ,
1072+ "Failed to get refio regulator\n" );
1073+
1074+ external_ref_en = ret != - ENODEV ;
1075+ st -> vref_mv = external_ref_en ? ret / 1000 : AD7380_INTERNAL_REF_MV ;
10421076 }
10431077
10441078 if (st -> chip_info -> num_vcm_supplies > ARRAY_SIZE (st -> vcm_mv ))
@@ -1050,27 +1084,13 @@ static int ad7380_probe(struct spi_device *spi)
10501084 * input pin.
10511085 */
10521086 for (i = 0 ; i < st -> chip_info -> num_vcm_supplies ; i ++ ) {
1053- struct regulator * vcm ;
1054-
1055- vcm = devm_regulator_get (& spi -> dev ,
1056- st -> chip_info -> vcm_supplies [i ]);
1057- if (IS_ERR (vcm ))
1058- return dev_err_probe (& spi -> dev , PTR_ERR (vcm ),
1059- "Failed to get %s regulator\n" ,
1060- st -> chip_info -> vcm_supplies [i ]);
1087+ const char * vcm = st -> chip_info -> vcm_supplies [i ];
10611088
1062- ret = regulator_enable (vcm );
1063- if (ret )
1064- return ret ;
1065-
1066- ret = devm_add_action_or_reset (& spi -> dev ,
1067- ad7380_regulator_disable , vcm );
1068- if (ret )
1069- return ret ;
1070-
1071- ret = regulator_get_voltage (vcm );
1089+ ret = devm_regulator_get_enable_read_voltage (& spi -> dev , vcm );
10721090 if (ret < 0 )
1073- return ret ;
1091+ return dev_err_probe (& spi -> dev , ret ,
1092+ "Failed to get %s regulator\n" ,
1093+ vcm );
10741094
10751095 st -> vcm_mv [i ] = ret / 1000 ;
10761096 }
@@ -1135,7 +1155,7 @@ static int ad7380_probe(struct spi_device *spi)
11351155 if (ret )
11361156 return ret ;
11371157
1138- ret = ad7380_init (st , vref );
1158+ ret = ad7380_init (st , external_ref_en );
11391159 if (ret )
11401160 return ret ;
11411161
0 commit comments