2 * HX711: analog to digital converter for weight sensor module
4 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/delay.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/gpio/consumer.h>
31 #include <linux/regulator/consumer.h>
33 /* gain to pulse and scale conversion */
34 #define HX711_GAIN_MAX 3
36 struct hx711_gain_to_scale
{
44 * .scale depends on AVDD which in turn is known as soon as the regulator
46 * therefore we set .scale in hx711_probe()
48 * channel A in documentation is channel 0 in source code
49 * channel B in documentation is channel 1 in source code
51 static struct hx711_gain_to_scale hx711_gain_to_scale
[HX711_GAIN_MAX
] = {
57 static int hx711_get_gain_to_pulse(int gain
)
61 for (i
= 0; i
< HX711_GAIN_MAX
; i
++)
62 if (hx711_gain_to_scale
[i
].gain
== gain
)
63 return hx711_gain_to_scale
[i
].gain_pulse
;
67 static int hx711_get_gain_to_scale(int gain
)
71 for (i
= 0; i
< HX711_GAIN_MAX
; i
++)
72 if (hx711_gain_to_scale
[i
].gain
== gain
)
73 return hx711_gain_to_scale
[i
].scale
;
77 static int hx711_get_scale_to_gain(int scale
)
81 for (i
= 0; i
< HX711_GAIN_MAX
; i
++)
82 if (hx711_gain_to_scale
[i
].scale
== scale
)
83 return hx711_gain_to_scale
[i
].gain
;
89 struct gpio_desc
*gpiod_pd_sck
;
90 struct gpio_desc
*gpiod_dout
;
91 struct regulator
*reg_avdd
;
92 int gain_set
; /* gain set on device */
93 int gain_chan_a
; /* gain for channel A */
97 * 2x32-bit channel + 64-bit timestamp
102 static int hx711_cycle(struct hx711_data
*hx711_data
)
107 * if preempted for more then 60us while PD_SCK is high:
108 * hx711 is going in reset
109 * ==> measuring is false
112 gpiod_set_value(hx711_data
->gpiod_pd_sck
, 1);
113 val
= gpiod_get_value(hx711_data
->gpiod_dout
);
115 * here we are not waiting for 0.2 us as suggested by the datasheet,
116 * because the oscilloscope showed in a test scenario
117 * at least 1.15 us for PD_SCK high (T3 in datasheet)
118 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
120 gpiod_set_value(hx711_data
->gpiod_pd_sck
, 0);
126 static int hx711_read(struct hx711_data
*hx711_data
)
130 int val
= gpiod_get_value(hx711_data
->gpiod_dout
);
132 /* we double check if it's really down */
136 for (i
= 0; i
< 24; i
++) {
138 ret
= hx711_cycle(hx711_data
);
145 for (i
= 0; i
< hx711_get_gain_to_pulse(hx711_data
->gain_set
); i
++)
146 hx711_cycle(hx711_data
);
151 static int hx711_wait_for_ready(struct hx711_data
*hx711_data
)
156 * in some rare cases the reset takes quite a long time
157 * especially when the channel is changed.
158 * Allow up to one second for it
160 for (i
= 0; i
< 100; i
++) {
161 val
= gpiod_get_value(hx711_data
->gpiod_dout
);
164 /* sleep at least 10 ms */
173 static int hx711_reset(struct hx711_data
*hx711_data
)
176 int val
= gpiod_get_value(hx711_data
->gpiod_dout
);
180 * an examination with the oszilloscope indicated
181 * that the first value read after the reset is not stable
182 * if we reset too short;
183 * the shorter the reset cycle
184 * the less reliable the first value after reset is;
185 * there were no problems encountered with a value
188 gpiod_set_value(hx711_data
->gpiod_pd_sck
, 1);
190 gpiod_set_value(hx711_data
->gpiod_pd_sck
, 0);
192 ret
= hx711_wait_for_ready(hx711_data
);
196 * after a reset the gain is 128 so we do a dummy read
197 * to set the gain for the next read
199 ret
= hx711_read(hx711_data
);
204 * after a dummy read we need to wait vor readiness
205 * for not mixing gain pulses with the clock
207 val
= hx711_wait_for_ready(hx711_data
);
213 static int hx711_set_gain_for_channel(struct hx711_data
*hx711_data
, int chan
)
218 if (hx711_data
->gain_set
== 32) {
219 hx711_data
->gain_set
= hx711_data
->gain_chan_a
;
221 ret
= hx711_read(hx711_data
);
225 ret
= hx711_wait_for_ready(hx711_data
);
230 if (hx711_data
->gain_set
!= 32) {
231 hx711_data
->gain_set
= 32;
233 ret
= hx711_read(hx711_data
);
237 ret
= hx711_wait_for_ready(hx711_data
);
246 static int hx711_reset_read(struct hx711_data
*hx711_data
, int chan
)
252 * hx711_reset() must be called from here
253 * because it could be calling hx711_read() by itself
255 if (hx711_reset(hx711_data
)) {
256 dev_err(hx711_data
->dev
, "reset failed!");
260 ret
= hx711_set_gain_for_channel(hx711_data
, chan
);
264 val
= hx711_read(hx711_data
);
269 static int hx711_read_raw(struct iio_dev
*indio_dev
,
270 const struct iio_chan_spec
*chan
,
271 int *val
, int *val2
, long mask
)
273 struct hx711_data
*hx711_data
= iio_priv(indio_dev
);
276 case IIO_CHAN_INFO_RAW
:
277 mutex_lock(&hx711_data
->lock
);
279 *val
= hx711_reset_read(hx711_data
, chan
->channel
);
281 mutex_unlock(&hx711_data
->lock
);
286 case IIO_CHAN_INFO_SCALE
:
288 mutex_lock(&hx711_data
->lock
);
290 *val2
= hx711_get_gain_to_scale(hx711_data
->gain_set
);
292 mutex_unlock(&hx711_data
->lock
);
294 return IIO_VAL_INT_PLUS_NANO
;
300 static int hx711_write_raw(struct iio_dev
*indio_dev
,
301 struct iio_chan_spec
const *chan
,
306 struct hx711_data
*hx711_data
= iio_priv(indio_dev
);
311 case IIO_CHAN_INFO_SCALE
:
313 * a scale greater than 1 mV per LSB is not possible
314 * with the HX711, therefore val must be 0
319 mutex_lock(&hx711_data
->lock
);
321 gain
= hx711_get_scale_to_gain(val2
);
323 mutex_unlock(&hx711_data
->lock
);
327 if (gain
!= hx711_data
->gain_set
) {
328 hx711_data
->gain_set
= gain
;
330 hx711_data
->gain_chan_a
= gain
;
332 ret
= hx711_read(hx711_data
);
334 mutex_unlock(&hx711_data
->lock
);
339 mutex_unlock(&hx711_data
->lock
);
348 static int hx711_write_raw_get_fmt(struct iio_dev
*indio_dev
,
349 struct iio_chan_spec
const *chan
,
352 return IIO_VAL_INT_PLUS_NANO
;
355 static irqreturn_t
hx711_trigger(int irq
, void *p
)
357 struct iio_poll_func
*pf
= p
;
358 struct iio_dev
*indio_dev
= pf
->indio_dev
;
359 struct hx711_data
*hx711_data
= iio_priv(indio_dev
);
362 mutex_lock(&hx711_data
->lock
);
364 memset(hx711_data
->buffer
, 0, sizeof(hx711_data
->buffer
));
366 for (i
= 0; i
< indio_dev
->masklength
; i
++) {
367 if (!test_bit(i
, indio_dev
->active_scan_mask
))
370 hx711_data
->buffer
[j
] = hx711_reset_read(hx711_data
,
371 indio_dev
->channels
[i
].channel
);
375 iio_push_to_buffers_with_timestamp(indio_dev
, hx711_data
->buffer
,
378 mutex_unlock(&hx711_data
->lock
);
380 iio_trigger_notify_done(indio_dev
->trig
);
385 static ssize_t
hx711_scale_available_show(struct device
*dev
,
386 struct device_attribute
*attr
,
389 struct iio_dev_attr
*iio_attr
= to_iio_dev_attr(attr
);
390 int channel
= iio_attr
->address
;
393 for (i
= 0; i
< HX711_GAIN_MAX
; i
++)
394 if (hx711_gain_to_scale
[i
].channel
== channel
)
395 len
+= sprintf(buf
+ len
, "0.%09d ",
396 hx711_gain_to_scale
[i
].scale
);
398 len
+= sprintf(buf
+ len
, "\n");
403 static IIO_DEVICE_ATTR(in_voltage0_scale_available
, S_IRUGO
,
404 hx711_scale_available_show
, NULL
, 0);
406 static IIO_DEVICE_ATTR(in_voltage1_scale_available
, S_IRUGO
,
407 hx711_scale_available_show
, NULL
, 1);
409 static struct attribute
*hx711_attributes
[] = {
410 &iio_dev_attr_in_voltage0_scale_available
.dev_attr
.attr
,
411 &iio_dev_attr_in_voltage1_scale_available
.dev_attr
.attr
,
415 static const struct attribute_group hx711_attribute_group
= {
416 .attrs
= hx711_attributes
,
419 static const struct iio_info hx711_iio_info
= {
420 .read_raw
= hx711_read_raw
,
421 .write_raw
= hx711_write_raw
,
422 .write_raw_get_fmt
= hx711_write_raw_get_fmt
,
423 .attrs
= &hx711_attribute_group
,
426 static const struct iio_chan_spec hx711_chan_spec
[] = {
431 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
432 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
438 .endianness
= IIO_CPU
,
445 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
446 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
452 .endianness
= IIO_CPU
,
455 IIO_CHAN_SOFT_TIMESTAMP(2),
458 static int hx711_probe(struct platform_device
*pdev
)
460 struct device
*dev
= &pdev
->dev
;
461 struct hx711_data
*hx711_data
;
462 struct iio_dev
*indio_dev
;
466 indio_dev
= devm_iio_device_alloc(dev
, sizeof(struct hx711_data
));
468 dev_err(dev
, "failed to allocate IIO device\n");
472 hx711_data
= iio_priv(indio_dev
);
473 hx711_data
->dev
= dev
;
475 mutex_init(&hx711_data
->lock
);
478 * PD_SCK stands for power down and serial clock input of HX711
479 * in the driver it is an output
481 hx711_data
->gpiod_pd_sck
= devm_gpiod_get(dev
, "sck", GPIOD_OUT_LOW
);
482 if (IS_ERR(hx711_data
->gpiod_pd_sck
)) {
483 dev_err(dev
, "failed to get sck-gpiod: err=%ld\n",
484 PTR_ERR(hx711_data
->gpiod_pd_sck
));
485 return PTR_ERR(hx711_data
->gpiod_pd_sck
);
489 * DOUT stands for serial data output of HX711
490 * for the driver it is an input
492 hx711_data
->gpiod_dout
= devm_gpiod_get(dev
, "dout", GPIOD_IN
);
493 if (IS_ERR(hx711_data
->gpiod_dout
)) {
494 dev_err(dev
, "failed to get dout-gpiod: err=%ld\n",
495 PTR_ERR(hx711_data
->gpiod_dout
));
496 return PTR_ERR(hx711_data
->gpiod_dout
);
499 hx711_data
->reg_avdd
= devm_regulator_get(dev
, "avdd");
500 if (IS_ERR(hx711_data
->reg_avdd
))
501 return PTR_ERR(hx711_data
->reg_avdd
);
503 ret
= regulator_enable(hx711_data
->reg_avdd
);
509 * full scale differential input range: AVDD / GAIN
510 * full scale output data: 2^24
514 * 1 LSB = AVDD / GAIN / 2^24
515 * AVDD is in uV, but we need 10^-9 mV
516 * approximately to fit into a 32 bit number:
517 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
519 ret
= regulator_get_voltage(hx711_data
->reg_avdd
);
521 goto error_regulator
;
523 /* we need 10^-9 mV */
526 for (i
= 0; i
< HX711_GAIN_MAX
; i
++)
527 hx711_gain_to_scale
[i
].scale
=
528 ret
/ hx711_gain_to_scale
[i
].gain
/ 1678;
530 hx711_data
->gain_set
= 128;
531 hx711_data
->gain_chan_a
= 128;
533 platform_set_drvdata(pdev
, indio_dev
);
535 indio_dev
->name
= "hx711";
536 indio_dev
->dev
.parent
= &pdev
->dev
;
537 indio_dev
->info
= &hx711_iio_info
;
538 indio_dev
->modes
= INDIO_DIRECT_MODE
;
539 indio_dev
->channels
= hx711_chan_spec
;
540 indio_dev
->num_channels
= ARRAY_SIZE(hx711_chan_spec
);
542 ret
= iio_triggered_buffer_setup(indio_dev
, iio_pollfunc_store_time
,
543 hx711_trigger
, NULL
);
545 dev_err(dev
, "setup of iio triggered buffer failed\n");
546 goto error_regulator
;
549 ret
= iio_device_register(indio_dev
);
551 dev_err(dev
, "Couldn't register the device\n");
558 iio_triggered_buffer_cleanup(indio_dev
);
561 regulator_disable(hx711_data
->reg_avdd
);
566 static int hx711_remove(struct platform_device
*pdev
)
568 struct hx711_data
*hx711_data
;
569 struct iio_dev
*indio_dev
;
571 indio_dev
= platform_get_drvdata(pdev
);
572 hx711_data
= iio_priv(indio_dev
);
574 iio_device_unregister(indio_dev
);
576 iio_triggered_buffer_cleanup(indio_dev
);
578 regulator_disable(hx711_data
->reg_avdd
);
583 static const struct of_device_id of_hx711_match
[] = {
584 { .compatible
= "avia,hx711", },
588 MODULE_DEVICE_TABLE(of
, of_hx711_match
);
590 static struct platform_driver hx711_driver
= {
591 .probe
= hx711_probe
,
592 .remove
= hx711_remove
,
594 .name
= "hx711-gpio",
595 .of_match_table
= of_hx711_match
,
599 module_platform_driver(hx711_driver
);
601 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
602 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
603 MODULE_LICENSE("GPL");
604 MODULE_ALIAS("platform:hx711-gpio");