Lines Matching +full:lock +full:- +full:state
1 // SPDX-License-Identifier: GPL-2.0
67 struct mutex lock; member
73 static int scd4x_i2c_xfer(struct scd4x_state *state, char *txbuf, int txsize, in scd4x_i2c_xfer() argument
76 struct i2c_client *client = state->client; in scd4x_i2c_xfer()
84 return -EIO; in scd4x_i2c_xfer()
93 return -EIO; in scd4x_i2c_xfer()
98 static int scd4x_send_command(struct scd4x_state *state, enum scd4x_cmd cmd) in scd4x_send_command() argument
109 ret = scd4x_send_command(state, CMD_STOP_MEAS); in scd4x_send_command()
118 ret = scd4x_i2c_xfer(state, buf, 2, buf, 0); in scd4x_send_command()
123 ret = scd4x_send_command(state, CMD_START_MEAS); in scd4x_send_command()
131 static int scd4x_read(struct scd4x_state *state, enum scd4x_cmd cmd, in scd4x_read() argument
134 struct i2c_client *client = state->client; in scd4x_read()
146 ret = scd4x_send_command(state, CMD_STOP_MEAS); in scd4x_read()
158 ret = scd4x_i2c_xfer(state, buf, 2, buf, response_sz); in scd4x_read()
165 dev_err(&client->dev, "CRC error\n"); in scd4x_read()
166 return -EIO; in scd4x_read()
176 ret = scd4x_send_command(state, CMD_START_MEAS); in scd4x_read()
184 static int scd4x_write(struct scd4x_state *state, enum scd4x_cmd cmd, uint16_t arg) in scd4x_write() argument
198 ret = scd4x_send_command(state, CMD_STOP_MEAS); in scd4x_write()
206 ret = scd4x_i2c_xfer(state, buf, SCD4X_WRITE_BUF_SIZE, buf, 0); in scd4x_write()
212 ret = scd4x_send_command(state, CMD_START_MEAS); in scd4x_write()
220 static int scd4x_write_and_fetch(struct scd4x_state *state, enum scd4x_cmd cmd, in scd4x_write_and_fetch() argument
223 struct i2c_client *client = state->client; in scd4x_write_and_fetch()
229 ret = scd4x_write(state, CMD_FRC, arg); in scd4x_write_and_fetch()
243 ret = -EIO; in scd4x_write_and_fetch()
250 dev_err(&client->dev, "CRC error\n"); in scd4x_write_and_fetch()
251 ret = -EIO; in scd4x_write_and_fetch()
259 return scd4x_send_command(state, CMD_START_MEAS); in scd4x_write_and_fetch()
266 scd4x_send_command(state, CMD_START_MEAS); in scd4x_write_and_fetch()
271 static int scd4x_read_meas(struct scd4x_state *state, uint16_t *meas) in scd4x_read_meas() argument
276 ret = scd4x_read(state, CMD_READ_MEAS, buf, sizeof(buf)); in scd4x_read_meas()
286 static int scd4x_wait_meas_poll(struct scd4x_state *state) in scd4x_wait_meas_poll() argument
288 struct i2c_client *client = state->client; in scd4x_wait_meas_poll()
296 ret = scd4x_read(state, CMD_GET_DATA_READY, &bval, sizeof(bval)); in scd4x_wait_meas_poll()
298 return -EIO; in scd4x_wait_meas_poll()
306 } while (--tries); in scd4x_wait_meas_poll()
309 ret = scd4x_send_command(state, CMD_START_MEAS); in scd4x_wait_meas_poll()
311 dev_err(&client->dev, "failed to start measurement: %d\n", ret); in scd4x_wait_meas_poll()
313 return -ETIMEDOUT; in scd4x_wait_meas_poll()
316 static int scd4x_read_poll(struct scd4x_state *state, uint16_t *buf) in scd4x_read_poll() argument
320 ret = scd4x_wait_meas_poll(state); in scd4x_read_poll()
324 return scd4x_read_meas(state, buf); in scd4x_read_poll()
327 static int scd4x_read_channel(struct scd4x_state *state, int chan) in scd4x_read_channel() argument
332 ret = scd4x_read_poll(state, buf); in scd4x_read_channel()
343 struct scd4x_state *state = iio_priv(indio_dev); in scd4x_read_raw() local
349 if (chan->output) { in scd4x_read_raw()
350 mutex_lock(&state->lock); in scd4x_read_raw()
351 ret = scd4x_read(state, CMD_GET_AMB_PRESSURE, &tmp, sizeof(tmp)); in scd4x_read_raw()
352 mutex_unlock(&state->lock); in scd4x_read_raw()
365 mutex_lock(&state->lock); in scd4x_read_raw()
366 ret = scd4x_read_channel(state, chan->address); in scd4x_read_raw()
367 mutex_unlock(&state->lock); in scd4x_read_raw()
376 if (chan->type == IIO_CONCENTRATION) { in scd4x_read_raw()
380 } else if (chan->type == IIO_TEMP) { in scd4x_read_raw()
384 } else if (chan->type == IIO_HUMIDITYRELATIVE) { in scd4x_read_raw()
389 return -EINVAL; in scd4x_read_raw()
391 *val = -16852; in scd4x_read_raw()
395 mutex_lock(&state->lock); in scd4x_read_raw()
396 ret = scd4x_read(state, CMD_GET_TEMP_OFFSET, &tmp, sizeof(tmp)); in scd4x_read_raw()
397 mutex_unlock(&state->lock); in scd4x_read_raw()
405 return -EINVAL; in scd4x_read_raw()
424 return -EINVAL; in scd4x_read_avail()
431 struct scd4x_state *state = iio_priv(indio_dev); in scd4x_write_raw() local
436 mutex_lock(&state->lock); in scd4x_write_raw()
437 ret = scd4x_write(state, CMD_SET_TEMP_OFFSET, val); in scd4x_write_raw()
438 mutex_unlock(&state->lock); in scd4x_write_raw()
442 switch (chan->type) { in scd4x_write_raw()
446 return -EINVAL; in scd4x_write_raw()
448 mutex_lock(&state->lock); in scd4x_write_raw()
449 ret = scd4x_write(state, CMD_SET_AMB_PRESSURE, val); in scd4x_write_raw()
450 mutex_unlock(&state->lock); in scd4x_write_raw()
454 return -EINVAL; in scd4x_write_raw()
457 return -EINVAL; in scd4x_write_raw()
465 struct scd4x_state *state = iio_priv(indio_dev); in calibration_auto_enable_show() local
470 mutex_lock(&state->lock); in calibration_auto_enable_show()
471 ret = scd4x_read(state, CMD_GET_ASC, &bval, sizeof(bval)); in calibration_auto_enable_show()
472 mutex_unlock(&state->lock); in calibration_auto_enable_show()
488 struct scd4x_state *state = iio_priv(indio_dev); in calibration_auto_enable_store() local
499 mutex_lock(&state->lock); in calibration_auto_enable_store()
500 ret = scd4x_write(state, CMD_SET_ASC, value); in calibration_auto_enable_store()
501 mutex_unlock(&state->lock); in calibration_auto_enable_store()
513 struct scd4x_state *state = iio_priv(indio_dev); in calibration_forced_value_store() local
522 return -EINVAL; in calibration_forced_value_store()
524 mutex_lock(&state->lock); in calibration_forced_value_store()
525 ret = scd4x_write_and_fetch(state, CMD_FRC, arg, &val, sizeof(val)); in calibration_forced_value_store()
526 mutex_unlock(&state->lock); in calibration_forced_value_store()
533 return -EINVAL; in calibration_forced_value_store()
574 .scan_index = -1,
624 struct scd4x_state *state = iio_priv(indio_dev); in scd4x_suspend() local
627 ret = scd4x_send_command(state, CMD_STOP_MEAS); in scd4x_suspend()
631 return regulator_disable(state->vdd); in scd4x_suspend()
637 struct scd4x_state *state = iio_priv(indio_dev); in scd4x_resume() local
640 ret = regulator_enable(state->vdd); in scd4x_resume()
644 return scd4x_send_command(state, CMD_START_MEAS); in scd4x_resume()
649 static void scd4x_stop_meas(void *state) in scd4x_stop_meas() argument
651 scd4x_send_command(state, CMD_STOP_MEAS); in scd4x_stop_meas()
656 struct scd4x_state *state = data; in scd4x_disable_regulator() local
658 regulator_disable(state->vdd); in scd4x_disable_regulator()
664 struct iio_dev *indio_dev = pf->indio_dev; in scd4x_trigger_handler()
665 struct scd4x_state *state = iio_priv(indio_dev); in scd4x_trigger_handler() local
673 mutex_lock(&state->lock); in scd4x_trigger_handler()
674 ret = scd4x_read_poll(state, scan.data); in scd4x_trigger_handler()
675 mutex_unlock(&state->lock); in scd4x_trigger_handler()
681 iio_trigger_notify_done(indio_dev->trig); in scd4x_trigger_handler()
688 struct device *dev = &client->dev; in scd4x_probe()
690 struct scd4x_state *state; in scd4x_probe() local
693 indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); in scd4x_probe()
695 return -ENOMEM; in scd4x_probe()
697 state = iio_priv(indio_dev); in scd4x_probe()
698 mutex_init(&state->lock); in scd4x_probe()
699 state->client = client; in scd4x_probe()
702 indio_dev->info = &scd4x_info; in scd4x_probe()
703 indio_dev->name = client->name; in scd4x_probe()
704 indio_dev->channels = scd4x_channels; in scd4x_probe()
705 indio_dev->num_channels = ARRAY_SIZE(scd4x_channels); in scd4x_probe()
706 indio_dev->modes = INDIO_DIRECT_MODE; in scd4x_probe()
707 indio_dev->available_scan_masks = scd4x_scan_masks; in scd4x_probe()
709 state->vdd = devm_regulator_get(dev, "vdd"); in scd4x_probe()
710 if (IS_ERR(state->vdd)) in scd4x_probe()
711 return dev_err_probe(dev, PTR_ERR(state->vdd), "failed to get regulator\n"); in scd4x_probe()
713 ret = regulator_enable(state->vdd); in scd4x_probe()
717 ret = devm_add_action_or_reset(dev, scd4x_disable_regulator, state); in scd4x_probe()
721 ret = scd4x_send_command(state, CMD_STOP_MEAS); in scd4x_probe()
734 ret = scd4x_send_command(state, CMD_START_MEAS); in scd4x_probe()
740 ret = devm_add_action_or_reset(dev, scd4x_stop_meas, state); in scd4x_probe()