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 naturally aligned timestamp
99 u32 buffer[4] __aligned(8);
101 * delay after a rising edge on SCK until the data is ready DOUT
102 * this is dependent on the hx711 where the datasheet tells a
103 * maximum value of 100 ns
104 * but also on potential parasitic capacities on the wiring
106 u32 data_ready_delay_ns;
110 static int hx711_cycle(struct hx711_data *hx711_data)
115 * if preempted for more then 60us while PD_SCK is high:
116 * hx711 is going in reset
117 * ==> measuring is false
119 local_irq_save(flags);
120 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
123 * wait until DOUT is ready
124 * it turned out that parasitic capacities are extending the time
125 * until DOUT has reached it's value
127 ndelay(hx711_data->data_ready_delay_ns);
130 * here we are not waiting for 0.2 us as suggested by the datasheet,
131 * because the oscilloscope showed in a test scenario
132 * at least 1.15 us for PD_SCK high (T3 in datasheet)
133 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
135 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
136 local_irq_restore(flags);
139 * make it a square wave for addressing cases with capacitance on
142 ndelay(hx711_data->data_ready_delay_ns);
144 /* sample as late as possible */
145 return gpiod_get_value(hx711_data->gpiod_dout);
148 static int hx711_read(struct hx711_data *hx711_data)
152 int val = gpiod_get_value(hx711_data->gpiod_dout);
154 /* we double check if it's really down */
158 for (i = 0; i < 24; i++) {
160 ret = hx711_cycle(hx711_data);
167 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
168 hx711_cycle(hx711_data);
173 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
178 * in some rare cases the reset takes quite a long time
179 * especially when the channel is changed.
180 * Allow up to one second for it
182 for (i = 0; i < 100; i++) {
183 val = gpiod_get_value(hx711_data->gpiod_dout);
186 /* sleep at least 10 ms */
195 static int hx711_reset(struct hx711_data *hx711_data)
198 int val = gpiod_get_value(hx711_data->gpiod_dout);
202 * an examination with the oszilloscope indicated
203 * that the first value read after the reset is not stable
204 * if we reset too short;
205 * the shorter the reset cycle
206 * the less reliable the first value after reset is;
207 * there were no problems encountered with a value
210 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
212 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
214 ret = hx711_wait_for_ready(hx711_data);
218 * after a reset the gain is 128 so we do a dummy read
219 * to set the gain for the next read
221 ret = hx711_read(hx711_data);
226 * after a dummy read we need to wait vor readiness
227 * for not mixing gain pulses with the clock
229 val = hx711_wait_for_ready(hx711_data);
235 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
240 if (hx711_data->gain_set == 32) {
241 hx711_data->gain_set = hx711_data->gain_chan_a;
243 ret = hx711_read(hx711_data);
247 ret = hx711_wait_for_ready(hx711_data);
252 if (hx711_data->gain_set != 32) {
253 hx711_data->gain_set = 32;
255 ret = hx711_read(hx711_data);
259 ret = hx711_wait_for_ready(hx711_data);
268 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
274 * hx711_reset() must be called from here
275 * because it could be calling hx711_read() by itself
277 if (hx711_reset(hx711_data)) {
278 dev_err(hx711_data->dev, "reset failed!");
282 ret = hx711_set_gain_for_channel(hx711_data, chan);
286 val = hx711_read(hx711_data);
291 static int hx711_read_raw(struct iio_dev *indio_dev,
292 const struct iio_chan_spec *chan,
293 int *val, int *val2, long mask)
295 struct hx711_data *hx711_data = iio_priv(indio_dev);
298 case IIO_CHAN_INFO_RAW:
299 mutex_lock(&hx711_data->lock);
301 *val = hx711_reset_read(hx711_data, chan->channel);
303 mutex_unlock(&hx711_data->lock);
308 case IIO_CHAN_INFO_SCALE:
310 mutex_lock(&hx711_data->lock);
312 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
314 mutex_unlock(&hx711_data->lock);
316 return IIO_VAL_INT_PLUS_NANO;
322 static int hx711_write_raw(struct iio_dev *indio_dev,
323 struct iio_chan_spec const *chan,
328 struct hx711_data *hx711_data = iio_priv(indio_dev);
333 case IIO_CHAN_INFO_SCALE:
335 * a scale greater than 1 mV per LSB is not possible
336 * with the HX711, therefore val must be 0
341 mutex_lock(&hx711_data->lock);
343 gain = hx711_get_scale_to_gain(val2);
345 mutex_unlock(&hx711_data->lock);
349 if (gain != hx711_data->gain_set) {
350 hx711_data->gain_set = gain;
352 hx711_data->gain_chan_a = gain;
354 ret = hx711_read(hx711_data);
356 mutex_unlock(&hx711_data->lock);
361 mutex_unlock(&hx711_data->lock);
370 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
371 struct iio_chan_spec const *chan,
374 return IIO_VAL_INT_PLUS_NANO;
377 static irqreturn_t hx711_trigger(int irq, void *p)
379 struct iio_poll_func *pf = p;
380 struct iio_dev *indio_dev = pf->indio_dev;
381 struct hx711_data *hx711_data = iio_priv(indio_dev);
384 mutex_lock(&hx711_data->lock);
386 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
388 for (i = 0; i < indio_dev->masklength; i++) {
389 if (!test_bit(i, indio_dev->active_scan_mask))
392 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
393 indio_dev->channels[i].channel);
397 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
400 mutex_unlock(&hx711_data->lock);
402 iio_trigger_notify_done(indio_dev->trig);
407 static ssize_t hx711_scale_available_show(struct device *dev,
408 struct device_attribute *attr,
411 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
412 int channel = iio_attr->address;
415 for (i = 0; i < HX711_GAIN_MAX; i++)
416 if (hx711_gain_to_scale[i].channel == channel)
417 len += sprintf(buf + len, "0.%09d ",
418 hx711_gain_to_scale[i].scale);
420 len += sprintf(buf + len, "\n");
425 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
426 hx711_scale_available_show, NULL, 0);
428 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
429 hx711_scale_available_show, NULL, 1);
431 static struct attribute *hx711_attributes[] = {
432 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
433 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
437 static const struct attribute_group hx711_attribute_group = {
438 .attrs = hx711_attributes,
441 static const struct iio_info hx711_iio_info = {
442 .read_raw = hx711_read_raw,
443 .write_raw = hx711_write_raw,
444 .write_raw_get_fmt = hx711_write_raw_get_fmt,
445 .attrs = &hx711_attribute_group,
448 static const struct iio_chan_spec hx711_chan_spec[] = {
453 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
454 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
460 .endianness = IIO_CPU,
467 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
468 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
474 .endianness = IIO_CPU,
477 IIO_CHAN_SOFT_TIMESTAMP(2),
480 static int hx711_probe(struct platform_device *pdev)
482 struct device *dev = &pdev->dev;
483 struct device_node *np = dev->of_node;
484 struct hx711_data *hx711_data;
485 struct iio_dev *indio_dev;
489 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
491 dev_err(dev, "failed to allocate IIO device\n");
495 hx711_data = iio_priv(indio_dev);
496 hx711_data->dev = dev;
498 mutex_init(&hx711_data->lock);
501 * PD_SCK stands for power down and serial clock input of HX711
502 * in the driver it is an output
504 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
505 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
506 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
507 PTR_ERR(hx711_data->gpiod_pd_sck));
508 return PTR_ERR(hx711_data->gpiod_pd_sck);
512 * DOUT stands for serial data output of HX711
513 * for the driver it is an input
515 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
516 if (IS_ERR(hx711_data->gpiod_dout)) {
517 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
518 PTR_ERR(hx711_data->gpiod_dout));
519 return PTR_ERR(hx711_data->gpiod_dout);
522 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
523 if (IS_ERR(hx711_data->reg_avdd))
524 return PTR_ERR(hx711_data->reg_avdd);
526 ret = regulator_enable(hx711_data->reg_avdd);
532 * full scale differential input range: AVDD / GAIN
533 * full scale output data: 2^24
537 * 1 LSB = AVDD / GAIN / 2^24
538 * AVDD is in uV, but we need 10^-9 mV
539 * approximately to fit into a 32 bit number:
540 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
542 ret = regulator_get_voltage(hx711_data->reg_avdd);
544 goto error_regulator;
546 /* we need 10^-9 mV */
549 for (i = 0; i < HX711_GAIN_MAX; i++)
550 hx711_gain_to_scale[i].scale =
551 ret / hx711_gain_to_scale[i].gain / 1678;
553 hx711_data->gain_set = 128;
554 hx711_data->gain_chan_a = 128;
556 hx711_data->clock_frequency = 400000;
557 ret = of_property_read_u32(np, "clock-frequency",
558 &hx711_data->clock_frequency);
561 * datasheet says the high level of PD_SCK has a maximum duration
564 if (hx711_data->clock_frequency < 20000) {
565 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
566 hx711_data->clock_frequency = 400000;
569 hx711_data->data_ready_delay_ns =
570 1000000000 / hx711_data->clock_frequency;
572 platform_set_drvdata(pdev, indio_dev);
574 indio_dev->name = "hx711";
575 indio_dev->dev.parent = &pdev->dev;
576 indio_dev->info = &hx711_iio_info;
577 indio_dev->modes = INDIO_DIRECT_MODE;
578 indio_dev->channels = hx711_chan_spec;
579 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
581 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
582 hx711_trigger, NULL);
584 dev_err(dev, "setup of iio triggered buffer failed\n");
585 goto error_regulator;
588 ret = iio_device_register(indio_dev);
590 dev_err(dev, "Couldn't register the device\n");
597 iio_triggered_buffer_cleanup(indio_dev);
600 regulator_disable(hx711_data->reg_avdd);
605 static int hx711_remove(struct platform_device *pdev)
607 struct hx711_data *hx711_data;
608 struct iio_dev *indio_dev;
610 indio_dev = platform_get_drvdata(pdev);
611 hx711_data = iio_priv(indio_dev);
613 iio_device_unregister(indio_dev);
615 iio_triggered_buffer_cleanup(indio_dev);
617 regulator_disable(hx711_data->reg_avdd);
622 static const struct of_device_id of_hx711_match[] = {
623 { .compatible = "avia,hx711", },
627 MODULE_DEVICE_TABLE(of, of_hx711_match);
629 static struct platform_driver hx711_driver = {
630 .probe = hx711_probe,
631 .remove = hx711_remove,
633 .name = "hx711-gpio",
634 .of_match_table = of_hx711_match,
638 module_platform_driver(hx711_driver);
640 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
641 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
642 MODULE_LICENSE("GPL");
643 MODULE_ALIAS("platform:hx711-gpio");