Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / adc / hx711.c
blob6a173531d355bab573dd874db2cd9fdbeb3c5af1
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * HX711: analog to digital converter for weight sensor module
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/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>
24 /* gain to pulse and scale conversion */
25 #define HX711_GAIN_MAX 3
26 #define HX711_RESET_GAIN 128
28 struct hx711_gain_to_scale {
29 int gain;
30 int gain_pulse;
31 int scale;
32 int channel;
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()
40 * channel A in documentation is channel 0 in source code
41 * channel B in documentation is channel 1 in source code
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 }
49 static int hx711_get_gain_to_pulse(int gain)
51 int i;
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;
59 static int hx711_get_gain_to_scale(int gain)
61 int i;
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;
69 static int hx711_get_scale_to_gain(int scale)
71 int i;
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;
79 struct hx711_data {
80 struct device *dev;
81 struct gpio_desc *gpiod_pd_sck;
82 struct gpio_desc *gpiod_dout;
83 struct regulator *reg_avdd;
84 int gain_set; /* gain set on device */
85 int gain_chan_a; /* gain for channel A */
86 struct mutex lock;
88 * triggered buffer
89 * 2x32-bit channel + 64-bit timestamp
91 u32 buffer[4];
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
98 u32 data_ready_delay_ns;
99 u32 clock_frequency;
102 static int hx711_cycle(struct hx711_data *hx711_data)
104 unsigned long flags;
107 * if preempted for more then 60us while PD_SCK is high:
108 * hx711 is going in reset
109 * ==> measuring is false
111 local_irq_save(flags);
112 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
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
119 ndelay(hx711_data->data_ready_delay_ns);
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
127 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
128 local_irq_restore(flags);
131 * make it a square wave for addressing cases with capacitance on
132 * PC_SCK
134 ndelay(hx711_data->data_ready_delay_ns);
136 /* sample as late as possible */
137 return gpiod_get_value(hx711_data->gpiod_dout);
140 static int hx711_read(struct hx711_data *hx711_data)
142 int i, ret;
143 int value = 0;
144 int val = gpiod_get_value(hx711_data->gpiod_dout);
146 /* we double check if it's really down */
147 if (val)
148 return -EIO;
150 for (i = 0; i < 24; i++) {
151 value <<= 1;
152 ret = hx711_cycle(hx711_data);
153 if (ret)
154 value++;
157 value ^= 0x800000;
159 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
160 hx711_cycle(hx711_data);
162 return value;
165 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
167 int i, val;
170 * in some rare cases the reset takes quite a long time
171 * especially when the channel is changed.
172 * Allow up to one second for it
174 for (i = 0; i < 100; i++) {
175 val = gpiod_get_value(hx711_data->gpiod_dout);
176 if (!val)
177 break;
178 /* sleep at least 10 ms */
179 msleep(10);
181 if (val)
182 return -EIO;
184 return 0;
187 static int hx711_reset(struct hx711_data *hx711_data)
189 int val = hx711_wait_for_ready(hx711_data);
191 if (val) {
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
201 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
202 msleep(10);
203 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
205 val = hx711_wait_for_ready(hx711_data);
207 /* after a reset the gain is 128 */
208 hx711_data->gain_set = HX711_RESET_GAIN;
211 return val;
214 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
216 int ret;
218 if (chan == 0) {
219 if (hx711_data->gain_set == 32) {
220 hx711_data->gain_set = hx711_data->gain_chan_a;
222 ret = hx711_read(hx711_data);
223 if (ret < 0)
224 return ret;
226 ret = hx711_wait_for_ready(hx711_data);
227 if (ret)
228 return ret;
230 } else {
231 if (hx711_data->gain_set != 32) {
232 hx711_data->gain_set = 32;
234 ret = hx711_read(hx711_data);
235 if (ret < 0)
236 return ret;
238 ret = hx711_wait_for_ready(hx711_data);
239 if (ret)
240 return ret;
244 return 0;
247 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
249 int ret;
250 int val;
253 * hx711_reset() must be called from here
254 * because it could be calling hx711_read() by itself
256 if (hx711_reset(hx711_data)) {
257 dev_err(hx711_data->dev, "reset failed!");
258 return -EIO;
261 ret = hx711_set_gain_for_channel(hx711_data, chan);
262 if (ret < 0)
263 return ret;
265 val = hx711_read(hx711_data);
267 return val;
270 static int hx711_read_raw(struct iio_dev *indio_dev,
271 const struct iio_chan_spec *chan,
272 int *val, int *val2, long mask)
274 struct hx711_data *hx711_data = iio_priv(indio_dev);
276 switch (mask) {
277 case IIO_CHAN_INFO_RAW:
278 mutex_lock(&hx711_data->lock);
280 *val = hx711_reset_read(hx711_data, chan->channel);
282 mutex_unlock(&hx711_data->lock);
284 if (*val < 0)
285 return *val;
286 return IIO_VAL_INT;
287 case IIO_CHAN_INFO_SCALE:
288 *val = 0;
289 mutex_lock(&hx711_data->lock);
291 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
293 mutex_unlock(&hx711_data->lock);
295 return IIO_VAL_INT_PLUS_NANO;
296 default:
297 return -EINVAL;
301 static int hx711_write_raw(struct iio_dev *indio_dev,
302 struct iio_chan_spec const *chan,
303 int val,
304 int val2,
305 long mask)
307 struct hx711_data *hx711_data = iio_priv(indio_dev);
308 int ret;
309 int gain;
311 switch (mask) {
312 case IIO_CHAN_INFO_SCALE:
314 * a scale greater than 1 mV per LSB is not possible
315 * with the HX711, therefore val must be 0
317 if (val != 0)
318 return -EINVAL;
320 mutex_lock(&hx711_data->lock);
322 gain = hx711_get_scale_to_gain(val2);
323 if (gain < 0) {
324 mutex_unlock(&hx711_data->lock);
325 return gain;
328 if (gain != hx711_data->gain_set) {
329 hx711_data->gain_set = gain;
330 if (gain != 32)
331 hx711_data->gain_chan_a = gain;
333 ret = hx711_read(hx711_data);
334 if (ret < 0) {
335 mutex_unlock(&hx711_data->lock);
336 return ret;
340 mutex_unlock(&hx711_data->lock);
341 return 0;
342 default:
343 return -EINVAL;
346 return 0;
349 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
350 struct iio_chan_spec const *chan,
351 long mask)
353 return IIO_VAL_INT_PLUS_NANO;
356 static irqreturn_t hx711_trigger(int irq, void *p)
358 struct iio_poll_func *pf = p;
359 struct iio_dev *indio_dev = pf->indio_dev;
360 struct hx711_data *hx711_data = iio_priv(indio_dev);
361 int i, j = 0;
363 mutex_lock(&hx711_data->lock);
365 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
367 for (i = 0; i < indio_dev->masklength; i++) {
368 if (!test_bit(i, indio_dev->active_scan_mask))
369 continue;
371 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
372 indio_dev->channels[i].channel);
373 j++;
376 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
377 pf->timestamp);
379 mutex_unlock(&hx711_data->lock);
381 iio_trigger_notify_done(indio_dev->trig);
383 return IRQ_HANDLED;
386 static ssize_t hx711_scale_available_show(struct device *dev,
387 struct device_attribute *attr,
388 char *buf)
390 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
391 int channel = iio_attr->address;
392 int i, len = 0;
394 for (i = 0; i < HX711_GAIN_MAX; i++)
395 if (hx711_gain_to_scale[i].channel == channel)
396 len += sprintf(buf + len, "0.%09d ",
397 hx711_gain_to_scale[i].scale);
399 len += sprintf(buf + len, "\n");
401 return len;
404 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
405 hx711_scale_available_show, NULL, 0);
407 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
408 hx711_scale_available_show, NULL, 1);
410 static struct attribute *hx711_attributes[] = {
411 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
412 &iio_dev_attr_in_voltage1_scale_available.dev_attr.attr,
413 NULL,
416 static const struct attribute_group hx711_attribute_group = {
417 .attrs = hx711_attributes,
420 static const struct iio_info hx711_iio_info = {
421 .read_raw = hx711_read_raw,
422 .write_raw = hx711_write_raw,
423 .write_raw_get_fmt = hx711_write_raw_get_fmt,
424 .attrs = &hx711_attribute_group,
427 static const struct iio_chan_spec hx711_chan_spec[] = {
429 .type = IIO_VOLTAGE,
430 .channel = 0,
431 .indexed = 1,
432 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
433 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
434 .scan_index = 0,
435 .scan_type = {
436 .sign = 'u',
437 .realbits = 24,
438 .storagebits = 32,
439 .endianness = IIO_CPU,
443 .type = IIO_VOLTAGE,
444 .channel = 1,
445 .indexed = 1,
446 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
447 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
448 .scan_index = 1,
449 .scan_type = {
450 .sign = 'u',
451 .realbits = 24,
452 .storagebits = 32,
453 .endianness = IIO_CPU,
456 IIO_CHAN_SOFT_TIMESTAMP(2),
459 static int hx711_probe(struct platform_device *pdev)
461 struct device *dev = &pdev->dev;
462 struct device_node *np = dev->of_node;
463 struct hx711_data *hx711_data;
464 struct iio_dev *indio_dev;
465 int ret;
466 int i;
468 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data));
469 if (!indio_dev) {
470 dev_err(dev, "failed to allocate IIO device\n");
471 return -ENOMEM;
474 hx711_data = iio_priv(indio_dev);
475 hx711_data->dev = dev;
477 mutex_init(&hx711_data->lock);
480 * PD_SCK stands for power down and serial clock input of HX711
481 * in the driver it is an output
483 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
484 if (IS_ERR(hx711_data->gpiod_pd_sck)) {
485 dev_err(dev, "failed to get sck-gpiod: err=%ld\n",
486 PTR_ERR(hx711_data->gpiod_pd_sck));
487 return PTR_ERR(hx711_data->gpiod_pd_sck);
491 * DOUT stands for serial data output of HX711
492 * for the driver it is an input
494 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN);
495 if (IS_ERR(hx711_data->gpiod_dout)) {
496 dev_err(dev, "failed to get dout-gpiod: err=%ld\n",
497 PTR_ERR(hx711_data->gpiod_dout));
498 return PTR_ERR(hx711_data->gpiod_dout);
501 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd");
502 if (IS_ERR(hx711_data->reg_avdd))
503 return PTR_ERR(hx711_data->reg_avdd);
505 ret = regulator_enable(hx711_data->reg_avdd);
506 if (ret < 0)
507 return ret;
510 * with
511 * full scale differential input range: AVDD / GAIN
512 * full scale output data: 2^24
513 * we can say:
514 * AVDD / GAIN = 2^24
515 * therefore:
516 * 1 LSB = AVDD / GAIN / 2^24
517 * AVDD is in uV, but we need 10^-9 mV
518 * approximately to fit into a 32 bit number:
519 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV]
521 ret = regulator_get_voltage(hx711_data->reg_avdd);
522 if (ret < 0)
523 goto error_regulator;
525 /* we need 10^-9 mV */
526 ret *= 100;
528 for (i = 0; i < HX711_GAIN_MAX; i++)
529 hx711_gain_to_scale[i].scale =
530 ret / hx711_gain_to_scale[i].gain / 1678;
532 hx711_data->gain_set = 128;
533 hx711_data->gain_chan_a = 128;
535 hx711_data->clock_frequency = 400000;
536 ret = of_property_read_u32(np, "clock-frequency",
537 &hx711_data->clock_frequency);
540 * datasheet says the high level of PD_SCK has a maximum duration
541 * of 50 microseconds
543 if (hx711_data->clock_frequency < 20000) {
544 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n");
545 hx711_data->clock_frequency = 400000;
548 hx711_data->data_ready_delay_ns =
549 1000000000 / hx711_data->clock_frequency;
551 platform_set_drvdata(pdev, indio_dev);
553 indio_dev->name = "hx711";
554 indio_dev->info = &hx711_iio_info;
555 indio_dev->modes = INDIO_DIRECT_MODE;
556 indio_dev->channels = hx711_chan_spec;
557 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
559 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
560 hx711_trigger, NULL);
561 if (ret < 0) {
562 dev_err(dev, "setup of iio triggered buffer failed\n");
563 goto error_regulator;
566 ret = iio_device_register(indio_dev);
567 if (ret < 0) {
568 dev_err(dev, "Couldn't register the device\n");
569 goto error_buffer;
572 return 0;
574 error_buffer:
575 iio_triggered_buffer_cleanup(indio_dev);
577 error_regulator:
578 regulator_disable(hx711_data->reg_avdd);
580 return ret;
583 static int hx711_remove(struct platform_device *pdev)
585 struct hx711_data *hx711_data;
586 struct iio_dev *indio_dev;
588 indio_dev = platform_get_drvdata(pdev);
589 hx711_data = iio_priv(indio_dev);
591 iio_device_unregister(indio_dev);
593 iio_triggered_buffer_cleanup(indio_dev);
595 regulator_disable(hx711_data->reg_avdd);
597 return 0;
600 static const struct of_device_id of_hx711_match[] = {
601 { .compatible = "avia,hx711", },
605 MODULE_DEVICE_TABLE(of, of_hx711_match);
607 static struct platform_driver hx711_driver = {
608 .probe = hx711_probe,
609 .remove = hx711_remove,
610 .driver = {
611 .name = "hx711-gpio",
612 .of_match_table = of_hx711_match,
616 module_platform_driver(hx711_driver);
618 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
619 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
620 MODULE_LICENSE("GPL");
621 MODULE_ALIAS("platform:hx711-gpio");