96
96
/* Turn off device regulators etc after 5 seconds of inactivity */
97
97
#define YAS5XX_AUTOSUSPEND_DELAY_MS 5000
98
98
99
+ enum chip_ids {
100
+ yas530 ,
101
+ yas532 ,
102
+ yas533 ,
103
+ };
104
+
99
105
struct yas5xx_calibration {
100
106
/* Linearization calibration x, y1, y2 */
101
107
s32 r [3 ];
@@ -110,12 +116,25 @@ struct yas5xx_calibration {
110
116
u8 dck ;
111
117
};
112
118
119
+ struct yas5xx ;
120
+
121
+ /**
122
+ * struct yas5xx_chip_info - device-specific data and function pointers
123
+ * @devid: device ID number
124
+ * @product_name: product name of the YAS variant
125
+ * @version_names: version letters or namings
126
+ */
127
+ struct yas5xx_chip_info {
128
+ unsigned int devid ;
129
+ char * product_name ;
130
+ char * version_names [2 ];
131
+ };
132
+
113
133
/**
114
134
* struct yas5xx - state container for the YAS5xx driver
115
135
* @dev: parent device pointer
116
- * @devid : device ID number
136
+ * @chip_info : device-specific data
117
137
* @version: device version
118
- * @name: device name
119
138
* @calibration: calibration settings from the OTP storage
120
139
* @hard_offsets: offsets for each axis measured with initcoil actuated
121
140
* @orientation: mounting matrix, flipped axis etc
@@ -129,9 +148,8 @@ struct yas5xx_calibration {
129
148
*/
130
149
struct yas5xx {
131
150
struct device * dev ;
132
- unsigned int devid ;
151
+ const struct yas5xx_chip_info * chip_info ;
133
152
unsigned int version ;
134
- char name [16 ];
135
153
struct yas5xx_calibration calibration ;
136
154
s8 hard_offsets [3 ];
137
155
struct iio_mount_matrix orientation ;
@@ -192,6 +210,7 @@ static u16 yas532_extract_axis(u8 *data)
192
210
*/
193
211
static int yas530_measure (struct yas5xx * yas5xx , u16 * t , u16 * x , u16 * y1 , u16 * y2 )
194
212
{
213
+ const struct yas5xx_chip_info * ci = yas5xx -> chip_info ;
195
214
unsigned int busy ;
196
215
u8 data [8 ];
197
216
int ret ;
@@ -222,7 +241,7 @@ static int yas530_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y
222
241
223
242
mutex_unlock (& yas5xx -> lock );
224
243
225
- switch (yas5xx -> devid ) {
244
+ switch (ci -> devid ) {
226
245
case YAS530_DEVICE_ID :
227
246
/*
228
247
* The t value is 9 bits in big endian format
@@ -267,6 +286,7 @@ static int yas530_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y
267
286
/* Used by YAS530, YAS532 and YAS533 */
268
287
static s32 yas530_linearize (struct yas5xx * yas5xx , u16 val , int axis )
269
288
{
289
+ const struct yas5xx_chip_info * ci = yas5xx -> chip_info ;
270
290
struct yas5xx_calibration * c = & yas5xx -> calibration ;
271
291
static const s32 yas532ac_coef [] = {
272
292
YAS532_VERSION_AC_COEF_X ,
@@ -276,7 +296,7 @@ static s32 yas530_linearize(struct yas5xx *yas5xx, u16 val, int axis)
276
296
s32 coef ;
277
297
278
298
/* Select coefficients */
279
- switch (yas5xx -> devid ) {
299
+ switch (ci -> devid ) {
280
300
case YAS530_DEVICE_ID :
281
301
if (yas5xx -> version == YAS530_VERSION_A )
282
302
coef = YAS530_VERSION_A_COEF ;
@@ -319,6 +339,7 @@ static s32 yas530_linearize(struct yas5xx *yas5xx, u16 val, int axis)
319
339
*/
320
340
static int yas530_get_measure (struct yas5xx * yas5xx , s32 * to , s32 * xo , s32 * yo , s32 * zo )
321
341
{
342
+ const struct yas5xx_chip_info * ci = yas5xx -> chip_info ;
322
343
struct yas5xx_calibration * c = & yas5xx -> calibration ;
323
344
u16 t_ref , t , x , y1 , y2 ;
324
345
/* These are signed x, signed y1 etc */
@@ -336,7 +357,7 @@ static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo,
336
357
sy2 = yas530_linearize (yas5xx , y2 , 2 );
337
358
338
359
/* Set the temperature reference value (unit: counts) */
339
- switch (yas5xx -> devid ) {
360
+ switch (ci -> devid ) {
340
361
case YAS530_DEVICE_ID :
341
362
t_ref = YAS530_20DEGREES ;
342
363
break ;
@@ -349,7 +370,7 @@ static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo,
349
370
}
350
371
351
372
/* Temperature compensation for x, y1, y2 respectively */
352
- if (yas5xx -> devid == YAS532_DEVICE_ID &&
373
+ if (ci -> devid == YAS532_DEVICE_ID &&
353
374
yas5xx -> version == YAS532_VERSION_AC ) {
354
375
/*
355
376
* YAS532 version AC uses the temperature deviation as a
@@ -384,7 +405,7 @@ static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo,
384
405
sz = - sy1 - sy2 ;
385
406
386
407
/* Process temperature readout */
387
- switch (yas5xx -> devid ) {
408
+ switch (ci -> devid ) {
388
409
case YAS530_DEVICE_ID :
389
410
/*
390
411
* Raw temperature value t is the number of counts starting
@@ -442,6 +463,7 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
442
463
long mask )
443
464
{
444
465
struct yas5xx * yas5xx = iio_priv (indio_dev );
466
+ const struct yas5xx_chip_info * ci = yas5xx -> chip_info ;
445
467
s32 t , x , y , z ;
446
468
int ret ;
447
469
@@ -473,7 +495,7 @@ static int yas5xx_read_raw(struct iio_dev *indio_dev,
473
495
}
474
496
return IIO_VAL_INT ;
475
497
case IIO_CHAN_INFO_SCALE :
476
- switch (yas5xx -> devid ) {
498
+ switch (ci -> devid ) {
477
499
case YAS530_DEVICE_ID :
478
500
/*
479
501
* Raw values of YAS530 are in picotesla. Divide by
@@ -802,6 +824,7 @@ static s8 yas530_adjust_offset(s8 old, int bit, u16 center, u16 measure)
802
824
/* Used by YAS530, YAS532 and YAS533 */
803
825
static int yas530_measure_offsets (struct yas5xx * yas5xx )
804
826
{
827
+ const struct yas5xx_chip_info * ci = yas5xx -> chip_info ;
805
828
int ret ;
806
829
u16 center ;
807
830
u16 t , x , y1 , y2 ;
@@ -814,7 +837,7 @@ static int yas530_measure_offsets(struct yas5xx *yas5xx)
814
837
return ret ;
815
838
816
839
/* When the initcoil is active this should be around the center */
817
- switch (yas5xx -> devid ) {
840
+ switch (ci -> devid ) {
818
841
case YAS530_DEVICE_ID :
819
842
center = YAS530_DATA_CENTER ;
820
843
break ;
@@ -895,12 +918,32 @@ static int yas530_power_on(struct yas5xx *yas5xx)
895
918
return regmap_write (yas5xx -> map , YAS530_MEASURE_INTERVAL , 0 );
896
919
}
897
920
921
+ static const struct yas5xx_chip_info yas5xx_chip_info_tbl [] = {
922
+ [yas530 ] = {
923
+ .devid = YAS530_DEVICE_ID ,
924
+ .product_name = "YAS530 MS-3E" ,
925
+ .version_names = { "A" , "B" },
926
+ },
927
+ [yas532 ] = {
928
+ .devid = YAS532_DEVICE_ID ,
929
+ .product_name = "YAS532 MS-3R" ,
930
+ .version_names = { "AB" , "AC" },
931
+ },
932
+ [yas533 ] = {
933
+ .devid = YAS532_DEVICE_ID ,
934
+ .product_name = "YAS533 MS-3F" ,
935
+ .version_names = { "AB" , "AC" },
936
+ },
937
+ };
938
+
898
939
static int yas5xx_probe (struct i2c_client * i2c ,
899
940
const struct i2c_device_id * id )
900
941
{
901
942
struct iio_dev * indio_dev ;
902
943
struct device * dev = & i2c -> dev ;
903
944
struct yas5xx * yas5xx ;
945
+ const struct yas5xx_chip_info * ci ;
946
+ int id_check ;
904
947
int ret ;
905
948
906
949
indio_dev = devm_iio_device_alloc (dev , sizeof (* yas5xx ));
@@ -947,33 +990,40 @@ static int yas5xx_probe(struct i2c_client *i2c,
947
990
goto assert_reset ;
948
991
}
949
992
950
- ret = regmap_read (yas5xx -> map , YAS5XX_DEVICE_ID , & yas5xx -> devid );
993
+ yas5xx -> chip_info = & yas5xx_chip_info_tbl [id -> driver_data ];
994
+ ci = yas5xx -> chip_info ;
995
+
996
+ ret = regmap_read (yas5xx -> map , YAS5XX_DEVICE_ID , & id_check );
951
997
if (ret )
952
998
goto assert_reset ;
953
999
954
- switch (yas5xx -> devid ) {
1000
+ if (id_check != ci -> devid ) {
1001
+ ret = dev_err_probe (dev , - ENODEV ,
1002
+ "device ID %02x doesn't match %s\n" ,
1003
+ id_check , id -> name );
1004
+ goto assert_reset ;
1005
+ }
1006
+
1007
+ switch (ci -> devid ) {
955
1008
case YAS530_DEVICE_ID :
956
1009
ret = yas530_get_calibration_data (yas5xx );
957
1010
if (ret )
958
1011
goto assert_reset ;
959
- dev_info (dev , "detected YAS530 MS-3E %s" ,
960
- yas5xx -> version ? "B" : "A" );
961
- strncpy (yas5xx -> name , "yas530" , sizeof (yas5xx -> name ));
962
1012
break ;
963
1013
case YAS532_DEVICE_ID :
964
1014
ret = yas532_get_calibration_data (yas5xx );
965
1015
if (ret )
966
1016
goto assert_reset ;
967
- dev_info (dev , "detected YAS532/YAS533 MS-3R/F %s" ,
968
- yas5xx -> version ? "AC" : "AB" );
969
- strncpy (yas5xx -> name , "yas532" , sizeof (yas5xx -> name ));
970
1017
break ;
971
1018
default :
972
1019
ret = - ENODEV ;
973
- dev_err (dev , "unhandled device ID %02x\n" , yas5xx -> devid );
1020
+ dev_err (dev , "unhandled device ID %02x\n" , ci -> devid );
974
1021
goto assert_reset ;
975
1022
}
976
1023
1024
+ dev_info (dev , "detected %s %s\n" , ci -> product_name ,
1025
+ ci -> version_names [yas5xx -> version ]);
1026
+
977
1027
yas530_dump_calibration (yas5xx );
978
1028
ret = yas530_power_on (yas5xx );
979
1029
if (ret )
@@ -985,7 +1035,7 @@ static int yas5xx_probe(struct i2c_client *i2c,
985
1035
indio_dev -> info = & yas5xx_info ;
986
1036
indio_dev -> available_scan_masks = yas5xx_scan_masks ;
987
1037
indio_dev -> modes = INDIO_DIRECT_MODE ;
988
- indio_dev -> name = yas5xx -> name ;
1038
+ indio_dev -> name = id -> name ;
989
1039
indio_dev -> channels = yas5xx_channels ;
990
1040
indio_dev -> num_channels = ARRAY_SIZE (yas5xx_channels );
991
1041
@@ -1096,9 +1146,9 @@ static DEFINE_RUNTIME_DEV_PM_OPS(yas5xx_dev_pm_ops, yas5xx_runtime_suspend,
1096
1146
yas5xx_runtime_resume , NULL) ;
1097
1147
1098
1148
static const struct i2c_device_id yas5xx_id [] = {
1099
- {"yas530" , },
1100
- {"yas532" , },
1101
- {"yas533" , },
1149
+ {"yas530" , yas530 },
1150
+ {"yas532" , yas532 },
1151
+ {"yas533" , yas533 },
1102
1152
{}
1103
1153
};
1104
1154
MODULE_DEVICE_TABLE (i2c , yas5xx_id );
0 commit comments