drivers/iio/light/gp2ap002.c
Source file repositories/reference/linux-study-clean/drivers/iio/light/gp2ap002.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/iio/light/gp2ap002.c- Extension
.c- Size
- 20645 bytes
- Lines
- 720
- 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/module.hlinux/i2c.hlinux/regmap.hlinux/iio/iio.hlinux/iio/sysfs.hlinux/iio/events.hlinux/iio/consumer.hlinux/iio/types.hlinux/init.hlinux/delay.hlinux/regulator/consumer.hlinux/pm_runtime.hlinux/interrupt.hlinux/bits.hlinux/math64.hlinux/pm.h
Detected Declarations
struct gp2ap002function gp2ap002_prox_irqfunction gp2ap002_get_luxfunction gp2ap002_read_rawfunction gp2ap002_initfunction gp2ap002_read_event_configfunction gp2ap002_write_event_configfunction i2c_smbus_read_word_datafunction gp2ap002_regmap_i2c_writefunction gp2ap002_probefunction gp2ap002_removefunction gp2ap002_runtime_suspendfunction gp2ap002_runtime_resume
Annotated Snippet
struct gp2ap002 {
struct regmap *map;
struct device *dev;
struct regulator *vdd;
struct regulator *vio;
struct iio_channel *alsout;
u8 hys_far;
u8 hys_close;
bool is_gp2ap002s00f;
int irq;
bool enabled;
};
static irqreturn_t gp2ap002_prox_irq(int irq, void *d)
{
struct iio_dev *indio_dev = d;
struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
u64 ev;
int val;
int ret;
if (!gp2ap002->enabled)
goto err_retrig;
ret = regmap_read(gp2ap002->map, GP2AP002_PROX, &val);
if (ret) {
dev_err(gp2ap002->dev, "error reading proximity\n");
goto err_retrig;
}
if (val & GP2AP002_PROX_VO_DETECT) {
/* Close */
dev_dbg(gp2ap002->dev, "close\n");
ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
gp2ap002->hys_far);
if (ret)
dev_err(gp2ap002->dev,
"error setting up proximity hysteresis\n");
ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING);
} else {
/* Far */
dev_dbg(gp2ap002->dev, "far\n");
ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
gp2ap002->hys_close);
if (ret)
dev_err(gp2ap002->dev,
"error setting up proximity hysteresis\n");
ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING);
}
iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
/*
* After changing hysteresis, we need to wait for one detection
* cycle to see if anything changed, or we will just trigger the
* previous interrupt again. A detection cycle depends on the CYCLE
* register, we are hard-coding ~8 ms in probe() so wait some more
* than this, 20-30 ms.
*/
usleep_range(20000, 30000);
err_retrig:
ret = regmap_write(gp2ap002->map, GP2AP002_CON,
GP2AP002_CON_OCON_ENABLE);
if (ret)
dev_err(gp2ap002->dev, "error setting up VOUT control\n");
return IRQ_HANDLED;
}
/*
* This array maps current and lux.
*
* Ambient light sensing range is 3 to 55000 lux.
*
* This mapping is based on the following formula.
* illuminance = 10 ^ (current[mA] / 10)
*
* When the ADC measures 0, return 0 lux.
*/
static const u16 gp2ap002_illuminance_table[] = {
0, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 63, 79,
100, 126, 158, 200, 251, 316, 398, 501, 631, 794, 1000, 1259, 1585,
1995, 2512, 3162, 3981, 5012, 6310, 7943, 10000, 12589, 15849, 19953,
25119, 31623, 39811, 50119,
};
static int gp2ap002_get_lux(struct gp2ap002 *gp2ap002)
{
Annotation
- Immediate include surface: `linux/module.h`, `linux/i2c.h`, `linux/regmap.h`, `linux/iio/iio.h`, `linux/iio/sysfs.h`, `linux/iio/events.h`, `linux/iio/consumer.h`, `linux/iio/types.h`.
- Detected declarations: `struct gp2ap002`, `function gp2ap002_prox_irq`, `function gp2ap002_get_lux`, `function gp2ap002_read_raw`, `function gp2ap002_init`, `function gp2ap002_read_event_config`, `function gp2ap002_write_event_config`, `function i2c_smbus_read_word_data`, `function gp2ap002_regmap_i2c_write`, `function gp2ap002_probe`.
- 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.