Lines Matching +full:x +full:- +full:powers

1 // SPDX-License-Identifier: GPL-2.0-only
9 * Color light sensor with 16-bit channels for x, y, z and temperature);
10 * 7-bit I2C slave address 0x74 .. 0x77.
12 * Datasheet: https://ams.com/documents/20143/36005/AS73211_DS000556_3-01.pdf
53 #define AS73211_AGEN_DEVID(x) FIELD_PREP(AS73211_AGEN_DEVID_MASK, (x)) argument
55 #define AS73211_AGEN_MUT(x) FIELD_PREP(AS73211_AGEN_MUT_MASK, (x)) argument
73 #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1)
75 /* Available sample frequencies are 1.024MHz multiplied by powers of two. */
88 * struct as73211_data - Instance data for one AS73211
132 #define AS73211_OFFSET_TEMP_INT (-66)
173 AS73211_COLOR_CHANNEL(X, AS73211_SCAN_INDEX_X, AS73211_OUT_MRES1),
183 * in CREG1 is in powers of 2 (x 1024 cycles). in as73211_integration_time_1024cyc()
185 return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1)); in as73211_integration_time_1024cyc()
192 * f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) in as73211_integration_time_us()
193 * t_cycl is configured in CREG1 in powers of 2 (x 1024 cycles) in as73211_integration_time_us()
196 * = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000 in as73211_integration_time_us()
199 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125 in as73211_integration_time_us()
201 return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) * in as73211_integration_time_us()
209 for (i = 0; i < ARRAY_SIZE(data->int_time_avail) / 2; i++) { in as73211_integration_time_calc_avail()
212 data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC; in as73211_integration_time_calc_avail()
213 data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC; in as73211_integration_time_calc_avail()
219 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */ in as73211_gain()
220 return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1)); in as73211_gain()
228 struct device *dev = &data->client->dev; in as73211_req_data()
233 if (data->client->irq) in as73211_req_data()
234 reinit_completion(&data->completion); in as73211_req_data()
240 i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
242 data->osr &= ~AS73211_OSR_DOS_MASK; in as73211_req_data()
243 data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS; in as73211_req_data()
245 smbus_data.byte = data->osr; in as73211_req_data()
246 ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr, in as73211_req_data()
247 data->client->flags, I2C_SMBUS_WRITE, in as73211_req_data()
250 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
258 data->osr &= ~AS73211_OSR_SS; in as73211_req_data()
261 * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%. in as73211_req_data()
264 if (data->client->irq) { in as73211_req_data()
265 ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us)); in as73211_req_data()
268 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
269 return -ETIMEDOUT; in as73211_req_data()
276 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
278 ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS); in as73211_req_data()
286 return -ETIME; in as73211_req_data()
290 return -ENODATA; in as73211_req_data()
294 return -ENODATA; in as73211_req_data()
298 return -ENOBUFS; in as73211_req_data()
302 return -EOVERFLOW; in as73211_req_data()
306 return -EOVERFLOW; in as73211_req_data()
310 return -EOVERFLOW; in as73211_req_data()
313 return -EIO; in as73211_req_data()
338 ret = i2c_smbus_read_word_data(data->client, chan->address); in as73211_read_raw()
352 switch (chan->type) { in as73211_read_raw()
361 switch (chan->channel2) { in as73211_read_raw()
372 return -EINVAL; in as73211_read_raw()
380 return -EINVAL; in as73211_read_raw()
384 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */ in as73211_read_raw()
385 *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) * in as73211_read_raw()
396 mutex_lock(&data->mutex); in as73211_read_raw()
398 mutex_unlock(&data->mutex); in as73211_read_raw()
404 return -EINVAL; in as73211_read_raw()
427 *length = ARRAY_SIZE(data->int_time_avail); in as73211_read_avail()
428 *vals = data->int_time_avail; in as73211_read_avail()
433 return -EINVAL; in as73211_read_avail()
448 /* val must be 1024 * 2^x */ in _as73211_write_raw()
451 return -EINVAL; in _as73211_write_raw()
453 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz (=2^10)) */ in _as73211_write_raw()
454 reg_bits = ilog2(freq_kHz) - 10; in _as73211_write_raw()
456 return -EINVAL; in _as73211_write_raw()
458 data->creg3 &= ~AS73211_CREG3_CCLK_MASK; in _as73211_write_raw()
459 data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits); in _as73211_write_raw()
462 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3); in _as73211_write_raw()
472 return -EINVAL; in _as73211_write_raw()
474 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */ in _as73211_write_raw()
475 reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val); in _as73211_write_raw()
477 return -EINVAL; in _as73211_write_raw()
479 data->creg1 &= ~AS73211_CREG1_GAIN_MASK; in _as73211_write_raw()
480 data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits); in _as73211_write_raw()
482 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1); in _as73211_write_raw()
493 /* f_samp is configured in CREG3 in powers of 2 (x 1.024 MHz) */ in _as73211_write_raw()
494 int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)); in _as73211_write_raw()
502 return -EINVAL; in _as73211_write_raw()
506 return -EINVAL; /* not possible due to previous tests */ in _as73211_write_raw()
508 data->creg1 &= ~AS73211_CREG1_TIME_MASK; in _as73211_write_raw()
509 data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits); in _as73211_write_raw()
511 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1); in _as73211_write_raw()
518 return -EINVAL; in _as73211_write_raw()
528 mutex_lock(&data->mutex); in as73211_write_raw()
535 if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) { in as73211_write_raw()
536 data->osr &= ~AS73211_OSR_DOS_MASK; in as73211_write_raw()
537 data->osr |= AS73211_OSR_DOS_CONFIG; in as73211_write_raw()
539 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); in as73211_write_raw()
549 mutex_unlock(&data->mutex); in as73211_write_raw()
557 complete(&data->completion); in as73211_ready_handler()
565 struct iio_dev *indio_dev = pf->indio_dev; in as73211_trigger_handler()
573 mutex_lock(&data->mutex); in as73211_trigger_handler()
576 if (data_result < 0 && data_result != -EOVERFLOW) in as73211_trigger_handler()
579 if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) { in as73211_trigger_handler()
584 .addr = data->client->addr, in as73211_trigger_handler()
590 .addr = data->client->addr, in as73211_trigger_handler()
597 ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs)); in as73211_trigger_handler()
604 ret = i2c_master_recv(data->client, in as73211_trigger_handler()
623 mutex_unlock(&data->mutex); in as73211_trigger_handler()
624 iio_trigger_notify_done(indio_dev->trig); in as73211_trigger_handler()
640 mutex_lock(&data->mutex); in as73211_power()
643 data->osr &= ~AS73211_OSR_PD; in as73211_power()
645 data->osr |= AS73211_OSR_PD; in as73211_power()
647 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); in as73211_power()
649 mutex_unlock(&data->mutex); in as73211_power()
666 struct device *dev = &client->dev; in as73211_probe()
673 return -ENOMEM; in as73211_probe()
677 data->client = client; in as73211_probe()
679 mutex_init(&data->mutex); in as73211_probe()
680 init_completion(&data->completion); in as73211_probe()
682 indio_dev->info = &as73211_info; in as73211_probe()
683 indio_dev->name = AS73211_DRV_NAME; in as73211_probe()
684 indio_dev->channels = as73211_channels; in as73211_probe()
685 indio_dev->num_channels = ARRAY_SIZE(as73211_channels); in as73211_probe()
686 indio_dev->modes = INDIO_DIRECT_MODE; in as73211_probe()
688 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR); in as73211_probe()
691 data->osr = ret; in as73211_probe()
694 data->osr |= AS73211_OSR_SW_RES; in as73211_probe()
695 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); in as73211_probe()
699 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR); in as73211_probe()
702 data->osr = ret; in as73211_probe()
708 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN); in as73211_probe()
715 return -ENODEV; in as73211_probe()
717 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1); in as73211_probe()
720 data->creg1 = ret; in as73211_probe()
722 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2); in as73211_probe()
725 data->creg2 = ret; in as73211_probe()
727 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3); in as73211_probe()
730 data->creg3 = ret; in as73211_probe()
745 if (client->irq) { in as73211_probe()
746 ret = devm_request_threaded_irq(&client->dev, client->irq, in as73211_probe()
750 client->name, indio_dev); in as73211_probe()