10
10
#include <linux/module.h>
11
11
#include <linux/err.h>
12
12
#include <linux/mutex.h>
13
+ #include <linux/bitfield.h>
13
14
#include <linux/completion.h>
14
15
#include <linux/delay.h>
15
16
#include <linux/hid.h>
16
17
#include <linux/hidraw.h>
17
18
#include <linux/i2c.h>
18
19
#include <linux/gpio/driver.h>
20
+ #include <linux/iio/iio.h>
19
21
#include "hid-ids.h"
20
22
21
23
/* Commands codes in a raw output report */
30
32
MCP2221_I2C_CANCEL = 0x10 ,
31
33
MCP2221_GPIO_SET = 0x50 ,
32
34
MCP2221_GPIO_GET = 0x51 ,
35
+ MCP2221_SET_SRAM_SETTINGS = 0x60 ,
36
+ MCP2221_GET_SRAM_SETTINGS = 0x61 ,
37
+ MCP2221_READ_FLASH_DATA = 0xb0 ,
33
38
};
34
39
35
40
/* Response codes in a raw input report */
@@ -89,6 +94,7 @@ struct mcp2221 {
89
94
struct i2c_adapter adapter ;
90
95
struct mutex lock ;
91
96
struct completion wait_in_report ;
97
+ struct delayed_work init_work ;
92
98
u8 * rxbuf ;
93
99
u8 txbuf [64 ];
94
100
int rxbuf_idx ;
@@ -97,6 +103,18 @@ struct mcp2221 {
97
103
struct gpio_chip * gc ;
98
104
u8 gp_idx ;
99
105
u8 gpio_dir ;
106
+ u8 mode [4 ];
107
+ #if IS_REACHABLE (CONFIG_IIO )
108
+ struct iio_chan_spec iio_channels [3 ];
109
+ u16 adc_values [3 ];
110
+ u8 adc_scale ;
111
+ u8 dac_value ;
112
+ u16 dac_scale ;
113
+ #endif
114
+ };
115
+
116
+ struct mcp2221_iio {
117
+ struct mcp2221 * mcp ;
100
118
};
101
119
102
120
/*
@@ -713,7 +731,7 @@ static int mcp_get_i2c_eng_state(struct mcp2221 *mcp,
713
731
static int mcp2221_raw_event (struct hid_device * hdev ,
714
732
struct hid_report * report , u8 * data , int size )
715
733
{
716
- u8 * buf ;
734
+ u8 * buf , tmp ;
717
735
struct mcp2221 * mcp = hid_get_drvdata (hdev );
718
736
719
737
switch (data [0 ]) {
@@ -745,6 +763,9 @@ static int mcp2221_raw_event(struct hid_device *hdev,
745
763
break ;
746
764
}
747
765
mcp -> status = mcp_get_i2c_eng_state (mcp , data , 8 );
766
+ #if IS_REACHABLE (CONFIG_IIO )
767
+ memcpy (& mcp -> adc_values , & data [50 ], sizeof (mcp -> adc_values ));
768
+ #endif
748
769
break ;
749
770
default :
750
771
mcp -> status = - EIO ;
@@ -816,6 +837,66 @@ static int mcp2221_raw_event(struct hid_device *hdev,
816
837
complete (& mcp -> wait_in_report );
817
838
break ;
818
839
840
+ case MCP2221_SET_SRAM_SETTINGS :
841
+ switch (data [1 ]) {
842
+ case MCP2221_SUCCESS :
843
+ mcp -> status = 0 ;
844
+ break ;
845
+ default :
846
+ mcp -> status = - EAGAIN ;
847
+ }
848
+ complete (& mcp -> wait_in_report );
849
+ break ;
850
+
851
+ case MCP2221_GET_SRAM_SETTINGS :
852
+ switch (data [1 ]) {
853
+ case MCP2221_SUCCESS :
854
+ memcpy (& mcp -> mode , & data [22 ], 4 );
855
+ #if IS_REACHABLE (CONFIG_IIO )
856
+ mcp -> dac_value = data [6 ] & GENMASK (4 , 0 );
857
+ #endif
858
+ mcp -> status = 0 ;
859
+ break ;
860
+ default :
861
+ mcp -> status = - EAGAIN ;
862
+ }
863
+ complete (& mcp -> wait_in_report );
864
+ break ;
865
+
866
+ case MCP2221_READ_FLASH_DATA :
867
+ switch (data [1 ]) {
868
+ case MCP2221_SUCCESS :
869
+ mcp -> status = 0 ;
870
+
871
+ /* Only handles CHIP SETTINGS subpage currently */
872
+ if (mcp -> txbuf [1 ] != 0 ) {
873
+ mcp -> status = - EIO ;
874
+ break ;
875
+ }
876
+
877
+ #if IS_REACHABLE (CONFIG_IIO )
878
+ /* DAC scale value */
879
+ tmp = FIELD_GET (GENMASK (7 , 6 ), data [6 ]);
880
+ if ((data [6 ] & BIT (5 )) && tmp )
881
+ mcp -> dac_scale = tmp + 4 ;
882
+ else
883
+ mcp -> dac_scale = 5 ;
884
+
885
+ /* ADC scale value */
886
+ tmp = FIELD_GET (GENMASK (4 , 3 ), data [7 ]);
887
+ if ((data [7 ] & BIT (2 )) && tmp )
888
+ mcp -> adc_scale = tmp - 1 ;
889
+ else
890
+ mcp -> adc_scale = 0 ;
891
+ #endif
892
+
893
+ break ;
894
+ default :
895
+ mcp -> status = - EAGAIN ;
896
+ }
897
+ complete (& mcp -> wait_in_report );
898
+ break ;
899
+
819
900
default :
820
901
mcp -> status = - EIO ;
821
902
complete (& mcp -> wait_in_report );
@@ -838,6 +919,176 @@ static void mcp2221_remove(struct hid_device *hdev)
838
919
{
839
920
}
840
921
922
+ #if IS_REACHABLE (CONFIG_IIO )
923
+ static int mcp2221_read_raw (struct iio_dev * indio_dev ,
924
+ struct iio_chan_spec const * channel , int * val ,
925
+ int * val2 , long mask )
926
+ {
927
+ struct mcp2221_iio * priv = iio_priv (indio_dev );
928
+ struct mcp2221 * mcp = priv -> mcp ;
929
+ int ret ;
930
+
931
+ if (mask == IIO_CHAN_INFO_SCALE ) {
932
+ if (channel -> output )
933
+ * val = 1 << mcp -> dac_scale ;
934
+ else
935
+ * val = 1 << mcp -> adc_scale ;
936
+
937
+ return IIO_VAL_INT ;
938
+ }
939
+
940
+ mutex_lock (& mcp -> lock );
941
+
942
+ if (channel -> output ) {
943
+ * val = mcp -> dac_value ;
944
+ ret = IIO_VAL_INT ;
945
+ } else {
946
+ /* Read ADC values */
947
+ ret = mcp_chk_last_cmd_status (mcp );
948
+
949
+ if (!ret ) {
950
+ * val = le16_to_cpu (mcp -> adc_values [channel -> address ]);
951
+ if (* val >= BIT (10 ))
952
+ ret = - EINVAL ;
953
+ else
954
+ ret = IIO_VAL_INT ;
955
+ }
956
+ }
957
+
958
+ mutex_unlock (& mcp -> lock );
959
+
960
+ return ret ;
961
+ }
962
+
963
+ static int mcp2221_write_raw (struct iio_dev * indio_dev ,
964
+ struct iio_chan_spec const * chan ,
965
+ int val , int val2 , long mask )
966
+ {
967
+ struct mcp2221_iio * priv = iio_priv (indio_dev );
968
+ struct mcp2221 * mcp = priv -> mcp ;
969
+ int ret ;
970
+
971
+ if (val < 0 || val >= BIT (5 ))
972
+ return - EINVAL ;
973
+
974
+ mutex_lock (& mcp -> lock );
975
+
976
+ memset (mcp -> txbuf , 0 , 12 );
977
+ mcp -> txbuf [0 ] = MCP2221_SET_SRAM_SETTINGS ;
978
+ mcp -> txbuf [4 ] = BIT (7 ) | val ;
979
+
980
+ ret = mcp_send_data_req_status (mcp , mcp -> txbuf , 12 );
981
+ if (!ret )
982
+ mcp -> dac_value = val ;
983
+
984
+ mutex_unlock (& mcp -> lock );
985
+
986
+ return ret ;
987
+ }
988
+
989
+ static const struct iio_info mcp2221_info = {
990
+ .read_raw = & mcp2221_read_raw ,
991
+ .write_raw = & mcp2221_write_raw ,
992
+ };
993
+
994
+ static int mcp_iio_channels (struct mcp2221 * mcp )
995
+ {
996
+ int idx , cnt = 0 ;
997
+ bool dac_created = false;
998
+
999
+ /* GP0 doesn't have ADC/DAC alternative function */
1000
+ for (idx = 1 ; idx < MCP_NGPIO ; idx ++ ) {
1001
+ struct iio_chan_spec * chan = & mcp -> iio_channels [cnt ];
1002
+
1003
+ switch (mcp -> mode [idx ]) {
1004
+ case 2 :
1005
+ chan -> address = idx - 1 ;
1006
+ chan -> channel = cnt ++ ;
1007
+ break ;
1008
+ case 3 :
1009
+ /* GP1 doesn't have DAC alternative function */
1010
+ if (idx == 1 || dac_created )
1011
+ continue ;
1012
+ /* DAC1 and DAC2 outputs are connected to the same DAC */
1013
+ dac_created = true;
1014
+ chan -> output = 1 ;
1015
+ cnt ++ ;
1016
+ break ;
1017
+ default :
1018
+ continue ;
1019
+ };
1020
+
1021
+ chan -> type = IIO_VOLTAGE ;
1022
+ chan -> indexed = 1 ;
1023
+ chan -> info_mask_separate = BIT (IIO_CHAN_INFO_RAW );
1024
+ chan -> info_mask_shared_by_type = BIT (IIO_CHAN_INFO_SCALE );
1025
+ chan -> scan_index = -1 ;
1026
+ }
1027
+
1028
+ return cnt ;
1029
+ }
1030
+
1031
+ static void mcp_init_work (struct work_struct * work )
1032
+ {
1033
+ struct iio_dev * indio_dev ;
1034
+ struct mcp2221 * mcp = container_of (work , struct mcp2221 , init_work .work );
1035
+ struct mcp2221_iio * data ;
1036
+ static int retries = 5 ;
1037
+ int ret , num_channels ;
1038
+
1039
+ hid_hw_power (mcp -> hdev , PM_HINT_FULLON );
1040
+ mutex_lock (& mcp -> lock );
1041
+
1042
+ mcp -> txbuf [0 ] = MCP2221_GET_SRAM_SETTINGS ;
1043
+ ret = mcp_send_data_req_status (mcp , mcp -> txbuf , 1 );
1044
+
1045
+ if (ret == - EAGAIN )
1046
+ goto reschedule_task ;
1047
+
1048
+ num_channels = mcp_iio_channels (mcp );
1049
+ if (!num_channels )
1050
+ goto unlock ;
1051
+
1052
+ mcp -> txbuf [0 ] = MCP2221_READ_FLASH_DATA ;
1053
+ mcp -> txbuf [1 ] = 0 ;
1054
+ ret = mcp_send_data_req_status (mcp , mcp -> txbuf , 2 );
1055
+
1056
+ if (ret == - EAGAIN )
1057
+ goto reschedule_task ;
1058
+
1059
+ indio_dev = devm_iio_device_alloc (& mcp -> hdev -> dev , sizeof (* data ));
1060
+ if (!indio_dev )
1061
+ goto unlock ;
1062
+
1063
+ data = iio_priv (indio_dev );
1064
+ data -> mcp = mcp ;
1065
+
1066
+ indio_dev -> name = "mcp2221" ;
1067
+ indio_dev -> modes = INDIO_DIRECT_MODE ;
1068
+ indio_dev -> info = & mcp2221_info ;
1069
+ indio_dev -> channels = mcp -> iio_channels ;
1070
+ indio_dev -> num_channels = num_channels ;
1071
+
1072
+ devm_iio_device_register (& mcp -> hdev -> dev , indio_dev );
1073
+
1074
+ unlock :
1075
+ mutex_unlock (& mcp -> lock );
1076
+ hid_hw_power (mcp -> hdev , PM_HINT_NORMAL );
1077
+
1078
+ return ;
1079
+
1080
+ reschedule_task :
1081
+ mutex_unlock (& mcp -> lock );
1082
+ hid_hw_power (mcp -> hdev , PM_HINT_NORMAL );
1083
+
1084
+ if (!retries -- )
1085
+ return ;
1086
+
1087
+ /* Device is not ready to read SRAM or FLASH data, try again */
1088
+ schedule_delayed_work (& mcp -> init_work , msecs_to_jiffies (100 ));
1089
+ }
1090
+ #endif
1091
+
841
1092
static int mcp2221_probe (struct hid_device * hdev ,
842
1093
const struct hid_device_id * id )
843
1094
{
@@ -921,6 +1172,11 @@ static int mcp2221_probe(struct hid_device *hdev,
921
1172
return ret ;
922
1173
#endif
923
1174
1175
+ #if IS_REACHABLE (CONFIG_IIO )
1176
+ INIT_DELAYED_WORK (& mcp -> init_work , mcp_init_work );
1177
+ schedule_delayed_work (& mcp -> init_work , msecs_to_jiffies (100 ));
1178
+ #endif
1179
+
924
1180
return 0 ;
925
1181
}
926
1182
0 commit comments