xref: /openbmc/linux/drivers/hwmon/tmp108.c (revision d35ac6ac)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Texas Instruments TMP108 SMBus temperature sensor driver
3  *
4  * Copyright (C) 2016 John Muir <john@jmuir.com>
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/of.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/jiffies.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
20 
21 #define	DRIVER_NAME "tmp108"
22 
23 #define	TMP108_REG_TEMP		0x00
24 #define	TMP108_REG_CONF		0x01
25 #define	TMP108_REG_TLOW		0x02
26 #define	TMP108_REG_THIGH	0x03
27 
28 #define TMP108_TEMP_MIN_MC	-50000 /* Minimum millicelcius. */
29 #define TMP108_TEMP_MAX_MC	127937 /* Maximum millicelcius. */
30 
31 /* Configuration register bits.
32  * Note: these bit definitions are byte swapped.
33  */
34 #define TMP108_CONF_M0		0x0100 /* Sensor mode. */
35 #define TMP108_CONF_M1		0x0200
36 #define TMP108_CONF_TM		0x0400 /* Thermostat mode. */
37 #define TMP108_CONF_FL		0x0800 /* Watchdog flag - TLOW */
38 #define TMP108_CONF_FH		0x1000 /* Watchdog flag - THIGH */
39 #define TMP108_CONF_CR0		0x2000 /* Conversion rate. */
40 #define TMP108_CONF_CR1		0x4000
41 #define TMP108_CONF_ID		0x8000
42 #define TMP108_CONF_HYS0	0x0010 /* Hysteresis. */
43 #define TMP108_CONF_HYS1	0x0020
44 #define TMP108_CONF_POL		0x0080 /* Polarity of alert. */
45 
46 /* Defaults set by the hardware upon reset. */
47 #define TMP108_CONF_DEFAULTS		(TMP108_CONF_CR0 | TMP108_CONF_TM |\
48 					 TMP108_CONF_HYS0 | TMP108_CONF_M1)
49 /* These bits are read-only. */
50 #define TMP108_CONF_READ_ONLY		(TMP108_CONF_FL | TMP108_CONF_FH |\
51 					 TMP108_CONF_ID)
52 
53 #define TMP108_CONF_MODE_MASK		(TMP108_CONF_M0|TMP108_CONF_M1)
54 #define TMP108_MODE_SHUTDOWN		0x0000
55 #define TMP108_MODE_ONE_SHOT		TMP108_CONF_M0
56 #define TMP108_MODE_CONTINUOUS		TMP108_CONF_M1		/* Default */
57 					/* When M1 is set, M0 is ignored. */
58 
59 #define TMP108_CONF_CONVRATE_MASK	(TMP108_CONF_CR0|TMP108_CONF_CR1)
60 #define TMP108_CONVRATE_0P25HZ		0x0000
61 #define TMP108_CONVRATE_1HZ		TMP108_CONF_CR0		/* Default */
62 #define TMP108_CONVRATE_4HZ		TMP108_CONF_CR1
63 #define TMP108_CONVRATE_16HZ		(TMP108_CONF_CR0|TMP108_CONF_CR1)
64 
65 #define TMP108_CONF_HYSTERESIS_MASK	(TMP108_CONF_HYS0|TMP108_CONF_HYS1)
66 #define TMP108_HYSTERESIS_0C		0x0000
67 #define TMP108_HYSTERESIS_1C		TMP108_CONF_HYS0	/* Default */
68 #define TMP108_HYSTERESIS_2C		TMP108_CONF_HYS1
69 #define TMP108_HYSTERESIS_4C		(TMP108_CONF_HYS0|TMP108_CONF_HYS1)
70 
71 #define TMP108_CONVERSION_TIME_MS	30	/* in milli-seconds */
72 
73 struct tmp108 {
74 	struct regmap *regmap;
75 	u16 orig_config;
76 	unsigned long ready_time;
77 };
78 
79 /* convert 12-bit TMP108 register value to milliCelsius */
80 static inline int tmp108_temp_reg_to_mC(s16 val)
81 {
82 	return (val & ~0x0f) * 1000 / 256;
83 }
84 
85 /* convert milliCelsius to left adjusted 12-bit TMP108 register value */
86 static inline u16 tmp108_mC_to_temp_reg(int val)
87 {
88 	return (val * 256) / 1000;
89 }
90 
91 static int tmp108_read(struct device *dev, enum hwmon_sensor_types type,
92 		       u32 attr, int channel, long *temp)
93 {
94 	struct tmp108 *tmp108 = dev_get_drvdata(dev);
95 	unsigned int regval;
96 	int err, hyst;
97 
98 	if (type == hwmon_chip) {
99 		if (attr == hwmon_chip_update_interval) {
100 			err = regmap_read(tmp108->regmap, TMP108_REG_CONF,
101 					  &regval);
102 			if (err < 0)
103 				return err;
104 			switch (regval & TMP108_CONF_CONVRATE_MASK) {
105 			case TMP108_CONVRATE_0P25HZ:
106 			default:
107 				*temp = 4000;
108 				break;
109 			case TMP108_CONVRATE_1HZ:
110 				*temp = 1000;
111 				break;
112 			case TMP108_CONVRATE_4HZ:
113 				*temp = 250;
114 				break;
115 			case TMP108_CONVRATE_16HZ:
116 				*temp = 63;
117 				break;
118 			}
119 			return 0;
120 		}
121 		return -EOPNOTSUPP;
122 	}
123 
124 	switch (attr) {
125 	case hwmon_temp_input:
126 		/* Is it too early to return a conversion ? */
127 		if (time_before(jiffies, tmp108->ready_time)) {
128 			dev_dbg(dev, "%s: Conversion not ready yet..\n",
129 				__func__);
130 			return -EAGAIN;
131 		}
132 		err = regmap_read(tmp108->regmap, TMP108_REG_TEMP, &regval);
133 		if (err < 0)
134 			return err;
135 		*temp = tmp108_temp_reg_to_mC(regval);
136 		break;
137 	case hwmon_temp_min:
138 	case hwmon_temp_max:
139 		err = regmap_read(tmp108->regmap, attr == hwmon_temp_min ?
140 				  TMP108_REG_TLOW : TMP108_REG_THIGH, &regval);
141 		if (err < 0)
142 			return err;
143 		*temp = tmp108_temp_reg_to_mC(regval);
144 		break;
145 	case hwmon_temp_min_alarm:
146 	case hwmon_temp_max_alarm:
147 		err = regmap_read(tmp108->regmap, TMP108_REG_CONF, &regval);
148 		if (err < 0)
149 			return err;
150 		*temp = !!(regval & (attr == hwmon_temp_min_alarm ?
151 				     TMP108_CONF_FL : TMP108_CONF_FH));
152 		break;
153 	case hwmon_temp_min_hyst:
154 	case hwmon_temp_max_hyst:
155 		err = regmap_read(tmp108->regmap, TMP108_REG_CONF, &regval);
156 		if (err < 0)
157 			return err;
158 		switch (regval & TMP108_CONF_HYSTERESIS_MASK) {
159 		case TMP108_HYSTERESIS_0C:
160 		default:
161 			hyst = 0;
162 			break;
163 		case TMP108_HYSTERESIS_1C:
164 			hyst = 1000;
165 			break;
166 		case TMP108_HYSTERESIS_2C:
167 			hyst = 2000;
168 			break;
169 		case TMP108_HYSTERESIS_4C:
170 			hyst = 4000;
171 			break;
172 		}
173 		err = regmap_read(tmp108->regmap, attr == hwmon_temp_min_hyst ?
174 				  TMP108_REG_TLOW : TMP108_REG_THIGH, &regval);
175 		if (err < 0)
176 			return err;
177 		*temp = tmp108_temp_reg_to_mC(regval);
178 		if (attr == hwmon_temp_min_hyst)
179 			*temp += hyst;
180 		else
181 			*temp -= hyst;
182 		break;
183 	default:
184 		return -EOPNOTSUPP;
185 	}
186 
187 	return 0;
188 }
189 
190 static int tmp108_write(struct device *dev, enum hwmon_sensor_types type,
191 			u32 attr, int channel, long temp)
192 {
193 	struct tmp108 *tmp108 = dev_get_drvdata(dev);
194 	u32 regval, mask;
195 	int err;
196 
197 	if (type == hwmon_chip) {
198 		if (attr == hwmon_chip_update_interval) {
199 			if (temp < 156)
200 				mask = TMP108_CONVRATE_16HZ;
201 			else if (temp < 625)
202 				mask = TMP108_CONVRATE_4HZ;
203 			else if (temp < 2500)
204 				mask = TMP108_CONVRATE_1HZ;
205 			else
206 				mask = TMP108_CONVRATE_0P25HZ;
207 			return regmap_update_bits(tmp108->regmap,
208 						  TMP108_REG_CONF,
209 						  TMP108_CONF_CONVRATE_MASK,
210 						  mask);
211 		}
212 		return -EOPNOTSUPP;
213 	}
214 
215 	switch (attr) {
216 	case hwmon_temp_min:
217 	case hwmon_temp_max:
218 		temp = clamp_val(temp, TMP108_TEMP_MIN_MC, TMP108_TEMP_MAX_MC);
219 		return regmap_write(tmp108->regmap,
220 				    attr == hwmon_temp_min ?
221 					TMP108_REG_TLOW : TMP108_REG_THIGH,
222 				    tmp108_mC_to_temp_reg(temp));
223 	case hwmon_temp_min_hyst:
224 	case hwmon_temp_max_hyst:
225 		temp = clamp_val(temp, TMP108_TEMP_MIN_MC, TMP108_TEMP_MAX_MC);
226 		err = regmap_read(tmp108->regmap,
227 				  attr == hwmon_temp_min_hyst ?
228 					TMP108_REG_TLOW : TMP108_REG_THIGH,
229 				  &regval);
230 		if (err < 0)
231 			return err;
232 		if (attr == hwmon_temp_min_hyst)
233 			temp -= tmp108_temp_reg_to_mC(regval);
234 		else
235 			temp = tmp108_temp_reg_to_mC(regval) - temp;
236 		if (temp < 500)
237 			mask = TMP108_HYSTERESIS_0C;
238 		else if (temp < 1500)
239 			mask = TMP108_HYSTERESIS_1C;
240 		else if (temp < 3000)
241 			mask = TMP108_HYSTERESIS_2C;
242 		else
243 			mask = TMP108_HYSTERESIS_4C;
244 		return regmap_update_bits(tmp108->regmap, TMP108_REG_CONF,
245 					  TMP108_CONF_HYSTERESIS_MASK, mask);
246 	default:
247 		return -EOPNOTSUPP;
248 	}
249 }
250 
251 static umode_t tmp108_is_visible(const void *data, enum hwmon_sensor_types type,
252 				 u32 attr, int channel)
253 {
254 	if (type == hwmon_chip && attr == hwmon_chip_update_interval)
255 		return 0644;
256 
257 	if (type != hwmon_temp)
258 		return 0;
259 
260 	switch (attr) {
261 	case hwmon_temp_input:
262 	case hwmon_temp_min_alarm:
263 	case hwmon_temp_max_alarm:
264 		return 0444;
265 	case hwmon_temp_min:
266 	case hwmon_temp_max:
267 	case hwmon_temp_min_hyst:
268 	case hwmon_temp_max_hyst:
269 		return 0644;
270 	default:
271 		return 0;
272 	}
273 }
274 
275 static const struct hwmon_channel_info * const tmp108_info[] = {
276 	HWMON_CHANNEL_INFO(chip,
277 			   HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
278 	HWMON_CHANNEL_INFO(temp,
279 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MIN |
280 			   HWMON_T_MIN_HYST | HWMON_T_MAX_HYST |
281 			   HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM),
282 	NULL
283 };
284 
285 static const struct hwmon_ops tmp108_hwmon_ops = {
286 	.is_visible = tmp108_is_visible,
287 	.read = tmp108_read,
288 	.write = tmp108_write,
289 };
290 
291 static const struct hwmon_chip_info tmp108_chip_info = {
292 	.ops = &tmp108_hwmon_ops,
293 	.info = tmp108_info,
294 };
295 
296 static void tmp108_restore_config(void *data)
297 {
298 	struct tmp108 *tmp108 = data;
299 
300 	regmap_write(tmp108->regmap, TMP108_REG_CONF, tmp108->orig_config);
301 }
302 
303 static bool tmp108_is_writeable_reg(struct device *dev, unsigned int reg)
304 {
305 	return reg != TMP108_REG_TEMP;
306 }
307 
308 static bool tmp108_is_volatile_reg(struct device *dev, unsigned int reg)
309 {
310 	/* Configuration register must be volatile to enable FL and FH. */
311 	return reg == TMP108_REG_TEMP || reg == TMP108_REG_CONF;
312 }
313 
314 static const struct regmap_config tmp108_regmap_config = {
315 	.reg_bits = 8,
316 	.val_bits = 16,
317 	.max_register = TMP108_REG_THIGH,
318 	.writeable_reg = tmp108_is_writeable_reg,
319 	.volatile_reg = tmp108_is_volatile_reg,
320 	.val_format_endian = REGMAP_ENDIAN_BIG,
321 	.cache_type = REGCACHE_MAPLE,
322 	.use_single_read = true,
323 	.use_single_write = true,
324 };
325 
326 static int tmp108_probe(struct i2c_client *client)
327 {
328 	struct device *dev = &client->dev;
329 	struct device *hwmon_dev;
330 	struct tmp108 *tmp108;
331 	int err;
332 	u32 config;
333 
334 	if (!i2c_check_functionality(client->adapter,
335 				     I2C_FUNC_SMBUS_WORD_DATA)) {
336 		dev_err(dev,
337 			"adapter doesn't support SMBus word transactions\n");
338 		return -ENODEV;
339 	}
340 
341 	tmp108 = devm_kzalloc(dev, sizeof(*tmp108), GFP_KERNEL);
342 	if (!tmp108)
343 		return -ENOMEM;
344 
345 	dev_set_drvdata(dev, tmp108);
346 
347 	tmp108->regmap = devm_regmap_init_i2c(client, &tmp108_regmap_config);
348 	if (IS_ERR(tmp108->regmap)) {
349 		err = PTR_ERR(tmp108->regmap);
350 		dev_err(dev, "regmap init failed: %d", err);
351 		return err;
352 	}
353 
354 	err = regmap_read(tmp108->regmap, TMP108_REG_CONF, &config);
355 	if (err < 0) {
356 		dev_err(dev, "error reading config register: %d", err);
357 		return err;
358 	}
359 	tmp108->orig_config = config;
360 
361 	/* Only continuous mode is supported. */
362 	config &= ~TMP108_CONF_MODE_MASK;
363 	config |= TMP108_MODE_CONTINUOUS;
364 
365 	/* Only comparator mode is supported. */
366 	config &= ~TMP108_CONF_TM;
367 
368 	err = regmap_write(tmp108->regmap, TMP108_REG_CONF, config);
369 	if (err < 0) {
370 		dev_err(dev, "error writing config register: %d", err);
371 		return err;
372 	}
373 
374 	tmp108->ready_time = jiffies;
375 	if ((tmp108->orig_config & TMP108_CONF_MODE_MASK) ==
376 	    TMP108_MODE_SHUTDOWN)
377 		tmp108->ready_time +=
378 			msecs_to_jiffies(TMP108_CONVERSION_TIME_MS);
379 
380 	err = devm_add_action_or_reset(dev, tmp108_restore_config, tmp108);
381 	if (err) {
382 		dev_err(dev, "add action or reset failed: %d", err);
383 		return err;
384 	}
385 
386 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
387 							 tmp108,
388 							 &tmp108_chip_info,
389 							 NULL);
390 	return PTR_ERR_OR_ZERO(hwmon_dev);
391 }
392 
393 static int tmp108_suspend(struct device *dev)
394 {
395 	struct tmp108 *tmp108 = dev_get_drvdata(dev);
396 
397 	return regmap_update_bits(tmp108->regmap, TMP108_REG_CONF,
398 				  TMP108_CONF_MODE_MASK, TMP108_MODE_SHUTDOWN);
399 }
400 
401 static int tmp108_resume(struct device *dev)
402 {
403 	struct tmp108 *tmp108 = dev_get_drvdata(dev);
404 	int err;
405 
406 	err = regmap_update_bits(tmp108->regmap, TMP108_REG_CONF,
407 				 TMP108_CONF_MODE_MASK, TMP108_MODE_CONTINUOUS);
408 	tmp108->ready_time = jiffies +
409 			     msecs_to_jiffies(TMP108_CONVERSION_TIME_MS);
410 	return err;
411 }
412 
413 static DEFINE_SIMPLE_DEV_PM_OPS(tmp108_dev_pm_ops, tmp108_suspend, tmp108_resume);
414 
415 static const struct i2c_device_id tmp108_i2c_ids[] = {
416 	{ "tmp108", 0 },
417 	{ }
418 };
419 MODULE_DEVICE_TABLE(i2c, tmp108_i2c_ids);
420 
421 #ifdef CONFIG_OF
422 static const struct of_device_id tmp108_of_ids[] = {
423 	{ .compatible = "ti,tmp108", },
424 	{}
425 };
426 MODULE_DEVICE_TABLE(of, tmp108_of_ids);
427 #endif
428 
429 static struct i2c_driver tmp108_driver = {
430 	.driver = {
431 		.name	= DRIVER_NAME,
432 		.pm	= pm_sleep_ptr(&tmp108_dev_pm_ops),
433 		.of_match_table = of_match_ptr(tmp108_of_ids),
434 	},
435 	.probe		= tmp108_probe,
436 	.id_table	= tmp108_i2c_ids,
437 };
438 
439 module_i2c_driver(tmp108_driver);
440 
441 MODULE_AUTHOR("John Muir <john@jmuir.com>");
442 MODULE_DESCRIPTION("Texas Instruments TMP108 temperature sensor driver");
443 MODULE_LICENSE("GPL");
444