xref: /linux/drivers/hwmon/max1668.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2011 David George <david.george@ska.ac.za>
4  *
5  * based on adm1021.c
6  * some credit to Christoph Scheurer, but largely a rewrite
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/bits.h>
11 #include <linux/err.h>
12 #include <linux/hwmon.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
18 
19 /* Addresses to scan */
20 static const unsigned short max1668_addr_list[] = {
21 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
22 
23 /* max1668 registers */
24 
25 #define MAX1668_REG_TEMP(nr)	(nr)
26 #define MAX1668_REG_STAT1	0x05
27 #define MAX1668_REG_STAT2	0x06
28 #define MAX1668_REG_MAN_ID	0xfe
29 #define MAX1668_REG_DEV_ID	0xff
30 
31 /* limits */
32 
33 /* high limits */
34 #define MAX1668_REG_LIMH(nr)	(0x08 + 2 * (nr))
35 /* read low limits */
36 #define MAX1668_REG_LIML(nr)	(0x09 + 2 * (nr))
37 
38 /* manufacturer and device ID Constants */
39 #define MAN_ID_MAXIM		0x4d
40 #define DEV_ID_MAX1668		0x3
41 #define DEV_ID_MAX1805		0x5
42 #define DEV_ID_MAX1989		0xb
43 
44 /* read only mode module parameter */
45 static bool read_only;
46 module_param(read_only, bool, 0);
47 MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
48 
49 struct max1668_data {
50 	struct regmap *regmap;
51 	int channels;
52 };
53 
54 static int max1668_read(struct device *dev, enum hwmon_sensor_types type,
55 			u32 attr, int channel, long *val)
56 {
57 	struct max1668_data *data = dev_get_drvdata(dev);
58 	struct regmap *regmap = data->regmap;
59 	u32 regs[2] = { MAX1668_REG_STAT1, MAX1668_REG_TEMP(channel) };
60 	u8 regvals[2];
61 	u32 regval;
62 	int ret;
63 
64 	switch (attr) {
65 	case hwmon_temp_input:
66 		ret = regmap_read(regmap, MAX1668_REG_TEMP(channel), &regval);
67 		if (ret)
68 			return ret;
69 		*val = sign_extend32(regval, 7) * 1000;
70 		break;
71 	case hwmon_temp_min:
72 		ret = regmap_read(regmap, MAX1668_REG_LIML(channel), &regval);
73 		if (ret)
74 			return ret;
75 		*val = sign_extend32(regval, 7) * 1000;
76 		break;
77 	case hwmon_temp_max:
78 		ret = regmap_read(regmap, MAX1668_REG_LIMH(channel), &regval);
79 		if (ret)
80 			return ret;
81 		*val = sign_extend32(regval, 7) * 1000;
82 		break;
83 	case hwmon_temp_min_alarm:
84 		ret = regmap_read(regmap,
85 				  channel ? MAX1668_REG_STAT2 : MAX1668_REG_STAT1,
86 				  &regval);
87 		if (ret)
88 			return ret;
89 		if (channel)
90 			*val = !!(regval & BIT(9 - channel * 2));
91 		else
92 			*val = !!(regval & BIT(5));
93 		break;
94 	case hwmon_temp_max_alarm:
95 		ret = regmap_read(regmap,
96 				  channel ? MAX1668_REG_STAT2 : MAX1668_REG_STAT1,
97 				  &regval);
98 		if (ret)
99 			return ret;
100 		if (channel)
101 			*val = !!(regval & BIT(8 - channel * 2));
102 		else
103 			*val = !!(regval & BIT(6));
104 		break;
105 	case hwmon_temp_fault:
106 		ret = regmap_multi_reg_read(regmap, regs, regvals, 2);
107 		if (ret)
108 			return ret;
109 		*val = !!((regvals[0] & BIT(4)) && regvals[1] == 127);
110 		break;
111 	default:
112 		return -EOPNOTSUPP;
113 	}
114 	return 0;
115 }
116 
117 static int max1668_write(struct device *dev, enum hwmon_sensor_types type,
118 			 u32 attr, int channel, long val)
119 {
120 	struct max1668_data *data = dev_get_drvdata(dev);
121 	struct regmap *regmap = data->regmap;
122 
123 	val = clamp_val(val / 1000, -128, 127);
124 
125 	switch (attr) {
126 	case hwmon_temp_min:
127 		return regmap_write(regmap, MAX1668_REG_LIML(channel), val);
128 	case hwmon_temp_max:
129 		return regmap_write(regmap, MAX1668_REG_LIMH(channel), val);
130 	default:
131 		return -EOPNOTSUPP;
132 	}
133 }
134 
135 static umode_t max1668_is_visible(const void *_data, enum hwmon_sensor_types type,
136 				  u32 attr, int channel)
137 {
138 	const struct max1668_data *data = _data;
139 
140 	if (channel >= data->channels)
141 		return 0;
142 
143 	switch (attr) {
144 	case hwmon_temp_min:
145 	case hwmon_temp_max:
146 		return read_only ? 0444 : 0644;
147 	case hwmon_temp_input:
148 	case hwmon_temp_min_alarm:
149 	case hwmon_temp_max_alarm:
150 		return 0444;
151 	case hwmon_temp_fault:
152 		if (channel)
153 			return 0444;
154 		break;
155 	default:
156 		break;
157 	}
158 	return 0;
159 }
160 
161 static const struct hwmon_channel_info * const max1668_info[] = {
162 	HWMON_CHANNEL_INFO(temp,
163 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
164 			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM,
165 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
166 			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
167 			   HWMON_T_FAULT,
168 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
169 			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
170 			   HWMON_T_FAULT,
171 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
172 			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
173 			   HWMON_T_FAULT,
174 			   HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX |
175 			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
176 			   HWMON_T_FAULT),
177 	NULL
178 };
179 
180 static const struct hwmon_ops max1668_hwmon_ops = {
181 	.is_visible = max1668_is_visible,
182 	.read = max1668_read,
183 	.write = max1668_write,
184 };
185 
186 static const struct hwmon_chip_info max1668_chip_info = {
187 	.ops = &max1668_hwmon_ops,
188 	.info = max1668_info,
189 };
190 
191 /* Return 0 if detection is successful, -ENODEV otherwise */
192 static int max1668_detect(struct i2c_client *client,
193 			  struct i2c_board_info *info)
194 {
195 	struct i2c_adapter *adapter = client->adapter;
196 	const char *type_name;
197 	int man_id, dev_id;
198 
199 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
200 		return -ENODEV;
201 
202 	/* Check for unsupported part */
203 	man_id = i2c_smbus_read_byte_data(client, MAX1668_REG_MAN_ID);
204 	if (man_id != MAN_ID_MAXIM)
205 		return -ENODEV;
206 
207 	dev_id = i2c_smbus_read_byte_data(client, MAX1668_REG_DEV_ID);
208 	if (dev_id < 0)
209 		return -ENODEV;
210 
211 	type_name = NULL;
212 	if (dev_id == DEV_ID_MAX1668)
213 		type_name = "max1668";
214 	else if (dev_id == DEV_ID_MAX1805)
215 		type_name = "max1805";
216 	else if (dev_id == DEV_ID_MAX1989)
217 		type_name = "max1989";
218 
219 	if (!type_name)
220 		return -ENODEV;
221 
222 	strscpy(info->type, type_name, I2C_NAME_SIZE);
223 
224 	return 0;
225 }
226 
227 /* regmap */
228 
229 static int max1668_reg_read(void *context, unsigned int reg, unsigned int *val)
230 {
231 	int ret;
232 
233 	ret = i2c_smbus_read_byte_data(context, reg);
234 	if (ret < 0)
235 		return ret;
236 
237 	*val = ret;
238 	return 0;
239 }
240 
241 static int max1668_reg_write(void *context, unsigned int reg, unsigned int val)
242 {
243 	return i2c_smbus_write_byte_data(context, reg + 11, val);
244 }
245 
246 static bool max1668_regmap_is_volatile(struct device *dev, unsigned int reg)
247 {
248 	return reg <= MAX1668_REG_STAT2;
249 }
250 
251 static bool max1668_regmap_is_writeable(struct device *dev, unsigned int reg)
252 {
253 	return reg > MAX1668_REG_STAT2 && reg <= MAX1668_REG_LIML(4);
254 }
255 
256 static const struct regmap_config max1668_regmap_config = {
257 	.reg_bits = 8,
258 	.val_bits = 8,
259 	.cache_type = REGCACHE_MAPLE,
260 	.volatile_reg = max1668_regmap_is_volatile,
261 	.writeable_reg = max1668_regmap_is_writeable,
262 };
263 
264 static const struct regmap_bus max1668_regmap_bus = {
265 	.reg_write = max1668_reg_write,
266 	.reg_read = max1668_reg_read,
267 };
268 
269 static int max1668_probe(struct i2c_client *client)
270 {
271 	struct i2c_adapter *adapter = client->adapter;
272 	struct device *dev = &client->dev;
273 	struct device *hwmon_dev;
274 	struct max1668_data *data;
275 
276 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
277 		return -ENODEV;
278 
279 	data = devm_kzalloc(dev, sizeof(struct max1668_data), GFP_KERNEL);
280 	if (!data)
281 		return -ENOMEM;
282 
283 	data->regmap = devm_regmap_init(dev, &max1668_regmap_bus, client,
284 					&max1668_regmap_config);
285 	if (IS_ERR(data->regmap))
286 		return PTR_ERR(data->regmap);
287 
288 	data->channels = (uintptr_t)i2c_get_match_data(client);
289 
290 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
291 							 &max1668_chip_info, NULL);
292 	return PTR_ERR_OR_ZERO(hwmon_dev);
293 }
294 
295 static const struct i2c_device_id max1668_id[] = {
296 	{ "max1668", 5 },
297 	{ "max1805", 3 },
298 	{ "max1989", 5 },
299 	{ }
300 };
301 MODULE_DEVICE_TABLE(i2c, max1668_id);
302 
303 /* This is the driver that will be inserted */
304 static struct i2c_driver max1668_driver = {
305 	.class = I2C_CLASS_HWMON,
306 	.driver = {
307 		  .name	= "max1668",
308 		  },
309 	.probe = max1668_probe,
310 	.id_table = max1668_id,
311 	.detect	= max1668_detect,
312 	.address_list = max1668_addr_list,
313 };
314 
315 module_i2c_driver(max1668_driver);
316 
317 MODULE_AUTHOR("David George <david.george@ska.ac.za>");
318 MODULE_DESCRIPTION("MAX1668 remote temperature sensor driver");
319 MODULE_LICENSE("GPL");
320