102102#define AMS_PS_SEQ_MASK GENMASK(21, 0)
103103#define AMS_PL_SEQ_MASK GENMASK_ULL(59, 22)
104104
105+ #define AMS_ALARM_NONE 0x000 /* not a real offset */
105106#define AMS_ALARM_TEMP 0x140
106107#define AMS_ALARM_SUPPLY1 0x144
107108#define AMS_ALARM_SUPPLY2 0x148
@@ -763,9 +764,49 @@ static int ams_read_raw(struct iio_dev *indio_dev,
763764 }
764765}
765766
767+ struct ams_alarm_map {
768+ enum ams_ps_pl_seq scan_index ;
769+ unsigned int base_offset ;
770+ };
771+
772+ /*
773+ * Array index matches enum ams_alarm_bit.
774+ * Entries with base_offset == AMS_ALARM_NONE are unused/invalid
775+ * (e.g. RESERVED) and must be skipped.
776+ */
777+ static const struct ams_alarm_map alarm_map [] = {
778+ [AMS_ALARM_BIT_TEMP ] = { AMS_SEQ_TEMP , AMS_ALARM_TEMP },
779+ [AMS_ALARM_BIT_SUPPLY1 ] = { AMS_SEQ_SUPPLY1 , AMS_ALARM_SUPPLY1 },
780+ [AMS_ALARM_BIT_SUPPLY2 ] = { AMS_SEQ_SUPPLY2 , AMS_ALARM_SUPPLY2 },
781+ [AMS_ALARM_BIT_SUPPLY3 ] = { AMS_SEQ_SUPPLY3 , AMS_ALARM_SUPPLY3 },
782+ [AMS_ALARM_BIT_SUPPLY4 ] = { AMS_SEQ_SUPPLY4 , AMS_ALARM_SUPPLY4 },
783+ [AMS_ALARM_BIT_SUPPLY5 ] = { AMS_SEQ_SUPPLY5 , AMS_ALARM_SUPPLY5 },
784+ [AMS_ALARM_BIT_SUPPLY6 ] = { AMS_SEQ_SUPPLY6 , AMS_ALARM_SUPPLY6 },
785+ [AMS_ALARM_BIT_RESERVED ] = { 0 , AMS_ALARM_NONE },
786+ [AMS_ALARM_BIT_SUPPLY7 ] = { AMS_SEQ_SUPPLY7 , AMS_ALARM_SUPPLY7 },
787+ [AMS_ALARM_BIT_SUPPLY8 ] = { AMS_SEQ_SUPPLY8 , AMS_ALARM_SUPPLY8 },
788+ [AMS_ALARM_BIT_SUPPLY9 ] = { AMS_SEQ_SUPPLY9 , AMS_ALARM_SUPPLY9 },
789+ [AMS_ALARM_BIT_SUPPLY10 ] = { AMS_SEQ_SUPPLY10 , AMS_ALARM_SUPPLY10 },
790+ [AMS_ALARM_BIT_VCCAMS ] = { AMS_SEQ_VCCAMS , AMS_ALARM_VCCAMS },
791+ [AMS_ALARM_BIT_TEMP_REMOTE ] = { AMS_SEQ_TEMP_REMOTE , AMS_ALARM_TEMP_REMOTE },
792+ };
793+
794+ static int ams_scan_index_to_event (int scan_index )
795+ {
796+ for (unsigned int i = 0 ; i < ARRAY_SIZE (alarm_map ); i ++ ) {
797+ if (alarm_map [i ].base_offset == AMS_ALARM_NONE )
798+ continue ;
799+
800+ if (alarm_map [i ].scan_index == scan_index )
801+ return i ;
802+ }
803+
804+ return - EINVAL ;
805+ }
806+
766807static int ams_get_alarm_offset (int scan_index , enum iio_event_direction dir )
767808{
768- int offset ;
809+ int offset , event ;
769810
770811 if (scan_index >= AMS_PS_SEQ_MAX )
771812 scan_index -= AMS_PS_SEQ_MAX ;
@@ -779,36 +820,11 @@ static int ams_get_alarm_offset(int scan_index, enum iio_event_direction dir)
779820 offset = 0 ;
780821 }
781822
782- switch (scan_index ) {
783- case AMS_SEQ_TEMP :
784- return AMS_ALARM_TEMP + offset ;
785- case AMS_SEQ_SUPPLY1 :
786- return AMS_ALARM_SUPPLY1 + offset ;
787- case AMS_SEQ_SUPPLY2 :
788- return AMS_ALARM_SUPPLY2 + offset ;
789- case AMS_SEQ_SUPPLY3 :
790- return AMS_ALARM_SUPPLY3 + offset ;
791- case AMS_SEQ_SUPPLY4 :
792- return AMS_ALARM_SUPPLY4 + offset ;
793- case AMS_SEQ_SUPPLY5 :
794- return AMS_ALARM_SUPPLY5 + offset ;
795- case AMS_SEQ_SUPPLY6 :
796- return AMS_ALARM_SUPPLY6 + offset ;
797- case AMS_SEQ_SUPPLY7 :
798- return AMS_ALARM_SUPPLY7 + offset ;
799- case AMS_SEQ_SUPPLY8 :
800- return AMS_ALARM_SUPPLY8 + offset ;
801- case AMS_SEQ_SUPPLY9 :
802- return AMS_ALARM_SUPPLY9 + offset ;
803- case AMS_SEQ_SUPPLY10 :
804- return AMS_ALARM_SUPPLY10 + offset ;
805- case AMS_SEQ_VCCAMS :
806- return AMS_ALARM_VCCAMS + offset ;
807- case AMS_SEQ_TEMP_REMOTE :
808- return AMS_ALARM_TEMP_REMOTE + offset ;
809- default :
823+ event = ams_scan_index_to_event (scan_index );
824+ if (event < 0 || alarm_map [event ].base_offset == AMS_ALARM_NONE )
810825 return 0 ;
811- }
826+
827+ return alarm_map [event ].base_offset + offset ;
812828}
813829
814830static const struct iio_chan_spec * ams_event_to_channel (struct iio_dev * dev ,
@@ -821,49 +837,13 @@ static const struct iio_chan_spec *ams_event_to_channel(struct iio_dev *dev,
821837 scan_index = AMS_PS_SEQ_MAX ;
822838 }
823839
824- switch (event ) {
825- case AMS_ALARM_BIT_TEMP :
826- scan_index += AMS_SEQ_TEMP ;
827- break ;
828- case AMS_ALARM_BIT_SUPPLY1 :
829- scan_index += AMS_SEQ_SUPPLY1 ;
830- break ;
831- case AMS_ALARM_BIT_SUPPLY2 :
832- scan_index += AMS_SEQ_SUPPLY2 ;
833- break ;
834- case AMS_ALARM_BIT_SUPPLY3 :
835- scan_index += AMS_SEQ_SUPPLY3 ;
836- break ;
837- case AMS_ALARM_BIT_SUPPLY4 :
838- scan_index += AMS_SEQ_SUPPLY4 ;
839- break ;
840- case AMS_ALARM_BIT_SUPPLY5 :
841- scan_index += AMS_SEQ_SUPPLY5 ;
842- break ;
843- case AMS_ALARM_BIT_SUPPLY6 :
844- scan_index += AMS_SEQ_SUPPLY6 ;
845- break ;
846- case AMS_ALARM_BIT_SUPPLY7 :
847- scan_index += AMS_SEQ_SUPPLY7 ;
848- break ;
849- case AMS_ALARM_BIT_SUPPLY8 :
850- scan_index += AMS_SEQ_SUPPLY8 ;
851- break ;
852- case AMS_ALARM_BIT_SUPPLY9 :
853- scan_index += AMS_SEQ_SUPPLY9 ;
854- break ;
855- case AMS_ALARM_BIT_SUPPLY10 :
856- scan_index += AMS_SEQ_SUPPLY10 ;
857- break ;
858- case AMS_ALARM_BIT_VCCAMS :
859- scan_index += AMS_SEQ_VCCAMS ;
860- break ;
861- case AMS_ALARM_BIT_TEMP_REMOTE :
862- scan_index += AMS_SEQ_TEMP_REMOTE ;
863- break ;
864- default :
865- break ;
866- }
840+ if (event >= ARRAY_SIZE (alarm_map ))
841+ return NULL ;
842+
843+ if (alarm_map [event ].base_offset == AMS_ALARM_NONE )
844+ return NULL ;
845+
846+ scan_index += alarm_map [event ].scan_index ;
867847
868848 for (i = 0 ; i < dev -> num_channels ; i ++ )
869849 if (dev -> channels [i ].scan_index == scan_index )
@@ -877,43 +857,18 @@ static const struct iio_chan_spec *ams_event_to_channel(struct iio_dev *dev,
877857
878858static int ams_get_alarm_mask (int scan_index )
879859{
880- int bit = 0 ;
860+ int bit = 0 , event ;
881861
882862 if (scan_index >= AMS_PS_SEQ_MAX ) {
883863 bit = AMS_PL_ALARM_START ;
884864 scan_index -= AMS_PS_SEQ_MAX ;
885865 }
886866
887- switch (scan_index ) {
888- case AMS_SEQ_TEMP :
889- return BIT (AMS_ALARM_BIT_TEMP + bit );
890- case AMS_SEQ_SUPPLY1 :
891- return BIT (AMS_ALARM_BIT_SUPPLY1 + bit );
892- case AMS_SEQ_SUPPLY2 :
893- return BIT (AMS_ALARM_BIT_SUPPLY2 + bit );
894- case AMS_SEQ_SUPPLY3 :
895- return BIT (AMS_ALARM_BIT_SUPPLY3 + bit );
896- case AMS_SEQ_SUPPLY4 :
897- return BIT (AMS_ALARM_BIT_SUPPLY4 + bit );
898- case AMS_SEQ_SUPPLY5 :
899- return BIT (AMS_ALARM_BIT_SUPPLY5 + bit );
900- case AMS_SEQ_SUPPLY6 :
901- return BIT (AMS_ALARM_BIT_SUPPLY6 + bit );
902- case AMS_SEQ_SUPPLY7 :
903- return BIT (AMS_ALARM_BIT_SUPPLY7 + bit );
904- case AMS_SEQ_SUPPLY8 :
905- return BIT (AMS_ALARM_BIT_SUPPLY8 + bit );
906- case AMS_SEQ_SUPPLY9 :
907- return BIT (AMS_ALARM_BIT_SUPPLY9 + bit );
908- case AMS_SEQ_SUPPLY10 :
909- return BIT (AMS_ALARM_BIT_SUPPLY10 + bit );
910- case AMS_SEQ_VCCAMS :
911- return BIT (AMS_ALARM_BIT_VCCAMS + bit );
912- case AMS_SEQ_TEMP_REMOTE :
913- return BIT (AMS_ALARM_BIT_TEMP_REMOTE + bit );
914- default :
867+ event = ams_scan_index_to_event (scan_index );
868+ if (event < 0 )
915869 return 0 ;
916- }
870+
871+ return BIT (event + bit );
917872}
918873
919874static int ams_read_event_config (struct iio_dev * indio_dev ,
0 commit comments