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/gpio/consumer.h>
28 #include <linux/regulator/consumer.h>
30 /* gain to pulse and scale conversion */
31 #define HX711_GAIN_MAX 3
33 struct hx711_gain_to_scale {
41 * .scale depends on AVDD which in turn is known as soon as the regulator
43 * therefore we set .scale in hx711_probe()
45 * channel A in documentation is channel 0 in source code
46 * channel B in documentation is channel 1 in source code
48 static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
54 static int hx711_get_gain_to_pulse(int gain)
58 for (i = 0; i < HX711_GAIN_MAX; i++)
59 if (hx711_gain_to_scale[i].gain == gain)
60 return hx711_gain_to_scale[i].gain_pulse;
64 static int hx711_get_gain_to_scale(int gain)
68 for (i = 0; i < HX711_GAIN_MAX; i++)
69 if (hx711_gain_to_scale[i].gain == gain)
70 return hx711_gain_to_scale[i].scale;
74 static int hx711_get_scale_to_gain(int scale)
78 for (i = 0; i < HX711_GAIN_MAX; i++)
79 if (hx711_gain_to_scale[i].scale == scale)
80 return hx711_gain_to_scale[i].gain;
86 struct gpio_desc *gpiod_pd_sck;
87 struct gpio_desc *gpiod_dout;
88 struct regulator *reg_avdd;
89 int gain_set; /* gain set on device */
90 int gain_chan_a; /* gain for channel A */
93 * delay after a rising edge on SCK until the data is ready DOUT
94 * this is dependent on the hx711 where the datasheet tells a
95 * maximum value of 100 ns
96 * but also on potential parasitic capacities on the wiring
98 u32 data_ready_delay_ns;
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
111 local_irq_save(flags);
112 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
115 * wait until DOUT is ready
116 * it turned out that parasitic capacities are extending the time
117 * until DOUT has reached it's value
119 ndelay(hx711_data->data_ready_delay_ns);
122 * here we are not waiting for 0.2 us as suggested by the datasheet,
123 * because the oscilloscope showed in a test scenario
124 * at least 1.15 us for PD_SCK high (T3 in datasheet)
125 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
127 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
128 local_irq_restore(flags);
131 * make it a square wave for addressing cases with capacitance on
134 ndelay(hx711_data->data_ready_delay_ns);
136 /* sample as late as possible */
137 return gpiod_get_value(hx711_data->gpiod_dout);
140 static int hx711_read(struct hx711_data *hx711_data)
144 int val = gpiod_get_value(hx711_data->gpiod_dout);
146 /* we double check if it's really down */
150 for (i = 0; i < 24; i++) {
152 ret = hx711_cycle(hx711_data);
159 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
160 hx711_cycle(hx711_data);
165 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
170 * a maximum reset cycle time of 56 ms was measured.
171 * we round it up to 100 ms
173 for (i = 0; i < 100; i++) {
174 val = gpiod_get_value(hx711_data->gpiod_dout);
177 /* sleep at least 1 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 ret = hx711_wait_for_ready(hx711_data);
228 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
233 if (hx711_data->gain_set == 32) {
234 hx711_data->gain_set = hx711_data->gain_chan_a;
236 ret = hx711_read(hx711_data);
240 ret = hx711_wait_for_ready(hx711_data);
245 if (hx711_data->gain_set != 32) {
246 hx711_data->gain_set = 32;
248 ret = hx711_read(hx711_data);
252 ret = hx711_wait_for_ready(hx711_data);
261 static int hx711_read_raw(struct iio_dev *indio_dev,
262 const struct iio_chan_spec *chan,
263 int *val, int *val2, long mask)
265 struct hx711_data *hx711_data = iio_priv(indio_dev);
269 case IIO_CHAN_INFO_RAW:
270 mutex_lock(&hx711_data->lock);
273 * hx711_reset() must be called from here
274 * because it could be calling hx711_read() by itself
276 if (hx711_reset(hx711_data)) {
277 mutex_unlock(&hx711_data->lock);
278 dev_err(hx711_data->dev, "reset failed!");
282 ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
284 mutex_unlock(&hx711_data->lock);
288 *val = hx711_read(hx711_data);
290 mutex_unlock(&hx711_data->lock);
295 case IIO_CHAN_INFO_SCALE:
297 mutex_lock(&hx711_data->lock);
299 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
301 mutex_unlock(&hx711_data->lock);
303 return IIO_VAL_INT_PLUS_NANO;
309 static int hx711_write_raw(struct iio_dev *indio_dev,
310 struct iio_chan_spec const *chan,
315 struct hx711_data *hx711_data = iio_priv(indio_dev);
320 case IIO_CHAN_INFO_SCALE:
322 * a scale greater than 1 mV per LSB is not possible
323 * with the HX711, therefore val must be 0
328 mutex_lock(&hx711_data->lock);
330 gain = hx711_get_scale_to_gain(val2);
332 mutex_unlock(&hx711_data->lock);
336 if (gain != hx711_data->gain_set) {
337 hx711_data->gain_set = gain;
339 hx711_data->gain_chan_a = gain;
341 ret = hx711_read(hx711_data);
343 mutex_unlock(&hx711_data->lock);
348 mutex_unlock(&hx711_data->lock);
357 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
358 struct iio_chan_spec const *chan,
361 return IIO_VAL_INT_PLUS_NANO;
364 static ssize_t hx711_scale_available_show(struct device *dev,
365 struct device_attribute *attr,
368 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
369 int channel = iio_attr->address;
372 for (i = 0; i < HX711_GAIN_MAX; i++)
373 if (hx711_gain_to_scale[i].channel == channel)
374 len += sprintf(buf + len, "0.%09d ",
375 hx711_gain_to_scale[i].scale);
377 len += sprintf(buf + len, "\n");
382 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
383 hx711_scale_available_show, NULL, 0);
385 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
386 hx711_scale_available_show, NULL, 1);
388 static struct attribute *hx711_attributes[] = {
389 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
390 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
394 static const struct attribute_group hx711_attribute_group = {
395 .attrs = hx711_attributes,
398 static const struct iio_info hx711_iio_info = {
399 .driver_module = THIS_MODULE,
400 .read_raw = hx711_read_raw,
401 .write_raw = hx711_write_raw,
402 .write_raw_get_fmt = hx711_write_raw_get_fmt,
403 .attrs = &hx711_attribute_group,
406 static const struct iio_chan_spec hx711_chan_spec[] = {
411 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
412 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
418 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
419 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
423 static int hx711_probe(struct platform_device *pdev)
425 struct device *dev = &pdev->dev;
426 struct device_node *np = dev->of_node;
427 struct hx711_data *hx711_data;
428 struct iio_dev *indio_dev;
432 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
434 dev_err(dev, "failed to allocate IIO device\n");
438 hx711_data = iio_priv(indio_dev);
439 hx711_data->dev = dev;
441 mutex_init(&hx711_data->lock);
444 * PD_SCK stands for power down and serial clock input of HX711
445 * in the driver it is an output
447 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
448 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
449 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
450 PTR_ERR(hx711_data->gpiod_pd_sck));
451 return PTR_ERR(hx711_data->gpiod_pd_sck);
455 * DOUT stands for serial data output of HX711
456 * for the driver it is an input
458 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
459 if (IS_ERR(hx711_data->gpiod_dout)) {
460 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
461 PTR_ERR(hx711_data->gpiod_dout));
462 return PTR_ERR(hx711_data->gpiod_dout);
465 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
466 if (IS_ERR(hx711_data->reg_avdd))
467 return PTR_ERR(hx711_data->reg_avdd);
469 ret = regulator_enable(hx711_data->reg_avdd);
475 * full scale differential input range: AVDD / GAIN
476 * full scale output data: 2^24
480 * 1 LSB = AVDD / GAIN / 2^24
481 * AVDD is in uV, but we need 10^-9 mV
482 * approximately to fit into a 32 bit number:
483 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
485 ret = regulator_get_voltage(hx711_data->reg_avdd);
487 regulator_disable(hx711_data->reg_avdd);
490 /* we need 10^-9 mV */
493 for (i = 0; i < HX711_GAIN_MAX; i++)
494 hx711_gain_to_scale[i].scale =
495 ret / hx711_gain_to_scale[i].gain / 1678;
497 hx711_data->gain_set = 128;
498 hx711_data->gain_chan_a = 128;
500 hx711_data->clock_frequency = 400000;
501 ret = of_property_read_u32(np, "clock-frequency",
502 &hx711_data->clock_frequency);
505 * datasheet says the high level of PD_SCK has a maximum duration
508 if (hx711_data->clock_frequency < 20000) {
509 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
510 hx711_data->clock_frequency = 400000;
513 hx711_data->data_ready_delay_ns =
514 1000000000 / hx711_data->clock_frequency;
516 platform_set_drvdata(pdev, indio_dev);
518 indio_dev->name = "hx711";
519 indio_dev->dev.parent = &pdev->dev;
520 indio_dev->info = &hx711_iio_info;
521 indio_dev->modes = INDIO_DIRECT_MODE;
522 indio_dev->channels = hx711_chan_spec;
523 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
525 ret = iio_device_register(indio_dev);
527 dev_err(dev, "Couldn't register the device\n");
528 regulator_disable(hx711_data->reg_avdd);
534 static int hx711_remove(struct platform_device *pdev)
536 struct hx711_data *hx711_data;
537 struct iio_dev *indio_dev;
539 indio_dev = platform_get_drvdata(pdev);
540 hx711_data = iio_priv(indio_dev);
542 iio_device_unregister(indio_dev);
544 regulator_disable(hx711_data->reg_avdd);
549 static const struct of_device_id of_hx711_match[] = {
550 { .compatible = "avia,hx711", },
554 MODULE_DEVICE_TABLE(of, of_hx711_match);
556 static struct platform_driver hx711_driver = {
557 .probe = hx711_probe,
558 .remove = hx711_remove,
560 .name = "hx711-gpio",
561 .of_match_table = of_hx711_match,
565 module_platform_driver(hx711_driver);
567 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
568 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
569 MODULE_LICENSE("GPL");
570 MODULE_ALIAS("platform:hx711-gpio");