drivers/hwmon/tmp102.c

Source file repositories/reference/linux-study-clean/drivers/hwmon/tmp102.c

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/tmp102.c
Extension
.c
Size
10069 bytes
Lines
423
Domain
Driver Families
Bucket
drivers/hwmon
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct tmp102 {
	const char *label;
	struct regmap *regmap;
	u16 config_orig;
	unsigned long ready_time;
	u16 sample_time;
};

/* convert left adjusted 13-bit TMP102 register value to milliCelsius */
static inline int tmp102_reg_to_mC(s16 val)
{
	return ((val & ~0x01) * 1000) / 128;
}

/* convert milliCelsius to left adjusted 13-bit TMP102 register value */
static inline u16 tmp102_mC_to_reg(int val)
{
	return (val * 128) / 1000;
}

static int tmp102_read_string(struct device *dev, enum hwmon_sensor_types type,
			      u32 attr, int channel, const char **str)
{
	struct tmp102 *tmp102 = dev_get_drvdata(dev);

	*str = tmp102->label;

	return 0;
}

static int tmp102_read_chip(struct device *dev, u32 attr, long *val)
{
	struct tmp102 *tmp102 = dev_get_drvdata(dev);

	switch (attr) {
	case hwmon_chip_update_interval:
		*val = tmp102->sample_time;
		return 0;
	default:
		return -EOPNOTSUPP;
	}
}

static int tmp102_read_temp(struct device *dev, u32 attr, long *val)
{
	struct tmp102 *tmp102 = dev_get_drvdata(dev);
	unsigned int regval;
	int err, reg;

	switch (attr) {
	case hwmon_temp_input:
		/* Is it too early to return a conversion ? */
		if (time_before(jiffies, tmp102->ready_time)) {
			dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
			return -EAGAIN;
		}
		reg = TMP102_TEMP_REG;
		break;
	case hwmon_temp_max_hyst:
		reg = TMP102_TLOW_REG;
		break;
	case hwmon_temp_max:
		reg = TMP102_THIGH_REG;
		break;
	default:
		return -EOPNOTSUPP;
	}

	err = regmap_read(tmp102->regmap, reg, &regval);
	if (err < 0)
		return err;

	*val = tmp102_reg_to_mC(regval);

	return 0;
}

static int tmp102_read(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long *val)
{
	switch (type) {
	case hwmon_chip:
		return tmp102_read_chip(dev, attr, val);
	case hwmon_temp:
		return tmp102_read_temp(dev, attr, val);
	default:
		return -EOPNOTSUPP;
	}
}

Annotation

Implementation Notes