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