xref: /linux/drivers/iio/adc/hx711.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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/gpio/consumer.h>
28 #include <linux/regulator/consumer.h>
29 
30 /* gain to pulse and scale conversion */
31 #define HX711_GAIN_MAX		3
32 
33 struct 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  */
48 static 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 
54 static 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 
64 static 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 
74 static 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 
84 struct 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 
94 static int hx711_cycle(struct hx711_data *hx711_data)
95 {
96 	int val;
97 
98 	/*
99 	 * if preempted for more then 60us while PD_SCK is high:
100 	 * hx711 is going in reset
101 	 * ==> measuring is false
102 	 */
103 	preempt_disable();
104 	gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
105 	val = gpiod_get_value(hx711_data->gpiod_dout);
106 	/*
107 	 * here we are not waiting for 0.2 us as suggested by the datasheet,
108 	 * because the oscilloscope showed in a test scenario
109 	 * at least 1.15 us for PD_SCK high (T3 in datasheet)
110 	 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz
111 	 */
112 	gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
113 	preempt_enable();
114 
115 	return val;
116 }
117 
118 static int hx711_read(struct hx711_data *hx711_data)
119 {
120 	int i, ret;
121 	int value = 0;
122 	int val = gpiod_get_value(hx711_data->gpiod_dout);
123 
124 	/* we double check if it's really down */
125 	if (val)
126 		return -EIO;
127 
128 	for (i = 0; i < 24; i++) {
129 		value <<= 1;
130 		ret = hx711_cycle(hx711_data);
131 		if (ret)
132 			value++;
133 	}
134 
135 	value ^= 0x800000;
136 
137 	for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
138 		hx711_cycle(hx711_data);
139 
140 	return value;
141 }
142 
143 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
144 {
145 	int i, val;
146 
147 	/*
148 	 * a maximum reset cycle time of 56 ms was measured.
149 	 * we round it up to 100 ms
150 	 */
151 	for (i = 0; i < 100; i++) {
152 		val = gpiod_get_value(hx711_data->gpiod_dout);
153 		if (!val)
154 			break;
155 		/* sleep at least 1 ms */
156 		msleep(1);
157 	}
158 	if (val)
159 		return -EIO;
160 
161 	return 0;
162 }
163 
164 static int hx711_reset(struct hx711_data *hx711_data)
165 {
166 	int ret;
167 	int val = gpiod_get_value(hx711_data->gpiod_dout);
168 
169 	if (val) {
170 		/*
171 		 * an examination with the oszilloscope indicated
172 		 * that the first value read after the reset is not stable
173 		 * if we reset too short;
174 		 * the shorter the reset cycle
175 		 * the less reliable the first value after reset is;
176 		 * there were no problems encountered with a value
177 		 * of 10 ms or higher
178 		 */
179 		gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
180 		msleep(10);
181 		gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
182 
183 		ret = hx711_wait_for_ready(hx711_data);
184 		if (ret)
185 			return ret;
186 		/*
187 		 * after a reset the gain is 128 so we do a dummy read
188 		 * to set the gain for the next read
189 		 */
190 		ret = hx711_read(hx711_data);
191 		if (ret < 0)
192 			return ret;
193 
194 		/*
195 		 * after a dummy read we need to wait vor readiness
196 		 * for not mixing gain pulses with the clock
197 		 */
198 		ret = hx711_wait_for_ready(hx711_data);
199 		if (ret)
200 			return ret;
201 	}
202 
203 	return val;
204 }
205 
206 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
207 {
208 	int ret;
209 
210 	if (chan == 0) {
211 		if (hx711_data->gain_set == 32) {
212 			hx711_data->gain_set = hx711_data->gain_chan_a;
213 
214 			ret = hx711_read(hx711_data);
215 			if (ret < 0)
216 				return ret;
217 
218 			ret = hx711_wait_for_ready(hx711_data);
219 			if (ret)
220 				return ret;
221 		}
222 	} else {
223 		if (hx711_data->gain_set != 32) {
224 			hx711_data->gain_set = 32;
225 
226 			ret = hx711_read(hx711_data);
227 			if (ret < 0)
228 				return ret;
229 
230 			ret = hx711_wait_for_ready(hx711_data);
231 			if (ret)
232 				return ret;
233 		}
234 	}
235 
236 	return 0;
237 }
238 
239 static int hx711_read_raw(struct iio_dev *indio_dev,
240 				const struct iio_chan_spec *chan,
241 				int *val, int *val2, long mask)
242 {
243 	struct hx711_data *hx711_data = iio_priv(indio_dev);
244 	int ret;
245 
246 	switch (mask) {
247 	case IIO_CHAN_INFO_RAW:
248 		mutex_lock(&hx711_data->lock);
249 
250 		/*
251 		 * hx711_reset() must be called from here
252 		 * because it could be calling hx711_read() by itself
253 		 */
254 		if (hx711_reset(hx711_data)) {
255 			mutex_unlock(&hx711_data->lock);
256 			dev_err(hx711_data->dev, "reset failed!");
257 			return -EIO;
258 		}
259 
260 		ret = hx711_set_gain_for_channel(hx711_data, chan->channel);
261 		if (ret < 0) {
262 			mutex_unlock(&hx711_data->lock);
263 			return ret;
264 		}
265 
266 		*val = hx711_read(hx711_data);
267 
268 		mutex_unlock(&hx711_data->lock);
269 
270 		if (*val < 0)
271 			return *val;
272 		return IIO_VAL_INT;
273 	case IIO_CHAN_INFO_SCALE:
274 		*val = 0;
275 		mutex_lock(&hx711_data->lock);
276 
277 		*val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
278 
279 		mutex_unlock(&hx711_data->lock);
280 
281 		return IIO_VAL_INT_PLUS_NANO;
282 	default:
283 		return -EINVAL;
284 	}
285 }
286 
287 static int hx711_write_raw(struct iio_dev *indio_dev,
288 				struct iio_chan_spec const *chan,
289 				int val,
290 				int val2,
291 				long mask)
292 {
293 	struct hx711_data *hx711_data = iio_priv(indio_dev);
294 	int ret;
295 	int gain;
296 
297 	switch (mask) {
298 	case IIO_CHAN_INFO_SCALE:
299 		/*
300 		 * a scale greater than 1 mV per LSB is not possible
301 		 * with the HX711, therefore val must be 0
302 		 */
303 		if (val != 0)
304 			return -EINVAL;
305 
306 		mutex_lock(&hx711_data->lock);
307 
308 		gain = hx711_get_scale_to_gain(val2);
309 		if (gain < 0) {
310 			mutex_unlock(&hx711_data->lock);
311 			return gain;
312 		}
313 
314 		if (gain != hx711_data->gain_set) {
315 			hx711_data->gain_set = gain;
316 			if (gain != 32)
317 				hx711_data->gain_chan_a = gain;
318 
319 			ret = hx711_read(hx711_data);
320 			if (ret < 0) {
321 				mutex_unlock(&hx711_data->lock);
322 				return ret;
323 			}
324 		}
325 
326 		mutex_unlock(&hx711_data->lock);
327 		return 0;
328 	default:
329 		return -EINVAL;
330 	}
331 
332 	return 0;
333 }
334 
335 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
336 		struct iio_chan_spec const *chan,
337 		long mask)
338 {
339 	return IIO_VAL_INT_PLUS_NANO;
340 }
341 
342 static ssize_t hx711_scale_available_show(struct device *dev,
343 				struct device_attribute *attr,
344 				char *buf)
345 {
346 	struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
347 	int channel = iio_attr->address;
348 	int i, len = 0;
349 
350 	for (i = 0; i < HX711_GAIN_MAX; i++)
351 		if (hx711_gain_to_scale[i].channel == channel)
352 			len += sprintf(buf + len, "0.%09d ",
353 					hx711_gain_to_scale[i].scale);
354 
355 	len += sprintf(buf + len, "\n");
356 
357 	return len;
358 }
359 
360 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
361 	hx711_scale_available_show, NULL, 0);
362 
363 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
364 	hx711_scale_available_show, NULL, 1);
365 
366 static struct attribute *hx711_attributes[] = {
367 	&iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
368 	&iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
369 	NULL,
370 };
371 
372 static struct attribute_group hx711_attribute_group = {
373 	.attrs = hx711_attributes,
374 };
375 
376 static const struct iio_info hx711_iio_info = {
377 	.driver_module		= THIS_MODULE,
378 	.read_raw		= hx711_read_raw,
379 	.write_raw		= hx711_write_raw,
380 	.write_raw_get_fmt	= hx711_write_raw_get_fmt,
381 	.attrs			= &hx711_attribute_group,
382 };
383 
384 static const struct iio_chan_spec hx711_chan_spec[] = {
385 	{
386 		.type = IIO_VOLTAGE,
387 		.channel = 0,
388 		.indexed = 1,
389 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
390 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
391 	},
392 	{
393 		.type = IIO_VOLTAGE,
394 		.channel = 1,
395 		.indexed = 1,
396 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
397 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
398 	},
399 };
400 
401 static int hx711_probe(struct platform_device *pdev)
402 {
403 	struct device *dev = &pdev->dev;
404 	struct hx711_data *hx711_data;
405 	struct iio_dev *indio_dev;
406 	int ret;
407 	int i;
408 
409 	indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
410 	if (!indio_dev) {
411 		dev_err(dev, "failed to allocate IIO device\n");
412 		return -ENOMEM;
413 	}
414 
415 	hx711_data = iio_priv(indio_dev);
416 	hx711_data->dev = dev;
417 
418 	mutex_init(&hx711_data->lock);
419 
420 	/*
421 	 * PD_SCK stands for power down and serial clock input of HX711
422 	 * in the driver it is an output
423 	 */
424 	hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
425 	if (IS_ERR(hx711_data->gpiod_pd_sck)) {
426 		dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
427 					PTR_ERR(hx711_data->gpiod_pd_sck));
428 		return PTR_ERR(hx711_data->gpiod_pd_sck);
429 	}
430 
431 	/*
432 	 * DOUT stands for serial data output of HX711
433 	 * for the driver it is an input
434 	 */
435 	hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
436 	if (IS_ERR(hx711_data->gpiod_dout)) {
437 		dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
438 					PTR_ERR(hx711_data->gpiod_dout));
439 		return PTR_ERR(hx711_data->gpiod_dout);
440 	}
441 
442 	hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
443 	if (IS_ERR(hx711_data->reg_avdd))
444 		return PTR_ERR(hx711_data->reg_avdd);
445 
446 	ret = regulator_enable(hx711_data->reg_avdd);
447 	if (ret < 0)
448 		return ret;
449 
450 	/*
451 	 * with
452 	 * full scale differential input range: AVDD / GAIN
453 	 * full scale output data: 2^24
454 	 * we can say:
455 	 *     AVDD / GAIN = 2^24
456 	 * therefore:
457 	 *     1 LSB = AVDD / GAIN / 2^24
458 	 * AVDD is in uV, but we need 10^-9 mV
459 	 * approximately to fit into a 32 bit number:
460 	 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
461 	 */
462 	ret = regulator_get_voltage(hx711_data->reg_avdd);
463 	if (ret < 0) {
464 		regulator_disable(hx711_data->reg_avdd);
465 		return ret;
466 	}
467 	/* we need 10^-9 mV */
468 	ret *= 100;
469 
470 	for (i = 0; i < HX711_GAIN_MAX; i++)
471 		hx711_gain_to_scale[i].scale =
472 			ret / hx711_gain_to_scale[i].gain / 1678;
473 
474 	hx711_data->gain_set = 128;
475 	hx711_data->gain_chan_a = 128;
476 
477 	platform_set_drvdata(pdev, indio_dev);
478 
479 	indio_dev->name = "hx711";
480 	indio_dev->dev.parent = &pdev->dev;
481 	indio_dev->info = &hx711_iio_info;
482 	indio_dev->modes = INDIO_DIRECT_MODE;
483 	indio_dev->channels = hx711_chan_spec;
484 	indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
485 
486 	ret = iio_device_register(indio_dev);
487 	if (ret < 0) {
488 		dev_err(dev, "Couldn't register the device\n");
489 		regulator_disable(hx711_data->reg_avdd);
490 	}
491 
492 	return ret;
493 }
494 
495 static int hx711_remove(struct platform_device *pdev)
496 {
497 	struct hx711_data *hx711_data;
498 	struct iio_dev *indio_dev;
499 
500 	indio_dev = platform_get_drvdata(pdev);
501 	hx711_data = iio_priv(indio_dev);
502 
503 	iio_device_unregister(indio_dev);
504 
505 	regulator_disable(hx711_data->reg_avdd);
506 
507 	return 0;
508 }
509 
510 static const struct of_device_id of_hx711_match[] = {
511 	{ .compatible = "avia,hx711", },
512 	{},
513 };
514 
515 MODULE_DEVICE_TABLE(of, of_hx711_match);
516 
517 static struct platform_driver hx711_driver = {
518 	.probe		= hx711_probe,
519 	.remove		= hx711_remove,
520 	.driver		= {
521 		.name		= "hx711-gpio",
522 		.of_match_table	= of_hx711_match,
523 	},
524 };
525 
526 module_platform_driver(hx711_driver);
527 
528 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
529 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
530 MODULE_LICENSE("GPL");
531 MODULE_ALIAS("platform:hx711-gpio");
532 
533