xref: /openbmc/linux/drivers/hwmon/lm75.c (revision c851b715)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28d5d45fbSJean Delvare /*
3caaa0f36SShubhrajyoti D  * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
4caaa0f36SShubhrajyoti D  *	 monitoring
5caaa0f36SShubhrajyoti D  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
68d5d45fbSJean Delvare  */
78d5d45fbSJean Delvare 
88d5d45fbSJean Delvare #include <linux/module.h>
98d5d45fbSJean Delvare #include <linux/init.h>
108d5d45fbSJean Delvare #include <linux/slab.h>
118d5d45fbSJean Delvare #include <linux/jiffies.h>
128d5d45fbSJean Delvare #include <linux/i2c.h>
13943b0830SMark M. Hoffman #include <linux/hwmon.h>
149ca8e40cSJean Delvare #include <linux/hwmon-sysfs.h>
15943b0830SMark M. Hoffman #include <linux/err.h>
16e97a45f1SJavier Martinez Canillas #include <linux/of_device.h>
1722e73183SEduardo Valentin #include <linux/of.h>
18e65365feSGuenter Roeck #include <linux/regmap.h>
194b5be3c1SIker Perez del Palomar Sustatxa #include <linux/util_macros.h>
20707d151bSAlban Bedel #include <linux/regulator/consumer.h>
218d5d45fbSJean Delvare #include "lm75.h"
228d5d45fbSJean Delvare 
2301a52397SDavid Brownell /*
2401a52397SDavid Brownell  * This driver handles the LM75 and compatible digital temperature sensors.
2501a52397SDavid Brownell  */
2601a52397SDavid Brownell 
279ebd3d82SDavid Brownell enum lm75_type {		/* keep sorted in alphabetical order */
28e96f9d89SMichael Hennerich 	adt75,
29*c851b715SPeter Rosin 	at30ts74,
301f86df49SJean Delvare 	ds1775,
319ebd3d82SDavid Brownell 	ds75,
323fbc81e3SJean Delvare 	ds7505,
33c98d6c65SArnaud Ebalard 	g751,
341f86df49SJean Delvare 	lm75,
359ebd3d82SDavid Brownell 	lm75a,
36799fc602SMichael Thalmeier 	lm75b,
379ebd3d82SDavid Brownell 	max6625,
389ebd3d82SDavid Brownell 	max6626,
39a54ca77aSKun Yi 	max31725,
409ebd3d82SDavid Brownell 	mcp980x,
41557c7ffaSDaniel Mack 	pct2075,
429ebd3d82SDavid Brownell 	stds75,
432e9a41bbSJagan Teki 	stlm75,
449ebd3d82SDavid Brownell 	tcn75,
459ebd3d82SDavid Brownell 	tmp100,
469ebd3d82SDavid Brownell 	tmp101,
476d034059SShubhrajyoti Datta 	tmp105,
48c83959f8SFrans Klaver 	tmp112,
499ebd3d82SDavid Brownell 	tmp175,
509ebd3d82SDavid Brownell 	tmp275,
519ebd3d82SDavid Brownell 	tmp75,
5239abe9d8SIker Perez del Palomar Sustatxa 	tmp75b,
539c32e815SBen Gardner 	tmp75c,
54ec081f91SRobert Marko 	tmp1075,
559ebd3d82SDavid Brownell };
569ebd3d82SDavid Brownell 
57dcb12653SIker Perez del Palomar Sustatxa /**
58dcb12653SIker Perez del Palomar Sustatxa  * struct lm75_params - lm75 configuration parameters.
59dcb12653SIker Perez del Palomar Sustatxa  * @set_mask:		Bits to set in configuration register when configuring
60dcb12653SIker Perez del Palomar Sustatxa  *			the chip.
61dcb12653SIker Perez del Palomar Sustatxa  * @clr_mask:		Bits to clear in configuration register when configuring
62dcb12653SIker Perez del Palomar Sustatxa  *			the chip.
63dcb12653SIker Perez del Palomar Sustatxa  * @default_resolution:	Default number of bits to represent the temperature
64dcb12653SIker Perez del Palomar Sustatxa  *			value.
65dcb12653SIker Perez del Palomar Sustatxa  * @resolution_limits:	Limit register resolution. Optional. Should be set if
66dcb12653SIker Perez del Palomar Sustatxa  *			the resolution of limit registers does not match the
67dcb12653SIker Perez del Palomar Sustatxa  *			resolution of the temperature register.
687f1a300fSIker Perez del Palomar Sustatxa  * @resolutions:	List of resolutions associated with sample times.
697f1a300fSIker Perez del Palomar Sustatxa  *			Optional. Should be set if num_sample_times is larger
707f1a300fSIker Perez del Palomar Sustatxa  *			than 1, and if the resolution changes with sample times.
717f1a300fSIker Perez del Palomar Sustatxa  *			If set, number of entries must match num_sample_times.
727f1a300fSIker Perez del Palomar Sustatxa  * @default_sample_time:Sample time to be set by default.
737f1a300fSIker Perez del Palomar Sustatxa  * @num_sample_times:	Number of possible sample times to be set. Optional.
747f1a300fSIker Perez del Palomar Sustatxa  *			Should be set if the number of sample times is larger
757f1a300fSIker Perez del Palomar Sustatxa  *			than one.
767f1a300fSIker Perez del Palomar Sustatxa  * @sample_times:	All the possible sample times to be set. Mandatory if
777f1a300fSIker Perez del Palomar Sustatxa  *			num_sample_times is larger than 1. If set, number of
787f1a300fSIker Perez del Palomar Sustatxa  *			entries must match num_sample_times.
79dcb12653SIker Perez del Palomar Sustatxa  */
80dcb12653SIker Perez del Palomar Sustatxa 
81dcb12653SIker Perez del Palomar Sustatxa struct lm75_params {
82dcb12653SIker Perez del Palomar Sustatxa 	u8			set_mask;
83dcb12653SIker Perez del Palomar Sustatxa 	u8			clr_mask;
84dcb12653SIker Perez del Palomar Sustatxa 	u8			default_resolution;
85dcb12653SIker Perez del Palomar Sustatxa 	u8			resolution_limits;
867f1a300fSIker Perez del Palomar Sustatxa 	const u8		*resolutions;
87dcb12653SIker Perez del Palomar Sustatxa 	unsigned int		default_sample_time;
887f1a300fSIker Perez del Palomar Sustatxa 	u8			num_sample_times;
897f1a300fSIker Perez del Palomar Sustatxa 	const unsigned int	*sample_times;
90dcb12653SIker Perez del Palomar Sustatxa };
91dcb12653SIker Perez del Palomar Sustatxa 
928ff69eebSJean Delvare /* Addresses scanned */
9325e9c86dSMark M. Hoffman static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
948d5d45fbSJean Delvare 					0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
958d5d45fbSJean Delvare 
968d5d45fbSJean Delvare /* The LM75 registers */
97e65365feSGuenter Roeck #define LM75_REG_TEMP		0x00
988d5d45fbSJean Delvare #define LM75_REG_CONF		0x01
99e65365feSGuenter Roeck #define LM75_REG_HYST		0x02
100e65365feSGuenter Roeck #define LM75_REG_MAX		0x03
101d7a85cdeSGuenter Roeck #define PCT2075_REG_IDLE	0x04
1028d5d45fbSJean Delvare 
1038d5d45fbSJean Delvare /* Each client has this additional data */
1048d5d45fbSJean Delvare struct lm75_data {
105d663ec49SGuenter Roeck 	struct i2c_client		*client;
106e65365feSGuenter Roeck 	struct regmap			*regmap;
107707d151bSAlban Bedel 	struct regulator		*vs;
1089ebd3d82SDavid Brownell 	u8				orig_conf;
109dcb12653SIker Perez del Palomar Sustatxa 	u8				current_conf;
110dcb12653SIker Perez del Palomar Sustatxa 	u8				resolution;	/* In bits, 9 to 16 */
111e65365feSGuenter Roeck 	unsigned int			sample_time;	/* In ms */
112dcb12653SIker Perez del Palomar Sustatxa 	enum lm75_type			kind;
113dcb12653SIker Perez del Palomar Sustatxa 	const struct lm75_params	*params;
1148d5d45fbSJean Delvare };
1158d5d45fbSJean Delvare 
11601a52397SDavid Brownell /*-----------------------------------------------------------------------*/
1177db0db3fSGuenter Roeck 
1187db0db3fSGuenter Roeck static const u8 lm75_sample_set_masks[] = { 0 << 5, 1 << 5, 2 << 5, 3 << 5 };
1197db0db3fSGuenter Roeck 
1207db0db3fSGuenter Roeck #define LM75_SAMPLE_CLEAR_MASK	(3 << 5)
1217db0db3fSGuenter Roeck 
122dcb12653SIker Perez del Palomar Sustatxa /* The structure below stores the configuration values of the supported devices.
123dcb12653SIker Perez del Palomar Sustatxa  * In case of being supported multiple configurations, the default one must
124dcb12653SIker Perez del Palomar Sustatxa  * always be the first element of the array
125dcb12653SIker Perez del Palomar Sustatxa  */
126dcb12653SIker Perez del Palomar Sustatxa static const struct lm75_params device_params[] = {
127dcb12653SIker Perez del Palomar Sustatxa 	[adt75] = {
128dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 5,	/* not one-shot mode */
129dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
13035cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 10,
131dcb12653SIker Perez del Palomar Sustatxa 	},
132*c851b715SPeter Rosin 	[at30ts74] = {
133*c851b715SPeter Rosin 		.set_mask = 3 << 5,	/* 12-bit mode*/
134*c851b715SPeter Rosin 		.default_resolution = 12,
135*c851b715SPeter Rosin 		.default_sample_time = 200,
136*c851b715SPeter Rosin 		.num_sample_times = 4,
137*c851b715SPeter Rosin 		.sample_times = (unsigned int []){ 25, 50, 100, 200 },
138*c851b715SPeter Rosin 		.resolutions = (u8 []) {9, 10, 11, 12 },
139*c851b715SPeter Rosin 	},
140dcb12653SIker Perez del Palomar Sustatxa 	[ds1775] = {
141dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 3 << 5,
142dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 2 << 5,	/* 11-bit mode */
143dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 11,
14435cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = 500,
1457db0db3fSGuenter Roeck 		.num_sample_times = 4,
14635cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 125, 250, 500, 1000 },
1477db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
148dcb12653SIker Perez del Palomar Sustatxa 	},
149dcb12653SIker Perez del Palomar Sustatxa 	[ds75] = {
150dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 3 << 5,
151dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 2 << 5,	/* 11-bit mode */
152dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 11,
1537db0db3fSGuenter Roeck 		.default_sample_time = 600,
1547db0db3fSGuenter Roeck 		.num_sample_times = 4,
1557db0db3fSGuenter Roeck 		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
1567db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
157dcb12653SIker Perez del Palomar Sustatxa 	},
158dcb12653SIker Perez del Palomar Sustatxa 	[stds75] = {
159dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 3 << 5,
160dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 2 << 5,	/* 11-bit mode */
161dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 11,
1627db0db3fSGuenter Roeck 		.default_sample_time = 600,
1637db0db3fSGuenter Roeck 		.num_sample_times = 4,
1647db0db3fSGuenter Roeck 		.sample_times = (unsigned int []){ 150, 300, 600, 1200 },
1657db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
166dcb12653SIker Perez del Palomar Sustatxa 	},
167dcb12653SIker Perez del Palomar Sustatxa 	[stlm75] = {
168dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 9,
16935cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 6,
170dcb12653SIker Perez del Palomar Sustatxa 	},
171dcb12653SIker Perez del Palomar Sustatxa 	[ds7505] = {
172dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode*/
173dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
1747db0db3fSGuenter Roeck 		.default_sample_time = 200,
1757db0db3fSGuenter Roeck 		.num_sample_times = 4,
1767db0db3fSGuenter Roeck 		.sample_times = (unsigned int []){ 25, 50, 100, 200 },
1777db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
178dcb12653SIker Perez del Palomar Sustatxa 	},
179dcb12653SIker Perez del Palomar Sustatxa 	[g751] = {
180dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 9,
18135cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 10,
182dcb12653SIker Perez del Palomar Sustatxa 	},
183dcb12653SIker Perez del Palomar Sustatxa 	[lm75] = {
184dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 9,
18535cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 10,
186dcb12653SIker Perez del Palomar Sustatxa 	},
187dcb12653SIker Perez del Palomar Sustatxa 	[lm75a] = {
188dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 9,
18935cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 10,
190dcb12653SIker Perez del Palomar Sustatxa 	},
191dcb12653SIker Perez del Palomar Sustatxa 	[lm75b] = {
192dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 11,
19335cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 10,
194dcb12653SIker Perez del Palomar Sustatxa 	},
195dcb12653SIker Perez del Palomar Sustatxa 	[max6625] = {
196dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 9,
19735cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 7,
198dcb12653SIker Perez del Palomar Sustatxa 	},
199dcb12653SIker Perez del Palomar Sustatxa 	[max6626] = {
200dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
20135cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 7,
202dcb12653SIker Perez del Palomar Sustatxa 		.resolution_limits = 9,
203dcb12653SIker Perez del Palomar Sustatxa 	},
204dcb12653SIker Perez del Palomar Sustatxa 	[max31725] = {
205dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 16,
20635cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 20,
207dcb12653SIker Perez del Palomar Sustatxa 	},
208dcb12653SIker Perez del Palomar Sustatxa 	[tcn75] = {
209dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 9,
21035cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 18,
211dcb12653SIker Perez del Palomar Sustatxa 	},
212dcb12653SIker Perez del Palomar Sustatxa 	[pct2075] = {
213dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 11,
214dcb12653SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 10,
215d7a85cdeSGuenter Roeck 		.num_sample_times = 31,
216d7a85cdeSGuenter Roeck 		.sample_times = (unsigned int []){ 100, 200, 300, 400, 500, 600,
217d7a85cdeSGuenter Roeck 		700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700,
218d7a85cdeSGuenter Roeck 		1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700,
219d7a85cdeSGuenter Roeck 		2800, 2900, 3000, 3100 },
220dcb12653SIker Perez del Palomar Sustatxa 	},
221dcb12653SIker Perez del Palomar Sustatxa 	[mcp980x] = {
222dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode */
223dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* not one-shot mode */
224dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
225dcb12653SIker Perez del Palomar Sustatxa 		.resolution_limits = 9,
2267db0db3fSGuenter Roeck 		.default_sample_time = 240,
2277db0db3fSGuenter Roeck 		.num_sample_times = 4,
22835cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 30, 60, 120, 240 },
2297db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
230dcb12653SIker Perez del Palomar Sustatxa 	},
231dcb12653SIker Perez del Palomar Sustatxa 	[tmp100] = {
232dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode */
233dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* not one-shot mode */
234dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
2357db0db3fSGuenter Roeck 		.default_sample_time = 320,
2367db0db3fSGuenter Roeck 		.num_sample_times = 4,
23735cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 40, 80, 160, 320 },
2387db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
239dcb12653SIker Perez del Palomar Sustatxa 	},
240dcb12653SIker Perez del Palomar Sustatxa 	[tmp101] = {
241dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode */
242dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* not one-shot mode */
243dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
2447db0db3fSGuenter Roeck 		.default_sample_time = 320,
2457db0db3fSGuenter Roeck 		.num_sample_times = 4,
24635cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 40, 80, 160, 320 },
2477db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
2487db0db3fSGuenter Roeck 	},
2497db0db3fSGuenter Roeck 	[tmp105] = {
2507db0db3fSGuenter Roeck 		.set_mask = 3 << 5,	/* 12-bit mode */
2517db0db3fSGuenter Roeck 		.clr_mask = 1 << 7,	/* not one-shot mode*/
2527db0db3fSGuenter Roeck 		.default_resolution = 12,
2537db0db3fSGuenter Roeck 		.default_sample_time = 220,
2547db0db3fSGuenter Roeck 		.num_sample_times = 4,
25535cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
2567db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
257dcb12653SIker Perez del Palomar Sustatxa 	},
258dcb12653SIker Perez del Palomar Sustatxa 	[tmp112] = {
25935cd1804SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 8 samples / second */
260dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* no one-shot mode*/
261dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
26235cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = 125,
263cee04a01SGuenter Roeck 		.num_sample_times = 4,
264cee04a01SGuenter Roeck 		.sample_times = (unsigned int []){ 125, 250, 1000, 4000 },
265dcb12653SIker Perez del Palomar Sustatxa 	},
266dcb12653SIker Perez del Palomar Sustatxa 	[tmp175] = {
267dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode */
268dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* not one-shot mode*/
269dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
2707db0db3fSGuenter Roeck 		.default_sample_time = 220,
2717db0db3fSGuenter Roeck 		.num_sample_times = 4,
27235cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
2737db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
274dcb12653SIker Perez del Palomar Sustatxa 	},
275dcb12653SIker Perez del Palomar Sustatxa 	[tmp275] = {
276dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode */
277dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* not one-shot mode*/
278dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
2797db0db3fSGuenter Roeck 		.default_sample_time = 220,
2807db0db3fSGuenter Roeck 		.num_sample_times = 4,
28135cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
2827db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
283dcb12653SIker Perez del Palomar Sustatxa 	},
284dcb12653SIker Perez del Palomar Sustatxa 	[tmp75] = {
285dcb12653SIker Perez del Palomar Sustatxa 		.set_mask = 3 << 5,	/* 12-bit mode */
286dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7,	/* not one-shot mode*/
287dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
2887db0db3fSGuenter Roeck 		.default_sample_time = 220,
2897db0db3fSGuenter Roeck 		.num_sample_times = 4,
29035cd1804SIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
2917db0db3fSGuenter Roeck 		.resolutions = (u8 []) {9, 10, 11, 12 },
292dcb12653SIker Perez del Palomar Sustatxa 	},
293dcb12653SIker Perez del Palomar Sustatxa 	[tmp75b] = { /* not one-shot mode, Conversion rate 37Hz */
294dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 7 | 3 << 5,
295dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
296dcb12653SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 37,
2977f1a300fSIker Perez del Palomar Sustatxa 		.sample_times = (unsigned int []){ MSEC_PER_SEC / 37,
2987f1a300fSIker Perez del Palomar Sustatxa 			MSEC_PER_SEC / 18,
2997f1a300fSIker Perez del Palomar Sustatxa 			MSEC_PER_SEC / 9, MSEC_PER_SEC / 4 },
3007f1a300fSIker Perez del Palomar Sustatxa 		.num_sample_times = 4,
301dcb12653SIker Perez del Palomar Sustatxa 	},
302dcb12653SIker Perez del Palomar Sustatxa 	[tmp75c] = {
303dcb12653SIker Perez del Palomar Sustatxa 		.clr_mask = 1 << 5,	/*not one-shot mode*/
304dcb12653SIker Perez del Palomar Sustatxa 		.default_resolution = 12,
30535cd1804SIker Perez del Palomar Sustatxa 		.default_sample_time = MSEC_PER_SEC / 12,
306ec081f91SRobert Marko 	},
307ec081f91SRobert Marko 	[tmp1075] = { /* not one-shot mode, 27.5 ms sample rate */
308ec081f91SRobert Marko 		.clr_mask = 1 << 5 | 1 << 6 | 1 << 7,
309ec081f91SRobert Marko 		.default_resolution = 12,
310ec081f91SRobert Marko 		.default_sample_time = 28,
311ec081f91SRobert Marko 		.num_sample_times = 4,
312ec081f91SRobert Marko 		.sample_times = (unsigned int []){ 28, 55, 110, 220 },
313dcb12653SIker Perez del Palomar Sustatxa 	}
314dcb12653SIker Perez del Palomar Sustatxa };
31501a52397SDavid Brownell 
31622e73183SEduardo Valentin static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
31722e73183SEduardo Valentin {
31822e73183SEduardo Valentin 	return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
31922e73183SEduardo Valentin }
32022e73183SEduardo Valentin 
32158608cfeSIker Perez del Palomar Sustatxa static int lm75_write_config(struct lm75_data *data, u8 set_mask,
32258608cfeSIker Perez del Palomar Sustatxa 			     u8 clr_mask)
32358608cfeSIker Perez del Palomar Sustatxa {
32458608cfeSIker Perez del Palomar Sustatxa 	u8 value;
32558608cfeSIker Perez del Palomar Sustatxa 
32658608cfeSIker Perez del Palomar Sustatxa 	clr_mask |= LM75_SHUTDOWN;
32758608cfeSIker Perez del Palomar Sustatxa 	value = data->current_conf & ~clr_mask;
32858608cfeSIker Perez del Palomar Sustatxa 	value |= set_mask;
32958608cfeSIker Perez del Palomar Sustatxa 
33058608cfeSIker Perez del Palomar Sustatxa 	if (data->current_conf != value) {
33158608cfeSIker Perez del Palomar Sustatxa 		s32 err;
33258608cfeSIker Perez del Palomar Sustatxa 
33358608cfeSIker Perez del Palomar Sustatxa 		err = i2c_smbus_write_byte_data(data->client, LM75_REG_CONF,
33458608cfeSIker Perez del Palomar Sustatxa 						value);
33558608cfeSIker Perez del Palomar Sustatxa 		if (err)
33658608cfeSIker Perez del Palomar Sustatxa 			return err;
33758608cfeSIker Perez del Palomar Sustatxa 		data->current_conf = value;
33858608cfeSIker Perez del Palomar Sustatxa 	}
33958608cfeSIker Perez del Palomar Sustatxa 	return 0;
34058608cfeSIker Perez del Palomar Sustatxa }
34158608cfeSIker Perez del Palomar Sustatxa 
34208b02433SGuenter Roeck static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
34308b02433SGuenter Roeck 		     u32 attr, int channel, long *val)
34422e73183SEduardo Valentin {
345e65365feSGuenter Roeck 	struct lm75_data *data = dev_get_drvdata(dev);
34608b02433SGuenter Roeck 	unsigned int regval;
34708b02433SGuenter Roeck 	int err, reg;
34822e73183SEduardo Valentin 
34908b02433SGuenter Roeck 	switch (type) {
35008b02433SGuenter Roeck 	case hwmon_chip:
35108b02433SGuenter Roeck 		switch (attr) {
35208b02433SGuenter Roeck 		case hwmon_chip_update_interval:
35308b02433SGuenter Roeck 			*val = data->sample_time;
354ccffe776SLuis de Bethencourt 			break;
35508b02433SGuenter Roeck 		default:
35608b02433SGuenter Roeck 			return -EINVAL;
35708b02433SGuenter Roeck 		}
35808b02433SGuenter Roeck 		break;
35908b02433SGuenter Roeck 	case hwmon_temp:
36008b02433SGuenter Roeck 		switch (attr) {
36108b02433SGuenter Roeck 		case hwmon_temp_input:
36208b02433SGuenter Roeck 			reg = LM75_REG_TEMP;
36308b02433SGuenter Roeck 			break;
36408b02433SGuenter Roeck 		case hwmon_temp_max:
36508b02433SGuenter Roeck 			reg = LM75_REG_MAX;
36608b02433SGuenter Roeck 			break;
36708b02433SGuenter Roeck 		case hwmon_temp_max_hyst:
36808b02433SGuenter Roeck 			reg = LM75_REG_HYST;
36908b02433SGuenter Roeck 			break;
37008b02433SGuenter Roeck 		default:
37108b02433SGuenter Roeck 			return -EINVAL;
37208b02433SGuenter Roeck 		}
37308b02433SGuenter Roeck 		err = regmap_read(data->regmap, reg, &regval);
374e65365feSGuenter Roeck 		if (err < 0)
375e65365feSGuenter Roeck 			return err;
37622e73183SEduardo Valentin 
37708b02433SGuenter Roeck 		*val = lm75_reg_to_mc(regval, data->resolution);
37808b02433SGuenter Roeck 		break;
37908b02433SGuenter Roeck 	default:
38008b02433SGuenter Roeck 		return -EINVAL;
38108b02433SGuenter Roeck 	}
38222e73183SEduardo Valentin 	return 0;
38322e73183SEduardo Valentin }
38422e73183SEduardo Valentin 
3854b5be3c1SIker Perez del Palomar Sustatxa static int lm75_write_temp(struct device *dev, u32 attr, long temp)
3869ca8e40cSJean Delvare {
387e65365feSGuenter Roeck 	struct lm75_data *data = dev_get_drvdata(dev);
38887d0621aSJean Delvare 	u8 resolution;
38908b02433SGuenter Roeck 	int reg;
390e3cd9528SShubhrajyoti D 
39108b02433SGuenter Roeck 	switch (attr) {
39208b02433SGuenter Roeck 	case hwmon_temp_max:
39308b02433SGuenter Roeck 		reg = LM75_REG_MAX;
39408b02433SGuenter Roeck 		break;
39508b02433SGuenter Roeck 	case hwmon_temp_max_hyst:
39608b02433SGuenter Roeck 		reg = LM75_REG_HYST;
39708b02433SGuenter Roeck 		break;
39808b02433SGuenter Roeck 	default:
39908b02433SGuenter Roeck 		return -EINVAL;
40008b02433SGuenter Roeck 	}
4019ca8e40cSJean Delvare 
40287d0621aSJean Delvare 	/*
40387d0621aSJean Delvare 	 * Resolution of limit registers is assumed to be the same as the
40487d0621aSJean Delvare 	 * temperature input register resolution unless given explicitly.
40587d0621aSJean Delvare 	 */
406dcb12653SIker Perez del Palomar Sustatxa 	if (data->params->resolution_limits)
407dcb12653SIker Perez del Palomar Sustatxa 		resolution = data->params->resolution_limits;
40887d0621aSJean Delvare 	else
40987d0621aSJean Delvare 		resolution = data->resolution;
41087d0621aSJean Delvare 
41187d0621aSJean Delvare 	temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
412e65365feSGuenter Roeck 	temp = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
41387d0621aSJean Delvare 				 1000) << (16 - resolution);
414e65365feSGuenter Roeck 
4157d82fcc9SGuenter Roeck 	return regmap_write(data->regmap, reg, (u16)temp);
4168d5d45fbSJean Delvare }
4178d5d45fbSJean Delvare 
418040b106fSGuenter Roeck static int lm75_update_interval(struct device *dev, long val)
4194b5be3c1SIker Perez del Palomar Sustatxa {
4204b5be3c1SIker Perez del Palomar Sustatxa 	struct lm75_data *data = dev_get_drvdata(dev);
421cee04a01SGuenter Roeck 	unsigned int reg;
4224b5be3c1SIker Perez del Palomar Sustatxa 	u8 index;
4234b5be3c1SIker Perez del Palomar Sustatxa 	s32 err;
4244b5be3c1SIker Perez del Palomar Sustatxa 
4254b5be3c1SIker Perez del Palomar Sustatxa 	index = find_closest(val, data->params->sample_times,
4264b5be3c1SIker Perez del Palomar Sustatxa 			     (int)data->params->num_sample_times);
4274b5be3c1SIker Perez del Palomar Sustatxa 
428cee04a01SGuenter Roeck 	switch (data->kind) {
429cee04a01SGuenter Roeck 	default:
430040b106fSGuenter Roeck 		err = lm75_write_config(data, lm75_sample_set_masks[index],
4317db0db3fSGuenter Roeck 					LM75_SAMPLE_CLEAR_MASK);
4324b5be3c1SIker Perez del Palomar Sustatxa 		if (err)
4334b5be3c1SIker Perez del Palomar Sustatxa 			return err;
4344b5be3c1SIker Perez del Palomar Sustatxa 
435040b106fSGuenter Roeck 		data->sample_time = data->params->sample_times[index];
4364b5be3c1SIker Perez del Palomar Sustatxa 		if (data->params->resolutions)
4374b5be3c1SIker Perez del Palomar Sustatxa 			data->resolution = data->params->resolutions[index];
438cee04a01SGuenter Roeck 		break;
439cee04a01SGuenter Roeck 	case tmp112:
440cee04a01SGuenter Roeck 		err = regmap_read(data->regmap, LM75_REG_CONF, &reg);
441cee04a01SGuenter Roeck 		if (err < 0)
442cee04a01SGuenter Roeck 			return err;
443cee04a01SGuenter Roeck 		reg &= ~0x00c0;
444cee04a01SGuenter Roeck 		reg |= (3 - index) << 6;
445cee04a01SGuenter Roeck 		err = regmap_write(data->regmap, LM75_REG_CONF, reg);
446cee04a01SGuenter Roeck 		if (err < 0)
447cee04a01SGuenter Roeck 			return err;
448cee04a01SGuenter Roeck 		data->sample_time = data->params->sample_times[index];
449cee04a01SGuenter Roeck 		break;
450d7a85cdeSGuenter Roeck 	case pct2075:
451d7a85cdeSGuenter Roeck 		err = i2c_smbus_write_byte_data(data->client, PCT2075_REG_IDLE,
452d7a85cdeSGuenter Roeck 						index + 1);
453d7a85cdeSGuenter Roeck 		if (err)
454d7a85cdeSGuenter Roeck 			return err;
455d7a85cdeSGuenter Roeck 		data->sample_time = data->params->sample_times[index];
456d7a85cdeSGuenter Roeck 		break;
457cee04a01SGuenter Roeck 	}
458040b106fSGuenter Roeck 	return 0;
459040b106fSGuenter Roeck }
460040b106fSGuenter Roeck 
461040b106fSGuenter Roeck static int lm75_write_chip(struct device *dev, u32 attr, long val)
462040b106fSGuenter Roeck {
463040b106fSGuenter Roeck 	switch (attr) {
464040b106fSGuenter Roeck 	case hwmon_chip_update_interval:
465040b106fSGuenter Roeck 		return lm75_update_interval(dev, val);
4664b5be3c1SIker Perez del Palomar Sustatxa 	default:
4674b5be3c1SIker Perez del Palomar Sustatxa 		return -EINVAL;
4684b5be3c1SIker Perez del Palomar Sustatxa 	}
4694b5be3c1SIker Perez del Palomar Sustatxa 	return 0;
4704b5be3c1SIker Perez del Palomar Sustatxa }
4714b5be3c1SIker Perez del Palomar Sustatxa 
4724b5be3c1SIker Perez del Palomar Sustatxa static int lm75_write(struct device *dev, enum hwmon_sensor_types type,
4734b5be3c1SIker Perez del Palomar Sustatxa 		      u32 attr, int channel, long val)
4744b5be3c1SIker Perez del Palomar Sustatxa {
4754b5be3c1SIker Perez del Palomar Sustatxa 	switch (type) {
4764b5be3c1SIker Perez del Palomar Sustatxa 	case hwmon_chip:
4774b5be3c1SIker Perez del Palomar Sustatxa 		return lm75_write_chip(dev, attr, val);
4784b5be3c1SIker Perez del Palomar Sustatxa 	case hwmon_temp:
4794b5be3c1SIker Perez del Palomar Sustatxa 		return lm75_write_temp(dev, attr, val);
4804b5be3c1SIker Perez del Palomar Sustatxa 	default:
4814b5be3c1SIker Perez del Palomar Sustatxa 		return -EINVAL;
4824b5be3c1SIker Perez del Palomar Sustatxa 	}
4834b5be3c1SIker Perez del Palomar Sustatxa 	return 0;
4844b5be3c1SIker Perez del Palomar Sustatxa }
4854b5be3c1SIker Perez del Palomar Sustatxa 
48608b02433SGuenter Roeck static umode_t lm75_is_visible(const void *data, enum hwmon_sensor_types type,
48708b02433SGuenter Roeck 			       u32 attr, int channel)
4885f7e5e29SGuenter Roeck {
4894b5be3c1SIker Perez del Palomar Sustatxa 	const struct lm75_data *config_data = data;
4904b5be3c1SIker Perez del Palomar Sustatxa 
49108b02433SGuenter Roeck 	switch (type) {
49208b02433SGuenter Roeck 	case hwmon_chip:
49308b02433SGuenter Roeck 		switch (attr) {
49408b02433SGuenter Roeck 		case hwmon_chip_update_interval:
4954b5be3c1SIker Perez del Palomar Sustatxa 			if (config_data->params->num_sample_times > 1)
4964b5be3c1SIker Perez del Palomar Sustatxa 				return 0644;
497e6ab6e0eSGuenter Roeck 			return 0444;
4985f7e5e29SGuenter Roeck 		}
49908b02433SGuenter Roeck 		break;
50008b02433SGuenter Roeck 	case hwmon_temp:
50108b02433SGuenter Roeck 		switch (attr) {
50208b02433SGuenter Roeck 		case hwmon_temp_input:
503e6ab6e0eSGuenter Roeck 			return 0444;
50408b02433SGuenter Roeck 		case hwmon_temp_max:
50508b02433SGuenter Roeck 		case hwmon_temp_max_hyst:
506e6ab6e0eSGuenter Roeck 			return 0644;
50708b02433SGuenter Roeck 		}
50808b02433SGuenter Roeck 		break;
50908b02433SGuenter Roeck 	default:
51008b02433SGuenter Roeck 		break;
51108b02433SGuenter Roeck 	}
51208b02433SGuenter Roeck 	return 0;
51308b02433SGuenter Roeck }
5142251aef6SEduardo Valentin 
51508b02433SGuenter Roeck static const struct hwmon_channel_info *lm75_info[] = {
516e4f6fed1SGuenter Roeck 	HWMON_CHANNEL_INFO(chip,
517e4f6fed1SGuenter Roeck 			   HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
518e4f6fed1SGuenter Roeck 	HWMON_CHANNEL_INFO(temp,
519e4f6fed1SGuenter Roeck 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
52008b02433SGuenter Roeck 	NULL
52108b02433SGuenter Roeck };
52208b02433SGuenter Roeck 
52308b02433SGuenter Roeck static const struct hwmon_ops lm75_hwmon_ops = {
52408b02433SGuenter Roeck 	.is_visible = lm75_is_visible,
52508b02433SGuenter Roeck 	.read = lm75_read,
52608b02433SGuenter Roeck 	.write = lm75_write,
52708b02433SGuenter Roeck };
52808b02433SGuenter Roeck 
52908b02433SGuenter Roeck static const struct hwmon_chip_info lm75_chip_info = {
53008b02433SGuenter Roeck 	.ops = &lm75_hwmon_ops,
53108b02433SGuenter Roeck 	.info = lm75_info,
53208b02433SGuenter Roeck };
53308b02433SGuenter Roeck 
534e65365feSGuenter Roeck static bool lm75_is_writeable_reg(struct device *dev, unsigned int reg)
535e65365feSGuenter Roeck {
536e65365feSGuenter Roeck 	return reg != LM75_REG_TEMP;
537e65365feSGuenter Roeck }
538e65365feSGuenter Roeck 
539e65365feSGuenter Roeck static bool lm75_is_volatile_reg(struct device *dev, unsigned int reg)
540e65365feSGuenter Roeck {
541cee04a01SGuenter Roeck 	return reg == LM75_REG_TEMP || reg == LM75_REG_CONF;
542e65365feSGuenter Roeck }
543e65365feSGuenter Roeck 
544e65365feSGuenter Roeck static const struct regmap_config lm75_regmap_config = {
545e65365feSGuenter Roeck 	.reg_bits = 8,
546e65365feSGuenter Roeck 	.val_bits = 16,
547d7a85cdeSGuenter Roeck 	.max_register = PCT2075_REG_IDLE,
548e65365feSGuenter Roeck 	.writeable_reg = lm75_is_writeable_reg,
549e65365feSGuenter Roeck 	.volatile_reg = lm75_is_volatile_reg,
550e65365feSGuenter Roeck 	.val_format_endian = REGMAP_ENDIAN_BIG,
551e65365feSGuenter Roeck 	.cache_type = REGCACHE_RBTREE,
5521c96a2f6SDavid Frey 	.use_single_read = true,
5531c96a2f6SDavid Frey 	.use_single_write = true,
554e65365feSGuenter Roeck };
555e65365feSGuenter Roeck 
556707d151bSAlban Bedel static void lm75_disable_regulator(void *data)
557707d151bSAlban Bedel {
558707d151bSAlban Bedel 	struct lm75_data *lm75 = data;
559707d151bSAlban Bedel 
560707d151bSAlban Bedel 	regulator_disable(lm75->vs);
561707d151bSAlban Bedel }
562707d151bSAlban Bedel 
5639e37d3e2SGuenter Roeck static void lm75_remove(void *data)
5649e37d3e2SGuenter Roeck {
5659e37d3e2SGuenter Roeck 	struct lm75_data *lm75 = data;
5669e37d3e2SGuenter Roeck 	struct i2c_client *client = lm75->client;
5679e37d3e2SGuenter Roeck 
5689e37d3e2SGuenter Roeck 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
5699e37d3e2SGuenter Roeck }
5709e37d3e2SGuenter Roeck 
57167487038SStephen Kitt static const struct i2c_device_id lm75_ids[];
57267487038SStephen Kitt 
57367487038SStephen Kitt static int lm75_probe(struct i2c_client *client)
5749ebd3d82SDavid Brownell {
575d663ec49SGuenter Roeck 	struct device *dev = &client->dev;
5769e37d3e2SGuenter Roeck 	struct device *hwmon_dev;
5779ebd3d82SDavid Brownell 	struct lm75_data *data;
57890e2b545SGuenter Roeck 	int status, err;
579e97a45f1SJavier Martinez Canillas 	enum lm75_type kind;
580e97a45f1SJavier Martinez Canillas 
581e97a45f1SJavier Martinez Canillas 	if (client->dev.of_node)
582e97a45f1SJavier Martinez Canillas 		kind = (enum lm75_type)of_device_get_match_data(&client->dev);
583e97a45f1SJavier Martinez Canillas 	else
58467487038SStephen Kitt 		kind = i2c_match_id(lm75_ids, client)->driver_data;
5859ebd3d82SDavid Brownell 
5869ebd3d82SDavid Brownell 	if (!i2c_check_functionality(client->adapter,
5879ebd3d82SDavid Brownell 			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
5889ebd3d82SDavid Brownell 		return -EIO;
5899ebd3d82SDavid Brownell 
590d663ec49SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct lm75_data), GFP_KERNEL);
5919ebd3d82SDavid Brownell 	if (!data)
5929ebd3d82SDavid Brownell 		return -ENOMEM;
5939ebd3d82SDavid Brownell 
594d663ec49SGuenter Roeck 	data->client = client;
595dcb12653SIker Perez del Palomar Sustatxa 	data->kind = kind;
596e65365feSGuenter Roeck 
597707d151bSAlban Bedel 	data->vs = devm_regulator_get(dev, "vs");
598707d151bSAlban Bedel 	if (IS_ERR(data->vs))
599707d151bSAlban Bedel 		return PTR_ERR(data->vs);
600707d151bSAlban Bedel 
601e65365feSGuenter Roeck 	data->regmap = devm_regmap_init_i2c(client, &lm75_regmap_config);
602e65365feSGuenter Roeck 	if (IS_ERR(data->regmap))
603e65365feSGuenter Roeck 		return PTR_ERR(data->regmap);
6049ebd3d82SDavid Brownell 
6059ebd3d82SDavid Brownell 	/* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
6069ebd3d82SDavid Brownell 	 * Then tweak to be more precise when appropriate.
6079ebd3d82SDavid Brownell 	 */
6088a5c5cc6SJean Delvare 
609dcb12653SIker Perez del Palomar Sustatxa 	data->params = &device_params[data->kind];
6109ebd3d82SDavid Brownell 
611dcb12653SIker Perez del Palomar Sustatxa 	/* Save default sample time and resolution*/
612dcb12653SIker Perez del Palomar Sustatxa 	data->sample_time = data->params->default_sample_time;
613dcb12653SIker Perez del Palomar Sustatxa 	data->resolution = data->params->default_resolution;
614dcb12653SIker Perez del Palomar Sustatxa 
615707d151bSAlban Bedel 	/* Enable the power */
616707d151bSAlban Bedel 	err = regulator_enable(data->vs);
617707d151bSAlban Bedel 	if (err) {
618707d151bSAlban Bedel 		dev_err(dev, "failed to enable regulator: %d\n", err);
619707d151bSAlban Bedel 		return err;
620707d151bSAlban Bedel 	}
621707d151bSAlban Bedel 
622707d151bSAlban Bedel 	err = devm_add_action_or_reset(dev, lm75_disable_regulator, data);
623707d151bSAlban Bedel 	if (err)
624707d151bSAlban Bedel 		return err;
625707d151bSAlban Bedel 
626dcb12653SIker Perez del Palomar Sustatxa 	/* Cache original configuration */
62738aefb41SGuenter Roeck 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
6289ebd3d82SDavid Brownell 	if (status < 0) {
629d663ec49SGuenter Roeck 		dev_dbg(dev, "Can't read config? %d\n", status);
63013ac7a01SGuenter Roeck 		return status;
6319ebd3d82SDavid Brownell 	}
6329ebd3d82SDavid Brownell 	data->orig_conf = status;
63358608cfeSIker Perez del Palomar Sustatxa 	data->current_conf = status;
63458608cfeSIker Perez del Palomar Sustatxa 
63558608cfeSIker Perez del Palomar Sustatxa 	err = lm75_write_config(data, data->params->set_mask,
63658608cfeSIker Perez del Palomar Sustatxa 				data->params->clr_mask);
63758608cfeSIker Perez del Palomar Sustatxa 	if (err)
63858608cfeSIker Perez del Palomar Sustatxa 		return err;
6399e37d3e2SGuenter Roeck 
64090e2b545SGuenter Roeck 	err = devm_add_action_or_reset(dev, lm75_remove, data);
64190e2b545SGuenter Roeck 	if (err)
64290e2b545SGuenter Roeck 		return err;
6439e37d3e2SGuenter Roeck 
64408b02433SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
64508b02433SGuenter Roeck 							 data, &lm75_chip_info,
64608b02433SGuenter Roeck 							 NULL);
6479e37d3e2SGuenter Roeck 	if (IS_ERR(hwmon_dev))
6489e37d3e2SGuenter Roeck 		return PTR_ERR(hwmon_dev);
6499ebd3d82SDavid Brownell 
6509e37d3e2SGuenter Roeck 	dev_info(dev, "%s: sensor '%s'\n", dev_name(hwmon_dev), client->name);
6519ebd3d82SDavid Brownell 
6529ebd3d82SDavid Brownell 	return 0;
6539ebd3d82SDavid Brownell }
6549ebd3d82SDavid Brownell 
6559ebd3d82SDavid Brownell static const struct i2c_device_id lm75_ids[] = {
656e96f9d89SMichael Hennerich 	{ "adt75", adt75, },
657*c851b715SPeter Rosin 	{ "at30ts74", at30ts74, },
6589ebd3d82SDavid Brownell 	{ "ds1775", ds1775, },
6599ebd3d82SDavid Brownell 	{ "ds75", ds75, },
6603fbc81e3SJean Delvare 	{ "ds7505", ds7505, },
661c98d6c65SArnaud Ebalard 	{ "g751", g751, },
6629ebd3d82SDavid Brownell 	{ "lm75", lm75, },
6639ebd3d82SDavid Brownell 	{ "lm75a", lm75a, },
664799fc602SMichael Thalmeier 	{ "lm75b", lm75b, },
6659ebd3d82SDavid Brownell 	{ "max6625", max6625, },
6669ebd3d82SDavid Brownell 	{ "max6626", max6626, },
667a54ca77aSKun Yi 	{ "max31725", max31725, },
668a54ca77aSKun Yi 	{ "max31726", max31725, },
6699ebd3d82SDavid Brownell 	{ "mcp980x", mcp980x, },
670557c7ffaSDaniel Mack 	{ "pct2075", pct2075, },
6719ebd3d82SDavid Brownell 	{ "stds75", stds75, },
6722e9a41bbSJagan Teki 	{ "stlm75", stlm75, },
6739ebd3d82SDavid Brownell 	{ "tcn75", tcn75, },
6749ebd3d82SDavid Brownell 	{ "tmp100", tmp100, },
6759ebd3d82SDavid Brownell 	{ "tmp101", tmp101, },
6766d034059SShubhrajyoti Datta 	{ "tmp105", tmp105, },
677c83959f8SFrans Klaver 	{ "tmp112", tmp112, },
6789ebd3d82SDavid Brownell 	{ "tmp175", tmp175, },
6799ebd3d82SDavid Brownell 	{ "tmp275", tmp275, },
6809ebd3d82SDavid Brownell 	{ "tmp75", tmp75, },
68139abe9d8SIker Perez del Palomar Sustatxa 	{ "tmp75b", tmp75b, },
6829c32e815SBen Gardner 	{ "tmp75c", tmp75c, },
683ec081f91SRobert Marko 	{ "tmp1075", tmp1075, },
6849ebd3d82SDavid Brownell 	{ /* LIST END */ }
6859ebd3d82SDavid Brownell };
6869ebd3d82SDavid Brownell MODULE_DEVICE_TABLE(i2c, lm75_ids);
6879ebd3d82SDavid Brownell 
688ffa83e78SGuenter Roeck static const struct of_device_id __maybe_unused lm75_of_match[] = {
689e97a45f1SJavier Martinez Canillas 	{
690e97a45f1SJavier Martinez Canillas 		.compatible = "adi,adt75",
691e97a45f1SJavier Martinez Canillas 		.data = (void *)adt75
692e97a45f1SJavier Martinez Canillas 	},
693e97a45f1SJavier Martinez Canillas 	{
694*c851b715SPeter Rosin 		.compatible = "atmel,at30ts74",
695*c851b715SPeter Rosin 		.data = (void *)at30ts74
696*c851b715SPeter Rosin 	},
697*c851b715SPeter Rosin 	{
698e97a45f1SJavier Martinez Canillas 		.compatible = "dallas,ds1775",
699e97a45f1SJavier Martinez Canillas 		.data = (void *)ds1775
700e97a45f1SJavier Martinez Canillas 	},
701e97a45f1SJavier Martinez Canillas 	{
702e97a45f1SJavier Martinez Canillas 		.compatible = "dallas,ds75",
703e97a45f1SJavier Martinez Canillas 		.data = (void *)ds75
704e97a45f1SJavier Martinez Canillas 	},
705e97a45f1SJavier Martinez Canillas 	{
706e97a45f1SJavier Martinez Canillas 		.compatible = "dallas,ds7505",
707e97a45f1SJavier Martinez Canillas 		.data = (void *)ds7505
708e97a45f1SJavier Martinez Canillas 	},
709e97a45f1SJavier Martinez Canillas 	{
710e97a45f1SJavier Martinez Canillas 		.compatible = "gmt,g751",
711e97a45f1SJavier Martinez Canillas 		.data = (void *)g751
712e97a45f1SJavier Martinez Canillas 	},
713e97a45f1SJavier Martinez Canillas 	{
714e97a45f1SJavier Martinez Canillas 		.compatible = "national,lm75",
715e97a45f1SJavier Martinez Canillas 		.data = (void *)lm75
716e97a45f1SJavier Martinez Canillas 	},
717e97a45f1SJavier Martinez Canillas 	{
718e97a45f1SJavier Martinez Canillas 		.compatible = "national,lm75a",
719e97a45f1SJavier Martinez Canillas 		.data = (void *)lm75a
720e97a45f1SJavier Martinez Canillas 	},
721e97a45f1SJavier Martinez Canillas 	{
722e97a45f1SJavier Martinez Canillas 		.compatible = "national,lm75b",
723e97a45f1SJavier Martinez Canillas 		.data = (void *)lm75b
724e97a45f1SJavier Martinez Canillas 	},
725e97a45f1SJavier Martinez Canillas 	{
726e97a45f1SJavier Martinez Canillas 		.compatible = "maxim,max6625",
727e97a45f1SJavier Martinez Canillas 		.data = (void *)max6625
728e97a45f1SJavier Martinez Canillas 	},
729e97a45f1SJavier Martinez Canillas 	{
730e97a45f1SJavier Martinez Canillas 		.compatible = "maxim,max6626",
731e97a45f1SJavier Martinez Canillas 		.data = (void *)max6626
732e97a45f1SJavier Martinez Canillas 	},
733e97a45f1SJavier Martinez Canillas 	{
734a54ca77aSKun Yi 		.compatible = "maxim,max31725",
735a54ca77aSKun Yi 		.data = (void *)max31725
736a54ca77aSKun Yi 	},
737a54ca77aSKun Yi 	{
738a54ca77aSKun Yi 		.compatible = "maxim,max31726",
739a54ca77aSKun Yi 		.data = (void *)max31725
740a54ca77aSKun Yi 	},
741a54ca77aSKun Yi 	{
742e97a45f1SJavier Martinez Canillas 		.compatible = "maxim,mcp980x",
743e97a45f1SJavier Martinez Canillas 		.data = (void *)mcp980x
744e97a45f1SJavier Martinez Canillas 	},
745e97a45f1SJavier Martinez Canillas 	{
746557c7ffaSDaniel Mack 		.compatible = "nxp,pct2075",
747557c7ffaSDaniel Mack 		.data = (void *)pct2075
748557c7ffaSDaniel Mack 	},
749557c7ffaSDaniel Mack 	{
750e97a45f1SJavier Martinez Canillas 		.compatible = "st,stds75",
751e97a45f1SJavier Martinez Canillas 		.data = (void *)stds75
752e97a45f1SJavier Martinez Canillas 	},
753e97a45f1SJavier Martinez Canillas 	{
7542e9a41bbSJagan Teki 		.compatible = "st,stlm75",
7552e9a41bbSJagan Teki 		.data = (void *)stlm75
7562e9a41bbSJagan Teki 	},
7572e9a41bbSJagan Teki 	{
758e97a45f1SJavier Martinez Canillas 		.compatible = "microchip,tcn75",
759e97a45f1SJavier Martinez Canillas 		.data = (void *)tcn75
760e97a45f1SJavier Martinez Canillas 	},
761e97a45f1SJavier Martinez Canillas 	{
762e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp100",
763e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp100
764e97a45f1SJavier Martinez Canillas 	},
765e97a45f1SJavier Martinez Canillas 	{
766e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp101",
767e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp101
768e97a45f1SJavier Martinez Canillas 	},
769e97a45f1SJavier Martinez Canillas 	{
770e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp105",
771e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp105
772e97a45f1SJavier Martinez Canillas 	},
773e97a45f1SJavier Martinez Canillas 	{
774e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp112",
775e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp112
776e97a45f1SJavier Martinez Canillas 	},
777e97a45f1SJavier Martinez Canillas 	{
778e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp175",
779e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp175
780e97a45f1SJavier Martinez Canillas 	},
781e97a45f1SJavier Martinez Canillas 	{
782e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp275",
783e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp275
784e97a45f1SJavier Martinez Canillas 	},
785e97a45f1SJavier Martinez Canillas 	{
786e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp75",
787e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp75
788e97a45f1SJavier Martinez Canillas 	},
789e97a45f1SJavier Martinez Canillas 	{
79039abe9d8SIker Perez del Palomar Sustatxa 		.compatible = "ti,tmp75b",
79139abe9d8SIker Perez del Palomar Sustatxa 		.data = (void *)tmp75b
79239abe9d8SIker Perez del Palomar Sustatxa 	},
79339abe9d8SIker Perez del Palomar Sustatxa 	{
794e97a45f1SJavier Martinez Canillas 		.compatible = "ti,tmp75c",
795e97a45f1SJavier Martinez Canillas 		.data = (void *)tmp75c
796e97a45f1SJavier Martinez Canillas 	},
797ec081f91SRobert Marko 	{
798ec081f91SRobert Marko 		.compatible = "ti,tmp1075",
799ec081f91SRobert Marko 		.data = (void *)tmp1075
800ec081f91SRobert Marko 	},
801e97a45f1SJavier Martinez Canillas 	{ },
802e97a45f1SJavier Martinez Canillas };
803e97a45f1SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, lm75_of_match);
804e97a45f1SJavier Martinez Canillas 
80505e82fe4SLen Sorensen #define LM75A_ID 0xA1
80605e82fe4SLen Sorensen 
8078ff69eebSJean Delvare /* Return 0 if detection is successful, -ENODEV otherwise */
808310ec792SJean Delvare static int lm75_detect(struct i2c_client *new_client,
8098ff69eebSJean Delvare 		       struct i2c_board_info *info)
8108d5d45fbSJean Delvare {
8118ff69eebSJean Delvare 	struct i2c_adapter *adapter = new_client->adapter;
8128d5d45fbSJean Delvare 	int i;
813e76f67b5SJean Delvare 	int conf, hyst, os;
81405e82fe4SLen Sorensen 	bool is_lm75a = 0;
8158d5d45fbSJean Delvare 
8168d5d45fbSJean Delvare 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
8178d5d45fbSJean Delvare 				     I2C_FUNC_SMBUS_WORD_DATA))
8188ff69eebSJean Delvare 		return -ENODEV;
8198d5d45fbSJean Delvare 
820426343efSJean Delvare 	/*
821426343efSJean Delvare 	 * Now, we do the remaining detection. There is no identification-
822426343efSJean Delvare 	 * dedicated register so we have to rely on several tricks:
823426343efSJean Delvare 	 * unused bits, registers cycling over 8-address boundaries,
824426343efSJean Delvare 	 * addresses 0x04-0x07 returning the last read value.
825426343efSJean Delvare 	 * The cycling+unused addresses combination is not tested,
826426343efSJean Delvare 	 * since it would significantly slow the detection down and would
827426343efSJean Delvare 	 * hardly add any value.
828426343efSJean Delvare 	 *
829426343efSJean Delvare 	 * The National Semiconductor LM75A is different than earlier
830426343efSJean Delvare 	 * LM75s.  It has an ID byte of 0xaX (where X is the chip
831426343efSJean Delvare 	 * revision, with 1 being the only revision in existence) in
832426343efSJean Delvare 	 * register 7, and unused registers return 0xff rather than the
833426343efSJean Delvare 	 * last read value.
834426343efSJean Delvare 	 *
835426343efSJean Delvare 	 * Note that this function only detects the original National
836426343efSJean Delvare 	 * Semiconductor LM75 and the LM75A. Clones from other vendors
837426343efSJean Delvare 	 * aren't detected, on purpose, because they are typically never
838426343efSJean Delvare 	 * found on PC hardware. They are found on embedded designs where
839426343efSJean Delvare 	 * they can be instantiated explicitly so detection is not needed.
840426343efSJean Delvare 	 * The absence of identification registers on all these clones
841426343efSJean Delvare 	 * would make their exhaustive detection very difficult and weak,
842426343efSJean Delvare 	 * and odds are that the driver would bind to unsupported devices.
843426343efSJean Delvare 	 */
84405e82fe4SLen Sorensen 
845e76f67b5SJean Delvare 	/* Unused bits */
8468d5d45fbSJean Delvare 	conf = i2c_smbus_read_byte_data(new_client, 1);
847e76f67b5SJean Delvare 	if (conf & 0xe0)
848e76f67b5SJean Delvare 		return -ENODEV;
84905e82fe4SLen Sorensen 
85005e82fe4SLen Sorensen 	/* First check for LM75A */
85105e82fe4SLen Sorensen 	if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
8528cbf2172SMichal Orzel 		/*
8538cbf2172SMichal Orzel 		 * LM75A returns 0xff on unused registers so
8548cbf2172SMichal Orzel 		 * just to be sure we check for that too.
8558cbf2172SMichal Orzel 		 */
85605e82fe4SLen Sorensen 		if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
85705e82fe4SLen Sorensen 		 || i2c_smbus_read_byte_data(new_client, 5) != 0xff
85805e82fe4SLen Sorensen 		 || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
85905e82fe4SLen Sorensen 			return -ENODEV;
86005e82fe4SLen Sorensen 		is_lm75a = 1;
861e76f67b5SJean Delvare 		hyst = i2c_smbus_read_byte_data(new_client, 2);
862e76f67b5SJean Delvare 		os = i2c_smbus_read_byte_data(new_client, 3);
86305e82fe4SLen Sorensen 	} else { /* Traditional style LM75 detection */
86405e82fe4SLen Sorensen 		/* Unused addresses */
865e76f67b5SJean Delvare 		hyst = i2c_smbus_read_byte_data(new_client, 2);
866e76f67b5SJean Delvare 		if (i2c_smbus_read_byte_data(new_client, 4) != hyst
867e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, 5) != hyst
868e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, 6) != hyst
869e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, 7) != hyst)
8708ff69eebSJean Delvare 			return -ENODEV;
871e76f67b5SJean Delvare 		os = i2c_smbus_read_byte_data(new_client, 3);
872e76f67b5SJean Delvare 		if (i2c_smbus_read_byte_data(new_client, 4) != os
873e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, 5) != os
874e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, 6) != os
875e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, 7) != os)
8768ff69eebSJean Delvare 			return -ENODEV;
87705e82fe4SLen Sorensen 	}
8784ad40cc5SGuenter Roeck 	/*
8794ad40cc5SGuenter Roeck 	 * It is very unlikely that this is a LM75 if both
8804ad40cc5SGuenter Roeck 	 * hysteresis and temperature limit registers are 0.
8814ad40cc5SGuenter Roeck 	 */
8824ad40cc5SGuenter Roeck 	if (hyst == 0 && os == 0)
8834ad40cc5SGuenter Roeck 		return -ENODEV;
8848d5d45fbSJean Delvare 
8858d5d45fbSJean Delvare 	/* Addresses cycling */
886e76f67b5SJean Delvare 	for (i = 8; i <= 248; i += 40) {
8878d5d45fbSJean Delvare 		if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
888e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
889e76f67b5SJean Delvare 		 || i2c_smbus_read_byte_data(new_client, i + 3) != os)
8908ff69eebSJean Delvare 			return -ENODEV;
89105e82fe4SLen Sorensen 		if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
89205e82fe4SLen Sorensen 				!= LM75A_ID)
89305e82fe4SLen Sorensen 			return -ENODEV;
8948d5d45fbSJean Delvare 	}
8958d5d45fbSJean Delvare 
89605e82fe4SLen Sorensen 	strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
8978d5d45fbSJean Delvare 
8988d5d45fbSJean Delvare 	return 0;
8998d5d45fbSJean Delvare }
9008d5d45fbSJean Delvare 
9019914518eSShubhrajyoti Datta #ifdef CONFIG_PM
9029914518eSShubhrajyoti Datta static int lm75_suspend(struct device *dev)
9039914518eSShubhrajyoti Datta {
9049914518eSShubhrajyoti Datta 	int status;
9059914518eSShubhrajyoti Datta 	struct i2c_client *client = to_i2c_client(dev);
9068cbf2172SMichal Orzel 
90738aefb41SGuenter Roeck 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
9089914518eSShubhrajyoti Datta 	if (status < 0) {
9099914518eSShubhrajyoti Datta 		dev_dbg(&client->dev, "Can't read config? %d\n", status);
9109914518eSShubhrajyoti Datta 		return status;
9119914518eSShubhrajyoti Datta 	}
9129914518eSShubhrajyoti Datta 	status = status | LM75_SHUTDOWN;
91338aefb41SGuenter Roeck 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
9149914518eSShubhrajyoti Datta 	return 0;
9159914518eSShubhrajyoti Datta }
9169914518eSShubhrajyoti Datta 
9179914518eSShubhrajyoti Datta static int lm75_resume(struct device *dev)
9189914518eSShubhrajyoti Datta {
9199914518eSShubhrajyoti Datta 	int status;
9209914518eSShubhrajyoti Datta 	struct i2c_client *client = to_i2c_client(dev);
9218cbf2172SMichal Orzel 
92238aefb41SGuenter Roeck 	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
9239914518eSShubhrajyoti Datta 	if (status < 0) {
9249914518eSShubhrajyoti Datta 		dev_dbg(&client->dev, "Can't read config? %d\n", status);
9259914518eSShubhrajyoti Datta 		return status;
9269914518eSShubhrajyoti Datta 	}
9279914518eSShubhrajyoti Datta 	status = status & ~LM75_SHUTDOWN;
92838aefb41SGuenter Roeck 	i2c_smbus_write_byte_data(client, LM75_REG_CONF, status);
9299914518eSShubhrajyoti Datta 	return 0;
9309914518eSShubhrajyoti Datta }
9319914518eSShubhrajyoti Datta 
9329914518eSShubhrajyoti Datta static const struct dev_pm_ops lm75_dev_pm_ops = {
9339914518eSShubhrajyoti Datta 	.suspend	= lm75_suspend,
9349914518eSShubhrajyoti Datta 	.resume		= lm75_resume,
9359914518eSShubhrajyoti Datta };
9369914518eSShubhrajyoti Datta #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
9379914518eSShubhrajyoti Datta #else
9389914518eSShubhrajyoti Datta #define LM75_DEV_PM_OPS NULL
9399914518eSShubhrajyoti Datta #endif /* CONFIG_PM */
9409914518eSShubhrajyoti Datta 
9418ff69eebSJean Delvare static struct i2c_driver lm75_driver = {
9428ff69eebSJean Delvare 	.class		= I2C_CLASS_HWMON,
94301a52397SDavid Brownell 	.driver = {
9448ff69eebSJean Delvare 		.name	= "lm75",
945e97a45f1SJavier Martinez Canillas 		.of_match_table = of_match_ptr(lm75_of_match),
9469914518eSShubhrajyoti Datta 		.pm	= LM75_DEV_PM_OPS,
94701a52397SDavid Brownell 	},
94867487038SStephen Kitt 	.probe_new	= lm75_probe,
9498ff69eebSJean Delvare 	.id_table	= lm75_ids,
9508ff69eebSJean Delvare 	.detect		= lm75_detect,
951c3813d6aSJean Delvare 	.address_list	= normal_i2c,
95201a52397SDavid Brownell };
95301a52397SDavid Brownell 
954f0967eeaSAxel Lin module_i2c_driver(lm75_driver);
9558d5d45fbSJean Delvare 
9568d5d45fbSJean Delvare MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
9578d5d45fbSJean Delvare MODULE_DESCRIPTION("LM75 driver");
9588d5d45fbSJean Delvare MODULE_LICENSE("GPL");
959