This source file includes following definitions.
- hx711_get_gain_to_pulse
- hx711_get_gain_to_scale
- hx711_get_scale_to_gain
- hx711_cycle
- hx711_read
- hx711_wait_for_ready
- hx711_reset
- hx711_set_gain_for_channel
- hx711_reset_read
- hx711_read_raw
- hx711_write_raw
- hx711_write_raw_get_fmt
- hx711_trigger
- hx711_scale_available_show
- hx711_probe
- hx711_remove
1
2
3
4
5
6
7 #include <linux/err.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/of.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
25 #define HX711_GAIN_MAX 3
26
27 struct hx711_gain_to_scale {
28 int gain;
29 int gain_pulse;
30 int scale;
31 int channel;
32 };
33
34
35
36
37
38
39
40
41
42 static struct hx711_gain_to_scale hx711_gain_to_scale[HX711_GAIN_MAX] = {
43 { 128, 1, 0, 0 },
44 { 32, 2, 0, 1 },
45 { 64, 3, 0, 0 }
46 };
47
48 static int hx711_get_gain_to_pulse(int gain)
49 {
50 int i;
51
52 for (i = 0; i < HX711_GAIN_MAX; i++)
53 if (hx711_gain_to_scale[i].gain == gain)
54 return hx711_gain_to_scale[i].gain_pulse;
55 return 1;
56 }
57
58 static int hx711_get_gain_to_scale(int gain)
59 {
60 int i;
61
62 for (i = 0; i < HX711_GAIN_MAX; i++)
63 if (hx711_gain_to_scale[i].gain == gain)
64 return hx711_gain_to_scale[i].scale;
65 return 0;
66 }
67
68 static int hx711_get_scale_to_gain(int scale)
69 {
70 int i;
71
72 for (i = 0; i < HX711_GAIN_MAX; i++)
73 if (hx711_gain_to_scale[i].scale == scale)
74 return hx711_gain_to_scale[i].gain;
75 return -EINVAL;
76 }
77
78 struct hx711_data {
79 struct device *dev;
80 struct gpio_desc *gpiod_pd_sck;
81 struct gpio_desc *gpiod_dout;
82 struct regulator *reg_avdd;
83 int gain_set;
84 int gain_chan_a;
85 struct mutex lock;
86
87
88
89
90 u32 buffer[4];
91
92
93
94
95
96
97 u32 data_ready_delay_ns;
98 u32 clock_frequency;
99 };
100
101 static int hx711_cycle(struct hx711_data *hx711_data)
102 {
103 unsigned long flags;
104
105
106
107
108
109
110 local_irq_save(flags);
111 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
112
113
114
115
116
117
118 ndelay(hx711_data->data_ready_delay_ns);
119
120
121
122
123
124
125
126 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
127 local_irq_restore(flags);
128
129
130
131
132
133 ndelay(hx711_data->data_ready_delay_ns);
134
135
136 return gpiod_get_value(hx711_data->gpiod_dout);
137 }
138
139 static int hx711_read(struct hx711_data *hx711_data)
140 {
141 int i, ret;
142 int value = 0;
143 int val = gpiod_get_value(hx711_data->gpiod_dout);
144
145
146 if (val)
147 return -EIO;
148
149 for (i = 0; i < 24; i++) {
150 value <<= 1;
151 ret = hx711_cycle(hx711_data);
152 if (ret)
153 value++;
154 }
155
156 value ^= 0x800000;
157
158 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
159 hx711_cycle(hx711_data);
160
161 return value;
162 }
163
164 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
165 {
166 int i, val;
167
168
169
170
171
172
173 for (i = 0; i < 100; i++) {
174 val = gpiod_get_value(hx711_data->gpiod_dout);
175 if (!val)
176 break;
177
178 msleep(10);
179 }
180 if (val)
181 return -EIO;
182
183 return 0;
184 }
185
186 static 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
194
195
196
197
198
199
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
210
211
212 ret = hx711_read(hx711_data);
213 if (ret < 0)
214 return ret;
215
216
217
218
219
220 val = hx711_wait_for_ready(hx711_data);
221 }
222
223 return val;
224 }
225
226 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
227 {
228 int ret;
229
230 if (chan == 0) {
231 if (hx711_data->gain_set == 32) {
232 hx711_data->gain_set = hx711_data->gain_chan_a;
233
234 ret = hx711_read(hx711_data);
235 if (ret < 0)
236 return ret;
237
238 ret = hx711_wait_for_ready(hx711_data);
239 if (ret)
240 return ret;
241 }
242 } else {
243 if (hx711_data->gain_set != 32) {
244 hx711_data->gain_set = 32;
245
246 ret = hx711_read(hx711_data);
247 if (ret < 0)
248 return ret;
249
250 ret = hx711_wait_for_ready(hx711_data);
251 if (ret)
252 return ret;
253 }
254 }
255
256 return 0;
257 }
258
259 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
260 {
261 int ret;
262 int val;
263
264
265
266
267
268 if (hx711_reset(hx711_data)) {
269 dev_err(hx711_data->dev, "reset failed!");
270 return -EIO;
271 }
272
273 ret = hx711_set_gain_for_channel(hx711_data, chan);
274 if (ret < 0)
275 return ret;
276
277 val = hx711_read(hx711_data);
278
279 return val;
280 }
281
282 static int hx711_read_raw(struct iio_dev *indio_dev,
283 const struct iio_chan_spec *chan,
284 int *val, int *val2, long mask)
285 {
286 struct hx711_data *hx711_data = iio_priv(indio_dev);
287
288 switch (mask) {
289 case IIO_CHAN_INFO_RAW:
290 mutex_lock(&hx711_data->lock);
291
292 *val = hx711_reset_read(hx711_data, chan->channel);
293
294 mutex_unlock(&hx711_data->lock);
295
296 if (*val < 0)
297 return *val;
298 return IIO_VAL_INT;
299 case IIO_CHAN_INFO_SCALE:
300 *val = 0;
301 mutex_lock(&hx711_data->lock);
302
303 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
304
305 mutex_unlock(&hx711_data->lock);
306
307 return IIO_VAL_INT_PLUS_NANO;
308 default:
309 return -EINVAL;
310 }
311 }
312
313 static int hx711_write_raw(struct iio_dev *indio_dev,
314 struct iio_chan_spec const *chan,
315 int val,
316 int val2,
317 long mask)
318 {
319 struct hx711_data *hx711_data = iio_priv(indio_dev);
320 int ret;
321 int gain;
322
323 switch (mask) {
324 case IIO_CHAN_INFO_SCALE:
325
326
327
328
329 if (val != 0)
330 return -EINVAL;
331
332 mutex_lock(&hx711_data->lock);
333
334 gain = hx711_get_scale_to_gain(val2);
335 if (gain < 0) {
336 mutex_unlock(&hx711_data->lock);
337 return gain;
338 }
339
340 if (gain != hx711_data->gain_set) {
341 hx711_data->gain_set = gain;
342 if (gain != 32)
343 hx711_data->gain_chan_a = gain;
344
345 ret = hx711_read(hx711_data);
346 if (ret < 0) {
347 mutex_unlock(&hx711_data->lock);
348 return ret;
349 }
350 }
351
352 mutex_unlock(&hx711_data->lock);
353 return 0;
354 default:
355 return -EINVAL;
356 }
357
358 return 0;
359 }
360
361 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
362 struct iio_chan_spec const *chan,
363 long mask)
364 {
365 return IIO_VAL_INT_PLUS_NANO;
366 }
367
368 static irqreturn_t hx711_trigger(int irq, void *p)
369 {
370 struct iio_poll_func *pf = p;
371 struct iio_dev *indio_dev = pf->indio_dev;
372 struct hx711_data *hx711_data = iio_priv(indio_dev);
373 int i, j = 0;
374
375 mutex_lock(&hx711_data->lock);
376
377 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
378
379 for (i = 0; i < indio_dev->masklength; i++) {
380 if (!test_bit(i, indio_dev->active_scan_mask))
381 continue;
382
383 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
384 indio_dev->channels[i].channel);
385 j++;
386 }
387
388 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
389 pf->timestamp);
390
391 mutex_unlock(&hx711_data->lock);
392
393 iio_trigger_notify_done(indio_dev->trig);
394
395 return IRQ_HANDLED;
396 }
397
398 static ssize_t hx711_scale_available_show(struct device *dev,
399 struct device_attribute *attr,
400 char *buf)
401 {
402 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
403 int channel = iio_attr->address;
404 int i, len = 0;
405
406 for (i = 0; i < HX711_GAIN_MAX; i++)
407 if (hx711_gain_to_scale[i].channel == channel)
408 len += sprintf(buf + len, "0.%09d ",
409 hx711_gain_to_scale[i].scale);
410
411 len += sprintf(buf + len, "\n");
412
413 return len;
414 }
415
416 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
417 hx711_scale_available_show, NULL, 0);
418
419 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
420 hx711_scale_available_show, NULL, 1);
421
422 static struct attribute *hx711_attributes[] = {
423 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
424 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
425 NULL,
426 };
427
428 static const struct attribute_group hx711_attribute_group = {
429 .attrs = hx711_attributes,
430 };
431
432 static const struct iio_info hx711_iio_info = {
433 .read_raw = hx711_read_raw,
434 .write_raw = hx711_write_raw,
435 .write_raw_get_fmt = hx711_write_raw_get_fmt,
436 .attrs = &hx711_attribute_group,
437 };
438
439 static const struct iio_chan_spec hx711_chan_spec[] = {
440 {
441 .type = IIO_VOLTAGE,
442 .channel = 0,
443 .indexed = 1,
444 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
445 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
446 .scan_index = 0,
447 .scan_type = {
448 .sign = 'u',
449 .realbits = 24,
450 .storagebits = 32,
451 .endianness = IIO_CPU,
452 },
453 },
454 {
455 .type = IIO_VOLTAGE,
456 .channel = 1,
457 .indexed = 1,
458 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
459 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
460 .scan_index = 1,
461 .scan_type = {
462 .sign = 'u',
463 .realbits = 24,
464 .storagebits = 32,
465 .endianness = IIO_CPU,
466 },
467 },
468 IIO_CHAN_SOFT_TIMESTAMP(2),
469 };
470
471 static int hx711_probe(struct platform_device *pdev)
472 {
473 struct device *dev = &pdev->dev;
474 struct device_node *np = dev->of_node;
475 struct hx711_data *hx711_data;
476 struct iio_dev *indio_dev;
477 int ret;
478 int i;
479
480 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
481 if (!indio_dev) {
482 dev_err(dev, "failed to allocate IIO device\n");
483 return -ENOMEM;
484 }
485
486 hx711_data = iio_priv(indio_dev);
487 hx711_data->dev = dev;
488
489 mutex_init(&hx711_data->lock);
490
491
492
493
494
495 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
496 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
497 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
498 PTR_ERR(hx711_data->gpiod_pd_sck));
499 return PTR_ERR(hx711_data->gpiod_pd_sck);
500 }
501
502
503
504
505
506 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
507 if (IS_ERR(hx711_data->gpiod_dout)) {
508 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
509 PTR_ERR(hx711_data->gpiod_dout));
510 return PTR_ERR(hx711_data->gpiod_dout);
511 }
512
513 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
514 if (IS_ERR(hx711_data->reg_avdd))
515 return PTR_ERR(hx711_data->reg_avdd);
516
517 ret = regulator_enable(hx711_data->reg_avdd);
518 if (ret < 0)
519 return ret;
520
521
522
523
524
525
526
527
528
529
530
531
532
533 ret = regulator_get_voltage(hx711_data->reg_avdd);
534 if (ret < 0)
535 goto error_regulator;
536
537
538 ret *= 100;
539
540 for (i = 0; i < HX711_GAIN_MAX; i++)
541 hx711_gain_to_scale[i].scale =
542 ret / hx711_gain_to_scale[i].gain / 1678;
543
544 hx711_data->gain_set = 128;
545 hx711_data->gain_chan_a = 128;
546
547 hx711_data->clock_frequency = 400000;
548 ret = of_property_read_u32(np, "clock-frequency",
549 &hx711_data->clock_frequency);
550
551
552
553
554
555 if (hx711_data->clock_frequency < 20000) {
556 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
557 hx711_data->clock_frequency = 400000;
558 }
559
560 hx711_data->data_ready_delay_ns =
561 1000000000 / hx711_data->clock_frequency;
562
563 platform_set_drvdata(pdev, indio_dev);
564
565 indio_dev->name = "hx711";
566 indio_dev->dev.parent = &pdev->dev;
567 indio_dev->info = &hx711_iio_info;
568 indio_dev->modes = INDIO_DIRECT_MODE;
569 indio_dev->channels = hx711_chan_spec;
570 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
571
572 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
573 hx711_trigger, NULL);
574 if (ret < 0) {
575 dev_err(dev, "setup of iio triggered buffer failed\n");
576 goto error_regulator;
577 }
578
579 ret = iio_device_register(indio_dev);
580 if (ret < 0) {
581 dev_err(dev, "Couldn't register the device\n");
582 goto error_buffer;
583 }
584
585 return 0;
586
587 error_buffer:
588 iio_triggered_buffer_cleanup(indio_dev);
589
590 error_regulator:
591 regulator_disable(hx711_data->reg_avdd);
592
593 return ret;
594 }
595
596 static int hx711_remove(struct platform_device *pdev)
597 {
598 struct hx711_data *hx711_data;
599 struct iio_dev *indio_dev;
600
601 indio_dev = platform_get_drvdata(pdev);
602 hx711_data = iio_priv(indio_dev);
603
604 iio_device_unregister(indio_dev);
605
606 iio_triggered_buffer_cleanup(indio_dev);
607
608 regulator_disable(hx711_data->reg_avdd);
609
610 return 0;
611 }
612
613 static const struct of_device_id of_hx711_match[] = {
614 { .compatible = "avia,hx711", },
615 {},
616 };
617
618 MODULE_DEVICE_TABLE(of, of_hx711_match);
619
620 static struct platform_driver hx711_driver = {
621 .probe = hx711_probe,
622 .remove = hx711_remove,
623 .driver = {
624 .name = "hx711-gpio",
625 .of_match_table = of_hx711_match,
626 },
627 };
628
629 module_platform_driver(hx711_driver);
630
631 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
632 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
633 MODULE_LICENSE("GPL");
634 MODULE_ALIAS("platform:hx711-gpio");
635