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