2828struct hx711_gain_to_scale {
2929 int gain ;
3030 int gain_pulse ;
31- int scale ;
3231 int channel ;
3332};
3433
3534/*
3635 * .scale depends on AVDD which in turn is known as soon as the regulator
37- * is available
38- * therefore we set .scale in hx711_probe()
36+ * is available; it is stored per device in hx711_data.gain_scale[]
3937 *
4038 * channel A in documentation is channel 0 in source code
4139 * channel B in documentation is channel 1 in source code
4240 */
43- static struct hx711_gain_to_scale hx711_gain_to_scale [HX711_GAIN_MAX ] = {
44- { 128 , 1 , 0 , 0 },
45- { 32 , 2 , 0 , 1 },
46- { 64 , 3 , 0 , 0 }
41+ static const struct hx711_gain_to_scale hx711_gain_to_scale [HX711_GAIN_MAX ] = {
42+ { 128 , 1 , 0 },
43+ { 32 , 2 , 1 },
44+ { 64 , 3 , 0 },
4745};
4846
4947static int hx711_get_gain_to_pulse (int gain )
@@ -56,22 +54,22 @@ static int hx711_get_gain_to_pulse(int gain)
5654 return 1 ;
5755}
5856
59- static int hx711_get_gain_to_scale (int gain )
57+ static int hx711_get_gain_to_scale (const int * gain_scale , int gain )
6058{
6159 int i ;
6260
6361 for (i = 0 ; i < HX711_GAIN_MAX ; i ++ )
6462 if (hx711_gain_to_scale [i ].gain == gain )
65- return hx711_gain_to_scale [i ]. scale ;
63+ return gain_scale [i ];
6664 return 0 ;
6765}
6866
69- static int hx711_get_scale_to_gain (int scale )
67+ static int hx711_get_scale_to_gain (const int * gain_scale , int scale )
7068{
7169 int i ;
7270
7371 for (i = 0 ; i < HX711_GAIN_MAX ; i ++ )
74- if (hx711_gain_to_scale [i ]. scale == scale )
72+ if (gain_scale [i ] == scale )
7573 return hx711_gain_to_scale [i ].gain ;
7674 return - EINVAL ;
7775}
@@ -82,6 +80,7 @@ struct hx711_data {
8280 struct gpio_desc * gpiod_dout ;
8381 int gain_set ; /* gain set on device */
8482 int gain_chan_a ; /* gain for channel A */
83+ int gain_scale [HX711_GAIN_MAX ];
8584 struct mutex lock ;
8685 /*
8786 * triggered buffer
@@ -290,7 +289,8 @@ static int hx711_read_raw(struct iio_dev *indio_dev,
290289 * val = 0 ;
291290 mutex_lock (& hx711_data -> lock );
292291
293- * val2 = hx711_get_gain_to_scale (hx711_data -> gain_set );
292+ * val2 = hx711_get_gain_to_scale (hx711_data -> gain_scale ,
293+ hx711_data -> gain_set );
294294
295295 mutex_unlock (& hx711_data -> lock );
296296
@@ -321,7 +321,7 @@ static int hx711_write_raw(struct iio_dev *indio_dev,
321321
322322 mutex_lock (& hx711_data -> lock );
323323
324- gain = hx711_get_scale_to_gain (val2 );
324+ gain = hx711_get_scale_to_gain (hx711_data -> gain_scale , val2 );
325325 if (gain < 0 ) {
326326 mutex_unlock (& hx711_data -> lock );
327327 return gain ;
@@ -386,14 +386,15 @@ static ssize_t hx711_scale_available_show(struct device *dev,
386386 struct device_attribute * attr ,
387387 char * buf )
388388{
389+ struct hx711_data * hx711_data = iio_priv (dev_to_iio_dev (dev ));
389390 struct iio_dev_attr * iio_attr = to_iio_dev_attr (attr );
390391 int channel = iio_attr -> address ;
391392 int i , len = 0 ;
392393
393394 for (i = 0 ; i < HX711_GAIN_MAX ; i ++ )
394395 if (hx711_gain_to_scale [i ].channel == channel )
395396 len += sprintf (buf + len , "0.%09d " ,
396- hx711_gain_to_scale [i ]. scale );
397+ hx711_data -> gain_scale [i ]);
397398
398399 len += sprintf (buf + len , "\n" );
399400
@@ -511,7 +512,7 @@ static int hx711_probe(struct platform_device *pdev)
511512 ret *= 100 ;
512513
513514 for (i = 0 ; i < HX711_GAIN_MAX ; i ++ )
514- hx711_gain_to_scale [i ]. scale =
515+ hx711_data -> gain_scale [i ] =
515516 ret / hx711_gain_to_scale [i ].gain / 1678 ;
516517
517518 hx711_data -> gain_set = 128 ;
0 commit comments