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