drivers/hwmon/max197.c

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

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/max197.c
Extension
.c
Size
8762 bytes
Lines
343
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 max197_data {
	struct max197_platform_data *pdata;
	struct device *hwmon_dev;
	struct mutex lock;
	int limit;
	bool scale;
	u8 ctrl_bytes[MAX197_NUM_CH];
};

static inline void max197_set_unipolarity(struct max197_data *data, int channel)
{
	data->ctrl_bytes[channel] &= ~MAX197_BIP;
}

static inline void max197_set_bipolarity(struct max197_data *data, int channel)
{
	data->ctrl_bytes[channel] |= MAX197_BIP;
}

static inline void max197_set_half_range(struct max197_data *data, int channel)
{
	data->ctrl_bytes[channel] &= ~MAX197_RNG;
}

static inline void max197_set_full_range(struct max197_data *data, int channel)
{
	data->ctrl_bytes[channel] |= MAX197_RNG;
}

static inline bool max197_is_bipolar(struct max197_data *data, int channel)
{
	return data->ctrl_bytes[channel] & MAX197_BIP;
}

static inline bool max197_is_full_range(struct max197_data *data, int channel)
{
	return data->ctrl_bytes[channel] & MAX197_RNG;
}

/* Function called on read access on in{0,1,2,3,4,5,6,7}_{min,max} */
static ssize_t max197_show_range(struct device *dev,
				 struct device_attribute *devattr, char *buf)
{
	struct max197_data *data = dev_get_drvdata(dev);
	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
	int channel = attr->index;
	bool is_min = attr->nr;
	int range;

	if (mutex_lock_interruptible(&data->lock))
		return -ERESTARTSYS;

	range = max197_is_full_range(data, channel) ?
		data->limit : data->limit / 2;
	if (is_min) {
		if (max197_is_bipolar(data, channel))
			range = -range;
		else
			range = 0;
	}

	mutex_unlock(&data->lock);

	return sprintf(buf, "%d\n", range);
}

/* Function called on write access on in{0,1,2,3,4,5,6,7}_{min,max} */
static ssize_t max197_store_range(struct device *dev,
				  struct device_attribute *devattr,
				  const char *buf, size_t count)
{
	struct max197_data *data = dev_get_drvdata(dev);
	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
	int channel = attr->index;
	bool is_min = attr->nr;
	long value;
	int half = data->limit / 2;
	int full = data->limit;

	if (kstrtol(buf, 10, &value))
		return -EINVAL;

	if (is_min) {
		if (value <= -full)
			value = -full;
		else if (value < 0)
			value = -half;
		else
			value = 0;
	} else {

Annotation

Implementation Notes