drivers/hwmon/adt7411.c

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

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/adt7411.c
Extension
.c
Size
17526 bytes
Lines
694
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 adt7411_data {
	unsigned long next_update;
	long vref_cached;
	struct i2c_client *client;
	bool use_ext_temp;
};

/*
 * When reading a register containing (up to 4) lsb, all associated
 * msb-registers get locked by the hardware. After _one_ of those msb is read,
 * _all_ are unlocked.
 */
static int adt7411_read_10_bit(struct i2c_client *client, u8 lsb_reg,
			       u8 msb_reg, u8 lsb_shift)
{
	int val, tmp;

	val = i2c_smbus_read_byte_data(client, lsb_reg);
	if (val < 0)
		return val;

	tmp = (val >> lsb_shift) & 3;
	val = i2c_smbus_read_byte_data(client, msb_reg);
	if (val < 0)
		return val;

	val = (val << 2) | tmp;
	return val;
}

static int adt7411_modify_bit(struct i2c_client *client, u8 reg, u8 bit,
			      bool flag)
{
	int ret, val;

	ret = i2c_smbus_read_byte_data(client, reg);
	if (ret < 0)
		return ret;

	if (flag)
		val = ret | bit;
	else
		val = ret & ~bit;

	return i2c_smbus_write_byte_data(client, reg, val);
}

static ssize_t adt7411_show_bit(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr);
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
	int ret = i2c_smbus_read_byte_data(client, attr2->index);

	return ret < 0 ? ret : sprintf(buf, "%u\n", !!(ret & attr2->nr));
}

static ssize_t adt7411_set_bit(struct device *dev,
			       struct device_attribute *attr, const char *buf,
			       size_t count)
{
	struct sensor_device_attribute_2 *s_attr2 = to_sensor_dev_attr_2(attr);
	struct adt7411_data *data = dev_get_drvdata(dev);
	struct i2c_client *client = data->client;
	int ret;
	unsigned long flag;

	ret = kstrtoul(buf, 0, &flag);
	if (ret || flag > 1)
		return -EINVAL;

	scoped_guard(hwmon_lock, dev) {
		ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
		/* force update */
		data->next_update = jiffies;
	}

	return ret < 0 ? ret : count;
}

#define ADT7411_BIT_ATTR(__name, __reg, __bit) \
	SENSOR_DEVICE_ATTR_2(__name, S_IRUGO | S_IWUSR, adt7411_show_bit, \
	adt7411_set_bit, __bit, __reg)

static ADT7411_BIT_ATTR(no_average, ADT7411_REG_CFG2, ADT7411_CFG2_DISABLE_AVG);
static ADT7411_BIT_ATTR(fast_sampling, ADT7411_REG_CFG3, ADT7411_CFG3_ADC_CLK_225);
static ADT7411_BIT_ATTR(adc_ref_vdd, ADT7411_REG_CFG3, ADT7411_CFG3_REF_VDD);

static struct attribute *adt7411_attrs[] = {

Annotation

Implementation Notes