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