xref: /openbmc/linux/drivers/iio/adc/hx711.c (revision 82e6fdd6)
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 
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 
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 
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 
102 static int hx711_cycle(struct hx711_data *hx711_data)
103 {
104 	int val;
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 	preempt_disable();
112 	gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
113 	val = gpiod_get_value(hx711_data->gpiod_dout);
114 	/*
115 	 * here we are not waiting for 0.2 us as suggested by the datasheet,
116 	 * because the oscilloscope showed in a test scenario
117 	 * at least 1.15 us for PD_SCK high (T3 in datasheet)
118 	 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
119 	 */
120 	gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
121 	preempt_enable();
122 
123 	return val;
124 }
125 
126 static int hx711_read(struct hx711_data *hx711_data)
127 {
128 	int i, ret;
129 	int value = 0;
130 	int val = gpiod_get_value(hx711_data->gpiod_dout);
131 
132 	/* we double check if it's really down */
133 	if (val)
134 		return -EIO;
135 
136 	for (i = 0; i < 24; i++) {
137 		value <<= 1;
138 		ret = hx711_cycle(hx711_data);
139 		if (ret)
140 			value++;
141 	}
142 
143 	value ^= 0x800000;
144 
145 	for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
146 		hx711_cycle(hx711_data);
147 
148 	return value;
149 }
150 
151 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
152 {
153 	int i, val;
154 
155 	/*
156 	 * in some rare cases the reset takes quite a long time
157 	 * especially when the channel is changed.
158 	 * Allow up to one second for it
159 	 */
160 	for (i = 0; i < 100; i++) {
161 		val = gpiod_get_value(hx711_data->gpiod_dout);
162 		if (!val)
163 			break;
164 		/* sleep at least 10 ms */
165 		msleep(10);
166 	}
167 	if (val)
168 		return -EIO;
169 
170 	return 0;
171 }
172 
173 static int hx711_reset(struct hx711_data *hx711_data)
174 {
175 	int ret;
176 	int val = gpiod_get_value(hx711_data->gpiod_dout);
177 
178 	if (val) {
179 		/*
180 		 * an examination with the oszilloscope indicated
181 		 * that the first value read after the reset is not stable
182 		 * if we reset too short;
183 		 * the shorter the reset cycle
184 		 * the less reliable the first value after reset is;
185 		 * there were no problems encountered with a value
186 		 * of 10 ms or higher
187 		 */
188 		gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
189 		msleep(10);
190 		gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
191 
192 		ret = hx711_wait_for_ready(hx711_data);
193 		if (ret)
194 			return ret;
195 		/*
196 		 * after a reset the gain is 128 so we do a dummy read
197 		 * to set the gain for the next read
198 		 */
199 		ret = hx711_read(hx711_data);
200 		if (ret < 0)
201 			return ret;
202 
203 		/*
204 		 * after a dummy read we need to wait vor readiness
205 		 * for not mixing gain pulses with the clock
206 		 */
207 		val = hx711_wait_for_ready(hx711_data);
208 	}
209 
210 	return val;
211 }
212 
213 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
214 {
215 	int ret;
216 
217 	if (chan == 0) {
218 		if (hx711_data->gain_set == 32) {
219 			hx711_data->gain_set = hx711_data->gain_chan_a;
220 
221 			ret = hx711_read(hx711_data);
222 			if (ret < 0)
223 				return ret;
224 
225 			ret = hx711_wait_for_ready(hx711_data);
226 			if (ret)
227 				return ret;
228 		}
229 	} else {
230 		if (hx711_data->gain_set != 32) {
231 			hx711_data->gain_set = 32;
232 
233 			ret = hx711_read(hx711_data);
234 			if (ret < 0)
235 				return ret;
236 
237 			ret = hx711_wait_for_ready(hx711_data);
238 			if (ret)
239 				return ret;
240 		}
241 	}
242 
243 	return 0;
244 }
245 
246 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
247 {
248 	int ret;
249 	int val;
250 
251 	/*
252 	 * hx711_reset() must be called from here
253 	 * because it could be calling hx711_read() by itself
254 	 */
255 	if (hx711_reset(hx711_data)) {
256 		dev_err(hx711_data->dev, "reset failed!");
257 		return -EIO;
258 	}
259 
260 	ret = hx711_set_gain_for_channel(hx711_data, chan);
261 	if (ret < 0)
262 		return ret;
263 
264 	val = hx711_read(hx711_data);
265 
266 	return val;
267 }
268 
269 static int hx711_read_raw(struct iio_dev *indio_dev,
270 				const struct iio_chan_spec *chan,
271 				int *val, int *val2, long mask)
272 {
273 	struct hx711_data *hx711_data = iio_priv(indio_dev);
274 
275 	switch (mask) {
276 	case IIO_CHAN_INFO_RAW:
277 		mutex_lock(&hx711_data->lock);
278 
279 		*val = hx711_reset_read(hx711_data, chan->channel);
280 
281 		mutex_unlock(&hx711_data->lock);
282 
283 		if (*val < 0)
284 			return *val;
285 		return IIO_VAL_INT;
286 	case IIO_CHAN_INFO_SCALE:
287 		*val = 0;
288 		mutex_lock(&hx711_data->lock);
289 
290 		*val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
291 
292 		mutex_unlock(&hx711_data->lock);
293 
294 		return IIO_VAL_INT_PLUS_NANO;
295 	default:
296 		return -EINVAL;
297 	}
298 }
299 
300 static int hx711_write_raw(struct iio_dev *indio_dev,
301 				struct iio_chan_spec const *chan,
302 				int val,
303 				int val2,
304 				long mask)
305 {
306 	struct hx711_data *hx711_data = iio_priv(indio_dev);
307 	int ret;
308 	int gain;
309 
310 	switch (mask) {
311 	case IIO_CHAN_INFO_SCALE:
312 		/*
313 		 * a scale greater than 1 mV per LSB is not possible
314 		 * with the HX711, therefore val must be 0
315 		 */
316 		if (val != 0)
317 			return -EINVAL;
318 
319 		mutex_lock(&hx711_data->lock);
320 
321 		gain = hx711_get_scale_to_gain(val2);
322 		if (gain < 0) {
323 			mutex_unlock(&hx711_data->lock);
324 			return gain;
325 		}
326 
327 		if (gain != hx711_data->gain_set) {
328 			hx711_data->gain_set = gain;
329 			if (gain != 32)
330 				hx711_data->gain_chan_a = gain;
331 
332 			ret = hx711_read(hx711_data);
333 			if (ret < 0) {
334 				mutex_unlock(&hx711_data->lock);
335 				return ret;
336 			}
337 		}
338 
339 		mutex_unlock(&hx711_data->lock);
340 		return 0;
341 	default:
342 		return -EINVAL;
343 	}
344 
345 	return 0;
346 }
347 
348 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
349 		struct iio_chan_spec const *chan,
350 		long mask)
351 {
352 	return IIO_VAL_INT_PLUS_NANO;
353 }
354 
355 static irqreturn_t hx711_trigger(int irq, void *p)
356 {
357 	struct iio_poll_func *pf = p;
358 	struct iio_dev *indio_dev = pf->indio_dev;
359 	struct hx711_data *hx711_data = iio_priv(indio_dev);
360 	int i, j = 0;
361 
362 	mutex_lock(&hx711_data->lock);
363 
364 	memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
365 
366 	for (i = 0; i < indio_dev->masklength; i++) {
367 		if (!test_bit(i, indio_dev->active_scan_mask))
368 			continue;
369 
370 		hx711_data->buffer[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 
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 
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 		dev_err(dev, "failed to allocate IIO device\n");
469 		return -ENOMEM;
470 	}
471 
472 	hx711_data = iio_priv(indio_dev);
473 	hx711_data->dev = dev;
474 
475 	mutex_init(&hx711_data->lock);
476 
477 	/*
478 	 * PD_SCK stands for power down and serial clock input of HX711
479 	 * in the driver it is an output
480 	 */
481 	hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
482 	if (IS_ERR(hx711_data->gpiod_pd_sck)) {
483 		dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
484 					PTR_ERR(hx711_data->gpiod_pd_sck));
485 		return PTR_ERR(hx711_data->gpiod_pd_sck);
486 	}
487 
488 	/*
489 	 * DOUT stands for serial data output of HX711
490 	 * for the driver it is an input
491 	 */
492 	hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
493 	if (IS_ERR(hx711_data->gpiod_dout)) {
494 		dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
495 					PTR_ERR(hx711_data->gpiod_dout));
496 		return PTR_ERR(hx711_data->gpiod_dout);
497 	}
498 
499 	hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
500 	if (IS_ERR(hx711_data->reg_avdd))
501 		return PTR_ERR(hx711_data->reg_avdd);
502 
503 	ret = regulator_enable(hx711_data->reg_avdd);
504 	if (ret < 0)
505 		return ret;
506 
507 	/*
508 	 * with
509 	 * full scale differential input range: AVDD / GAIN
510 	 * full scale output data: 2^24
511 	 * we can say:
512 	 *     AVDD / GAIN = 2^24
513 	 * therefore:
514 	 *     1 LSB = AVDD / GAIN / 2^24
515 	 * AVDD is in uV, but we need 10^-9 mV
516 	 * approximately to fit into a 32 bit number:
517 	 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
518 	 */
519 	ret = regulator_get_voltage(hx711_data->reg_avdd);
520 	if (ret < 0)
521 		goto error_regulator;
522 
523 	/* we need 10^-9 mV */
524 	ret *= 100;
525 
526 	for (i = 0; i < HX711_GAIN_MAX; i++)
527 		hx711_gain_to_scale[i].scale =
528 			ret / hx711_gain_to_scale[i].gain / 1678;
529 
530 	hx711_data->gain_set = 128;
531 	hx711_data->gain_chan_a = 128;
532 
533 	platform_set_drvdata(pdev, indio_dev);
534 
535 	indio_dev->name = "hx711";
536 	indio_dev->dev.parent = &pdev->dev;
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 = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
543 							hx711_trigger, NULL);
544 	if (ret < 0) {
545 		dev_err(dev, "setup of iio triggered buffer failed\n");
546 		goto error_regulator;
547 	}
548 
549 	ret = iio_device_register(indio_dev);
550 	if (ret < 0) {
551 		dev_err(dev, "Couldn't register the device\n");
552 		goto error_buffer;
553 	}
554 
555 	return 0;
556 
557 error_buffer:
558 	iio_triggered_buffer_cleanup(indio_dev);
559 
560 error_regulator:
561 	regulator_disable(hx711_data->reg_avdd);
562 
563 	return ret;
564 }
565 
566 static int hx711_remove(struct platform_device *pdev)
567 {
568 	struct hx711_data *hx711_data;
569 	struct iio_dev *indio_dev;
570 
571 	indio_dev = platform_get_drvdata(pdev);
572 	hx711_data = iio_priv(indio_dev);
573 
574 	iio_device_unregister(indio_dev);
575 
576 	iio_triggered_buffer_cleanup(indio_dev);
577 
578 	regulator_disable(hx711_data->reg_avdd);
579 
580 	return 0;
581 }
582 
583 static const struct of_device_id of_hx711_match[] = {
584 	{ .compatible = "avia,hx711", },
585 	{},
586 };
587 
588 MODULE_DEVICE_TABLE(of, of_hx711_match);
589 
590 static struct platform_driver hx711_driver = {
591 	.probe		= hx711_probe,
592 	.remove		= hx711_remove,
593 	.driver		= {
594 		.name		= "hx711-gpio",
595 		.of_match_table	= of_hx711_match,
596 	},
597 };
598 
599 module_platform_driver(hx711_driver);
600 
601 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
602 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
603 MODULE_LICENSE("GPL");
604 MODULE_ALIAS("platform:hx711-gpio");
605 
606