Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / adc / hx711.c
blob6c5d81a89aec9c6f7c077e2d78e19d9bfecd08d1
1 /*
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>
19 #include <linux/of.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 {
37 int gain;
38 int gain_pulse;
39 int scale;
40 int channel;
44 * .scale depends on AVDD which in turn is known as soon as the regulator
45 * is available
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] = {
52 { 128, 1, 0, 0 },
53 { 32, 2, 0, 1 },
54 { 64, 3, 0, 0 }
57 static int hx711_get_gain_to_pulse(int gain)
59 int i;
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;
64 return 1;
67 static int hx711_get_gain_to_scale(int gain)
69 int i;
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;
74 return 0;
77 static int hx711_get_scale_to_gain(int scale)
79 int i;
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;
84 return -EINVAL;
87 struct hx711_data {
88 struct device *dev;
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 */
94 struct mutex lock;
96 * triggered buffer
97 * 2x32-bit channel + 64-bit timestamp
99 u32 buffer[4];
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;
107 u32 clock_frequency;
110 static int hx711_cycle(struct hx711_data *hx711_data)
112 unsigned long flags;
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
140 * PC_SCK
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)
150 int i, ret;
151 int value = 0;
152 int val = gpiod_get_value(hx711_data->gpiod_dout);
154 /* we double check if it's really down */
155 if (val)
156 return -EIO;
158 for (i = 0; i < 24; i++) {
159 value <<= 1;
160 ret = hx711_cycle(hx711_data);
161 if (ret)
162 value++;
165 value ^= 0x800000;
167 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
168 hx711_cycle(hx711_data);
170 return value;
173 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
175 int i, val;
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);
184 if (!val)
185 break;
186 /* sleep at least 10 ms */
187 msleep(10);
189 if (val)
190 return -EIO;
192 return 0;
195 static int hx711_reset(struct hx711_data *hx711_data)
197 int ret;
198 int val = gpiod_get_value(hx711_data->gpiod_dout);
200 if (val) {
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
208 * of 10 ms or higher
210 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
211 msleep(10);
212 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
214 ret = hx711_wait_for_ready(hx711_data);
215 if (ret)
216 return ret;
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);
222 if (ret < 0)
223 return ret;
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);
232 return val;
235 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
237 int ret;
239 if (chan == 0) {
240 if (hx711_data->gain_set == 32) {
241 hx711_data->gain_set = hx711_data->gain_chan_a;
243 ret = hx711_read(hx711_data);
244 if (ret < 0)
245 return ret;
247 ret = hx711_wait_for_ready(hx711_data);
248 if (ret)
249 return ret;
251 } else {
252 if (hx711_data->gain_set != 32) {
253 hx711_data->gain_set = 32;
255 ret = hx711_read(hx711_data);
256 if (ret < 0)
257 return ret;
259 ret = hx711_wait_for_ready(hx711_data);
260 if (ret)
261 return ret;
265 return 0;
268 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
270 int ret;
271 int val;
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!");
279 return -EIO;
282 ret = hx711_set_gain_for_channel(hx711_data, chan);
283 if (ret < 0)
284 return ret;
286 val = hx711_read(hx711_data);
288 return val;
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);
297 switch (mask) {
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);
305 if (*val < 0)
306 return *val;
307 return IIO_VAL_INT;
308 case IIO_CHAN_INFO_SCALE:
309 *val = 0;
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;
317 default:
318 return -EINVAL;
322 static int hx711_write_raw(struct iio_dev *indio_dev,
323 struct iio_chan_spec const *chan,
324 int val,
325 int val2,
326 long mask)
328 struct hx711_data *hx711_data = iio_priv(indio_dev);
329 int ret;
330 int gain;
332 switch (mask) {
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
338 if (val != 0)
339 return -EINVAL;
341 mutex_lock(&hx711_data->lock);
343 gain = hx711_get_scale_to_gain(val2);
344 if (gain < 0) {
345 mutex_unlock(&hx711_data->lock);
346 return gain;
349 if (gain != hx711_data->gain_set) {
350 hx711_data->gain_set = gain;
351 if (gain != 32)
352 hx711_data->gain_chan_a = gain;
354 ret = hx711_read(hx711_data);
355 if (ret < 0) {
356 mutex_unlock(&hx711_data->lock);
357 return ret;
361 mutex_unlock(&hx711_data->lock);
362 return 0;
363 default:
364 return -EINVAL;
367 return 0;
370 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
371 struct iio_chan_spec const *chan,
372 long mask)
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);
382 int i, j = 0;
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))
390 continue;
392 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
393 indio_dev->channels[i].channel);
394 j++;
397 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
398 pf->timestamp);
400 mutex_unlock(&hx711_data->lock);
402 iio_trigger_notify_done(indio_dev->trig);
404 return IRQ_HANDLED;
407 static ssize_t hx711_scale_available_show(struct device *dev,
408 struct device_attribute *attr,
409 char *buf)
411 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
412 int channel = iio_attr->address;
413 int i, len = 0;
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");
422 return len;
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,
434 NULL,
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[] = {
450 .type = IIO_VOLTAGE,
451 .channel = 0,
452 .indexed = 1,
453 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
454 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
455 .scan_index = 0,
456 .scan_type = {
457 .sign = 'u',
458 .realbits = 24,
459 .storagebits = 32,
460 .endianness = IIO_CPU,
464 .type = IIO_VOLTAGE,
465 .channel = 1,
466 .indexed = 1,
467 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
468 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
469 .scan_index = 1,
470 .scan_type = {
471 .sign = 'u',
472 .realbits = 24,
473 .storagebits = 32,
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;
486 int ret;
487 int i;
489 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
490 if (!indio_dev) {
491 dev_err(dev, "failed to allocate IIO device\n");
492 return -ENOMEM;
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);
527 if (ret < 0)
528 return ret;
531 * with
532 * full scale differential input range: AVDD / GAIN
533 * full scale output data: 2^24
534 * we can say:
535 * AVDD / GAIN = 2^24
536 * therefore:
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);
543 if (ret < 0)
544 goto error_regulator;
546 /* we need 10^-9 mV */
547 ret *= 100;
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
562 * of 50 microseconds
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);
583 if (ret < 0) {
584 dev_err(dev, "setup of iio triggered buffer failed\n");
585 goto error_regulator;
588 ret = iio_device_register(indio_dev);
589 if (ret < 0) {
590 dev_err(dev, "Couldn't register the device\n");
591 goto error_buffer;
594 return 0;
596 error_buffer:
597 iio_triggered_buffer_cleanup(indio_dev);
599 error_regulator:
600 regulator_disable(hx711_data->reg_avdd);
602 return ret;
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);
619 return 0;
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,
632 .driver = {
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");