Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * via-cputemp.c - Driver for VIA CPU core temperature monitoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2009 VIA Technologies, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * based on existing coretemp.c, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/hwmon-vid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/hwmon-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/msr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/cpu_device_id.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define DRVNAME	"via_cputemp"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) enum { SHOW_TEMP, SHOW_LABEL, SHOW_NAME };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Functions declaration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) struct via_cputemp_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u8 vrm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u32 msr_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u32 msr_vid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * Sysfs stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct via_cputemp_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (attr->index == SHOW_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		ret = sprintf(buf, "%s\n", data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	else	/* show label */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		ret = sprintf(buf, "Core %d\n", data->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			 char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct via_cputemp_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u32 eax, edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return sprintf(buf, "%lu\n", ((unsigned long)eax & 0xffffff) * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static ssize_t cpu0_vid_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			     struct device_attribute *devattr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct via_cputemp_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u32 eax, edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	err = rdmsr_safe_on_cpu(data->id, data->msr_vid, &eax, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return sprintf(buf, "%d\n", vid_from_reg(~edx & 0x7f, data->vrm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, SHOW_TEMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static SENSOR_DEVICE_ATTR_RO(temp1_label, name, SHOW_LABEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static SENSOR_DEVICE_ATTR_RO(name, name, SHOW_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static struct attribute *via_cputemp_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	&sensor_dev_attr_name.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	&sensor_dev_attr_temp1_label.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static const struct attribute_group via_cputemp_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	.attrs = via_cputemp_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Optional attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static DEVICE_ATTR_RO(cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int via_cputemp_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct via_cputemp_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	struct cpuinfo_x86 *c = &cpu_data(pdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u32 eax, edx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	data = devm_kzalloc(&pdev->dev, sizeof(struct via_cputemp_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	data->id = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	data->name = "via_cputemp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (c->x86 == 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		data->msr_temp = 0x1423;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		switch (c->x86_model) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		case 0xA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			/* C7 A */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		case 0xD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			/* C7 D */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			data->msr_temp = 0x1169;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			data->msr_vid = 0x198;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		case 0xF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			/* Nano */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			data->msr_temp = 0x1423;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	/* test if we can access the TEMPERATURE MSR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			"Unable to access TEMPERATURE MSR, giving up\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	platform_set_drvdata(pdev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	err = sysfs_create_group(&pdev->dev.kobj, &via_cputemp_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (data->msr_vid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		data->vrm = vid_which_vrm();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (data->vrm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		err = device_create_file(&pdev->dev, &dev_attr_cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	data->hwmon_dev = hwmon_device_register(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (IS_ERR(data->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		err = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		dev_err(&pdev->dev, "Class registration failed (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) exit_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (data->vrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int via_cputemp_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct via_cputemp_data *data = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (data->vrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static struct platform_driver via_cputemp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		.name = DRVNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.probe = via_cputemp_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.remove = via_cputemp_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct pdev_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static LIST_HEAD(pdev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static DEFINE_MUTEX(pdev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int via_cputemp_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct pdev_entry *pdev_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	pdev = platform_device_alloc(DRVNAME, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (!pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		pr_err("Device allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (!pdev_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		goto exit_device_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	err = platform_device_add(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		pr_err("Device addition failed (%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		goto exit_device_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	pdev_entry->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	pdev_entry->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	mutex_lock(&pdev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	list_add_tail(&pdev_entry->list, &pdev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	mutex_unlock(&pdev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) exit_device_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	kfree(pdev_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) exit_device_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	platform_device_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int via_cputemp_down_prep(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct pdev_entry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	mutex_lock(&pdev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	list_for_each_entry(p, &pdev_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		if (p->cpu == cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			platform_device_unregister(p->pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			list_del(&p->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			mutex_unlock(&pdev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	mutex_unlock(&pdev_list_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static const struct x86_cpu_id __initconst cputemp_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 6, X86_CENTAUR_FAM6_C7_A,	NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 6, X86_CENTAUR_FAM6_C7_D,	NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 6, X86_CENTAUR_FAM6_NANO,	NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 7, X86_MODEL_ANY,		NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	{}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) MODULE_DEVICE_TABLE(x86cpu, cputemp_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static enum cpuhp_state via_temp_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int __init via_cputemp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (!x86_match_cpu(cputemp_ids))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	err = platform_driver_register(&via_cputemp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hwmon/via:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 				via_cputemp_online, via_cputemp_down_prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		goto exit_driver_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	via_temp_online = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) #ifndef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (list_empty(&pdev_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		goto exit_hp_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #ifndef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) exit_hp_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	cpuhp_remove_state_nocalls(via_temp_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) exit_driver_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	platform_driver_unregister(&via_cputemp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static void __exit via_cputemp_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	cpuhp_remove_state(via_temp_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	platform_driver_unregister(&via_cputemp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) MODULE_DESCRIPTION("VIA CPU temperature monitor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) module_init(via_cputemp_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) module_exit(via_cputemp_exit)