drivers/hwmon/sparx5-temp.c

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

File Facts

System
Linux kernel
Corpus path
drivers/hwmon/sparx5-temp.c
Extension
.c
Size
3394 bytes
Lines
152
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 s5_hwmon {
	void __iomem *base;
	struct clk *clk;
};

static void s5_temp_enable(struct s5_hwmon *hwmon)
{
	u32 val = readl(hwmon->base + TEMP_CFG);
	u32 clk = clk_get_rate(hwmon->clk) / USEC_PER_SEC;

	val &= ~TEMP_CFG_CYCLES;
	val |= FIELD_PREP(TEMP_CFG_CYCLES, clk);
	val |= TEMP_CFG_ENA;

	writel(val, hwmon->base + TEMP_CFG);
}

static int s5_read(struct device *dev, enum hwmon_sensor_types type,
		   u32 attr, int channel, long *temp)
{
	struct s5_hwmon *hwmon = dev_get_drvdata(dev);
	int rc = 0, value;
	u32 stat;

	switch (attr) {
	case hwmon_temp_input:
		stat = readl_relaxed(hwmon->base + TEMP_STAT);
		if (!(stat & TEMP_STAT_VALID))
			return -EAGAIN;
		value = stat & TEMP_STAT_TEMP;
		/*
		 * From register documentation:
		 * Temp(C) = TEMP_SENSOR_STAT.TEMP / 4096 * 352.2 - 109.4
		 */
		value = DIV_ROUND_CLOSEST(value * 3522, 4096) - 1094;
		/*
		 * Scale down by 10 from above and multiply by 1000 to
		 * have millidegrees as specified by the hwmon sysfs
		 * interface.
		 */
		value *= 100;
		*temp = value;
		break;
	default:
		rc = -EOPNOTSUPP;
		break;
	}

	return rc;
}

static umode_t s5_is_visible(const void *_data, enum hwmon_sensor_types type,
			     u32 attr, int channel)
{
	if (type != hwmon_temp)
		return 0;

	switch (attr) {
	case hwmon_temp_input:
		return 0444;
	default:
		return 0;
	}
}

static const struct hwmon_channel_info * const s5_info[] = {
	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
	NULL
};

static const struct hwmon_ops s5_hwmon_ops = {
	.is_visible = s5_is_visible,
	.read = s5_read,
};

static const struct hwmon_chip_info s5_chip_info = {
	.ops = &s5_hwmon_ops,
	.info = s5_info,
};

static int s5_temp_probe(struct platform_device *pdev)
{
	struct device *hwmon_dev;
	struct s5_hwmon *hwmon;

	hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
	if (!hwmon)
		return -ENOMEM;

Annotation

Implementation Notes