xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / iio / adc / hx711.c
blob9430b54121e002a9985879bf489f48fbd3c3e798
1 /*
2 * HX711: analog to digital converter for weight sensor module
4 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
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.
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.
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>
33 /* gain to pulse and scale conversion */
34 #define HX711_GAIN_MAX 3
36 struct hx711_gain_to_scale {
37 int gain;
38 int gain_pulse;
39 int scale;
40 int channel;
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()
48 * channel A in documentation is channel 0 in source code
49 * channel B in documentation is channel 1 in source code
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 }
57 static int hx711_get_gain_to_pulse(int gain)
59 int i;
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;
67 static int hx711_get_gain_to_scale(int gain)
69 int i;
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;
77 static int hx711_get_scale_to_gain(int scale)
79 int i;
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;
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;
96 * triggered buffer
97 * 2x32-bit channel + 64-bit timestamp
99 u32 buffer[4];
102 static int hx711_cycle(struct hx711_data *hx711_data)
104 int val;
107 * if preempted for more then 60us while PD_SCK is high:
108 * hx711 is going in reset
109 * ==> measuring is false
111 preempt_disable();
112 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
113 val = gpiod_get_value(hx711_data->gpiod_dout);
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
120 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
121 preempt_enable();
123 return val;
126 static int hx711_read(struct hx711_data *hx711_data)
128 int i, ret;
129 int value = 0;
130 int val = gpiod_get_value(hx711_data->gpiod_dout);
132 /* we double check if it's really down */
133 if (val)
134 return -EIO;
136 for (i = 0; i < 24; i++) {
137 value <<= 1;
138 ret = hx711_cycle(hx711_data);
139 if (ret)
140 value++;
143 value ^= 0x800000;
145 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++)
146 hx711_cycle(hx711_data);
148 return value;
151 static int hx711_wait_for_ready(struct hx711_data *hx711_data)
153 int i, val;
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
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);
167 if (val)
168 return -EIO;
170 return 0;
173 static int hx711_reset(struct hx711_data *hx711_data)
175 int ret;
176 int val = gpiod_get_value(hx711_data->gpiod_dout);
178 if (val) {
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
188 gpiod_set_value(hx711_data->gpiod_pd_sck, 1);
189 msleep(10);
190 gpiod_set_value(hx711_data->gpiod_pd_sck, 0);
192 ret = hx711_wait_for_ready(hx711_data);
193 if (ret)
194 return ret;
196 * after a reset the gain is 128 so we do a dummy read
197 * to set the gain for the next read
199 ret = hx711_read(hx711_data);
200 if (ret < 0)
201 return ret;
204 * after a dummy read we need to wait vor readiness
205 * for not mixing gain pulses with the clock
207 val = hx711_wait_for_ready(hx711_data);
210 return val;
213 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan)
215 int ret;
217 if (chan == 0) {
218 if (hx711_data->gain_set == 32) {
219 hx711_data->gain_set = hx711_data->gain_chan_a;
221 ret = hx711_read(hx711_data);
222 if (ret < 0)
223 return ret;
225 ret = hx711_wait_for_ready(hx711_data);
226 if (ret)
227 return ret;
229 } else {
230 if (hx711_data->gain_set != 32) {
231 hx711_data->gain_set = 32;
233 ret = hx711_read(hx711_data);
234 if (ret < 0)
235 return ret;
237 ret = hx711_wait_for_ready(hx711_data);
238 if (ret)
239 return ret;
243 return 0;
246 static int hx711_reset_read(struct hx711_data *hx711_data, int chan)
248 int ret;
249 int val;
252 * hx711_reset() must be called from here
253 * because it could be calling hx711_read() by itself
255 if (hx711_reset(hx711_data)) {
256 dev_err(hx711_data->dev, "reset failed!");
257 return -EIO;
260 ret = hx711_set_gain_for_channel(hx711_data, chan);
261 if (ret < 0)
262 return ret;
264 val = hx711_read(hx711_data);
266 return val;
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)
273 struct hx711_data *hx711_data = iio_priv(indio_dev);
275 switch (mask) {
276 case IIO_CHAN_INFO_RAW:
277 mutex_lock(&hx711_data->lock);
279 *val = hx711_reset_read(hx711_data, chan->channel);
281 mutex_unlock(&hx711_data->lock);
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);
290 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set);
292 mutex_unlock(&hx711_data->lock);
294 return IIO_VAL_INT_PLUS_NANO;
295 default:
296 return -EINVAL;
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)
306 struct hx711_data *hx711_data = iio_priv(indio_dev);
307 int ret;
308 int gain;
310 switch (mask) {
311 case IIO_CHAN_INFO_SCALE:
313 * a scale greater than 1 mV per LSB is not possible
314 * with the HX711, therefore val must be 0
316 if (val != 0)
317 return -EINVAL;
319 mutex_lock(&hx711_data->lock);
321 gain = hx711_get_scale_to_gain(val2);
322 if (gain < 0) {
323 mutex_unlock(&hx711_data->lock);
324 return gain;
327 if (gain != hx711_data->gain_set) {
328 hx711_data->gain_set = gain;
329 if (gain != 32)
330 hx711_data->gain_chan_a = gain;
332 ret = hx711_read(hx711_data);
333 if (ret < 0) {
334 mutex_unlock(&hx711_data->lock);
335 return ret;
339 mutex_unlock(&hx711_data->lock);
340 return 0;
341 default:
342 return -EINVAL;
345 return 0;
348 static int hx711_write_raw_get_fmt(struct iio_dev *indio_dev,
349 struct iio_chan_spec const *chan,
350 long mask)
352 return IIO_VAL_INT_PLUS_NANO;
355 static irqreturn_t hx711_trigger(int irq, void *p)
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;
362 mutex_lock(&hx711_data->lock);
364 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer));
366 for (i = 0; i < indio_dev->masklength; i++) {
367 if (!test_bit(i, indio_dev->active_scan_mask))
368 continue;
370 hx711_data->buffer[j] = hx711_reset_read(hx711_data,
371 indio_dev->channels[i].channel);
372 j++;
375 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer,
376 pf->timestamp);
378 mutex_unlock(&hx711_data->lock);
380 iio_trigger_notify_done(indio_dev->trig);
382 return IRQ_HANDLED;
385 static ssize_t hx711_scale_available_show(struct device *dev,
386 struct device_attribute *attr,
387 char *buf)
389 struct iio_dev_attr *iio_attr = to_iio_dev_attr(attr);
390 int channel = iio_attr->address;
391 int i, len = 0;
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);
398 len += sprintf(buf + len, "\n");
400 return len;
403 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
404 hx711_scale_available_show, NULL, 0);
406 static IIO_DEVICE_ATTR(in_voltage1_scale_available, S_IRUGO,
407 hx711_scale_available_show, NULL, 1);
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,
415 static const struct attribute_group hx711_attribute_group = {
416 .attrs = hx711_attributes,
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,
426 static const struct iio_chan_spec hx711_chan_spec[] = {
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,
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,
455 IIO_CHAN_SOFT_TIMESTAMP(2),
458 static int hx711_probe(struct platform_device *pdev)
460 struct device *dev = &pdev->dev;
461 struct hx711_data *hx711_data;
462 struct iio_dev *indio_dev;
463 int ret;
464 int i;
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;
472 hx711_data = iio_priv(indio_dev);
473 hx711_data->dev = dev;
475 mutex_init(&hx711_data->lock);
478 * PD_SCK stands for power down and serial clock input of HX711
479 * in the driver it is an output
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);
489 * DOUT stands for serial data output of HX711
490 * for the driver it is an input
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);
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);
503 ret = regulator_enable(hx711_data->reg_avdd);
504 if (ret < 0)
505 return ret;
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]
519 ret = regulator_get_voltage(hx711_data->reg_avdd);
520 if (ret < 0)
521 goto error_regulator;
523 /* we need 10^-9 mV */
524 ret *= 100;
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;
530 hx711_data->gain_set = 128;
531 hx711_data->gain_chan_a = 128;
533 platform_set_drvdata(pdev, indio_dev);
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);
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;
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;
555 return 0;
557 error_buffer:
558 iio_triggered_buffer_cleanup(indio_dev);
560 error_regulator:
561 regulator_disable(hx711_data->reg_avdd);
563 return ret;
566 static int hx711_remove(struct platform_device *pdev)
568 struct hx711_data *hx711_data;
569 struct iio_dev *indio_dev;
571 indio_dev = platform_get_drvdata(pdev);
572 hx711_data = iio_priv(indio_dev);
574 iio_device_unregister(indio_dev);
576 iio_triggered_buffer_cleanup(indio_dev);
578 regulator_disable(hx711_data->reg_avdd);
580 return 0;
583 static const struct of_device_id of_hx711_match[] = {
584 { .compatible = "avia,hx711", },
588 MODULE_DEVICE_TABLE(of, of_hx711_match);
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,
599 module_platform_driver(hx711_driver);
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");