1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HX711: analog to digital converter for weight sensor module
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
26 #define HX711_RESET_GAIN 128
28 struct hx711_gain_to_scale {
36 * .scale depends on AVDD which in turn is known as soon as the regulator
38 * therefore we set .scale in hx711_probe()
40 * channel A in documentation is channel 0 in source code
41 * channel B in documentation is channel 1 in source code
43 static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
49 static int hx711_get_gain_to_pulse(int gain)
53 for (i = 0; i < HX711_GAIN_MAX; i++)
54 if (hx711_gain_to_scale[i].gain == gain)
55 return hx711_gain_to_scale[i].gain_pulse;
59 static int hx711_get_gain_to_scale(int gain)
63 for (i = 0; i < HX711_GAIN_MAX; i++)
64 if (hx711_gain_to_scale[i].gain == gain)
65 return hx711_gain_to_scale[i].scale;
69 static int hx711_get_scale_to_gain(int scale)
73 for (i = 0; i < HX711_GAIN_MAX; i++)
74 if (hx711_gain_to_scale[i].scale == scale)
75 return hx711_gain_to_scale[i].gain;
81 struct gpio_desc *gpiod_pd_sck;
82 struct gpio_desc *gpiod_dout;
83 struct regulator *reg_avdd;
84 int gain_set; /* gain set on device */
85 int gain_chan_a; /* gain for channel A */
89 * 2x32-bit channel + 64-bit naturally aligned timestamp
91 u32 buffer[4] __aligned(8);
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 * in some rare cases the reset takes quite a long time
171 * especially when the channel is changed.
172 * Allow up to one second for it
174 for (i = 0; i < 100; i++) {
175 val = gpiod_get_value(hx711_data->gpiod_dout);
178 /* sleep at least 10 ms */
187 static int hx711_reset(struct hx711_data *hx711_data)
189 int val = hx711_wait_for_ready(hx711_data);
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 val = hx711_wait_for_ready(hx711_data);
207 /* after a reset the gain is 128 */
208 hx711_data->gain_set = HX711_RESET_GAIN;
214 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
219 if (hx711_data->gain_set == 32) {
220 hx711_data->gain_set = hx711_data->gain_chan_a;
222 ret = hx711_read(hx711_data);
226 ret = hx711_wait_for_ready(hx711_data);
231 if (hx711_data->gain_set != 32) {
232 hx711_data->gain_set = 32;
234 ret = hx711_read(hx711_data);
238 ret = hx711_wait_for_ready(hx711_data);
247 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
253 * hx711_reset() must be called from here
254 * because it could be calling hx711_read() by itself
256 if (hx711_reset(hx711_data)) {
257 dev_err(hx711_data->dev, "reset failed!");
261 ret = hx711_set_gain_for_channel(hx711_data, chan);
265 val = hx711_read(hx711_data);
270 static int hx711_read_raw(struct iio_dev *indio_dev,
271 const struct iio_chan_spec *chan,
272 int *val, int *val2, long mask)
274 struct hx711_data *hx711_data = iio_priv(indio_dev);
277 case IIO_CHAN_INFO_RAW:
278 mutex_lock(&hx711_data->lock);
280 *val = hx711_reset_read(hx711_data, chan->channel);
282 mutex_unlock(&hx711_data->lock);
287 case IIO_CHAN_INFO_SCALE:
289 mutex_lock(&hx711_data->lock);
291 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
293 mutex_unlock(&hx711_data->lock);
295 return IIO_VAL_INT_PLUS_NANO;
301 static int hx711_write_raw(struct iio_dev *indio_dev,
302 struct iio_chan_spec const *chan,
307 struct hx711_data *hx711_data = iio_priv(indio_dev);
312 case IIO_CHAN_INFO_SCALE:
314 * a scale greater than 1 mV per LSB is not possible
315 * with the HX711, therefore val must be 0
320 mutex_lock(&hx711_data->lock);
322 gain = hx711_get_scale_to_gain(val2);
324 mutex_unlock(&hx711_data->lock);
328 if (gain != hx711_data->gain_set) {
329 hx711_data->gain_set = gain;
331 hx711_data->gain_chan_a = gain;
333 ret = hx711_read(hx711_data);
335 mutex_unlock(&hx711_data->lock);
340 mutex_unlock(&hx711_data->lock);
349 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
350 struct iio_chan_spec const *chan,
353 return IIO_VAL_INT_PLUS_NANO;
356 static irqreturn_t hx711_trigger(int irq, void *p)
358 struct iio_poll_func *pf = p;
359 struct iio_dev *indio_dev = pf->indio_dev;
360 struct hx711_data *hx711_data = iio_priv(indio_dev);
363 mutex_lock(&hx711_data->lock);
365 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
367 for (i = 0; i < indio_dev->masklength; i++) {
368 if (!test_bit(i, indio_dev->active_scan_mask))
371 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
372 indio_dev->channels[i].channel);
376 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
379 mutex_unlock(&hx711_data->lock);
381 iio_trigger_notify_done(indio_dev->trig);
386 static ssize_t hx711_scale_available_show(struct device *dev,
387 struct device_attribute *attr,
390 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
391 int channel = iio_attr->address;
394 for (i = 0; i < HX711_GAIN_MAX; i++)
395 if (hx711_gain_to_scale[i].channel == channel)
396 len += sprintf(buf + len, "0.%09d ",
397 hx711_gain_to_scale[i].scale);
399 len += sprintf(buf + len, "\n");
404 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
405 hx711_scale_available_show, NULL, 0);
407 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
408 hx711_scale_available_show, NULL, 1);
410 static struct attribute *hx711_attributes[] = {
411 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
412 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
416 static const struct attribute_group hx711_attribute_group = {
417 .attrs = hx711_attributes,
420 static const struct iio_info hx711_iio_info = {
421 .read_raw = hx711_read_raw,
422 .write_raw = hx711_write_raw,
423 .write_raw_get_fmt = hx711_write_raw_get_fmt,
424 .attrs = &hx711_attribute_group,
427 static const struct iio_chan_spec hx711_chan_spec[] = {
432 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
433 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
439 .endianness = IIO_CPU,
446 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
447 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
453 .endianness = IIO_CPU,
456 IIO_CHAN_SOFT_TIMESTAMP(2),
459 static int hx711_probe(struct platform_device *pdev)
461 struct device *dev = &pdev->dev;
462 struct device_node *np = dev->of_node;
463 struct hx711_data *hx711_data;
464 struct iio_dev *indio_dev;
468 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
470 dev_err(dev, "failed to allocate IIO device\n");
474 hx711_data = iio_priv(indio_dev);
475 hx711_data->dev = dev;
477 mutex_init(&hx711_data->lock);
480 * PD_SCK stands for power down and serial clock input of HX711
481 * in the driver it is an output
483 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
484 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
485 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
486 PTR_ERR(hx711_data->gpiod_pd_sck));
487 return PTR_ERR(hx711_data->gpiod_pd_sck);
491 * DOUT stands for serial data output of HX711
492 * for the driver it is an input
494 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
495 if (IS_ERR(hx711_data->gpiod_dout)) {
496 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
497 PTR_ERR(hx711_data->gpiod_dout));
498 return PTR_ERR(hx711_data->gpiod_dout);
501 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
502 if (IS_ERR(hx711_data->reg_avdd))
503 return PTR_ERR(hx711_data->reg_avdd);
505 ret = regulator_enable(hx711_data->reg_avdd);
511 * full scale differential input range: AVDD / GAIN
512 * full scale output data: 2^24
516 * 1 LSB = AVDD / GAIN / 2^24
517 * AVDD is in uV, but we need 10^-9 mV
518 * approximately to fit into a 32 bit number:
519 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
521 ret = regulator_get_voltage(hx711_data->reg_avdd);
523 goto error_regulator;
525 /* we need 10^-9 mV */
528 for (i = 0; i < HX711_GAIN_MAX; i++)
529 hx711_gain_to_scale[i].scale =
530 ret / hx711_gain_to_scale[i].gain / 1678;
532 hx711_data->gain_set = 128;
533 hx711_data->gain_chan_a = 128;
535 hx711_data->clock_frequency = 400000;
536 ret = of_property_read_u32(np, "clock-frequency",
537 &hx711_data->clock_frequency);
540 * datasheet says the high level of PD_SCK has a maximum duration
543 if (hx711_data->clock_frequency < 20000) {
544 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
545 hx711_data->clock_frequency = 400000;
548 hx711_data->data_ready_delay_ns =
549 1000000000 / hx711_data->clock_frequency;
551 platform_set_drvdata(pdev, indio_dev);
553 indio_dev->name = "hx711";
554 indio_dev->info = &hx711_iio_info;
555 indio_dev->modes = INDIO_DIRECT_MODE;
556 indio_dev->channels = hx711_chan_spec;
557 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
559 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
560 hx711_trigger, NULL);
562 dev_err(dev, "setup of iio triggered buffer failed\n");
563 goto error_regulator;
566 ret = iio_device_register(indio_dev);
568 dev_err(dev, "Couldn't register the device\n");
575 iio_triggered_buffer_cleanup(indio_dev);
578 regulator_disable(hx711_data->reg_avdd);
583 static int hx711_remove(struct platform_device *pdev)
585 struct hx711_data *hx711_data;
586 struct iio_dev *indio_dev;
588 indio_dev = platform_get_drvdata(pdev);
589 hx711_data = iio_priv(indio_dev);
591 iio_device_unregister(indio_dev);
593 iio_triggered_buffer_cleanup(indio_dev);
595 regulator_disable(hx711_data->reg_avdd);
600 static const struct of_device_id of_hx711_match[] = {
601 { .compatible = "avia,hx711", },
605 MODULE_DEVICE_TABLE(of, of_hx711_match);
607 static struct platform_driver hx711_driver = {
608 .probe = hx711_probe,
609 .remove = hx711_remove,
611 .name = "hx711-gpio",
612 .of_match_table = of_hx711_match,
616 module_platform_driver(hx711_driver);
619 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
620 MODULE_LICENSE("GPL");
621 MODULE_ALIAS("platform:hx711-gpio");