Lines Matching +full:x +full:- +full:powers
1 // SPDX-License-Identifier: GPL-2.0-only
10 * Color light sensor with 16-bit channels for x, y, z and temperature);
11 * 7-bit I2C slave address 0x74 .. 0x77.
14 * AS73211: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
15 * AS7331: https://ams.com/documents/20143/9106314/AS7331_DS001047_4-00.pdf
56 #define AS73211_AGEN_DEVID(x) FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x))
58 #define AS73211_AGEN_MUT(x) FIELD_PREP(AS73211_AGEN_MUT_MASK, (x))
76 #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1)
78 /* Available sample frequencies are 1.024MHz multiplied by powers of two. */
93 * struct as73211_spec_dev_data - device-specific data
105 * struct as73211_data - Instance data for one AS73211
114 * @spec_dev: device-specific configuration.
151 #define AS73211_OFFSET_TEMP_INT (-66)
202 AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
234 * in CREG1 is in powers of 2 (x 1024 cycles).
236 return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1));
243 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz)
244 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles)
247 * = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000
250 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125
252 return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
260 for (i = 0; i < ARRAY_SIZE(data->int_time_avail) / 2; i++) {
263 data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC;
264 data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC;
270 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
271 return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1));
279 struct device *dev = &data->client->dev;
284 if (data->client->irq)
285 reinit_completion(&data->completion);
291 i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
293 data->osr &= ~AS73211_OSR_DOS_MASK;
294 data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS;
296 smbus_data.byte = data->osr;
297 ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr,
298 data->client->flags, I2C_SMBUS_WRITE,
301 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
309 data->osr &= ~AS73211_OSR_SS;
312 * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%.
315 if (data->client->irq) {
316 ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us));
319 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
320 return -ETIMEDOUT;
327 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT);
329 ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS);
337 return -ETIME;
341 return -ENODATA;
345 return -ENODATA;
349 return -ENOBUFS;
353 return -EOVERFLOW;
357 return -EOVERFLOW;
361 return -EOVERFLOW;
364 return -EIO;
384 return -EINVAL;
405 return -EINVAL;
431 ret = i2c_smbus_read_word_data(data->client, chan->address);
445 switch (chan->type) {
452 return data->spec_dev->intensity_scale(data, chan->channel2,
456 return -EINVAL;
460 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
461 *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) *
472 mutex_lock(&data->mutex);
474 mutex_unlock(&data->mutex);
480 return -EINVAL;
503 *length = ARRAY_SIZE(data->int_time_avail);
504 *vals = data->int_time_avail;
509 return -EINVAL;
524 /* val must be 1024 * 2^x */
527 return -EINVAL;
529 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */
530 reg_bits = ilog2(freq_kHz) - 10;
532 return -EINVAL;
534 data->creg3 &= ~AS73211_CREG3_CCLK_MASK;
535 data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits);
538 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3);
548 return -EINVAL;
550 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */
551 reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val);
553 return -EINVAL;
555 data->creg1 &= ~AS73211_CREG1_GAIN_MASK;
556 data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits);
558 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
569 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */
570 int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3));
578 return -EINVAL;
582 return -EINVAL; /* not possible due to previous tests */
584 data->creg1 &= ~AS73211_CREG1_TIME_MASK;
585 data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits);
587 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1);
594 return -EINVAL;
604 mutex_lock(&data->mutex);
611 if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) {
612 data->osr &= ~AS73211_OSR_DOS_MASK;
613 data->osr |= AS73211_OSR_DOS_CONFIG;
615 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
625 mutex_unlock(&data->mutex);
633 complete(&data->completion);
641 struct iio_dev *indio_dev = pf->indio_dev;
649 mutex_lock(&data->mutex);
652 if (data_result < 0 && data_result != -EOVERFLOW)
655 if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
660 .addr = data->client->addr,
666 .addr = data->client->addr,
673 ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs));
680 ret = i2c_master_recv(data->client,
694 if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) {
708 mutex_unlock(&data->mutex);
709 iio_trigger_notify_done(indio_dev->trig);
725 mutex_lock(&data->mutex);
728 data->osr &= ~AS73211_OSR_PD;
730 data->osr |= AS73211_OSR_PD;
732 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
734 mutex_unlock(&data->mutex);
751 struct device *dev = &client->dev;
758 return -ENOMEM;
762 data->client = client;
764 data->spec_dev = i2c_get_match_data(client);
765 if (!data->spec_dev)
766 return -EINVAL;
768 mutex_init(&data->mutex);
769 init_completion(&data->completion);
771 indio_dev->info = &as73211_info;
772 indio_dev->name = AS73211_DRV_NAME;
773 indio_dev->channels = data->spec_dev->channels;
774 indio_dev->num_channels = data->spec_dev->num_channels;
775 indio_dev->modes = INDIO_DIRECT_MODE;
776 indio_dev->available_scan_masks = as73211_scan_masks;
778 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
781 data->osr = ret;
784 data->osr |= AS73211_OSR_SW_RES;
785 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr);
789 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR);
792 data->osr = ret;
798 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN);
805 return -ENODEV;
807 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1);
810 data->creg1 = ret;
812 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2);
815 data->creg2 = ret;
817 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3);
820 data->creg3 = ret;
835 if (client->irq) {
836 ret = devm_request_threaded_irq(&client->dev, client->irq,
840 client->name, indio_dev);