xref: /openbmc/linux/drivers/hwmon/ltq-cputemp.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
27074d0a9SFlorian Eckert /* Lantiq cpu temperature sensor driver
37074d0a9SFlorian Eckert  *
47074d0a9SFlorian Eckert  * Copyright (C) 2017 Florian Eckert <fe@dev.tdt.de>
57074d0a9SFlorian Eckert  */
67074d0a9SFlorian Eckert 
77074d0a9SFlorian Eckert #include <linux/bitops.h>
87074d0a9SFlorian Eckert #include <linux/delay.h>
97074d0a9SFlorian Eckert #include <linux/hwmon.h>
107074d0a9SFlorian Eckert #include <linux/hwmon-sysfs.h>
117074d0a9SFlorian Eckert #include <linux/init.h>
12*39f03438SRob Herring #include <linux/mod_devicetable.h>
137074d0a9SFlorian Eckert #include <linux/module.h>
14*39f03438SRob Herring #include <linux/platform_device.h>
157074d0a9SFlorian Eckert 
167074d0a9SFlorian Eckert #include <lantiq_soc.h>
177074d0a9SFlorian Eckert 
187074d0a9SFlorian Eckert /* gphy1 configuration register contains cpu temperature */
197074d0a9SFlorian Eckert #define CGU_GPHY1_CR   0x0040
207074d0a9SFlorian Eckert #define CGU_TEMP_PD    BIT(19)
217074d0a9SFlorian Eckert 
ltq_cputemp_enable(void)227074d0a9SFlorian Eckert static void ltq_cputemp_enable(void)
237074d0a9SFlorian Eckert {
247074d0a9SFlorian Eckert 	ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) | CGU_TEMP_PD, CGU_GPHY1_CR);
257074d0a9SFlorian Eckert }
267074d0a9SFlorian Eckert 
ltq_cputemp_disable(void * data)277074d0a9SFlorian Eckert static void ltq_cputemp_disable(void *data)
287074d0a9SFlorian Eckert {
297074d0a9SFlorian Eckert 	ltq_cgu_w32(ltq_cgu_r32(CGU_GPHY1_CR) & ~CGU_TEMP_PD, CGU_GPHY1_CR);
307074d0a9SFlorian Eckert }
317074d0a9SFlorian Eckert 
ltq_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * temp)327074d0a9SFlorian Eckert static int ltq_read(struct device *dev, enum hwmon_sensor_types type,
337074d0a9SFlorian Eckert 		    u32 attr, int channel, long *temp)
347074d0a9SFlorian Eckert {
357074d0a9SFlorian Eckert 	int value;
367074d0a9SFlorian Eckert 
377074d0a9SFlorian Eckert 	switch (attr) {
387074d0a9SFlorian Eckert 	case hwmon_temp_input:
397074d0a9SFlorian Eckert 		/* get the temperature including one decimal place */
407074d0a9SFlorian Eckert 		value = (ltq_cgu_r32(CGU_GPHY1_CR) >> 9) & 0x01FF;
417074d0a9SFlorian Eckert 		value = value * 5;
427074d0a9SFlorian Eckert 		/* range -38 to +154 °C, register value zero is -38.0 °C */
437074d0a9SFlorian Eckert 		value -= 380;
447074d0a9SFlorian Eckert 		/* scale temp to millidegree */
457074d0a9SFlorian Eckert 		value = value * 100;
467074d0a9SFlorian Eckert 		break;
477074d0a9SFlorian Eckert 	default:
487074d0a9SFlorian Eckert 		return -EOPNOTSUPP;
497074d0a9SFlorian Eckert 	}
507074d0a9SFlorian Eckert 
517074d0a9SFlorian Eckert 	*temp = value;
527074d0a9SFlorian Eckert 	return 0;
537074d0a9SFlorian Eckert }
547074d0a9SFlorian Eckert 
ltq_is_visible(const void * _data,enum hwmon_sensor_types type,u32 attr,int channel)557074d0a9SFlorian Eckert static umode_t ltq_is_visible(const void *_data, enum hwmon_sensor_types type,
567074d0a9SFlorian Eckert 			      u32 attr, int channel)
577074d0a9SFlorian Eckert {
587074d0a9SFlorian Eckert 	if (type != hwmon_temp)
597074d0a9SFlorian Eckert 		return 0;
607074d0a9SFlorian Eckert 
617074d0a9SFlorian Eckert 	switch (attr) {
627074d0a9SFlorian Eckert 	case hwmon_temp_input:
637074d0a9SFlorian Eckert 		return 0444;
647074d0a9SFlorian Eckert 	default:
657074d0a9SFlorian Eckert 		return 0;
667074d0a9SFlorian Eckert 	}
677074d0a9SFlorian Eckert }
687074d0a9SFlorian Eckert 
69894fce11SKrzysztof Kozlowski static const struct hwmon_channel_info * const ltq_info[] = {
70f4a407f4SGuenter Roeck 	HWMON_CHANNEL_INFO(chip,
71f4a407f4SGuenter Roeck 			   HWMON_C_REGISTER_TZ),
72f4a407f4SGuenter Roeck 	HWMON_CHANNEL_INFO(temp,
73f4a407f4SGuenter Roeck 			   HWMON_T_INPUT),
747074d0a9SFlorian Eckert 	NULL
757074d0a9SFlorian Eckert };
767074d0a9SFlorian Eckert 
777074d0a9SFlorian Eckert static const struct hwmon_ops ltq_hwmon_ops = {
787074d0a9SFlorian Eckert 	.is_visible = ltq_is_visible,
797074d0a9SFlorian Eckert 	.read = ltq_read,
807074d0a9SFlorian Eckert };
817074d0a9SFlorian Eckert 
827074d0a9SFlorian Eckert static const struct hwmon_chip_info ltq_chip_info = {
837074d0a9SFlorian Eckert 	.ops = &ltq_hwmon_ops,
847074d0a9SFlorian Eckert 	.info = ltq_info,
857074d0a9SFlorian Eckert };
867074d0a9SFlorian Eckert 
ltq_cputemp_probe(struct platform_device * pdev)877074d0a9SFlorian Eckert static int ltq_cputemp_probe(struct platform_device *pdev)
887074d0a9SFlorian Eckert {
897074d0a9SFlorian Eckert 	struct device *hwmon_dev;
907074d0a9SFlorian Eckert 	int err = 0;
917074d0a9SFlorian Eckert 
927074d0a9SFlorian Eckert 	/* available on vr9 v1.2 SoCs only */
937074d0a9SFlorian Eckert 	if (ltq_soc_type() != SOC_TYPE_VR9_2)
947074d0a9SFlorian Eckert 		return -ENODEV;
957074d0a9SFlorian Eckert 
967074d0a9SFlorian Eckert 	err = devm_add_action(&pdev->dev, ltq_cputemp_disable, NULL);
977074d0a9SFlorian Eckert 	if (err)
987074d0a9SFlorian Eckert 		return err;
997074d0a9SFlorian Eckert 
1007074d0a9SFlorian Eckert 	ltq_cputemp_enable();
1017074d0a9SFlorian Eckert 
1027074d0a9SFlorian Eckert 	hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
1037074d0a9SFlorian Eckert 							 "ltq_cputemp",
1047074d0a9SFlorian Eckert 							 NULL,
1057074d0a9SFlorian Eckert 							 &ltq_chip_info,
1067074d0a9SFlorian Eckert 							 NULL);
1077074d0a9SFlorian Eckert 
1087074d0a9SFlorian Eckert 	if (IS_ERR(hwmon_dev)) {
1097074d0a9SFlorian Eckert 		dev_err(&pdev->dev, "Failed to register as hwmon device");
1107074d0a9SFlorian Eckert 		return PTR_ERR(hwmon_dev);
1117074d0a9SFlorian Eckert 	}
1127074d0a9SFlorian Eckert 
1137074d0a9SFlorian Eckert 	return 0;
1147074d0a9SFlorian Eckert }
1157074d0a9SFlorian Eckert 
1167074d0a9SFlorian Eckert const struct of_device_id ltq_cputemp_match[] = {
1177074d0a9SFlorian Eckert 	{ .compatible = "lantiq,cputemp" },
1187074d0a9SFlorian Eckert 	{},
1197074d0a9SFlorian Eckert };
1207074d0a9SFlorian Eckert MODULE_DEVICE_TABLE(of, ltq_cputemp_match);
1217074d0a9SFlorian Eckert 
1227074d0a9SFlorian Eckert static struct platform_driver ltq_cputemp_driver = {
1237074d0a9SFlorian Eckert 	.probe = ltq_cputemp_probe,
1247074d0a9SFlorian Eckert 	.driver = {
1257074d0a9SFlorian Eckert 		.name = "ltq-cputemp",
1267074d0a9SFlorian Eckert 		.of_match_table = ltq_cputemp_match,
1277074d0a9SFlorian Eckert 	},
1287074d0a9SFlorian Eckert };
1297074d0a9SFlorian Eckert 
1307074d0a9SFlorian Eckert module_platform_driver(ltq_cputemp_driver);
1317074d0a9SFlorian Eckert 
1327074d0a9SFlorian Eckert MODULE_AUTHOR("Florian Eckert <fe@dev.tdt.de>");
1337074d0a9SFlorian Eckert MODULE_DESCRIPTION("Lantiq cpu temperature sensor driver");
1347074d0a9SFlorian Eckert MODULE_LICENSE("GPL");
135