Lines Matching +full:fraction +full:- +full:z
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
76 #define AS73211_SAMPLE_TIME_MAX_MS BIT(AS73211_SAMPLE_TIME_NUM - 1)
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)
198 AS73211_COLOR_CHANNEL(Z, AS73211_SCAN_INDEX_Z, AS73211_OUT_MRES3),
230 return BIT(FIELD_GET(AS73211_CREG1_TIME_MASK, data->creg1)); in as73211_integration_time_1024cyc()
241 * = 2^(-CREG3_CCLK) * 2^CREG1_CYCLES * 1,000 in as73211_integration_time_us()
242 * In order to get rid of negative exponents, we extend the "fraction" in as73211_integration_time_us()
244 * t_int_us = 2^(3-CREG3_CCLK) * 2^CREG1_CYCLES * 125 in as73211_integration_time_us()
246 return BIT(3 - FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) * in as73211_integration_time_us()
254 for (i = 0; i < ARRAY_SIZE(data->int_time_avail) / 2; i++) { in as73211_integration_time_calc_avail()
257 data->int_time_avail[i * 2 + 0] = time_us / USEC_PER_SEC; in as73211_integration_time_calc_avail()
258 data->int_time_avail[i * 2 + 1] = time_us % USEC_PER_SEC; in as73211_integration_time_calc_avail()
264 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */ in as73211_gain()
265 return BIT(AS73211_CREG1_GAIN_1 - FIELD_GET(AS73211_CREG1_GAIN_MASK, data->creg1)); in as73211_gain()
273 struct device *dev = &data->client->dev; in as73211_req_data()
278 if (data->client->irq) in as73211_req_data()
279 reinit_completion(&data->completion); in as73211_req_data()
285 i2c_lock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
287 data->osr &= ~AS73211_OSR_DOS_MASK; in as73211_req_data()
288 data->osr |= AS73211_OSR_DOS_MEASURE | AS73211_OSR_SS; in as73211_req_data()
290 smbus_data.byte = data->osr; in as73211_req_data()
291 ret = __i2c_smbus_xfer(data->client->adapter, data->client->addr, in as73211_req_data()
292 data->client->flags, I2C_SMBUS_WRITE, in as73211_req_data()
295 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
303 data->osr &= ~AS73211_OSR_SS; in as73211_req_data()
306 * Add 33% extra margin for the timeout. fclk,min = fclk,typ - 27%. in as73211_req_data()
309 if (data->client->irq) { in as73211_req_data()
310 ret = wait_for_completion_timeout(&data->completion, usecs_to_jiffies(time_us)); in as73211_req_data()
313 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
314 return -ETIMEDOUT; in as73211_req_data()
321 i2c_unlock_bus(data->client->adapter, I2C_LOCK_SEGMENT); in as73211_req_data()
323 ret = i2c_smbus_read_word_data(data->client, AS73211_OUT_OSR_STATUS); in as73211_req_data()
331 return -ETIME; in as73211_req_data()
335 return -ENODATA; in as73211_req_data()
339 return -ENODATA; in as73211_req_data()
343 return -ENOBUFS; in as73211_req_data()
347 return -EOVERFLOW; in as73211_req_data()
351 return -EOVERFLOW; in as73211_req_data()
355 return -EOVERFLOW; in as73211_req_data()
358 return -EIO; in as73211_req_data()
378 return -EINVAL; in as73211_intensity_scale()
399 return -EINVAL; in as7331_intensity_scale()
425 ret = i2c_smbus_read_word_data(data->client, chan->address); in as73211_read_raw()
439 switch (chan->type) { in as73211_read_raw()
446 return data->spec_dev->intensity_scale(data, chan->channel2, in as73211_read_raw()
450 return -EINVAL; in as73211_read_raw()
455 *val = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)) * in as73211_read_raw()
466 mutex_lock(&data->mutex); in as73211_read_raw()
468 mutex_unlock(&data->mutex); in as73211_read_raw()
474 return -EINVAL; in as73211_read_raw()
497 *length = ARRAY_SIZE(data->int_time_avail); in as73211_read_avail()
498 *vals = data->int_time_avail; in as73211_read_avail()
503 return -EINVAL; in as73211_read_avail()
521 return -EINVAL; in _as73211_write_raw()
524 reg_bits = ilog2(freq_kHz) - 10; in _as73211_write_raw()
526 return -EINVAL; in _as73211_write_raw()
528 data->creg3 &= ~AS73211_CREG3_CCLK_MASK; in _as73211_write_raw()
529 data->creg3 |= FIELD_PREP(AS73211_CREG3_CCLK_MASK, reg_bits); in _as73211_write_raw()
532 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG3, data->creg3); in _as73211_write_raw()
542 return -EINVAL; in _as73211_write_raw()
544 /* gain can be calculated from CREG1 as 2^(11 - CREG1_GAIN) */ in _as73211_write_raw()
545 reg_bits = AS73211_CREG1_GAIN_1 - ilog2(val); in _as73211_write_raw()
547 return -EINVAL; in _as73211_write_raw()
549 data->creg1 &= ~AS73211_CREG1_GAIN_MASK; in _as73211_write_raw()
550 data->creg1 |= FIELD_PREP(AS73211_CREG1_GAIN_MASK, reg_bits); in _as73211_write_raw()
552 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1); in _as73211_write_raw()
564 int f_samp_1_024mhz = BIT(FIELD_GET(AS73211_CREG3_CCLK_MASK, data->creg3)); in _as73211_write_raw()
572 return -EINVAL; in _as73211_write_raw()
576 return -EINVAL; /* not possible due to previous tests */ in _as73211_write_raw()
578 data->creg1 &= ~AS73211_CREG1_TIME_MASK; in _as73211_write_raw()
579 data->creg1 |= FIELD_PREP(AS73211_CREG1_TIME_MASK, reg_bits); in _as73211_write_raw()
581 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_CREG1, data->creg1); in _as73211_write_raw()
588 return -EINVAL; in _as73211_write_raw()
598 mutex_lock(&data->mutex); in as73211_write_raw()
605 if ((data->osr & AS73211_OSR_DOS_MASK) != AS73211_OSR_DOS_CONFIG) { in as73211_write_raw()
606 data->osr &= ~AS73211_OSR_DOS_MASK; in as73211_write_raw()
607 data->osr |= AS73211_OSR_DOS_CONFIG; in as73211_write_raw()
609 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); in as73211_write_raw()
619 mutex_unlock(&data->mutex); in as73211_write_raw()
627 complete(&data->completion); in as73211_ready_handler()
635 struct iio_dev *indio_dev = pf->indio_dev; in as73211_trigger_handler()
643 mutex_lock(&data->mutex); in as73211_trigger_handler()
646 if (data_result < 0 && data_result != -EOVERFLOW) in as73211_trigger_handler()
649 if (*indio_dev->active_scan_mask == AS73211_SCAN_MASK_ALL) { in as73211_trigger_handler()
654 .addr = data->client->addr, in as73211_trigger_handler()
660 .addr = data->client->addr, in as73211_trigger_handler()
667 ret = i2c_transfer(data->client->adapter, msgs, ARRAY_SIZE(msgs)); in as73211_trigger_handler()
674 ret = i2c_master_recv(data->client, in as73211_trigger_handler()
693 mutex_unlock(&data->mutex); in as73211_trigger_handler()
694 iio_trigger_notify_done(indio_dev->trig); in as73211_trigger_handler()
710 mutex_lock(&data->mutex); in as73211_power()
713 data->osr &= ~AS73211_OSR_PD; in as73211_power()
715 data->osr |= AS73211_OSR_PD; in as73211_power()
717 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); in as73211_power()
719 mutex_unlock(&data->mutex); in as73211_power()
736 struct device *dev = &client->dev; in as73211_probe()
743 return -ENOMEM; in as73211_probe()
747 data->client = client; in as73211_probe()
749 data->spec_dev = i2c_get_match_data(client); in as73211_probe()
750 if (!data->spec_dev) in as73211_probe()
751 return -EINVAL; in as73211_probe()
753 mutex_init(&data->mutex); in as73211_probe()
754 init_completion(&data->completion); in as73211_probe()
756 indio_dev->info = &as73211_info; in as73211_probe()
757 indio_dev->name = AS73211_DRV_NAME; in as73211_probe()
758 indio_dev->channels = data->spec_dev->channels; in as73211_probe()
759 indio_dev->num_channels = data->spec_dev->num_channels; in as73211_probe()
760 indio_dev->modes = INDIO_DIRECT_MODE; in as73211_probe()
762 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR); in as73211_probe()
765 data->osr = ret; in as73211_probe()
768 data->osr |= AS73211_OSR_SW_RES; in as73211_probe()
769 ret = i2c_smbus_write_byte_data(data->client, AS73211_REG_OSR, data->osr); in as73211_probe()
773 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_OSR); in as73211_probe()
776 data->osr = ret; in as73211_probe()
782 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_AGEN); in as73211_probe()
789 return -ENODEV; in as73211_probe()
791 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG1); in as73211_probe()
794 data->creg1 = ret; in as73211_probe()
796 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG2); in as73211_probe()
799 data->creg2 = ret; in as73211_probe()
801 ret = i2c_smbus_read_byte_data(data->client, AS73211_REG_CREG3); in as73211_probe()
804 data->creg3 = ret; in as73211_probe()
819 if (client->irq) { in as73211_probe()
820 ret = devm_request_threaded_irq(&client->dev, client->irq, in as73211_probe()
824 client->name, indio_dev); in as73211_probe()