1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HX711: analog to digital converter for weight sensor module
5 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/property.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/delay.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/trigger_consumer.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/regulator/consumer.h>
24 /* gain to pulse and scale conversion */
25 #define HX711_GAIN_MAX 3
27 struct hx711_gain_to_scale {
35 * .scale depends on AVDD which in turn is known as soon as the regulator
37 * therefore we set .scale in hx711_probe()
39 * channel A in documentation is channel 0 in source code
40 * channel B in documentation is channel 1 in source code
42 static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
48 static int hx711_get_gain_to_pulse(int gain)
52 for (i = 0; i < HX711_GAIN_MAX; i++)
53 if (hx711_gain_to_scale[i].gain == gain)
54 return hx711_gain_to_scale[i].gain_pulse;
58 static int hx711_get_gain_to_scale(int gain)
62 for (i = 0; i < HX711_GAIN_MAX; i++)
63 if (hx711_gain_to_scale[i].gain == gain)
64 return hx711_gain_to_scale[i].scale;
68 static int hx711_get_scale_to_gain(int scale)
72 for (i = 0; i < HX711_GAIN_MAX; i++)
73 if (hx711_gain_to_scale[i].scale == scale)
74 return hx711_gain_to_scale[i].gain;
80 struct gpio_desc *gpiod_pd_sck;
81 struct gpio_desc *gpiod_dout;
82 struct regulator *reg_avdd;
83 int gain_set; /* gain set on device */
84 int gain_chan_a; /* gain for channel A */
88 * 2x32-bit channel + 64-bit timestamp
92 * delay after a rising edge on SCK until the data is ready DOUT
93 * this is dependent on the hx711 where the datasheet tells a
94 * maximum value of 100 ns
95 * but also on potential parasitic capacities on the wiring
97 u32 data_ready_delay_ns;
101 static int hx711_cycle(struct hx711_data *hx711_data)
106 * if preempted for more then 60us while PD_SCK is high:
107 * hx711 is going in reset
108 * ==> measuring is false
110 local_irq_save(flags);
111 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
114 * wait until DOUT is ready
115 * it turned out that parasitic capacities are extending the time
116 * until DOUT has reached it's value
118 ndelay(hx711_data->data_ready_delay_ns);
121 * here we are not waiting for 0.2 us as suggested by the datasheet,
122 * because the oscilloscope showed in a test scenario
123 * at least 1.15 us for PD_SCK high (T3 in datasheet)
124 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
126 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
127 local_irq_restore(flags);
130 * make it a square wave for addressing cases with capacitance on
133 ndelay(hx711_data->data_ready_delay_ns);
135 /* sample as late as possible */
136 return gpiod_get_value(hx711_data->gpiod_dout);
139 static int hx711_read(struct hx711_data *hx711_data)
143 int val = gpiod_get_value(hx711_data->gpiod_dout);
145 /* we double check if it's really down */
149 for (i = 0; i < 24; i++) {
151 ret = hx711_cycle(hx711_data);
158 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
159 hx711_cycle(hx711_data);
164 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
169 * in some rare cases the reset takes quite a long time
170 * especially when the channel is changed.
171 * Allow up to one second for it
173 for (i = 0; i < 100; i++) {
174 val = gpiod_get_value(hx711_data->gpiod_dout);
177 /* sleep at least 10 ms */
186 static int hx711_reset(struct hx711_data *hx711_data)
189 int val = gpiod_get_value(hx711_data->gpiod_dout);
193 * an examination with the oszilloscope indicated
194 * that the first value read after the reset is not stable
195 * if we reset too short;
196 * the shorter the reset cycle
197 * the less reliable the first value after reset is;
198 * there were no problems encountered with a value
201 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
203 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
205 ret = hx711_wait_for_ready(hx711_data);
209 * after a reset the gain is 128 so we do a dummy read
210 * to set the gain for the next read
212 ret = hx711_read(hx711_data);
217 * after a dummy read we need to wait vor readiness
218 * for not mixing gain pulses with the clock
220 val = hx711_wait_for_ready(hx711_data);
226 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
231 if (hx711_data->gain_set == 32) {
232 hx711_data->gain_set = hx711_data->gain_chan_a;
234 ret = hx711_read(hx711_data);
238 ret = hx711_wait_for_ready(hx711_data);
243 if (hx711_data->gain_set != 32) {
244 hx711_data->gain_set = 32;
246 ret = hx711_read(hx711_data);
250 ret = hx711_wait_for_ready(hx711_data);
259 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
265 * hx711_reset() must be called from here
266 * because it could be calling hx711_read() by itself
268 if (hx711_reset(hx711_data)) {
269 dev_err(hx711_data->dev, "reset failed!");
273 ret = hx711_set_gain_for_channel(hx711_data, chan);
277 val = hx711_read(hx711_data);
282 static int hx711_read_raw(struct iio_dev *indio_dev,
283 const struct iio_chan_spec *chan,
284 int *val, int *val2, long mask)
286 struct hx711_data *hx711_data = iio_priv(indio_dev);
289 case IIO_CHAN_INFO_RAW:
290 mutex_lock(&hx711_data->lock);
292 *val = hx711_reset_read(hx711_data, chan->channel);
294 mutex_unlock(&hx711_data->lock);
299 case IIO_CHAN_INFO_SCALE:
301 mutex_lock(&hx711_data->lock);
303 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
305 mutex_unlock(&hx711_data->lock);
307 return IIO_VAL_INT_PLUS_NANO;
313 static int hx711_write_raw(struct iio_dev *indio_dev,
314 struct iio_chan_spec const *chan,
319 struct hx711_data *hx711_data = iio_priv(indio_dev);
324 case IIO_CHAN_INFO_SCALE:
326 * a scale greater than 1 mV per LSB is not possible
327 * with the HX711, therefore val must be 0
332 mutex_lock(&hx711_data->lock);
334 gain = hx711_get_scale_to_gain(val2);
336 mutex_unlock(&hx711_data->lock);
340 if (gain != hx711_data->gain_set) {
341 hx711_data->gain_set = gain;
343 hx711_data->gain_chan_a = gain;
345 ret = hx711_read(hx711_data);
347 mutex_unlock(&hx711_data->lock);
352 mutex_unlock(&hx711_data->lock);
361 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
362 struct iio_chan_spec const *chan,
365 return IIO_VAL_INT_PLUS_NANO;
368 static irqreturn_t hx711_trigger(int irq, void *p)
370 struct iio_poll_func *pf = p;
371 struct iio_dev *indio_dev = pf->indio_dev;
372 struct hx711_data *hx711_data = iio_priv(indio_dev);
375 mutex_lock(&hx711_data->lock);
377 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
379 for (i = 0; i < indio_dev->masklength; i++) {
380 if (!test_bit(i, indio_dev->active_scan_mask))
383 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
384 indio_dev->channels[i].channel);
388 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
391 mutex_unlock(&hx711_data->lock);
393 iio_trigger_notify_done(indio_dev->trig);
398 static ssize_t hx711_scale_available_show(struct device *dev,
399 struct device_attribute *attr,
402 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
403 int channel = iio_attr->address;
406 for (i = 0; i < HX711_GAIN_MAX; i++)
407 if (hx711_gain_to_scale[i].channel == channel)
408 len += sprintf(buf + len, "0.%09d ",
409 hx711_gain_to_scale[i].scale);
411 len += sprintf(buf + len, "\n");
416 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
417 hx711_scale_available_show, NULL, 0);
419 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
420 hx711_scale_available_show, NULL, 1);
422 static struct attribute *hx711_attributes[] = {
423 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
424 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
428 static const struct attribute_group hx711_attribute_group = {
429 .attrs = hx711_attributes,
432 static const struct iio_info hx711_iio_info = {
433 .read_raw = hx711_read_raw,
434 .write_raw = hx711_write_raw,
435 .write_raw_get_fmt = hx711_write_raw_get_fmt,
436 .attrs = &hx711_attribute_group,
439 static const struct iio_chan_spec hx711_chan_spec[] = {
444 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
445 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
451 .endianness = IIO_CPU,
458 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
459 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
465 .endianness = IIO_CPU,
468 IIO_CHAN_SOFT_TIMESTAMP(2),
471 static int hx711_probe(struct platform_device *pdev)
473 struct device *dev = &pdev->dev;
474 struct device_node *np = dev->of_node;
475 struct hx711_data *hx711_data;
476 struct iio_dev *indio_dev;
480 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
482 dev_err(dev, "failed to allocate IIO device\n");
486 hx711_data = iio_priv(indio_dev);
487 hx711_data->dev = dev;
489 mutex_init(&hx711_data->lock);
492 * PD_SCK stands for power down and serial clock input of HX711
493 * in the driver it is an output
495 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
496 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
497 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
498 PTR_ERR(hx711_data->gpiod_pd_sck));
499 return PTR_ERR(hx711_data->gpiod_pd_sck);
503 * DOUT stands for serial data output of HX711
504 * for the driver it is an input
506 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
507 if (IS_ERR(hx711_data->gpiod_dout)) {
508 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
509 PTR_ERR(hx711_data->gpiod_dout));
510 return PTR_ERR(hx711_data->gpiod_dout);
513 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
514 if (IS_ERR(hx711_data->reg_avdd))
515 return PTR_ERR(hx711_data->reg_avdd);
517 ret = regulator_enable(hx711_data->reg_avdd);
523 * full scale differential input range: AVDD / GAIN
524 * full scale output data: 2^24
528 * 1 LSB = AVDD / GAIN / 2^24
529 * AVDD is in uV, but we need 10^-9 mV
530 * approximately to fit into a 32 bit number:
531 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
533 ret = regulator_get_voltage(hx711_data->reg_avdd);
535 goto error_regulator;
537 /* we need 10^-9 mV */
540 for (i = 0; i < HX711_GAIN_MAX; i++)
541 hx711_gain_to_scale[i].scale =
542 ret / hx711_gain_to_scale[i].gain / 1678;
544 hx711_data->gain_set = 128;
545 hx711_data->gain_chan_a = 128;
547 hx711_data->clock_frequency = 400000;
548 ret = of_property_read_u32(np, "clock-frequency",
549 &hx711_data->clock_frequency);
552 * datasheet says the high level of PD_SCK has a maximum duration
555 if (hx711_data->clock_frequency < 20000) {
556 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
557 hx711_data->clock_frequency = 400000;
560 hx711_data->data_ready_delay_ns =
561 1000000000 / hx711_data->clock_frequency;
563 platform_set_drvdata(pdev, indio_dev);
565 indio_dev->name = "hx711";
566 indio_dev->dev.parent = &pdev->dev;
567 indio_dev->info = &hx711_iio_info;
568 indio_dev->modes = INDIO_DIRECT_MODE;
569 indio_dev->channels = hx711_chan_spec;
570 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
572 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
573 hx711_trigger, NULL);
575 dev_err(dev, "setup of iio triggered buffer failed\n");
576 goto error_regulator;
579 ret = iio_device_register(indio_dev);
581 dev_err(dev, "Couldn't register the device\n");
588 iio_triggered_buffer_cleanup(indio_dev);
591 regulator_disable(hx711_data->reg_avdd);
596 static int hx711_remove(struct platform_device *pdev)
598 struct hx711_data *hx711_data;
599 struct iio_dev *indio_dev;
601 indio_dev = platform_get_drvdata(pdev);
602 hx711_data = iio_priv(indio_dev);
604 iio_device_unregister(indio_dev);
606 iio_triggered_buffer_cleanup(indio_dev);
608 regulator_disable(hx711_data->reg_avdd);
613 static const struct of_device_id of_hx711_match[] = {
614 { .compatible = "avia,hx711", },
618 MODULE_DEVICE_TABLE(of, of_hx711_match);
620 static struct platform_driver hx711_driver = {
621 .probe = hx711_probe,
622 .remove = hx711_remove,
624 .name = "hx711-gpio",
625 .of_match_table = of_hx711_match,
629 module_platform_driver(hx711_driver);
631 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
632 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
633 MODULE_LICENSE("GPL");
634 MODULE_ALIAS("platform:hx711-gpio");