1 /*
2 * HX711: analog to digital converter for weight sensor module
3 *
4 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
5 *
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.
10 *
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.
15 */
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>
32
33 /* gain to pulse and scale conversion */
34 #define HX711_GAIN_MAX 3
35
36 struct hx711_gain_to_scale {
37 int gain;
38 int gain_pulse;
39 int scale;
40 int channel;
41 };
42
43 /*
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()
47 *
48 * channel A in documentation is channel 0 in source code
49 * channel B in documentation is channel 1 in source code
50 */
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 }
55 };
56
hx711_get_gain_to_pulse(int gain)57 static int hx711_get_gain_to_pulse(int gain)
58 {
59 int i;
60
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;
65 }
66
hx711_get_gain_to_scale(int gain)67 static int hx711_get_gain_to_scale(int gain)
68 {
69 int i;
70
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;
75 }
76
hx711_get_scale_to_gain(int scale)77 static int hx711_get_scale_to_gain(int scale)
78 {
79 int i;
80
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;
85 }
86
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;
95 /*
96 * triggered buffer
97 * 2x32-bit channel + 64-bit timestamp
98 */
99 u32 buffer[4];
100 /*
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
105 */
106 u32 data_ready_delay_ns;
107 u32 clock_frequency;
108 };
109
hx711_cycle(struct hx711_data * hx711_data)110 static int hx711_cycle(struct hx711_data *hx711_data)
111 {
112 int val;
113
114 /*
115 * if preempted for more then 60us while PD_SCK is high:
116 * hx711 is going in reset
117 * ==> measuring is false
118 */
119 preempt_disable();
120 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
121
122 /*
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
126 */
127 ndelay(hx711_data->data_ready_delay_ns);
128
129 val = gpiod_get_value(hx711_data->gpiod_dout);
130 /*
131 * here we are not waiting for 0.2 us as suggested by the datasheet,
132 * because the oscilloscope showed in a test scenario
133 * at least 1.15 us for PD_SCK high (T3 in datasheet)
134 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
135 */
136 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
137 preempt_enable();
138
139 /*
140 * make it a square wave for addressing cases with capacitance on
141 * PC_SCK
142 */
143 ndelay(hx711_data->data_ready_delay_ns);
144
145 return val;
146 }
147
hx711_read(struct hx711_data * hx711_data)148 static int hx711_read(struct hx711_data *hx711_data)
149 {
150 int i, ret;
151 int value = 0;
152 int val = gpiod_get_value(hx711_data->gpiod_dout);
153
154 /* we double check if it's really down */
155 if (val)
156 return -EIO;
157
158 for (i = 0; i < 24; i++) {
159 value <<= 1;
160 ret = hx711_cycle(hx711_data);
161 if (ret)
162 value++;
163 }
164
165 value ^= 0x800000;
166
167 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
168 hx711_cycle(hx711_data);
169
170 return value;
171 }
172
hx711_wait_for_ready(struct hx711_data * hx711_data)173 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
174 {
175 int i, val;
176
177 /*
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
181 */
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);
188 }
189 if (val)
190 return -EIO;
191
192 return 0;
193 }
194
hx711_reset(struct hx711_data * hx711_data)195 static int hx711_reset(struct hx711_data *hx711_data)
196 {
197 int ret;
198 int val = gpiod_get_value(hx711_data->gpiod_dout);
199
200 if (val) {
201 /*
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
209 */
210 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
211 msleep(10);
212 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
213
214 ret = hx711_wait_for_ready(hx711_data);
215 if (ret)
216 return ret;
217 /*
218 * after a reset the gain is 128 so we do a dummy read
219 * to set the gain for the next read
220 */
221 ret = hx711_read(hx711_data);
222 if (ret < 0)
223 return ret;
224
225 /*
226 * after a dummy read we need to wait vor readiness
227 * for not mixing gain pulses with the clock
228 */
229 val = hx711_wait_for_ready(hx711_data);
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 if (ret < 0)
245 return ret;
246
247 ret = hx711_wait_for_ready(hx711_data);
248 if (ret)
249 return ret;
250 }
251 } else {
252 if (hx711_data->gain_set != 32) {
253 hx711_data->gain_set = 32;
254
255 ret = hx711_read(hx711_data);
256 if (ret < 0)
257 return ret;
258
259 ret = hx711_wait_for_ready(hx711_data);
260 if (ret)
261 return ret;
262 }
263 }
264
265 return 0;
266 }
267
hx711_reset_read(struct hx711_data * hx711_data,int chan)268 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
269 {
270 int ret;
271 int val;
272
273 /*
274 * hx711_reset() must be called from here
275 * because it could be calling hx711_read() by itself
276 */
277 if (hx711_reset(hx711_data)) {
278 dev_err(hx711_data->dev, "reset failed!");
279 return -EIO;
280 }
281
282 ret = hx711_set_gain_for_channel(hx711_data, chan);
283 if (ret < 0)
284 return ret;
285
286 val = hx711_read(hx711_data);
287
288 return val;
289 }
290
hx711_read_raw(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,int * val,int * val2,long mask)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)
294 {
295 struct hx711_data *hx711_data = iio_priv(indio_dev);
296
297 switch (mask) {
298 case IIO_CHAN_INFO_RAW:
299 mutex_lock(&hx711_data->lock);
300
301 *val = hx711_reset_read(hx711_data, chan->channel);
302
303 mutex_unlock(&hx711_data->lock);
304
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);
311
312 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
313
314 mutex_unlock(&hx711_data->lock);
315
316 return IIO_VAL_INT_PLUS_NANO;
317 default:
318 return -EINVAL;
319 }
320 }
321
hx711_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)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)
327 {
328 struct hx711_data *hx711_data = iio_priv(indio_dev);
329 int ret;
330 int gain;
331
332 switch (mask) {
333 case IIO_CHAN_INFO_SCALE:
334 /*
335 * a scale greater than 1 mV per LSB is not possible
336 * with the HX711, therefore val must be 0
337 */
338 if (val != 0)
339 return -EINVAL;
340
341 mutex_lock(&hx711_data->lock);
342
343 gain = hx711_get_scale_to_gain(val2);
344 if (gain < 0) {
345 mutex_unlock(&hx711_data->lock);
346 return gain;
347 }
348
349 if (gain != hx711_data->gain_set) {
350 hx711_data->gain_set = gain;
351 if (gain != 32)
352 hx711_data->gain_chan_a = gain;
353
354 ret = hx711_read(hx711_data);
355 if (ret < 0) {
356 mutex_unlock(&hx711_data->lock);
357 return ret;
358 }
359 }
360
361 mutex_unlock(&hx711_data->lock);
362 return 0;
363 default:
364 return -EINVAL;
365 }
366
367 return 0;
368 }
369
hx711_write_raw_get_fmt(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,long mask)370 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
371 struct iio_chan_spec const *chan,
372 long mask)
373 {
374 return IIO_VAL_INT_PLUS_NANO;
375 }
376
hx711_trigger(int irq,void * p)377 static irqreturn_t hx711_trigger(int irq, void *p)
378 {
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;
383
384 mutex_lock(&hx711_data->lock);
385
386 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
387
388 for (i = 0; i < indio_dev->masklength; i++) {
389 if (!test_bit(i, indio_dev->active_scan_mask))
390 continue;
391
392 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
393 indio_dev->channels[i].channel);
394 j++;
395 }
396
397 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
398 pf->timestamp);
399
400 mutex_unlock(&hx711_data->lock);
401
402 iio_trigger_notify_done(indio_dev->trig);
403
404 return IRQ_HANDLED;
405 }
406
hx711_scale_available_show(struct device * dev,struct device_attribute * attr,char * buf)407 static ssize_t hx711_scale_available_show(struct device *dev,
408 struct device_attribute *attr,
409 char *buf)
410 {
411 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
412 int channel = iio_attr->address;
413 int i, len = 0;
414
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);
419
420 len += sprintf(buf + len, "\n");
421
422 return len;
423 }
424
425 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
426 hx711_scale_available_show, NULL, 0);
427
428 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
429 hx711_scale_available_show, NULL, 1);
430
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,
435 };
436
437 static const struct attribute_group hx711_attribute_group = {
438 .attrs = hx711_attributes,
439 };
440
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,
446 };
447
448 static const struct iio_chan_spec hx711_chan_spec[] = {
449 {
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,
461 },
462 },
463 {
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,
475 },
476 },
477 IIO_CHAN_SOFT_TIMESTAMP(2),
478 };
479
hx711_probe(struct platform_device * pdev)480 static int hx711_probe(struct platform_device *pdev)
481 {
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;
488
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;
493 }
494
495 hx711_data = iio_priv(indio_dev);
496 hx711_data->dev = dev;
497
498 mutex_init(&hx711_data->lock);
499
500 /*
501 * PD_SCK stands for power down and serial clock input of HX711
502 * in the driver it is an output
503 */
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);
509 }
510
511 /*
512 * DOUT stands for serial data output of HX711
513 * for the driver it is an input
514 */
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);
520 }
521
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);
525
526 ret = regulator_enable(hx711_data->reg_avdd);
527 if (ret < 0)
528 return ret;
529
530 /*
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]
541 */
542 ret = regulator_get_voltage(hx711_data->reg_avdd);
543 if (ret < 0)
544 goto error_regulator;
545
546 /* we need 10^-9 mV */
547 ret *= 100;
548
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;
552
553 hx711_data->gain_set = 128;
554 hx711_data->gain_chan_a = 128;
555
556 hx711_data->clock_frequency = 400000;
557 ret = of_property_read_u32(np, "clock-frequency",
558 &hx711_data->clock_frequency);
559
560 /*
561 * datasheet says the high level of PD_SCK has a maximum duration
562 * of 50 microseconds
563 */
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;
567 }
568
569 hx711_data->data_ready_delay_ns =
570 1000000000 / hx711_data->clock_frequency;
571
572 platform_set_drvdata(pdev, indio_dev);
573
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);
580
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;
586 }
587
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;
592 }
593
594 return 0;
595
596 error_buffer:
597 iio_triggered_buffer_cleanup(indio_dev);
598
599 error_regulator:
600 regulator_disable(hx711_data->reg_avdd);
601
602 return ret;
603 }
604
hx711_remove(struct platform_device * pdev)605 static int hx711_remove(struct platform_device *pdev)
606 {
607 struct hx711_data *hx711_data;
608 struct iio_dev *indio_dev;
609
610 indio_dev = platform_get_drvdata(pdev);
611 hx711_data = iio_priv(indio_dev);
612
613 iio_device_unregister(indio_dev);
614
615 iio_triggered_buffer_cleanup(indio_dev);
616
617 regulator_disable(hx711_data->reg_avdd);
618
619 return 0;
620 }
621
622 static const struct of_device_id of_hx711_match[] = {
623 { .compatible = "avia,hx711", },
624 {},
625 };
626
627 MODULE_DEVICE_TABLE(of, of_hx711_match);
628
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,
635 },
636 };
637
638 module_platform_driver(hx711_driver);
639
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");
644
645