Lines Matching +full:capacitance +full:- +full:to +full:- +full:digital
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HX711: analog to digital converter for weight sensor module
5 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
24 /* gain to pulse and scale conversion */
76 return -EINVAL; in hx711_get_scale_to_gain()
88 * 2x32-bit channel + 64-bit naturally aligned timestamp
111 gpiod_set_value(hx711_data->gpiod_pd_sck, 1); in hx711_cycle()
118 ndelay(hx711_data->data_ready_delay_ns); in hx711_cycle()
126 gpiod_set_value(hx711_data->gpiod_pd_sck, 0); in hx711_cycle()
130 * make it a square wave for addressing cases with capacitance on in hx711_cycle()
133 ndelay(hx711_data->data_ready_delay_ns); in hx711_cycle()
136 return gpiod_get_value(hx711_data->gpiod_dout); in hx711_cycle()
143 int val = gpiod_get_value(hx711_data->gpiod_dout); in hx711_read()
147 return -EIO; in hx711_read()
158 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++) in hx711_read()
171 * Allow up to one second for it in hx711_wait_for_ready()
174 val = gpiod_get_value(hx711_data->gpiod_dout); in hx711_wait_for_ready()
181 return -EIO; in hx711_wait_for_ready()
200 gpiod_set_value(hx711_data->gpiod_pd_sck, 1); in hx711_reset()
202 gpiod_set_value(hx711_data->gpiod_pd_sck, 0); in hx711_reset()
207 hx711_data->gain_set = HX711_RESET_GAIN; in hx711_reset()
218 if (hx711_data->gain_set == 32) { in hx711_set_gain_for_channel()
219 hx711_data->gain_set = hx711_data->gain_chan_a; in hx711_set_gain_for_channel()
230 if (hx711_data->gain_set != 32) { in hx711_set_gain_for_channel()
231 hx711_data->gain_set = 32; in hx711_set_gain_for_channel()
256 dev_err(hx711_data->dev, "reset failed!"); in hx711_reset_read()
257 return -EIO; in hx711_reset_read()
277 mutex_lock(&hx711_data->lock); in hx711_read_raw()
279 *val = hx711_reset_read(hx711_data, chan->channel); in hx711_read_raw()
281 mutex_unlock(&hx711_data->lock); in hx711_read_raw()
288 mutex_lock(&hx711_data->lock); in hx711_read_raw()
290 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set); in hx711_read_raw()
292 mutex_unlock(&hx711_data->lock); in hx711_read_raw()
296 return -EINVAL; in hx711_read_raw()
317 return -EINVAL; in hx711_write_raw()
319 mutex_lock(&hx711_data->lock); in hx711_write_raw()
323 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
327 if (gain != hx711_data->gain_set) { in hx711_write_raw()
328 hx711_data->gain_set = gain; in hx711_write_raw()
330 hx711_data->gain_chan_a = gain; in hx711_write_raw()
334 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
339 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
342 return -EINVAL; in hx711_write_raw()
358 struct iio_dev *indio_dev = pf->indio_dev; in hx711_trigger()
362 mutex_lock(&hx711_data->lock); in hx711_trigger()
364 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer)); in hx711_trigger()
367 hx711_data->buffer[j] = hx711_reset_read(hx711_data, in hx711_trigger()
368 indio_dev->channels[i].channel); in hx711_trigger()
372 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer, in hx711_trigger()
373 pf->timestamp); in hx711_trigger()
375 mutex_unlock(&hx711_data->lock); in hx711_trigger()
377 iio_trigger_notify_done(indio_dev->trig); in hx711_trigger()
387 int channel = iio_attr->address; in hx711_scale_available_show()
457 struct device *dev = &pdev->dev; in hx711_probe()
465 return dev_err_probe(dev, -ENOMEM, "failed to allocate IIO device\n"); in hx711_probe()
468 hx711_data->dev = dev; in hx711_probe()
470 mutex_init(&hx711_data->lock); in hx711_probe()
476 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); in hx711_probe()
477 if (IS_ERR(hx711_data->gpiod_pd_sck)) in hx711_probe()
478 return dev_err_probe(dev, PTR_ERR(hx711_data->gpiod_pd_sck), in hx711_probe()
479 "failed to get sck-gpiod\n"); in hx711_probe()
485 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN); in hx711_probe()
486 if (IS_ERR(hx711_data->gpiod_dout)) in hx711_probe()
487 return dev_err_probe(dev, PTR_ERR(hx711_data->gpiod_dout), in hx711_probe()
488 "failed to get dout-gpiod\n"); in hx711_probe()
502 * AVDD is in uV, but we need 10^-9 mV in hx711_probe()
503 * approximately to fit into a 32 bit number: in hx711_probe()
504 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV] in hx711_probe()
507 /* we need 10^-9 mV */ in hx711_probe()
514 hx711_data->gain_set = 128; in hx711_probe()
515 hx711_data->gain_chan_a = 128; in hx711_probe()
517 hx711_data->clock_frequency = 400000; in hx711_probe()
518 ret = device_property_read_u32(&pdev->dev, "clock-frequency", in hx711_probe()
519 &hx711_data->clock_frequency); in hx711_probe()
525 if (hx711_data->clock_frequency < 20000) { in hx711_probe()
526 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n"); in hx711_probe()
527 hx711_data->clock_frequency = 400000; in hx711_probe()
530 hx711_data->data_ready_delay_ns = in hx711_probe()
531 1000000000 / hx711_data->clock_frequency; in hx711_probe()
533 indio_dev->name = "hx711"; in hx711_probe()
534 indio_dev->info = &hx711_iio_info; in hx711_probe()
535 indio_dev->modes = INDIO_DIRECT_MODE; in hx711_probe()
536 indio_dev->channels = hx711_chan_spec; in hx711_probe()
537 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec); in hx711_probe()
563 .name = "hx711-gpio",
570 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
571 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
573 MODULE_ALIAS("platform:hx711-gpio");