drivers/iio/light/opt4060.c
Source file repositories/reference/linux-study-clean/drivers/iio/light/opt4060.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/light/opt4060.c- Extension
.c- Size
- 36176 bytes
- Lines
- 1324
- Domain
- Driver Families
- Bucket
- drivers/iio
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bitfield.hlinux/i2c.hlinux/iio/iio.hlinux/math64.hlinux/units.hlinux/limits.hlinux/module.hlinux/property.hlinux/regmap.hlinux/mutex.hlinux/regulator/consumer.hlinux/iio/events.hlinux/iio/trigger.hlinux/iio/trigger_consumer.hlinux/iio/triggered_buffer.h
Detected Declarations
struct opt4060_chipstruct opt4060_channel_factorenum opt4060_chan_typefunction opt4060_als_time_to_indexfunction opt4060_calculate_crcfunction opt4060_set_int_statefunction opt4060_set_sampling_modefunction opt4060_event_activefunction opt4060_set_state_commonfunction opt4060_set_driver_statefunction opt4060_trigger_set_statefunction opt4060_read_raw_valuefunction opt4060_trigger_new_samplesfunction opt4060_read_chan_rawfunction opt4060_get_chan_scalefunction opt4060_calc_illuminancefunction opt4060_read_illuminancefunction opt4060_set_int_timefunction opt4060_power_downfunction opt4060_chip_off_actionfunction opt4060_read_rawfunction opt4060_write_rawfunction opt4060_write_raw_get_fmtfunction opt4060_calc_th_regfunction opt4060_calc_val_from_th_regfunction opt4060_read_availablefunction opt4060_read_ev_periodfunction opt4060_write_ev_periodfunction opt4060_get_channel_selfunction opt4060_set_channel_selfunction opt4060_get_thresholdsfunction opt4060_set_thresholdsfunction opt4060_read_eventfunction opt4060_write_eventfunction opt4060_read_event_configfunction opt4060_write_event_configfunction opt4060_load_defaultsfunction opt4060_volatile_regfunction opt4060_writable_regfunction opt4060_readonly_regfunction opt4060_readable_regfunction opt4060_trigger_handlerfunction iio_for_each_active_channelfunction opt4060_irq_threadfunction opt4060_setup_bufferfunction opt4060_setup_triggerfunction opt4060_probe
Annotated Snippet
struct opt4060_chip {
struct regmap *regmap;
struct device *dev;
struct iio_trigger *trig;
u8 int_time;
int irq;
/*
* Mutex for protecting sensor irq settings. Switching between interrupt
* on each sample and on thresholds needs to be synchronized.
*/
struct mutex irq_setting_lock;
/*
* Mutex for protecting event enabling.
*/
struct mutex event_enabling_lock;
struct completion completion;
bool thresh_event_lo_active;
bool thresh_event_hi_active;
};
struct opt4060_channel_factor {
u32 mul;
u32 div;
};
static const int opt4060_int_time_available[][2] = {
{ 0, 600 },
{ 0, 1000 },
{ 0, 1800 },
{ 0, 3400 },
{ 0, 6500 },
{ 0, 12700 },
{ 0, 25000 },
{ 0, 50000 },
{ 0, 100000 },
{ 0, 200000 },
{ 0, 400000 },
{ 0, 800000 },
};
/*
* Conversion time is integration time + time to set register
* this is used as integration time.
*/
static const int opt4060_int_time_reg[][2] = {
{ 600, OPT4060_CTRL_CONVERSION_0_6MS },
{ 1000, OPT4060_CTRL_CONVERSION_1MS },
{ 1800, OPT4060_CTRL_CONVERSION_1_8MS },
{ 3400, OPT4060_CTRL_CONVERSION_3_4MS },
{ 6500, OPT4060_CTRL_CONVERSION_6_5MS },
{ 12700, OPT4060_CTRL_CONVERSION_12_7MS },
{ 25000, OPT4060_CTRL_CONVERSION_25MS },
{ 50000, OPT4060_CTRL_CONVERSION_50MS },
{ 100000, OPT4060_CTRL_CONVERSION_100MS },
{ 200000, OPT4060_CTRL_CONVERSION_200MS },
{ 400000, OPT4060_CTRL_CONVERSION_400MS },
{ 800000, OPT4060_CTRL_CONVERSION_800MS },
};
static int opt4060_als_time_to_index(const u32 als_integration_time)
{
int i;
for (i = 0; i < ARRAY_SIZE(opt4060_int_time_available); i++) {
if (als_integration_time == opt4060_int_time_available[i][1])
return i;
}
return -EINVAL;
}
static u8 opt4060_calculate_crc(u8 exp, u32 mantissa, u8 count)
{
u8 crc;
/*
* Calculates a 4-bit CRC from a 20-bit mantissa, 4-bit exponent and a 4-bit counter.
* crc[0] = XOR(mantissa[19:0], exp[3:0], count[3:0])
* crc[1] = XOR(mantissa[1,3,5,7,9,11,13,15,17,19], exp[1,3], count[1,3])
* crc[2] = XOR(mantissa[3,7,11,15,19], exp[3], count[3])
* crc[3] = XOR(mantissa[3,11,19])
*/
crc = (hweight32(mantissa) + hweight32(exp) + hweight32(count)) % 2;
crc |= ((hweight32(mantissa & 0xAAAAA) + hweight32(exp & 0xA)
+ hweight32(count & 0xA)) % 2) << 1;
crc |= ((hweight32(mantissa & 0x88888) + hweight32(exp & 0x8)
+ hweight32(count & 0x8)) % 2) << 2;
crc |= (hweight32(mantissa & 0x80808) % 2) << 3;
return crc;
Annotation
- Immediate include surface: `linux/bitfield.h`, `linux/i2c.h`, `linux/iio/iio.h`, `linux/math64.h`, `linux/units.h`, `linux/limits.h`, `linux/module.h`, `linux/property.h`.
- Detected declarations: `struct opt4060_chip`, `struct opt4060_channel_factor`, `enum opt4060_chan_type`, `function opt4060_als_time_to_index`, `function opt4060_calculate_crc`, `function opt4060_set_int_state`, `function opt4060_set_sampling_mode`, `function opt4060_event_active`, `function opt4060_set_state_common`, `function opt4060_set_driver_state`.
- Atlas domain: Driver Families / drivers/iio.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.