xref: /linux/drivers/hwmon/w83773g.c (revision 39f034386fc023780a505ed02ca40e57e4031e23)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ee249f27SLei YU /*
3ee249f27SLei YU  * Copyright (C) 2017 IBM Corp.
4ee249f27SLei YU  *
5ee249f27SLei YU  * Driver for the Nuvoton W83773G SMBus temperature sensor IC.
6ee249f27SLei YU  * Supported models: W83773G
7ee249f27SLei YU  */
8ee249f27SLei YU 
9ee249f27SLei YU #include <linux/module.h>
10ee249f27SLei YU #include <linux/init.h>
11ee249f27SLei YU #include <linux/i2c.h>
12ee249f27SLei YU #include <linux/hwmon.h>
13ee249f27SLei YU #include <linux/hwmon-sysfs.h>
14ee249f27SLei YU #include <linux/err.h>
15*39f03438SRob Herring #include <linux/of.h>
16ee249f27SLei YU #include <linux/regmap.h>
17ee249f27SLei YU 
18ee249f27SLei YU /* W83773 has 3 channels */
19ee249f27SLei YU #define W83773_CHANNELS				3
20ee249f27SLei YU 
21ee249f27SLei YU /* The W83773 registers */
22ee249f27SLei YU #define W83773_CONVERSION_RATE_REG_READ		0x04
23ee249f27SLei YU #define W83773_CONVERSION_RATE_REG_WRITE	0x0A
24ee249f27SLei YU #define W83773_MANUFACTURER_ID_REG		0xFE
25ee249f27SLei YU #define W83773_LOCAL_TEMP			0x00
26ee249f27SLei YU 
27ee249f27SLei YU static const u8 W83773_STATUS[2] = { 0x02, 0x17 };
28ee249f27SLei YU 
29ee249f27SLei YU static const u8 W83773_TEMP_LSB[2] = { 0x10, 0x25 };
30ee249f27SLei YU static const u8 W83773_TEMP_MSB[2] = { 0x01, 0x24 };
31ee249f27SLei YU 
32ee249f27SLei YU static const u8 W83773_OFFSET_LSB[2] = { 0x12, 0x16 };
33ee249f27SLei YU static const u8 W83773_OFFSET_MSB[2] = { 0x11, 0x15 };
34ee249f27SLei YU 
35ee249f27SLei YU /* this is the number of sensors in the device */
36ee249f27SLei YU static const struct i2c_device_id w83773_id[] = {
37ee249f27SLei YU 	{ "w83773g" },
38ee249f27SLei YU 	{ }
39ee249f27SLei YU };
40ee249f27SLei YU 
41ee249f27SLei YU MODULE_DEVICE_TABLE(i2c, w83773_id);
42ee249f27SLei YU 
43acb614a3SGuenter Roeck static const struct of_device_id __maybe_unused w83773_of_match[] = {
44ee249f27SLei YU 	{
45ee249f27SLei YU 		.compatible = "nuvoton,w83773g"
46ee249f27SLei YU 	},
47ee249f27SLei YU 	{ },
48ee249f27SLei YU };
49ee249f27SLei YU MODULE_DEVICE_TABLE(of, w83773_of_match);
50ee249f27SLei YU 
51ee249f27SLei YU static inline long temp_of_local(s8 reg)
52ee249f27SLei YU {
53ee249f27SLei YU 	return reg * 1000;
54ee249f27SLei YU }
55ee249f27SLei YU 
56ee249f27SLei YU static inline long temp_of_remote(s8 hb, u8 lb)
57ee249f27SLei YU {
58ee249f27SLei YU 	return (hb << 3 | lb >> 5) * 125;
59ee249f27SLei YU }
60ee249f27SLei YU 
61ee249f27SLei YU static int get_local_temp(struct regmap *regmap, long *val)
62ee249f27SLei YU {
63ee249f27SLei YU 	unsigned int regval;
64ee249f27SLei YU 	int ret;
65ee249f27SLei YU 
66ee249f27SLei YU 	ret = regmap_read(regmap, W83773_LOCAL_TEMP, &regval);
67ee249f27SLei YU 	if (ret < 0)
68ee249f27SLei YU 		return ret;
69ee249f27SLei YU 
70ee249f27SLei YU 	*val = temp_of_local(regval);
71ee249f27SLei YU 	return 0;
72ee249f27SLei YU }
73ee249f27SLei YU 
74ee249f27SLei YU static int get_remote_temp(struct regmap *regmap, int index, long *val)
75ee249f27SLei YU {
76ee249f27SLei YU 	unsigned int regval_high;
77ee249f27SLei YU 	unsigned int regval_low;
78ee249f27SLei YU 	int ret;
79ee249f27SLei YU 
80ee249f27SLei YU 	ret = regmap_read(regmap, W83773_TEMP_MSB[index], &regval_high);
81ee249f27SLei YU 	if (ret < 0)
82ee249f27SLei YU 		return ret;
83ee249f27SLei YU 
84ee249f27SLei YU 	ret = regmap_read(regmap, W83773_TEMP_LSB[index], &regval_low);
85ee249f27SLei YU 	if (ret < 0)
86ee249f27SLei YU 		return ret;
87ee249f27SLei YU 
88ee249f27SLei YU 	*val = temp_of_remote(regval_high, regval_low);
89ee249f27SLei YU 	return 0;
90ee249f27SLei YU }
91ee249f27SLei YU 
92ee249f27SLei YU static int get_fault(struct regmap *regmap, int index, long *val)
93ee249f27SLei YU {
94ee249f27SLei YU 	unsigned int regval;
95ee249f27SLei YU 	int ret;
96ee249f27SLei YU 
97ee249f27SLei YU 	ret = regmap_read(regmap, W83773_STATUS[index], &regval);
98ee249f27SLei YU 	if (ret < 0)
99ee249f27SLei YU 		return ret;
100ee249f27SLei YU 
101571e3f3aSGuenter Roeck 	*val = (regval & 0x04) >> 2;
102ee249f27SLei YU 	return 0;
103ee249f27SLei YU }
104ee249f27SLei YU 
105ee249f27SLei YU static int get_offset(struct regmap *regmap, int index, long *val)
106ee249f27SLei YU {
107ee249f27SLei YU 	unsigned int regval_high;
108ee249f27SLei YU 	unsigned int regval_low;
109ee249f27SLei YU 	int ret;
110ee249f27SLei YU 
111ee249f27SLei YU 	ret = regmap_read(regmap, W83773_OFFSET_MSB[index], &regval_high);
112ee249f27SLei YU 	if (ret < 0)
113ee249f27SLei YU 		return ret;
114ee249f27SLei YU 
115ee249f27SLei YU 	ret = regmap_read(regmap, W83773_OFFSET_LSB[index], &regval_low);
116ee249f27SLei YU 	if (ret < 0)
117ee249f27SLei YU 		return ret;
118ee249f27SLei YU 
119ee249f27SLei YU 	*val = temp_of_remote(regval_high, regval_low);
120ee249f27SLei YU 	return 0;
121ee249f27SLei YU }
122ee249f27SLei YU 
123ee249f27SLei YU static int set_offset(struct regmap *regmap, int index, long val)
124ee249f27SLei YU {
125ee249f27SLei YU 	int ret;
126ee249f27SLei YU 	u8 high_byte;
127ee249f27SLei YU 	u8 low_byte;
128ee249f27SLei YU 
129ee249f27SLei YU 	val = clamp_val(val, -127825, 127825);
130ee249f27SLei YU 	/* offset value equals to (high_byte << 3 | low_byte >> 5) * 125 */
131ee249f27SLei YU 	val /= 125;
132ee249f27SLei YU 	high_byte = val >> 3;
133ee249f27SLei YU 	low_byte = (val & 0x07) << 5;
134ee249f27SLei YU 
135ee249f27SLei YU 	ret = regmap_write(regmap, W83773_OFFSET_MSB[index], high_byte);
136ee249f27SLei YU 	if (ret < 0)
137ee249f27SLei YU 		return ret;
138ee249f27SLei YU 
139ee249f27SLei YU 	return regmap_write(regmap, W83773_OFFSET_LSB[index], low_byte);
140ee249f27SLei YU }
141ee249f27SLei YU 
142ee249f27SLei YU static int get_update_interval(struct regmap *regmap, long *val)
143ee249f27SLei YU {
144ee249f27SLei YU 	unsigned int regval;
145ee249f27SLei YU 	int ret;
146ee249f27SLei YU 
147ee249f27SLei YU 	ret = regmap_read(regmap, W83773_CONVERSION_RATE_REG_READ, &regval);
148ee249f27SLei YU 	if (ret < 0)
149ee249f27SLei YU 		return ret;
150ee249f27SLei YU 
151ee249f27SLei YU 	*val = 16000 >> regval;
152ee249f27SLei YU 	return 0;
153ee249f27SLei YU }
154ee249f27SLei YU 
155ee249f27SLei YU static int set_update_interval(struct regmap *regmap, long val)
156ee249f27SLei YU {
157ee249f27SLei YU 	int rate;
158ee249f27SLei YU 
159ee249f27SLei YU 	/*
160ee249f27SLei YU 	 * For valid rates, interval can be calculated as
161ee249f27SLei YU 	 *	interval = (1 << (8 - rate)) * 62.5;
162ee249f27SLei YU 	 * Rounded rate is therefore
163ee249f27SLei YU 	 *	rate = 8 - __fls(interval * 8 / (62.5 * 7));
164ee249f27SLei YU 	 * Use clamp_val() to avoid overflows, and to ensure valid input
165ee249f27SLei YU 	 * for __fls.
166ee249f27SLei YU 	 */
167ee249f27SLei YU 	val = clamp_val(val, 62, 16000) * 10;
168ee249f27SLei YU 	rate = 8 - __fls((val * 8 / (625 * 7)));
169ee249f27SLei YU 	return regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, rate);
170ee249f27SLei YU }
171ee249f27SLei YU 
172ee249f27SLei YU static int w83773_read(struct device *dev, enum hwmon_sensor_types type,
173ee249f27SLei YU 		       u32 attr, int channel, long *val)
174ee249f27SLei YU {
175ee249f27SLei YU 	struct regmap *regmap = dev_get_drvdata(dev);
176ee249f27SLei YU 
177ee249f27SLei YU 	if (type == hwmon_chip) {
178ee249f27SLei YU 		if (attr == hwmon_chip_update_interval)
179ee249f27SLei YU 			return get_update_interval(regmap, val);
180ee249f27SLei YU 		return -EOPNOTSUPP;
181ee249f27SLei YU 	}
182ee249f27SLei YU 
183ee249f27SLei YU 	switch (attr) {
184ee249f27SLei YU 	case hwmon_temp_input:
185ee249f27SLei YU 		if (channel == 0)
186ee249f27SLei YU 			return get_local_temp(regmap, val);
187ee249f27SLei YU 		return get_remote_temp(regmap, channel - 1, val);
188ee249f27SLei YU 	case hwmon_temp_fault:
189ee249f27SLei YU 		return get_fault(regmap, channel - 1, val);
190ee249f27SLei YU 	case hwmon_temp_offset:
191ee249f27SLei YU 		return get_offset(regmap, channel - 1, val);
192ee249f27SLei YU 	default:
193ee249f27SLei YU 		return -EOPNOTSUPP;
194ee249f27SLei YU 	}
195ee249f27SLei YU }
196ee249f27SLei YU 
197ee249f27SLei YU static int w83773_write(struct device *dev, enum hwmon_sensor_types type,
198ee249f27SLei YU 			u32 attr, int channel, long val)
199ee249f27SLei YU {
200ee249f27SLei YU 	struct regmap *regmap = dev_get_drvdata(dev);
201ee249f27SLei YU 
202ee249f27SLei YU 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
203ee249f27SLei YU 		return set_update_interval(regmap, val);
204ee249f27SLei YU 
205ee249f27SLei YU 	if (type == hwmon_temp && attr == hwmon_temp_offset)
206ee249f27SLei YU 		return set_offset(regmap, channel - 1, val);
207ee249f27SLei YU 
208ee249f27SLei YU 	return -EOPNOTSUPP;
209ee249f27SLei YU }
210ee249f27SLei YU 
211ee249f27SLei YU static umode_t w83773_is_visible(const void *data, enum hwmon_sensor_types type,
212ee249f27SLei YU 				 u32 attr, int channel)
213ee249f27SLei YU {
214ee249f27SLei YU 	switch (type) {
215ee249f27SLei YU 	case hwmon_chip:
216ee249f27SLei YU 		switch (attr) {
217ee249f27SLei YU 		case hwmon_chip_update_interval:
218ee249f27SLei YU 			return 0644;
219ee249f27SLei YU 		}
220ee249f27SLei YU 		break;
221ee249f27SLei YU 	case hwmon_temp:
222ee249f27SLei YU 		switch (attr) {
223ee249f27SLei YU 		case hwmon_temp_input:
224ee249f27SLei YU 		case hwmon_temp_fault:
225ee249f27SLei YU 			return 0444;
226ee249f27SLei YU 		case hwmon_temp_offset:
227ee249f27SLei YU 			return 0644;
228ee249f27SLei YU 		}
229ee249f27SLei YU 		break;
230ee249f27SLei YU 	default:
231ee249f27SLei YU 		break;
232ee249f27SLei YU 	}
233ee249f27SLei YU 	return 0;
234ee249f27SLei YU }
235ee249f27SLei YU 
2363bf8437bSKrzysztof Kozlowski static const struct hwmon_channel_info * const w83773_info[] = {
2372f2defdaSGuenter Roeck 	HWMON_CHANNEL_INFO(chip,
2382f2defdaSGuenter Roeck 			   HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
2392f2defdaSGuenter Roeck 	HWMON_CHANNEL_INFO(temp,
240ee249f27SLei YU 			   HWMON_T_INPUT,
241ee249f27SLei YU 			   HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET,
2422f2defdaSGuenter Roeck 			   HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET),
243ee249f27SLei YU 	NULL
244ee249f27SLei YU };
245ee249f27SLei YU 
246ee249f27SLei YU static const struct hwmon_ops w83773_ops = {
247ee249f27SLei YU 	.is_visible = w83773_is_visible,
248ee249f27SLei YU 	.read = w83773_read,
249ee249f27SLei YU 	.write = w83773_write,
250ee249f27SLei YU };
251ee249f27SLei YU 
252ee249f27SLei YU static const struct hwmon_chip_info w83773_chip_info = {
253ee249f27SLei YU 	.ops = &w83773_ops,
254ee249f27SLei YU 	.info = w83773_info,
255ee249f27SLei YU };
256ee249f27SLei YU 
257ee249f27SLei YU static const struct regmap_config w83773_regmap_config = {
258ee249f27SLei YU 	.reg_bits = 8,
259ee249f27SLei YU 	.val_bits = 8,
260ee249f27SLei YU };
261ee249f27SLei YU 
26267487038SStephen Kitt static int w83773_probe(struct i2c_client *client)
263ee249f27SLei YU {
264ee249f27SLei YU 	struct device *dev = &client->dev;
265ee249f27SLei YU 	struct device *hwmon_dev;
266ee249f27SLei YU 	struct regmap *regmap;
267ee249f27SLei YU 	int ret;
268ee249f27SLei YU 
269ee249f27SLei YU 	regmap = devm_regmap_init_i2c(client, &w83773_regmap_config);
270ee249f27SLei YU 	if (IS_ERR(regmap)) {
271ee249f27SLei YU 		dev_err(dev, "failed to allocate register map\n");
272ee249f27SLei YU 		return PTR_ERR(regmap);
273ee249f27SLei YU 	}
274ee249f27SLei YU 
275ee249f27SLei YU 	/* Set the conversion rate to 2 Hz */
276ee249f27SLei YU 	ret = regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, 0x05);
277ee249f27SLei YU 	if (ret < 0) {
278ee249f27SLei YU 		dev_err(&client->dev, "error writing config rate register\n");
279ee249f27SLei YU 		return ret;
280ee249f27SLei YU 	}
281ee249f27SLei YU 
282ee249f27SLei YU 	i2c_set_clientdata(client, regmap);
283ee249f27SLei YU 
284ee249f27SLei YU 	hwmon_dev = devm_hwmon_device_register_with_info(dev,
285ee249f27SLei YU 							 client->name,
286ee249f27SLei YU 							 regmap,
287ee249f27SLei YU 							 &w83773_chip_info,
288ee249f27SLei YU 							 NULL);
289ee249f27SLei YU 	return PTR_ERR_OR_ZERO(hwmon_dev);
290ee249f27SLei YU }
291ee249f27SLei YU 
292ee249f27SLei YU static struct i2c_driver w83773_driver = {
293ee249f27SLei YU 	.class = I2C_CLASS_HWMON,
294ee249f27SLei YU 	.driver = {
295ee249f27SLei YU 		.name	= "w83773g",
296ee249f27SLei YU 		.of_match_table = of_match_ptr(w83773_of_match),
297ee249f27SLei YU 	},
2981975d167SUwe Kleine-König 	.probe = w83773_probe,
299ee249f27SLei YU 	.id_table = w83773_id,
300ee249f27SLei YU };
301ee249f27SLei YU 
302ee249f27SLei YU module_i2c_driver(w83773_driver);
303ee249f27SLei YU 
304ee249f27SLei YU MODULE_AUTHOR("Lei YU <mine260309@gmail.com>");
305ee249f27SLei YU MODULE_DESCRIPTION("W83773G temperature sensor driver");
306ee249f27SLei YU MODULE_LICENSE("GPL");
307