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 */
94 static int hx711_cycle(struct hx711_data *hx711_data)
99 * if preempted for more then 60us while PD_SCK is high:
100 * hx711 is going in reset
101 * ==> measuring is false
104 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
105 val = gpiod_get_value(hx711_data->gpiod_dout);
107 * here we are not waiting for 0.2 us as suggested by the datasheet,
108 * because the oscilloscope showed in a test scenario
109 * at least 1.15 us for PD_SCK high (T3 in datasheet)
110 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
112 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
118 static int hx711_read(struct hx711_data *hx711_data)
122 int val = gpiod_get_value(hx711_data->gpiod_dout);
124 /* we double check if it's really down */
128 for (i = 0; i < 24; i++) {
130 ret = hx711_cycle(hx711_data);
137 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
138 hx711_cycle(hx711_data);
143 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
148 * a maximum reset cycle time of 56 ms was measured.
149 * we round it up to 100 ms
151 for (i = 0; i < 100; i++) {
152 val = gpiod_get_value(hx711_data->gpiod_dout);
155 /* sleep at least 1 ms */
164 static int hx711_reset(struct hx711_data *hx711_data)
167 int val = gpiod_get_value(hx711_data->gpiod_dout);
171 * an examination with the oszilloscope indicated
172 * that the first value read after the reset is not stable
173 * if we reset too short;
174 * the shorter the reset cycle
175 * the less reliable the first value after reset is;
176 * there were no problems encountered with a value
179 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
181 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
183 ret = hx711_wait_for_ready(hx711_data);
187 * after a reset the gain is 128 so we do a dummy read
188 * to set the gain for the next read
190 ret = hx711_read(hx711_data);
195 * after a dummy read we need to wait vor readiness
196 * for not mixing gain pulses with the clock
198 ret = hx711_wait_for_ready(hx711_data);
206 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
211 if (hx711_data->gain_set == 32) {
212 hx711_data->gain_set = hx711_data->gain_chan_a;
214 ret = hx711_read(hx711_data);
218 ret = hx711_wait_for_ready(hx711_data);
223 if (hx711_data->gain_set != 32) {
224 hx711_data->gain_set = 32;
226 ret = hx711_read(hx711_data);
230 ret = hx711_wait_for_ready(hx711_data);
239 static int hx711_read_raw(struct iio_dev *indio_dev,
240 const struct iio_chan_spec *chan,
241 int *val, int *val2, long mask)
243 struct hx711_data *hx711_data = iio_priv(indio_dev);
247 case IIO_CHAN_INFO_RAW:
248 mutex_lock(&hx711_data->lock);
251 * hx711_reset() must be called from here
252 * because it could be calling hx711_read() by itself
254 if (hx711_reset(hx711_data)) {
255 mutex_unlock(&hx711_data->lock);
256 dev_err(hx711_data->dev, "reset failed!");
260 ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
262 mutex_unlock(&hx711_data->lock);
266 *val = hx711_read(hx711_data);
268 mutex_unlock(&hx711_data->lock);
273 case IIO_CHAN_INFO_SCALE:
275 mutex_lock(&hx711_data->lock);
277 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
279 mutex_unlock(&hx711_data->lock);
281 return IIO_VAL_INT_PLUS_NANO;
287 static int hx711_write_raw(struct iio_dev *indio_dev,
288 struct iio_chan_spec const *chan,
293 struct hx711_data *hx711_data = iio_priv(indio_dev);
298 case IIO_CHAN_INFO_SCALE:
300 * a scale greater than 1 mV per LSB is not possible
301 * with the HX711, therefore val must be 0
306 mutex_lock(&hx711_data->lock);
308 gain = hx711_get_scale_to_gain(val2);
310 mutex_unlock(&hx711_data->lock);
314 if (gain != hx711_data->gain_set) {
315 hx711_data->gain_set = gain;
317 hx711_data->gain_chan_a = gain;
319 ret = hx711_read(hx711_data);
321 mutex_unlock(&hx711_data->lock);
326 mutex_unlock(&hx711_data->lock);
335 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
336 struct iio_chan_spec const *chan,
339 return IIO_VAL_INT_PLUS_NANO;
342 static ssize_t hx711_scale_available_show(struct device *dev,
343 struct device_attribute *attr,
346 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
347 int channel = iio_attr->address;
350 for (i = 0; i < HX711_GAIN_MAX; i++)
351 if (hx711_gain_to_scale[i].channel == channel)
352 len += sprintf(buf + len, "0.%09d ",
353 hx711_gain_to_scale[i].scale);
355 len += sprintf(buf + len, "\n");
360 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
361 hx711_scale_available_show, NULL, 0);
363 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
364 hx711_scale_available_show, NULL, 1);
366 static struct attribute *hx711_attributes[] = {
367 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
368 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
372 static const struct attribute_group hx711_attribute_group = {
373 .attrs = hx711_attributes,
376 static const struct iio_info hx711_iio_info = {
377 .read_raw = hx711_read_raw,
378 .write_raw = hx711_write_raw,
379 .write_raw_get_fmt = hx711_write_raw_get_fmt,
380 .attrs = &hx711_attribute_group,
383 static const struct iio_chan_spec hx711_chan_spec[] = {
388 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
389 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
395 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
396 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
400 static int hx711_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
403 struct hx711_data *hx711_data;
404 struct iio_dev *indio_dev;
408 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
410 dev_err(dev, "failed to allocate IIO device\n");
414 hx711_data = iio_priv(indio_dev);
415 hx711_data->dev = dev;
417 mutex_init(&hx711_data->lock);
420 * PD_SCK stands for power down and serial clock input of HX711
421 * in the driver it is an output
423 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
424 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
425 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
426 PTR_ERR(hx711_data->gpiod_pd_sck));
427 return PTR_ERR(hx711_data->gpiod_pd_sck);
431 * DOUT stands for serial data output of HX711
432 * for the driver it is an input
434 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
435 if (IS_ERR(hx711_data->gpiod_dout)) {
436 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
437 PTR_ERR(hx711_data->gpiod_dout));
438 return PTR_ERR(hx711_data->gpiod_dout);
441 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
442 if (IS_ERR(hx711_data->reg_avdd))
443 return PTR_ERR(hx711_data->reg_avdd);
445 ret = regulator_enable(hx711_data->reg_avdd);
451 * full scale differential input range: AVDD / GAIN
452 * full scale output data: 2^24
456 * 1 LSB = AVDD / GAIN / 2^24
457 * AVDD is in uV, but we need 10^-9 mV
458 * approximately to fit into a 32 bit number:
459 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
461 ret = regulator_get_voltage(hx711_data->reg_avdd);
463 regulator_disable(hx711_data->reg_avdd);
466 /* we need 10^-9 mV */
469 for (i = 0; i < HX711_GAIN_MAX; i++)
470 hx711_gain_to_scale[i].scale =
471 ret / hx711_gain_to_scale[i].gain / 1678;
473 hx711_data->gain_set = 128;
474 hx711_data->gain_chan_a = 128;
476 platform_set_drvdata(pdev, indio_dev);
478 indio_dev->name = "hx711";
479 indio_dev->dev.parent = &pdev->dev;
480 indio_dev->info = &hx711_iio_info;
481 indio_dev->modes = INDIO_DIRECT_MODE;
482 indio_dev->channels = hx711_chan_spec;
483 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
485 ret = iio_device_register(indio_dev);
487 dev_err(dev, "Couldn't register the device\n");
488 regulator_disable(hx711_data->reg_avdd);
494 static int hx711_remove(struct platform_device *pdev)
496 struct hx711_data *hx711_data;
497 struct iio_dev *indio_dev;
499 indio_dev = platform_get_drvdata(pdev);
500 hx711_data = iio_priv(indio_dev);
502 iio_device_unregister(indio_dev);
504 regulator_disable(hx711_data->reg_avdd);
509 static const struct of_device_id of_hx711_match[] = {
510 { .compatible = "avia,hx711", },
514 MODULE_DEVICE_TABLE(of, of_hx711_match);
516 static struct platform_driver hx711_driver = {
517 .probe = hx711_probe,
518 .remove = hx711_remove,
520 .name = "hx711-gpio",
521 .of_match_table = of_hx711_match,
525 module_platform_driver(hx711_driver);
527 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
528 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
529 MODULE_LICENSE("GPL");
530 MODULE_ALIAS("platform:hx711-gpio");