Lines Matching +full:ld +full:- +full:pulse +full:- +full:delay +full:- +full:us

1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (c) 2016 Andreas Klinger <ak@it-klinger.de>
15 #include <linux/delay.h>
24 /* gain to pulse and scale conversion */
76 return -EINVAL; in hx711_get_scale_to_gain()
89 * 2x32-bit channel + 64-bit timestamp
93 * delay after a rising edge on SCK until the data is ready DOUT
107 * if preempted for more then 60us while PD_SCK is high: in hx711_cycle()
112 gpiod_set_value(hx711_data->gpiod_pd_sck, 1); in hx711_cycle()
119 ndelay(hx711_data->data_ready_delay_ns); in hx711_cycle()
122 * here we are not waiting for 0.2 us as suggested by the datasheet, in hx711_cycle()
124 * at least 1.15 us for PD_SCK high (T3 in datasheet) in hx711_cycle()
125 * and 0.56 us for PD_SCK low on TI Sitara with 800 MHz in hx711_cycle()
127 gpiod_set_value(hx711_data->gpiod_pd_sck, 0); in hx711_cycle()
134 ndelay(hx711_data->data_ready_delay_ns); in hx711_cycle()
137 return gpiod_get_value(hx711_data->gpiod_dout); in hx711_cycle()
144 int val = gpiod_get_value(hx711_data->gpiod_dout); in hx711_read()
148 return -EIO; in hx711_read()
159 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++) in hx711_read()
175 val = gpiod_get_value(hx711_data->gpiod_dout); in hx711_wait_for_ready()
182 return -EIO; in hx711_wait_for_ready()
201 gpiod_set_value(hx711_data->gpiod_pd_sck, 1); in hx711_reset()
203 gpiod_set_value(hx711_data->gpiod_pd_sck, 0); in hx711_reset()
208 hx711_data->gain_set = HX711_RESET_GAIN; in hx711_reset()
219 if (hx711_data->gain_set == 32) { in hx711_set_gain_for_channel()
220 hx711_data->gain_set = hx711_data->gain_chan_a; in hx711_set_gain_for_channel()
231 if (hx711_data->gain_set != 32) { in hx711_set_gain_for_channel()
232 hx711_data->gain_set = 32; in hx711_set_gain_for_channel()
257 dev_err(hx711_data->dev, "reset failed!"); in hx711_reset_read()
258 return -EIO; in hx711_reset_read()
278 mutex_lock(&hx711_data->lock); in hx711_read_raw()
280 *val = hx711_reset_read(hx711_data, chan->channel); in hx711_read_raw()
282 mutex_unlock(&hx711_data->lock); in hx711_read_raw()
289 mutex_lock(&hx711_data->lock); in hx711_read_raw()
291 *val2 = hx711_get_gain_to_scale(hx711_data->gain_set); in hx711_read_raw()
293 mutex_unlock(&hx711_data->lock); in hx711_read_raw()
297 return -EINVAL; in hx711_read_raw()
318 return -EINVAL; in hx711_write_raw()
320 mutex_lock(&hx711_data->lock); in hx711_write_raw()
324 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
328 if (gain != hx711_data->gain_set) { in hx711_write_raw()
329 hx711_data->gain_set = gain; in hx711_write_raw()
331 hx711_data->gain_chan_a = gain; in hx711_write_raw()
335 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
340 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
343 return -EINVAL; in hx711_write_raw()
359 struct iio_dev *indio_dev = pf->indio_dev; in hx711_trigger()
363 mutex_lock(&hx711_data->lock); in hx711_trigger()
365 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer)); in hx711_trigger()
367 for (i = 0; i < indio_dev->masklength; i++) { in hx711_trigger()
368 if (!test_bit(i, indio_dev->active_scan_mask)) in hx711_trigger()
371 hx711_data->buffer[j] = hx711_reset_read(hx711_data, in hx711_trigger()
372 indio_dev->channels[i].channel); in hx711_trigger()
376 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer, in hx711_trigger()
377 pf->timestamp); in hx711_trigger()
379 mutex_unlock(&hx711_data->lock); in hx711_trigger()
381 iio_trigger_notify_done(indio_dev->trig); in hx711_trigger()
391 int channel = iio_attr->address; in hx711_scale_available_show()
461 struct device *dev = &pdev->dev; in hx711_probe()
462 struct device_node *np = dev->of_node; in hx711_probe()
471 return -ENOMEM; in hx711_probe()
475 hx711_data->dev = dev; in hx711_probe()
477 mutex_init(&hx711_data->lock); in hx711_probe()
483 hx711_data->gpiod_pd_sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW); in hx711_probe()
484 if (IS_ERR(hx711_data->gpiod_pd_sck)) { in hx711_probe()
485 dev_err(dev, "failed to get sck-gpiod: err=%ld\n", in hx711_probe()
486 PTR_ERR(hx711_data->gpiod_pd_sck)); in hx711_probe()
487 return PTR_ERR(hx711_data->gpiod_pd_sck); in hx711_probe()
494 hx711_data->gpiod_dout = devm_gpiod_get(dev, "dout", GPIOD_IN); in hx711_probe()
495 if (IS_ERR(hx711_data->gpiod_dout)) { in hx711_probe()
496 dev_err(dev, "failed to get dout-gpiod: err=%ld\n", in hx711_probe()
497 PTR_ERR(hx711_data->gpiod_dout)); in hx711_probe()
498 return PTR_ERR(hx711_data->gpiod_dout); in hx711_probe()
501 hx711_data->reg_avdd = devm_regulator_get(dev, "avdd"); in hx711_probe()
502 if (IS_ERR(hx711_data->reg_avdd)) in hx711_probe()
503 return PTR_ERR(hx711_data->reg_avdd); in hx711_probe()
505 ret = regulator_enable(hx711_data->reg_avdd); in hx711_probe()
517 * AVDD is in uV, but we need 10^-9 mV in hx711_probe()
519 * 1 LSB = (AVDD * 100) / GAIN / 1678 [10^-9 mV] in hx711_probe()
521 ret = regulator_get_voltage(hx711_data->reg_avdd); in hx711_probe()
525 /* we need 10^-9 mV */ in hx711_probe()
532 hx711_data->gain_set = 128; in hx711_probe()
533 hx711_data->gain_chan_a = 128; in hx711_probe()
535 hx711_data->clock_frequency = 400000; in hx711_probe()
536 ret = of_property_read_u32(np, "clock-frequency", in hx711_probe()
537 &hx711_data->clock_frequency); in hx711_probe()
543 if (hx711_data->clock_frequency < 20000) { in hx711_probe()
544 dev_warn(dev, "clock-frequency too low - assuming 400 kHz\n"); in hx711_probe()
545 hx711_data->clock_frequency = 400000; in hx711_probe()
548 hx711_data->data_ready_delay_ns = in hx711_probe()
549 1000000000 / hx711_data->clock_frequency; in hx711_probe()
553 indio_dev->name = "hx711"; in hx711_probe()
554 indio_dev->info = &hx711_iio_info; in hx711_probe()
555 indio_dev->modes = INDIO_DIRECT_MODE; in hx711_probe()
556 indio_dev->channels = hx711_chan_spec; in hx711_probe()
557 indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec); in hx711_probe()
578 regulator_disable(hx711_data->reg_avdd); in hx711_probe()
595 regulator_disable(hx711_data->reg_avdd); in hx711_remove()
611 .name = "hx711-gpio",
618 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
619 MODULE_DESCRIPTION("HX711 bitbanging driver - ADC for weight cells");
621 MODULE_ALIAS("platform:hx711-gpio");