Lines Matching refs:hx711_data

79 struct hx711_data {  struct
102 static int hx711_cycle(struct hx711_data *hx711_data) in hx711_cycle() argument
112 gpiod_set_value(hx711_data->gpiod_pd_sck, 1); in hx711_cycle()
119 ndelay(hx711_data->data_ready_delay_ns); 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()
140 static int hx711_read(struct hx711_data *hx711_data) in hx711_read() argument
144 int val = gpiod_get_value(hx711_data->gpiod_dout); in hx711_read()
152 ret = hx711_cycle(hx711_data); in hx711_read()
159 for (i = 0; i < hx711_get_gain_to_pulse(hx711_data->gain_set); i++) in hx711_read()
160 hx711_cycle(hx711_data); in hx711_read()
165 static int hx711_wait_for_ready(struct hx711_data *hx711_data) in hx711_wait_for_ready() argument
175 val = gpiod_get_value(hx711_data->gpiod_dout); in hx711_wait_for_ready()
187 static int hx711_reset(struct hx711_data *hx711_data) in hx711_reset() argument
189 int val = hx711_wait_for_ready(hx711_data); in hx711_reset()
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()
205 val = hx711_wait_for_ready(hx711_data); in hx711_reset()
208 hx711_data->gain_set = HX711_RESET_GAIN; in hx711_reset()
214 static int hx711_set_gain_for_channel(struct hx711_data *hx711_data, int chan) in hx711_set_gain_for_channel() argument
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()
222 ret = hx711_read(hx711_data); in hx711_set_gain_for_channel()
226 ret = hx711_wait_for_ready(hx711_data); 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()
234 ret = hx711_read(hx711_data); in hx711_set_gain_for_channel()
238 ret = hx711_wait_for_ready(hx711_data); in hx711_set_gain_for_channel()
247 static int hx711_reset_read(struct hx711_data *hx711_data, int chan) in hx711_reset_read() argument
256 if (hx711_reset(hx711_data)) { in hx711_reset_read()
257 dev_err(hx711_data->dev, "reset failed!"); in hx711_reset_read()
261 ret = hx711_set_gain_for_channel(hx711_data, chan); in hx711_reset_read()
265 val = hx711_read(hx711_data); in hx711_reset_read()
274 struct hx711_data *hx711_data = iio_priv(indio_dev); in hx711_read_raw() local
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()
307 struct hx711_data *hx711_data = iio_priv(indio_dev); in hx711_write_raw() local
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()
333 ret = hx711_read(hx711_data); in hx711_write_raw()
335 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
340 mutex_unlock(&hx711_data->lock); in hx711_write_raw()
360 struct hx711_data *hx711_data = iio_priv(indio_dev); in hx711_trigger() local
363 mutex_lock(&hx711_data->lock); in hx711_trigger()
365 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer)); in hx711_trigger()
371 hx711_data->buffer[j] = hx711_reset_read(hx711_data, in hx711_trigger()
376 iio_push_to_buffers_with_timestamp(indio_dev, hx711_data->buffer, in hx711_trigger()
379 mutex_unlock(&hx711_data->lock); in hx711_trigger()
463 struct hx711_data *hx711_data; in hx711_probe() local
468 indio_dev = devm_iio_device_alloc(dev, sizeof(struct hx711_data)); in hx711_probe()
474 hx711_data = iio_priv(indio_dev); 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()
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()
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()
521 ret = regulator_get_voltage(hx711_data->reg_avdd); 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()
537 &hx711_data->clock_frequency); in hx711_probe()
543 if (hx711_data->clock_frequency < 20000) { 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()
578 regulator_disable(hx711_data->reg_avdd); in hx711_probe()
585 struct hx711_data *hx711_data; in hx711_remove() local
589 hx711_data = iio_priv(indio_dev); in hx711_remove()
595 regulator_disable(hx711_data->reg_avdd); in hx711_remove()