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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Texas Instruments TMP103 SMBus temperature sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2014 Heiko Schocher <hs@denx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Based on:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Texas Instruments TMP102 SMBus temperature sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/i2c.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-sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define TMP103_TEMP_REG		0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define TMP103_CONF_REG		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define TMP103_TLOW_REG		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define TMP103_THIGH_REG	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define TMP103_CONF_M0		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define TMP103_CONF_M1		0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define TMP103_CONF_LC		0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define TMP103_CONF_FL		0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define TMP103_CONF_FH		0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define TMP103_CONF_CR0		0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define TMP103_CONF_CR1		0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define TMP103_CONF_ID		0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define TMP103_CONF_SD		(TMP103_CONF_M1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define TMP103_CONF_SD_MASK	(TMP103_CONF_M0 | TMP103_CONF_M1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define TMP103_CONFIG		(TMP103_CONF_CR1 | TMP103_CONF_M1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define TMP103_CONFIG_MASK	(TMP103_CONF_CR0 | TMP103_CONF_CR1 | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 				 TMP103_CONF_M0 | TMP103_CONF_M1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static inline int tmp103_reg_to_mc(s8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return val * 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static inline u8 tmp103_mc_to_reg(int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return DIV_ROUND_CLOSEST(val, 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static ssize_t tmp103_temp_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 				struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct regmap *regmap = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unsigned int regval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ret = regmap_read(regmap, sda->index, &regval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return sprintf(buf, "%d\n", tmp103_reg_to_mc(regval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static ssize_t tmp103_temp_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				 struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				 const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct regmap *regmap = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (kstrtol(buf, 10, &val) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	val = clamp_val(val, -55000, 127000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ret = regmap_write(regmap, sda->index, tmp103_mc_to_reg(val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static SENSOR_DEVICE_ATTR_RO(temp1_input, tmp103_temp, TMP103_TEMP_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static SENSOR_DEVICE_ATTR_RW(temp1_min, tmp103_temp, TMP103_TLOW_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static SENSOR_DEVICE_ATTR_RW(temp1_max, tmp103_temp, TMP103_THIGH_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static struct attribute *tmp103_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	&sensor_dev_attr_temp1_input.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	&sensor_dev_attr_temp1_min.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	&sensor_dev_attr_temp1_max.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) ATTRIBUTE_GROUPS(tmp103);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static bool tmp103_regmap_is_volatile(struct device *dev, unsigned int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return reg == TMP103_TEMP_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct regmap_config tmp103_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	.reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	.max_register = TMP103_THIGH_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	.volatile_reg = tmp103_regmap_is_volatile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int tmp103_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct device *dev = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	regmap = devm_regmap_init_i2c(client, &tmp103_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		dev_err(dev, "failed to allocate register map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	ret = regmap_update_bits(regmap, TMP103_CONF_REG, TMP103_CONFIG_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				 TMP103_CONFIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		dev_err(&client->dev, "error writing config register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	i2c_set_clientdata(client, regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 						      regmap, tmp103_groups);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	return PTR_ERR_OR_ZERO(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static int __maybe_unused tmp103_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct regmap *regmap = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return regmap_update_bits(regmap, TMP103_CONF_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				  TMP103_CONF_SD_MASK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int __maybe_unused tmp103_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	struct regmap *regmap = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return regmap_update_bits(regmap, TMP103_CONF_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				  TMP103_CONF_SD_MASK, TMP103_CONF_SD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static SIMPLE_DEV_PM_OPS(tmp103_dev_pm_ops, tmp103_suspend, tmp103_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static const struct i2c_device_id tmp103_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	{ "tmp103", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) MODULE_DEVICE_TABLE(i2c, tmp103_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static const struct of_device_id __maybe_unused tmp103_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	{ .compatible = "ti,tmp103" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_DEVICE_TABLE(of, tmp103_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static struct i2c_driver tmp103_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		.name	= "tmp103",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		.of_match_table = of_match_ptr(tmp103_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		.pm	= &tmp103_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	.probe_new	= tmp103_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	.id_table	= tmp103_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) module_i2c_driver(tmp103_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) MODULE_AUTHOR("Heiko Schocher <hs@denx.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) MODULE_DESCRIPTION("Texas Instruments TMP103 temperature sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) MODULE_LICENSE("GPL");