Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / w83773g.c
blobe858093ac806912da9874f35659ef6152c7ba7d1
1 /*
2 * Copyright (C) 2017 IBM Corp.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * Driver for the Nuvoton W83773G SMBus temperature sensor IC.
10 * Supported models: W83773G
13 #include <linux/module.h>
14 #include <linux/init.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/of_device.h>
20 #include <linux/regmap.h>
22 /* W83773 has 3 channels */
23 #define W83773_CHANNELS 3
25 /* The W83773 registers */
26 #define W83773_CONVERSION_RATE_REG_READ 0x04
27 #define W83773_CONVERSION_RATE_REG_WRITE 0x0A
28 #define W83773_MANUFACTURER_ID_REG 0xFE
29 #define W83773_LOCAL_TEMP 0x00
31 static const u8 W83773_STATUS[2] = { 0x02, 0x17 };
33 static const u8 W83773_TEMP_LSB[2] = { 0x10, 0x25 };
34 static const u8 W83773_TEMP_MSB[2] = { 0x01, 0x24 };
36 static const u8 W83773_OFFSET_LSB[2] = { 0x12, 0x16 };
37 static const u8 W83773_OFFSET_MSB[2] = { 0x11, 0x15 };
39 /* this is the number of sensors in the device */
40 static const struct i2c_device_id w83773_id[] = {
41 { "w83773g" },
42 { }
45 MODULE_DEVICE_TABLE(i2c, w83773_id);
47 static const struct of_device_id w83773_of_match[] = {
49 .compatible = "nuvoton,w83773g"
51 { },
53 MODULE_DEVICE_TABLE(of, w83773_of_match);
55 static inline long temp_of_local(s8 reg)
57 return reg * 1000;
60 static inline long temp_of_remote(s8 hb, u8 lb)
62 return (hb << 3 | lb >> 5) * 125;
65 static int get_local_temp(struct regmap *regmap, long *val)
67 unsigned int regval;
68 int ret;
70 ret = regmap_read(regmap, W83773_LOCAL_TEMP, &regval);
71 if (ret < 0)
72 return ret;
74 *val = temp_of_local(regval);
75 return 0;
78 static int get_remote_temp(struct regmap *regmap, int index, long *val)
80 unsigned int regval_high;
81 unsigned int regval_low;
82 int ret;
84 ret = regmap_read(regmap, W83773_TEMP_MSB[index], &regval_high);
85 if (ret < 0)
86 return ret;
88 ret = regmap_read(regmap, W83773_TEMP_LSB[index], &regval_low);
89 if (ret < 0)
90 return ret;
92 *val = temp_of_remote(regval_high, regval_low);
93 return 0;
96 static int get_fault(struct regmap *regmap, int index, long *val)
98 unsigned int regval;
99 int ret;
101 ret = regmap_read(regmap, W83773_STATUS[index], &regval);
102 if (ret < 0)
103 return ret;
105 *val = (regval & 0x04) >> 2;
106 return 0;
109 static int get_offset(struct regmap *regmap, int index, long *val)
111 unsigned int regval_high;
112 unsigned int regval_low;
113 int ret;
115 ret = regmap_read(regmap, W83773_OFFSET_MSB[index], &regval_high);
116 if (ret < 0)
117 return ret;
119 ret = regmap_read(regmap, W83773_OFFSET_LSB[index], &regval_low);
120 if (ret < 0)
121 return ret;
123 *val = temp_of_remote(regval_high, regval_low);
124 return 0;
127 static int set_offset(struct regmap *regmap, int index, long val)
129 int ret;
130 u8 high_byte;
131 u8 low_byte;
133 val = clamp_val(val, -127825, 127825);
134 /* offset value equals to (high_byte << 3 | low_byte >> 5) * 125 */
135 val /= 125;
136 high_byte = val >> 3;
137 low_byte = (val & 0x07) << 5;
139 ret = regmap_write(regmap, W83773_OFFSET_MSB[index], high_byte);
140 if (ret < 0)
141 return ret;
143 return regmap_write(regmap, W83773_OFFSET_LSB[index], low_byte);
146 static int get_update_interval(struct regmap *regmap, long *val)
148 unsigned int regval;
149 int ret;
151 ret = regmap_read(regmap, W83773_CONVERSION_RATE_REG_READ, &regval);
152 if (ret < 0)
153 return ret;
155 *val = 16000 >> regval;
156 return 0;
159 static int set_update_interval(struct regmap *regmap, long val)
161 int rate;
164 * For valid rates, interval can be calculated as
165 * interval = (1 << (8 - rate)) * 62.5;
166 * Rounded rate is therefore
167 * rate = 8 - __fls(interval * 8 / (62.5 * 7));
168 * Use clamp_val() to avoid overflows, and to ensure valid input
169 * for __fls.
171 val = clamp_val(val, 62, 16000) * 10;
172 rate = 8 - __fls((val * 8 / (625 * 7)));
173 return regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, rate);
176 static int w83773_read(struct device *dev, enum hwmon_sensor_types type,
177 u32 attr, int channel, long *val)
179 struct regmap *regmap = dev_get_drvdata(dev);
181 if (type == hwmon_chip) {
182 if (attr == hwmon_chip_update_interval)
183 return get_update_interval(regmap, val);
184 return -EOPNOTSUPP;
187 switch (attr) {
188 case hwmon_temp_input:
189 if (channel == 0)
190 return get_local_temp(regmap, val);
191 return get_remote_temp(regmap, channel - 1, val);
192 case hwmon_temp_fault:
193 return get_fault(regmap, channel - 1, val);
194 case hwmon_temp_offset:
195 return get_offset(regmap, channel - 1, val);
196 default:
197 return -EOPNOTSUPP;
201 static int w83773_write(struct device *dev, enum hwmon_sensor_types type,
202 u32 attr, int channel, long val)
204 struct regmap *regmap = dev_get_drvdata(dev);
206 if (type == hwmon_chip && attr == hwmon_chip_update_interval)
207 return set_update_interval(regmap, val);
209 if (type == hwmon_temp && attr == hwmon_temp_offset)
210 return set_offset(regmap, channel - 1, val);
212 return -EOPNOTSUPP;
215 static umode_t w83773_is_visible(const void *data, enum hwmon_sensor_types type,
216 u32 attr, int channel)
218 switch (type) {
219 case hwmon_chip:
220 switch (attr) {
221 case hwmon_chip_update_interval:
222 return 0644;
224 break;
225 case hwmon_temp:
226 switch (attr) {
227 case hwmon_temp_input:
228 case hwmon_temp_fault:
229 return 0444;
230 case hwmon_temp_offset:
231 return 0644;
233 break;
234 default:
235 break;
237 return 0;
240 static const u32 w83773_chip_config[] = {
241 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
245 static const struct hwmon_channel_info w83773_chip = {
246 .type = hwmon_chip,
247 .config = w83773_chip_config,
250 static const u32 w83773_temp_config[] = {
251 HWMON_T_INPUT,
252 HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET,
253 HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_OFFSET,
257 static const struct hwmon_channel_info w83773_temp = {
258 .type = hwmon_temp,
259 .config = w83773_temp_config,
262 static const struct hwmon_channel_info *w83773_info[] = {
263 &w83773_chip,
264 &w83773_temp,
265 NULL
268 static const struct hwmon_ops w83773_ops = {
269 .is_visible = w83773_is_visible,
270 .read = w83773_read,
271 .write = w83773_write,
274 static const struct hwmon_chip_info w83773_chip_info = {
275 .ops = &w83773_ops,
276 .info = w83773_info,
279 static const struct regmap_config w83773_regmap_config = {
280 .reg_bits = 8,
281 .val_bits = 8,
284 static int w83773_probe(struct i2c_client *client,
285 const struct i2c_device_id *id)
287 struct device *dev = &client->dev;
288 struct device *hwmon_dev;
289 struct regmap *regmap;
290 int ret;
292 regmap = devm_regmap_init_i2c(client, &w83773_regmap_config);
293 if (IS_ERR(regmap)) {
294 dev_err(dev, "failed to allocate register map\n");
295 return PTR_ERR(regmap);
298 /* Set the conversion rate to 2 Hz */
299 ret = regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, 0x05);
300 if (ret < 0) {
301 dev_err(&client->dev, "error writing config rate register\n");
302 return ret;
305 i2c_set_clientdata(client, regmap);
307 hwmon_dev = devm_hwmon_device_register_with_info(dev,
308 client->name,
309 regmap,
310 &w83773_chip_info,
311 NULL);
312 return PTR_ERR_OR_ZERO(hwmon_dev);
315 static struct i2c_driver w83773_driver = {
316 .class = I2C_CLASS_HWMON,
317 .driver = {
318 .name = "w83773g",
319 .of_match_table = of_match_ptr(w83773_of_match),
321 .probe = w83773_probe,
322 .id_table = w83773_id,
325 module_i2c_driver(w83773_driver);
327 MODULE_AUTHOR("Lei YU <mine260309@gmail.com>");
328 MODULE_DESCRIPTION("W83773G temperature sensor driver");
329 MODULE_LICENSE("GPL");