1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * HX711: analog to digital converter for weight sensor module
4 *
5 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
6 */
7 #include <linux/array_size.h>
8 #include <linux/dev_printk.h>
9 #include <linux/err.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/regulator/consumer.h>
24
25 #define HX711_DATA_BITS 24
26
27 /* gain to pulse and scale conversion */
28 #define HX711_GAIN_MAX 3
29 #define HX711_RESET_GAIN 128
30
31 struct hx711_gain_to_scale {
32 int gain;
33 int gain_pulse;
34 int channel;
35 };
36
37 /*
38 * .scale depends on AVDD which in turn is known as soon as the regulator
39 * is available; it is stored per device in hx711_data.gain_scale[]
40 *
41 * channel A in documentation is channel 0 in source code
42 * channel B in documentation is channel 1 in source code
43 */
44 static const struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
45 { 128, 1, 0 },
46 { 32, 2, 1 },
47 { 64, 3, 0 },
48 };
49
hx711_get_gain_to_pulse(int gain)50 static int hx711_get_gain_to_pulse(int gain)
51 {
52 int i;
53
54 for (i = 0; i < HX711_GAIN_MAX; i++)
55 if (hx711_gain_to_scale[i].gain == gain)
56 return hx711_gain_to_scale[i].gain_pulse;
57 return 1;
58 }
59
hx711_get_gain_to_scale(const int * gain_scale,int gain)60 static int hx711_get_gain_to_scale(const int *gain_scale, int gain)
61 {
62 int i;
63
64 for (i = 0; i < HX711_GAIN_MAX; i++)
65 if (hx711_gain_to_scale[i].gain == gain)
66 return gain_scale[i];
67 return 0;
68 }
69
hx711_get_scale_to_gain(const int * gain_scale,int scale)70 static int hx711_get_scale_to_gain(const int *gain_scale, int scale)
71 {
72 int i;
73
74 for (i = 0; i < HX711_GAIN_MAX; i++)
75 if (gain_scale[i] == scale)
76 return hx711_gain_to_scale[i].gain;
77 return -EINVAL;
78 }
79
80 /**
81 * struct hx711_chip_info - per-variant static configuration
82 * @name: IIO device name
83 * @channels: channel specification array
84 * @num_channels: number of entries in @channels
85 * @iio_info: IIO info ops for this variant
86 */
87 struct hx711_chip_info {
88 const char *name;
89 const struct iio_chan_spec *channels __counted_by_ptr(num_channels);
90 unsigned int num_channels;
91 const struct iio_info *iio_info;
92 };
93
94 struct hx711_data {
95 struct device *dev;
96 struct gpio_desc *gpiod_pd_sck;
97 struct gpio_desc *gpiod_dout;
98 int gain_set; /* gain set on device */
99 int gain_chan_a; /* gain for channel A */
100 int gain_scale[HX711_GAIN_MAX];
101 const struct hx711_chip_info *chip_info;
102 struct mutex lock;
103 /*
104 * triggered buffer
105 * 2x32-bit channel + 64-bit naturally aligned timestamp
106 */
107 struct {
108 u32 channel[2];
109 aligned_s64 timestamp;
110 } buffer;
111 /*
112 * delay after a rising edge on SCK until the data is ready DOUT
113 * this is dependent on the hx711 where the datasheet tells a
114 * maximum value of 100 ns
115 * but also on potential parasitic capacities on the wiring
116 */
117 u32 data_ready_delay_ns;
118 u32 clock_frequency;
119 };
120
hx711_cycle(struct hx711_data * hx711_data)121 static int hx711_cycle(struct hx711_data *hx711_data)
122 {
123 unsigned long flags;
124
125 /*
126 * if preempted for more then 60us while PD_SCK is high:
127 * hx711 is going in reset
128 * ==> measuring is false
129 */
130 local_irq_save(flags);
131 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
132
133 /*
134 * wait until DOUT is ready
135 * it turned out that parasitic capacities are extending the time
136 * until DOUT has reached it's value
137 */
138 ndelay(hx711_data->data_ready_delay_ns);
139
140 /*
141 * here we are not waiting for 0.2 us as suggested by the datasheet,
142 * because the oscilloscope showed in a test scenario
143 * at least 1.15 us for PD_SCK high (T3 in datasheet)
144 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
145 */
146 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
147 local_irq_restore(flags);
148
149 /*
150 * make it a square wave for addressing cases with capacitance on
151 * PC_SCK
152 */
153 ndelay(hx711_data->data_ready_delay_ns);
154
155 /* sample as late as possible */
156 return gpiod_get_value(hx711_data->gpiod_dout);
157 }
158
hx711_read(struct hx711_data * hx711_data,int trailing_pulses)159 static int hx711_read(struct hx711_data *hx711_data, int trailing_pulses)
160 {
161 int value = 0;
162 int val;
163 int ret;
164
165 /* we double check if it's really down */
166 val = gpiod_get_value(hx711_data->gpiod_dout);
167 if (val)
168 return -EIO;
169
170 for (int i = 0; i < HX711_DATA_BITS; i++) {
171 value <<= 1;
172 ret = hx711_cycle(hx711_data);
173 if (ret)
174 value++;
175 }
176
177 value ^= 0x800000;
178
179 for (int i = 0; i < trailing_pulses; i++)
180 hx711_cycle(hx711_data);
181
182 return value;
183 }
184
hx711_wait_for_ready(struct hx711_data * hx711_data)185 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
186 {
187 int i, val;
188
189 /*
190 * in some rare cases the reset takes quite a long time
191 * especially when the channel is changed.
192 * Allow up to one second for it
193 */
194 for (i = 0; i < 100; i++) {
195 val = gpiod_get_value(hx711_data->gpiod_dout);
196 if (!val)
197 break;
198 /* sleep at least 10 ms */
199 msleep(10);
200 }
201 if (val)
202 return -EIO;
203
204 return 0;
205 }
206
hx711_reset(struct hx711_data * hx711_data)207 static int hx711_reset(struct hx711_data *hx711_data)
208 {
209 int val;
210
211 val = hx711_wait_for_ready(hx711_data);
212 if (val) {
213 /*
214 * an examination with the oszilloscope indicated
215 * that the first value read after the reset is not stable
216 * if we reset too short;
217 * the shorter the reset cycle
218 * the less reliable the first value after reset is;
219 * there were no problems encountered with a value
220 * of 10 ms or higher
221 */
222 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
223 msleep(10);
224 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
225
226 val = hx711_wait_for_ready(hx711_data);
227
228 /* after a reset the gain is 128 */
229 hx711_data->gain_set = HX711_RESET_GAIN;
230 }
231
232 return val;
233 }
234
hx711_set_gain_for_channel(struct hx711_data * hx711_data,int chan)235 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
236 {
237 int ret;
238
239 if (chan == 0) {
240 if (hx711_data->gain_set == 32) {
241 hx711_data->gain_set = hx711_data->gain_chan_a;
242
243 ret = hx711_read(hx711_data,
244 hx711_get_gain_to_pulse(hx711_data->gain_set));
245 if (ret < 0)
246 return ret;
247
248 ret = hx711_wait_for_ready(hx711_data);
249 if (ret)
250 return ret;
251 }
252 } else {
253 if (hx711_data->gain_set != 32) {
254 hx711_data->gain_set = 32;
255
256 ret = hx711_read(hx711_data,
257 hx711_get_gain_to_pulse(hx711_data->gain_set));
258 if (ret < 0)
259 return ret;
260
261 ret = hx711_wait_for_ready(hx711_data);
262 if (ret)
263 return ret;
264 }
265 }
266
267 return 0;
268 }
269
hx711_set_hx711_channel(struct hx711_data * hx711_data,const struct iio_chan_spec * chan,int * trailing_pulses)270 static int hx711_set_hx711_channel(struct hx711_data *hx711_data,
271 const struct iio_chan_spec *chan,
272 int *trailing_pulses)
273 {
274 int ret;
275
276 ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
277 if (ret < 0)
278 return ret;
279
280 *trailing_pulses = hx711_get_gain_to_pulse(hx711_data->gain_set);
281
282 return 0;
283 }
284
hx711_reset_read(struct hx711_data * hx711_data,const struct iio_chan_spec * chan)285 static int hx711_reset_read(struct hx711_data *hx711_data,
286 const struct iio_chan_spec *chan)
287 {
288 int trailing_pulses;
289 int ret;
290
291 /*
292 * hx711_reset() must be called from here
293 * because it could be calling hx711_read() by itself
294 */
295 if (hx711_reset(hx711_data)) {
296 dev_err(hx711_data->dev, "reset failed!");
297 return -EIO;
298 }
299
300 ret = hx711_set_hx711_channel(hx711_data, chan, &trailing_pulses);
301 if (ret < 0)
302 return ret;
303
304 return hx711_read(hx711_data, trailing_pulses);
305 }
306
hx711_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long mask)307 static int hx711_read_raw(struct iio_dev *indio_dev,
308 const struct iio_chan_spec *chan,
309 int *val, int *val2, long mask)
310 {
311 struct hx711_data *hx711_data = iio_priv(indio_dev);
312
313 switch (mask) {
314 case IIO_CHAN_INFO_RAW:
315 mutex_lock(&hx711_data->lock);
316
317 *val = hx711_reset_read(hx711_data, chan);
318
319 mutex_unlock(&hx711_data->lock);
320
321 if (*val < 0)
322 return *val;
323 return IIO_VAL_INT;
324 case IIO_CHAN_INFO_SCALE:
325 *val = 0;
326 mutex_lock(&hx711_data->lock);
327
328 *val2 = hx711_get_gain_to_scale(hx711_data->gain_scale,
329 hx711_data->gain_set);
330
331 mutex_unlock(&hx711_data->lock);
332
333 return IIO_VAL_INT_PLUS_NANO;
334 default:
335 return -EINVAL;
336 }
337 }
338
hx711_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)339 static int hx711_write_raw(struct iio_dev *indio_dev,
340 struct iio_chan_spec const *chan,
341 int val,
342 int val2,
343 long mask)
344 {
345 struct hx711_data *hx711_data = iio_priv(indio_dev);
346 int ret;
347 int gain;
348
349 switch (mask) {
350 case IIO_CHAN_INFO_SCALE:
351 /*
352 * a scale greater than 1 mV per LSB is not possible
353 * with the HX711, therefore val must be 0
354 */
355 if (val != 0)
356 return -EINVAL;
357
358 mutex_lock(&hx711_data->lock);
359
360 gain = hx711_get_scale_to_gain(hx711_data->gain_scale, val2);
361 if (gain < 0) {
362 mutex_unlock(&hx711_data->lock);
363 return gain;
364 }
365
366 if (gain != hx711_data->gain_set) {
367 hx711_data->gain_set = gain;
368 if (gain != 32)
369 hx711_data->gain_chan_a = gain;
370
371 ret = hx711_read(hx711_data,
372 hx711_get_gain_to_pulse(hx711_data->gain_set));
373 if (ret < 0) {
374 mutex_unlock(&hx711_data->lock);
375 return ret;
376 }
377 }
378
379 mutex_unlock(&hx711_data->lock);
380 return 0;
381 default:
382 return -EINVAL;
383 }
384
385 return 0;
386 }
387
hx711_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)388 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
389 struct iio_chan_spec const *chan,
390 long mask)
391 {
392 return IIO_VAL_INT_PLUS_NANO;
393 }
394
hx711_trigger(int irq,void * p)395 static irqreturn_t hx711_trigger(int irq, void *p)
396 {
397 struct iio_poll_func *pf = p;
398 struct iio_dev *indio_dev = pf->indio_dev;
399 struct hx711_data *hx711_data = iio_priv(indio_dev);
400 int i, j = 0;
401
402 mutex_lock(&hx711_data->lock);
403
404 memset(&hx711_data->buffer, 0, sizeof(hx711_data->buffer));
405
406 iio_for_each_active_channel(indio_dev, i) {
407 hx711_data->buffer.channel[j] =
408 hx711_reset_read(hx711_data, &indio_dev->channels[i]);
409 j++;
410 }
411
412 iio_push_to_buffers_with_timestamp(indio_dev, &hx711_data->buffer,
413 pf->timestamp);
414
415 mutex_unlock(&hx711_data->lock);
416
417 iio_trigger_notify_done(indio_dev->trig);
418
419 return IRQ_HANDLED;
420 }
421
hx711_scale_available_show(struct device * dev,struct device_attribute * attr,char * buf)422 static ssize_t hx711_scale_available_show(struct device *dev,
423 struct device_attribute *attr,
424 char *buf)
425 {
426 struct hx711_data *hx711_data = iio_priv(dev_to_iio_dev(dev));
427 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
428 int channel = iio_attr->address;
429 int i, len = 0;
430
431 for (i = 0; i < HX711_GAIN_MAX; i++)
432 if (hx711_gain_to_scale[i].channel == channel)
433 len += sprintf(buf + len, "0.%09d ",
434 hx711_data->gain_scale[i]);
435
436 len += sprintf(buf + len, "\n");
437
438 return len;
439 }
440
441 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
442 hx711_scale_available_show, NULL, 0);
443
444 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
445 hx711_scale_available_show, NULL, 1);
446
447 static struct attribute *hx711_attributes[] = {
448 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
449 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
450 NULL,
451 };
452
453 static const struct attribute_group hx711_attribute_group = {
454 .attrs = hx711_attributes,
455 };
456
457 static const struct iio_info hx711_iio_info = {
458 .read_raw = hx711_read_raw,
459 .write_raw = hx711_write_raw,
460 .write_raw_get_fmt = hx711_write_raw_get_fmt,
461 .attrs = &hx711_attribute_group,
462 };
463
464 static const struct iio_chan_spec hx711_chan_spec[] = {
465 {
466 .type = IIO_VOLTAGE,
467 .channel = 0,
468 .indexed = 1,
469 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
470 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
471 .scan_index = 0,
472 .scan_type = {
473 .sign = 'u',
474 .realbits = 24,
475 .storagebits = 32,
476 .endianness = IIO_CPU,
477 },
478 },
479 {
480 .type = IIO_VOLTAGE,
481 .channel = 1,
482 .indexed = 1,
483 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
484 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
485 .scan_index = 1,
486 .scan_type = {
487 .sign = 'u',
488 .realbits = 24,
489 .storagebits = 32,
490 .endianness = IIO_CPU,
491 },
492 },
493 IIO_CHAN_SOFT_TIMESTAMP(2),
494 };
495
496 static const struct hx711_chip_info hx711_chip = {
497 .name = "hx711",
498 .channels = hx711_chan_spec,
499 .iio_info = &hx711_iio_info,
500 .num_channels = ARRAY_SIZE(hx711_chan_spec),
501 };
502
hx711_probe(struct platform_device * pdev)503 static int hx711_probe(struct platform_device *pdev)
504 {
505 const struct hx711_chip_info *chip_info;
506 struct device *dev = &pdev->dev;
507 struct hx711_data *hx711_data;
508 struct iio_dev *indio_dev;
509 int ret;
510 int i;
511
512 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
513 if (!indio_dev)
514 return -ENOMEM;
515
516 hx711_data = iio_priv(indio_dev);
517 hx711_data->dev = dev;
518
519 mutex_init(&hx711_data->lock);
520
521 chip_info = device_get_match_data(dev);
522 if (!chip_info)
523 return dev_err_probe(dev, -ENODEV, "missing driver data\n");
524
525 hx711_data->chip_info = chip_info;
526
527 /*
528 * PD_SCK stands for power down and serial clock input of HX711
529 * in the driver it is an output
530 */
531 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
532 if (IS_ERR(hx711_data->gpiod_pd_sck))
533 return dev_err_probe(dev, PTR_ERR(hx711_data->gpiod_pd_sck),
534 "failed to get sck-gpiod\n");
535
536 /*
537 * DOUT stands for serial data output of HX711
538 * for the driver it is an input
539 */
540 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
541 if (IS_ERR(hx711_data->gpiod_dout))
542 return dev_err_probe(dev, PTR_ERR(hx711_data->gpiod_dout),
543 "failed to get dout-gpiod\n");
544
545 ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
546 if (ret < 0)
547 return ret;
548
549 /*
550 * with
551 * full scale differential input range: AVDD / GAIN
552 * full scale output data: 2^24
553 * we can say:
554 * AVDD / GAIN = 2^24
555 * therefore:
556 * 1 LSB = AVDD / GAIN / 2^24
557 * AVDD is in uV, but we need 10^-9 mV
558 * approximately to fit into a 32 bit number:
559 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
560 */
561
562 /* we need 10^-9 mV */
563 ret *= 100;
564
565 for (i = 0; i < HX711_GAIN_MAX; i++)
566 hx711_data->gain_scale[i] =
567 ret / hx711_gain_to_scale[i].gain / 1678;
568
569 hx711_data->gain_set = 128;
570 hx711_data->gain_chan_a = 128;
571
572 hx711_data->clock_frequency = 400000;
573 ret = device_property_read_u32(&pdev->dev, "clock-frequency",
574 &hx711_data->clock_frequency);
575
576 /*
577 * datasheet says the high level of PD_SCK has a maximum duration
578 * of 50 microseconds
579 */
580 if (hx711_data->clock_frequency < 20000) {
581 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
582 hx711_data->clock_frequency = 400000;
583 }
584
585 hx711_data->data_ready_delay_ns =
586 1000000000 / hx711_data->clock_frequency;
587
588 indio_dev->name = chip_info->name;
589 indio_dev->info = chip_info->iio_info;
590 indio_dev->modes = INDIO_DIRECT_MODE;
591 indio_dev->channels = chip_info->channels;
592 indio_dev->num_channels = chip_info->num_channels;
593
594 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
595 iio_pollfunc_store_time,
596 hx711_trigger, NULL);
597 if (ret < 0)
598 return dev_err_probe(dev, ret,
599 "setup of iio triggered buffer failed\n");
600
601 ret = devm_iio_device_register(dev, indio_dev);
602 if (ret < 0)
603 return dev_err_probe(dev, ret, "Couldn't register the device\n");
604
605 return 0;
606 }
607
608 static const struct of_device_id of_hx711_match[] = {
609 { .compatible = "avia,hx711", .data = &hx711_chip },
610 { }
611 };
612
613 MODULE_DEVICE_TABLE(of, of_hx711_match);
614
615 static struct platform_driver hx711_driver = {
616 .probe = hx711_probe,
617 .driver = {
618 .name = "hx711-gpio",
619 .of_match_table = of_hx711_match,
620 },
621 };
622
623 module_platform_driver(hx711_driver);
624
625 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
626 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
627 MODULE_LICENSE("GPL");
628 MODULE_ALIAS("platform:hx711-gpio");
629
630