Lines Matching +full:adc +full:- +full:chan

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Nuvoton NAU7802 ADC
63 #define NAU7802_CHANNEL(chan) { \ argument
66 .channel = (chan), \
67 .scan_index = (chan), \
87 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) in nau7802_show_scales()
88 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%09d ", in nau7802_show_scales()
89 st->scale_avail[i]); in nau7802_show_scales()
91 buf[len-1] = '\n'; in nau7802_show_scales()
115 mutex_lock(&st->lock); in nau7802_set_gain()
116 st->conversion_count = 0; in nau7802_set_gain()
118 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1); in nau7802_set_gain()
121 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL1, in nau7802_set_gain()
126 mutex_unlock(&st->lock); in nau7802_set_gain()
135 mutex_lock(&st->data_lock); in nau7802_read_conversion()
136 data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B2); in nau7802_read_conversion()
139 st->last_value = data << 16; in nau7802_read_conversion()
141 data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B1); in nau7802_read_conversion()
144 st->last_value |= data << 8; in nau7802_read_conversion()
146 data = i2c_smbus_read_byte_data(st->client, NAU7802_REG_ADC_B0); in nau7802_read_conversion()
149 st->last_value |= data; in nau7802_read_conversion()
151 st->last_value = sign_extend32(st->last_value, 23); in nau7802_read_conversion()
154 mutex_unlock(&st->data_lock); in nau7802_read_conversion()
166 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); in nau7802_sync()
169 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, in nau7802_sync()
181 status = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); in nau7802_eoc_trigger()
192 * Because there is actually only one ADC for both channels, we have to in nau7802_eoc_trigger()
196 if (st->conversion_count < NAU7802_MIN_CONVERSIONS) in nau7802_eoc_trigger()
197 st->conversion_count++; in nau7802_eoc_trigger()
198 if (st->conversion_count >= NAU7802_MIN_CONVERSIONS) in nau7802_eoc_trigger()
199 complete(&st->value_ok); in nau7802_eoc_trigger()
205 struct iio_chan_spec const *chan, in nau7802_read_irq() argument
211 reinit_completion(&st->value_ok); in nau7802_read_irq()
212 enable_irq(st->client->irq); in nau7802_read_irq()
222 ret = wait_for_completion_interruptible_timeout(&st->value_ok, in nau7802_read_irq()
225 ret = -ETIMEDOUT; in nau7802_read_irq()
230 disable_irq(st->client->irq); in nau7802_read_irq()
232 *val = st->last_value; in nau7802_read_irq()
237 disable_irq(st->client->irq); in nau7802_read_irq()
243 struct iio_chan_spec const *chan, in nau7802_read_poll() argument
257 * Because there is actually only one ADC for both channels, we have to in nau7802_read_poll()
262 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); in nau7802_read_poll()
267 if (st->sample_rate != NAU7802_SAMP_FREQ_320) in nau7802_read_poll()
271 ret = i2c_smbus_read_byte_data(st->client, in nau7802_read_poll()
280 if (st->conversion_count < NAU7802_MIN_CONVERSIONS) in nau7802_read_poll()
281 st->conversion_count++; in nau7802_read_poll()
282 } while (st->conversion_count < NAU7802_MIN_CONVERSIONS); in nau7802_read_poll()
284 *val = st->last_value; in nau7802_read_poll()
290 struct iio_chan_spec const *chan, in nau7802_read_raw() argument
298 mutex_lock(&st->lock); in nau7802_read_raw()
301 * - Channel 1 is value 0 in the CHS register in nau7802_read_raw()
302 * - Channel 2 is value 1 in the CHS register in nau7802_read_raw()
304 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL2); in nau7802_read_raw()
306 mutex_unlock(&st->lock); in nau7802_read_raw()
310 if (((ret & NAU7802_CTRL2_CHS_BIT) && !chan->channel) || in nau7802_read_raw()
312 chan->channel)) { in nau7802_read_raw()
313 st->conversion_count = 0; in nau7802_read_raw()
314 ret = i2c_smbus_write_byte_data(st->client, in nau7802_read_raw()
316 NAU7802_CTRL2_CHS(chan->channel) | in nau7802_read_raw()
317 NAU7802_CTRL2_CRS(st->sample_rate)); in nau7802_read_raw()
320 mutex_unlock(&st->lock); in nau7802_read_raw()
325 if (st->client->irq) in nau7802_read_raw()
326 ret = nau7802_read_irq(indio_dev, chan, val); in nau7802_read_raw()
328 ret = nau7802_read_poll(indio_dev, chan, val); in nau7802_read_raw()
330 mutex_unlock(&st->lock); in nau7802_read_raw()
334 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1); in nau7802_read_raw()
342 *val = st->vref_mv; in nau7802_read_raw()
348 *val = nau7802_sample_freq_avail[st->sample_rate]; in nau7802_read_raw()
356 return -EINVAL; in nau7802_read_raw()
360 struct iio_chan_spec const *chan, in nau7802_write_raw() argument
368 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) in nau7802_write_raw()
369 if (val2 == st->scale_avail[i]) in nau7802_write_raw()
377 mutex_lock(&st->lock); in nau7802_write_raw()
378 st->sample_rate = i; in nau7802_write_raw()
379 st->conversion_count = 0; in nau7802_write_raw()
380 ret = i2c_smbus_write_byte_data(st->client, in nau7802_write_raw()
382 NAU7802_CTRL2_CRS(st->sample_rate)); in nau7802_write_raw()
383 mutex_unlock(&st->lock); in nau7802_write_raw()
393 return -EINVAL; in nau7802_write_raw()
397 struct iio_chan_spec const *chan, in nau7802_write_raw_get_fmt() argument
415 struct device_node *np = client->dev.of_node; in nau7802_probe()
420 if (!client->dev.of_node) { in nau7802_probe()
421 dev_err(&client->dev, "No device tree node available.\n"); in nau7802_probe()
422 return -EINVAL; in nau7802_probe()
425 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); in nau7802_probe()
427 return -ENOMEM; in nau7802_probe()
433 indio_dev->name = dev_name(&client->dev); in nau7802_probe()
434 indio_dev->modes = INDIO_DIRECT_MODE; in nau7802_probe()
435 indio_dev->info = &nau7802_info; in nau7802_probe()
437 st->client = client; in nau7802_probe()
440 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, in nau7802_probe()
446 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, in nau7802_probe()
456 ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); in nau7802_probe()
463 st->vref_mv = tmp; in nau7802_probe()
470 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, data); in nau7802_probe()
473 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_ADC_CTRL, 0x30); in nau7802_probe()
478 data = NAU7802_CTRL1_VLDO((4500 - tmp) / 300); in nau7802_probe()
479 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL1, in nau7802_probe()
485 /* Populate available ADC input ranges */ in nau7802_probe()
486 for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) in nau7802_probe()
487 st->scale_avail[i] = (((u64)st->vref_mv) * 1000000000ULL) in nau7802_probe()
490 init_completion(&st->value_ok); in nau7802_probe()
493 * The ADC fires continuously and we can't do anything about in nau7802_probe()
497 if (client->irq) { in nau7802_probe()
498 ret = request_threaded_irq(client->irq, in nau7802_probe()
502 client->dev.driver->name, in nau7802_probe()
508 * by this ADC as when going over 40 sample per second, in nau7802_probe()
513 dev_info(&client->dev, in nau7802_probe()
515 client->irq = 0; in nau7802_probe()
517 disable_irq(client->irq); in nau7802_probe()
520 if (!client->irq) { in nau7802_probe()
525 st->sample_rate = NAU7802_SAMP_FREQ_320; in nau7802_probe()
526 ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL2, in nau7802_probe()
527 NAU7802_CTRL2_CRS(st->sample_rate)); in nau7802_probe()
532 /* Setup the ADC channels available on the board */ in nau7802_probe()
533 indio_dev->num_channels = ARRAY_SIZE(nau7802_chan_array); in nau7802_probe()
534 indio_dev->channels = nau7802_chan_array; in nau7802_probe()
536 mutex_init(&st->lock); in nau7802_probe()
537 mutex_init(&st->data_lock); in nau7802_probe()
541 dev_err(&client->dev, "Couldn't register the device.\n"); in nau7802_probe()
548 mutex_destroy(&st->lock); in nau7802_probe()
549 mutex_destroy(&st->data_lock); in nau7802_probe()
551 if (client->irq) in nau7802_probe()
552 free_irq(client->irq, indio_dev); in nau7802_probe()
563 mutex_destroy(&st->lock); in nau7802_remove()
564 mutex_destroy(&st->data_lock); in nau7802_remove()
565 if (client->irq) in nau7802_remove()
566 free_irq(client->irq, indio_dev); in nau7802_remove()
596 MODULE_DESCRIPTION("Nuvoton NAU7802 ADC Driver");
597 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
598 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");