drivers/hwmon/kbatt.c

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

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/kbatt.c
Extension
.c
Size
3451 bytes
Lines
148
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 kbatt {
	/* update lock */
	struct mutex lock;
	void __iomem *base;

	unsigned long next_update; /* in jiffies */
	bool alarm;
};

static bool kbatt_alarm(struct kbatt *kbatt)
{
	mutex_lock(&kbatt->lock);

	if (!kbatt->next_update || time_after(jiffies, kbatt->next_update)) {
		/* switch load on */
		iowrite8(KBATT_CONTROL_BAT_TEST,
			 kbatt->base + KBATT_CONTROL_REG);

		/* wait some time to let things settle */
		fsleep(KBATT_SETTLE_TIME_US);

		/* check battery state */
		if (ioread8(kbatt->base + KBATT_STATUS_REG) &
		    KBATT_STATUS_BAT_OK)
			kbatt->alarm = false;
		else
			kbatt->alarm = true;

		/* switch load off */
		iowrite8(0, kbatt->base + KBATT_CONTROL_REG);

		kbatt->next_update = jiffies + KBATT_MAX_UPD_INTERVAL;
	}

	mutex_unlock(&kbatt->lock);

	return kbatt->alarm;
}

static int kbatt_read(struct device *dev, enum hwmon_sensor_types type,
		      u32 attr, int channel, long *val)
{
	struct kbatt *kbatt = dev_get_drvdata(dev);

	*val = kbatt_alarm(kbatt) ? 1 : 0;

	return 0;
}

static umode_t kbatt_is_visible(const void *data, enum hwmon_sensor_types type,
				u32 attr, int channel)
{
	if (channel == 0 && attr == hwmon_in_min_alarm)
		return 0444;

	return 0;
}

static const struct hwmon_channel_info *kbatt_info[] = {
	HWMON_CHANNEL_INFO(in,
			   /* 0: input minimum alarm channel */
			   HWMON_I_MIN_ALARM),
	NULL
};

static const struct hwmon_ops kbatt_hwmon_ops = {
	.is_visible = kbatt_is_visible,
	.read = kbatt_read,
};

static const struct hwmon_chip_info kbatt_chip_info = {
	.ops = &kbatt_hwmon_ops,
	.info = kbatt_info,
};

static int kbatt_probe(struct auxiliary_device *auxdev,
		       const struct auxiliary_device_id *id)
{
	struct keba_batt_auxdev *kbatt_auxdev =
		container_of(auxdev, struct keba_batt_auxdev, auxdev);
	struct device *dev = &auxdev->dev;
	struct device *hwmon_dev;
	struct kbatt *kbatt;
	int retval;

	kbatt = devm_kzalloc(dev, sizeof(*kbatt), GFP_KERNEL);
	if (!kbatt)
		return -ENOMEM;

	retval = devm_mutex_init(dev, &kbatt->lock);

Annotation

Implementation Notes