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