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