xref: /linux/drivers/hwmon/pmbus/lm25066.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
203e9bd8dSGuenter Roeck /*
3666c1490SGuenter Roeck  * Hardware monitoring driver for LM25056 / LM25066 / LM5064 / LM5066
403e9bd8dSGuenter Roeck  *
503e9bd8dSGuenter Roeck  * Copyright (c) 2011 Ericsson AB.
6a7c69118SGuenter Roeck  * Copyright (c) 2013 Guenter Roeck
703e9bd8dSGuenter Roeck  */
803e9bd8dSGuenter Roeck 
92c052d42SGuenter Roeck #include <linux/bitops.h>
1003e9bd8dSGuenter Roeck #include <linux/kernel.h>
1103e9bd8dSGuenter Roeck #include <linux/module.h>
1203e9bd8dSGuenter Roeck #include <linux/init.h>
1303e9bd8dSGuenter Roeck #include <linux/err.h>
1403e9bd8dSGuenter Roeck #include <linux/slab.h>
1503e9bd8dSGuenter Roeck #include <linux/i2c.h>
165d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) #include <linux/log2.h>
17ac0c26baSRob Herring #include <linux/of.h>
1803e9bd8dSGuenter Roeck #include "pmbus.h"
1903e9bd8dSGuenter Roeck 
20*cbbb76e4SGuenter Roeck enum chips { lm25056, lm25066, lm5064, lm5066, lm5066i };
2103e9bd8dSGuenter Roeck 
2203e9bd8dSGuenter Roeck #define LM25066_READ_VAUX		0xd0
2303e9bd8dSGuenter Roeck #define LM25066_MFR_READ_IIN		0xd1
2403e9bd8dSGuenter Roeck #define LM25066_MFR_READ_PIN		0xd2
2503e9bd8dSGuenter Roeck #define LM25066_MFR_IIN_OC_WARN_LIMIT	0xd3
2603e9bd8dSGuenter Roeck #define LM25066_MFR_PIN_OP_WARN_LIMIT	0xd4
2703e9bd8dSGuenter Roeck #define LM25066_READ_PIN_PEAK		0xd5
2803e9bd8dSGuenter Roeck #define LM25066_CLEAR_PIN_PEAK		0xd6
2903e9bd8dSGuenter Roeck #define LM25066_DEVICE_SETUP		0xd9
3003e9bd8dSGuenter Roeck #define LM25066_READ_AVG_VIN		0xdc
315d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) #define LM25066_SAMPLES_FOR_AVG		0xdb
3203e9bd8dSGuenter Roeck #define LM25066_READ_AVG_VOUT		0xdd
3303e9bd8dSGuenter Roeck #define LM25066_READ_AVG_IIN		0xde
3403e9bd8dSGuenter Roeck #define LM25066_READ_AVG_PIN		0xdf
3503e9bd8dSGuenter Roeck 
362c052d42SGuenter Roeck #define LM25066_DEV_SETUP_CL		BIT(4)	/* Current limit */
3703e9bd8dSGuenter Roeck 
385d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) #define LM25066_SAMPLES_FOR_AVG_MAX	4096
395d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 
4058615a94SGuenter Roeck /* LM25056 only */
4158615a94SGuenter Roeck 
4258615a94SGuenter Roeck #define LM25056_VAUX_OV_WARN_LIMIT	0xe3
4358615a94SGuenter Roeck #define LM25056_VAUX_UV_WARN_LIMIT	0xe4
4458615a94SGuenter Roeck 
452c052d42SGuenter Roeck #define LM25056_MFR_STS_VAUX_OV_WARN	BIT(1)
462c052d42SGuenter Roeck #define LM25056_MFR_STS_VAUX_UV_WARN	BIT(0)
4758615a94SGuenter Roeck 
48e53e6497SGuenter Roeck struct __coeff {
49e53e6497SGuenter Roeck 	short m, b, R;
50e53e6497SGuenter Roeck };
51e53e6497SGuenter Roeck 
52e53e6497SGuenter Roeck #define PSC_CURRENT_IN_L	(PSC_NUM_CLASSES)
53e53e6497SGuenter Roeck #define PSC_POWER_L		(PSC_NUM_CLASSES + 1)
54e53e6497SGuenter Roeck 
55df60a5daSZev Weiss static const struct __coeff lm25066_coeff[][PSC_NUM_CLASSES + 2] = {
5658615a94SGuenter Roeck 	[lm25056] = {
5758615a94SGuenter Roeck 		[PSC_VOLTAGE_IN] = {
5858615a94SGuenter Roeck 			.m = 16296,
59ae59dc45SZev Weiss 			.b = 1343,
6058615a94SGuenter Roeck 			.R = -2,
6158615a94SGuenter Roeck 		},
6258615a94SGuenter Roeck 		[PSC_CURRENT_IN] = {
6358615a94SGuenter Roeck 			.m = 13797,
64ae59dc45SZev Weiss 			.b = -1833,
6558615a94SGuenter Roeck 			.R = -2,
6658615a94SGuenter Roeck 		},
6758615a94SGuenter Roeck 		[PSC_CURRENT_IN_L] = {
6858615a94SGuenter Roeck 			.m = 6726,
69ae59dc45SZev Weiss 			.b = -537,
7058615a94SGuenter Roeck 			.R = -2,
7158615a94SGuenter Roeck 		},
7258615a94SGuenter Roeck 		[PSC_POWER] = {
7358615a94SGuenter Roeck 			.m = 5501,
74ae59dc45SZev Weiss 			.b = -2908,
7558615a94SGuenter Roeck 			.R = -3,
7658615a94SGuenter Roeck 		},
7758615a94SGuenter Roeck 		[PSC_POWER_L] = {
7858615a94SGuenter Roeck 			.m = 26882,
79ae59dc45SZev Weiss 			.b = -5646,
8058615a94SGuenter Roeck 			.R = -4,
8158615a94SGuenter Roeck 		},
8258615a94SGuenter Roeck 		[PSC_TEMPERATURE] = {
8358615a94SGuenter Roeck 			.m = 1580,
8458615a94SGuenter Roeck 			.b = -14500,
8558615a94SGuenter Roeck 			.R = -2,
8658615a94SGuenter Roeck 		},
8758615a94SGuenter Roeck 	},
88e53e6497SGuenter Roeck 	[lm25066] = {
89e53e6497SGuenter Roeck 		[PSC_VOLTAGE_IN] = {
90e53e6497SGuenter Roeck 			.m = 22070,
91ae59dc45SZev Weiss 			.b = -1800,
92e53e6497SGuenter Roeck 			.R = -2,
93e53e6497SGuenter Roeck 		},
94e53e6497SGuenter Roeck 		[PSC_VOLTAGE_OUT] = {
95e53e6497SGuenter Roeck 			.m = 22070,
96ae59dc45SZev Weiss 			.b = -1800,
97e53e6497SGuenter Roeck 			.R = -2,
98e53e6497SGuenter Roeck 		},
99e53e6497SGuenter Roeck 		[PSC_CURRENT_IN] = {
100e53e6497SGuenter Roeck 			.m = 13661,
101ae59dc45SZev Weiss 			.b = -5200,
102e53e6497SGuenter Roeck 			.R = -2,
103e53e6497SGuenter Roeck 		},
104e53e6497SGuenter Roeck 		[PSC_CURRENT_IN_L] = {
105fa16188fSZev Weiss 			.m = 6854,
106ae59dc45SZev Weiss 			.b = -3100,
107e53e6497SGuenter Roeck 			.R = -2,
108e53e6497SGuenter Roeck 		},
109e53e6497SGuenter Roeck 		[PSC_POWER] = {
110e53e6497SGuenter Roeck 			.m = 736,
111ae59dc45SZev Weiss 			.b = -3300,
112e53e6497SGuenter Roeck 			.R = -2,
113e53e6497SGuenter Roeck 		},
114e53e6497SGuenter Roeck 		[PSC_POWER_L] = {
115e53e6497SGuenter Roeck 			.m = 369,
116ae59dc45SZev Weiss 			.b = -1900,
117e53e6497SGuenter Roeck 			.R = -2,
118e53e6497SGuenter Roeck 		},
119e53e6497SGuenter Roeck 		[PSC_TEMPERATURE] = {
120e53e6497SGuenter Roeck 			.m = 16,
121e53e6497SGuenter Roeck 		},
122e53e6497SGuenter Roeck 	},
123e53e6497SGuenter Roeck 	[lm5064] = {
124e53e6497SGuenter Roeck 		[PSC_VOLTAGE_IN] = {
125e53e6497SGuenter Roeck 			.m = 4611,
126ae59dc45SZev Weiss 			.b = -642,
127e53e6497SGuenter Roeck 			.R = -2,
128e53e6497SGuenter Roeck 		},
129e53e6497SGuenter Roeck 		[PSC_VOLTAGE_OUT] = {
130e53e6497SGuenter Roeck 			.m = 4621,
131ae59dc45SZev Weiss 			.b = 423,
132e53e6497SGuenter Roeck 			.R = -2,
133e53e6497SGuenter Roeck 		},
134e53e6497SGuenter Roeck 		[PSC_CURRENT_IN] = {
135e53e6497SGuenter Roeck 			.m = 10742,
136ae59dc45SZev Weiss 			.b = 1552,
137e53e6497SGuenter Roeck 			.R = -2,
138e53e6497SGuenter Roeck 		},
139e53e6497SGuenter Roeck 		[PSC_CURRENT_IN_L] = {
140e53e6497SGuenter Roeck 			.m = 5456,
141ae59dc45SZev Weiss 			.b = 2118,
142e53e6497SGuenter Roeck 			.R = -2,
143e53e6497SGuenter Roeck 		},
144e53e6497SGuenter Roeck 		[PSC_POWER] = {
145e53e6497SGuenter Roeck 			.m = 1204,
146ae59dc45SZev Weiss 			.b = 8524,
147e53e6497SGuenter Roeck 			.R = -3,
148e53e6497SGuenter Roeck 		},
149e53e6497SGuenter Roeck 		[PSC_POWER_L] = {
150e53e6497SGuenter Roeck 			.m = 612,
151ae59dc45SZev Weiss 			.b = 11202,
152e53e6497SGuenter Roeck 			.R = -3,
153e53e6497SGuenter Roeck 		},
154e53e6497SGuenter Roeck 		[PSC_TEMPERATURE] = {
155e53e6497SGuenter Roeck 			.m = 16,
156e53e6497SGuenter Roeck 		},
157e53e6497SGuenter Roeck 	},
158e53e6497SGuenter Roeck 	[lm5066] = {
159e53e6497SGuenter Roeck 		[PSC_VOLTAGE_IN] = {
160e53e6497SGuenter Roeck 			.m = 4587,
161ae59dc45SZev Weiss 			.b = -1200,
162e53e6497SGuenter Roeck 			.R = -2,
163e53e6497SGuenter Roeck 		},
164e53e6497SGuenter Roeck 		[PSC_VOLTAGE_OUT] = {
165e53e6497SGuenter Roeck 			.m = 4587,
166ae59dc45SZev Weiss 			.b = -2400,
167e53e6497SGuenter Roeck 			.R = -2,
168e53e6497SGuenter Roeck 		},
169e53e6497SGuenter Roeck 		[PSC_CURRENT_IN] = {
170e53e6497SGuenter Roeck 			.m = 10753,
171ae59dc45SZev Weiss 			.b = -1200,
172e53e6497SGuenter Roeck 			.R = -2,
173e53e6497SGuenter Roeck 		},
174e53e6497SGuenter Roeck 		[PSC_CURRENT_IN_L] = {
175e53e6497SGuenter Roeck 			.m = 5405,
176ae59dc45SZev Weiss 			.b = -600,
177e53e6497SGuenter Roeck 			.R = -2,
178e53e6497SGuenter Roeck 		},
179e53e6497SGuenter Roeck 		[PSC_POWER] = {
180e53e6497SGuenter Roeck 			.m = 1204,
181ae59dc45SZev Weiss 			.b = -6000,
182e53e6497SGuenter Roeck 			.R = -3,
183e53e6497SGuenter Roeck 		},
184e53e6497SGuenter Roeck 		[PSC_POWER_L] = {
185e53e6497SGuenter Roeck 			.m = 605,
186ae59dc45SZev Weiss 			.b = -8000,
187e53e6497SGuenter Roeck 			.R = -3,
188e53e6497SGuenter Roeck 		},
189e53e6497SGuenter Roeck 		[PSC_TEMPERATURE] = {
190e53e6497SGuenter Roeck 			.m = 16,
191e53e6497SGuenter Roeck 		},
192e53e6497SGuenter Roeck 	},
1935783ec2eSXo Wang 	[lm5066i] = {
1945783ec2eSXo Wang 		[PSC_VOLTAGE_IN] = {
1955783ec2eSXo Wang 			.m = 4617,
1965783ec2eSXo Wang 			.b = -140,
1975783ec2eSXo Wang 			.R = -2,
1985783ec2eSXo Wang 		},
1995783ec2eSXo Wang 		[PSC_VOLTAGE_OUT] = {
2005783ec2eSXo Wang 			.m = 4602,
2015783ec2eSXo Wang 			.b = 500,
2025783ec2eSXo Wang 			.R = -2,
2035783ec2eSXo Wang 		},
2045783ec2eSXo Wang 		[PSC_CURRENT_IN] = {
2055783ec2eSXo Wang 			.m = 15076,
2065783ec2eSXo Wang 			.b = -504,
2075783ec2eSXo Wang 			.R = -2,
2085783ec2eSXo Wang 		},
2095783ec2eSXo Wang 		[PSC_CURRENT_IN_L] = {
2105783ec2eSXo Wang 			.m = 7645,
2115783ec2eSXo Wang 			.b = 100,
2125783ec2eSXo Wang 			.R = -2,
2135783ec2eSXo Wang 		},
2145783ec2eSXo Wang 		[PSC_POWER] = {
2155783ec2eSXo Wang 			.m = 1701,
2165783ec2eSXo Wang 			.b = -4000,
2175783ec2eSXo Wang 			.R = -3,
2185783ec2eSXo Wang 		},
2195783ec2eSXo Wang 		[PSC_POWER_L] = {
2205783ec2eSXo Wang 			.m = 861,
2215783ec2eSXo Wang 			.b = -965,
2225783ec2eSXo Wang 			.R = -3,
2235783ec2eSXo Wang 		},
2245783ec2eSXo Wang 		[PSC_TEMPERATURE] = {
2255783ec2eSXo Wang 			.m = 16,
2265783ec2eSXo Wang 		},
2275783ec2eSXo Wang 	},
228e53e6497SGuenter Roeck };
229e53e6497SGuenter Roeck 
23003e9bd8dSGuenter Roeck struct lm25066_data {
23103e9bd8dSGuenter Roeck 	int id;
232c8ccab7aSGuenter Roeck 	u16 rlimit;			/* Maximum register value */
23303e9bd8dSGuenter Roeck 	struct pmbus_driver_info info;
23403e9bd8dSGuenter Roeck };
23503e9bd8dSGuenter Roeck 
23603e9bd8dSGuenter Roeck #define to_lm25066_data(x)  container_of(x, struct lm25066_data, info)
23703e9bd8dSGuenter Roeck 
lm25066_read_word_data(struct i2c_client * client,int page,int phase,int reg)23843f33b6eSGuenter Roeck static int lm25066_read_word_data(struct i2c_client *client, int page,
23943f33b6eSGuenter Roeck 				  int phase, int reg)
24003e9bd8dSGuenter Roeck {
24103e9bd8dSGuenter Roeck 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
24203e9bd8dSGuenter Roeck 	const struct lm25066_data *data = to_lm25066_data(info);
24303e9bd8dSGuenter Roeck 	int ret;
24403e9bd8dSGuenter Roeck 
24503e9bd8dSGuenter Roeck 	switch (reg) {
246a7c69118SGuenter Roeck 	case PMBUS_VIRT_READ_VMON:
24743f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff, LM25066_READ_VAUX);
24803e9bd8dSGuenter Roeck 		if (ret < 0)
24903e9bd8dSGuenter Roeck 			break;
250a7c69118SGuenter Roeck 		/* Adjust returned value to match VIN coefficients */
25103e9bd8dSGuenter Roeck 		switch (data->id) {
25258615a94SGuenter Roeck 		case lm25056:
25358615a94SGuenter Roeck 			/* VIN: 6.14 mV VAUX: 293 uV LSB */
25458615a94SGuenter Roeck 			ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
25558615a94SGuenter Roeck 			break;
25603e9bd8dSGuenter Roeck 		case lm25066:
257a7c69118SGuenter Roeck 			/* VIN: 4.54 mV VAUX: 283.2 uV LSB */
25803e9bd8dSGuenter Roeck 			ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
25903e9bd8dSGuenter Roeck 			break;
26003e9bd8dSGuenter Roeck 		case lm5064:
261a7c69118SGuenter Roeck 			/* VIN: 4.53 mV VAUX: 700 uV LSB */
26203e9bd8dSGuenter Roeck 			ret = DIV_ROUND_CLOSEST(ret * 70, 453);
26303e9bd8dSGuenter Roeck 			break;
26403e9bd8dSGuenter Roeck 		case lm5066:
2655783ec2eSXo Wang 		case lm5066i:
266a7c69118SGuenter Roeck 			/* VIN: 2.18 mV VAUX: 725 uV LSB */
26703e9bd8dSGuenter Roeck 			ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
26803e9bd8dSGuenter Roeck 			break;
26903e9bd8dSGuenter Roeck 		}
27003e9bd8dSGuenter Roeck 		break;
27103e9bd8dSGuenter Roeck 	case PMBUS_READ_IIN:
27243f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
27343f33b6eSGuenter Roeck 					   LM25066_MFR_READ_IIN);
27403e9bd8dSGuenter Roeck 		break;
27503e9bd8dSGuenter Roeck 	case PMBUS_READ_PIN:
27643f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
27743f33b6eSGuenter Roeck 					   LM25066_MFR_READ_PIN);
27803e9bd8dSGuenter Roeck 		break;
27903e9bd8dSGuenter Roeck 	case PMBUS_IIN_OC_WARN_LIMIT:
28043f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
28103e9bd8dSGuenter Roeck 					   LM25066_MFR_IIN_OC_WARN_LIMIT);
28203e9bd8dSGuenter Roeck 		break;
28303e9bd8dSGuenter Roeck 	case PMBUS_PIN_OP_WARN_LIMIT:
28443f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
28503e9bd8dSGuenter Roeck 					   LM25066_MFR_PIN_OP_WARN_LIMIT);
28603e9bd8dSGuenter Roeck 		break;
28703e9bd8dSGuenter Roeck 	case PMBUS_VIRT_READ_VIN_AVG:
28843f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
28943f33b6eSGuenter Roeck 					   LM25066_READ_AVG_VIN);
29003e9bd8dSGuenter Roeck 		break;
29103e9bd8dSGuenter Roeck 	case PMBUS_VIRT_READ_VOUT_AVG:
29243f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
29343f33b6eSGuenter Roeck 					   LM25066_READ_AVG_VOUT);
29403e9bd8dSGuenter Roeck 		break;
29503e9bd8dSGuenter Roeck 	case PMBUS_VIRT_READ_IIN_AVG:
29643f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
29743f33b6eSGuenter Roeck 					   LM25066_READ_AVG_IIN);
29803e9bd8dSGuenter Roeck 		break;
29903e9bd8dSGuenter Roeck 	case PMBUS_VIRT_READ_PIN_AVG:
30043f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
30143f33b6eSGuenter Roeck 					   LM25066_READ_AVG_PIN);
30203e9bd8dSGuenter Roeck 		break;
30303e9bd8dSGuenter Roeck 	case PMBUS_VIRT_READ_PIN_MAX:
30443f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
30543f33b6eSGuenter Roeck 					   LM25066_READ_PIN_PEAK);
30603e9bd8dSGuenter Roeck 		break;
30703e9bd8dSGuenter Roeck 	case PMBUS_VIRT_RESET_PIN_HISTORY:
30803e9bd8dSGuenter Roeck 		ret = 0;
30903e9bd8dSGuenter Roeck 		break;
3105d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 	case PMBUS_VIRT_SAMPLES:
3115d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		ret = pmbus_read_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG);
3125d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		if (ret < 0)
3135d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 			break;
3145d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		ret = 1 << ret;
3155d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		break;
31603e9bd8dSGuenter Roeck 	default:
31703e9bd8dSGuenter Roeck 		ret = -ENODATA;
31803e9bd8dSGuenter Roeck 		break;
31903e9bd8dSGuenter Roeck 	}
32003e9bd8dSGuenter Roeck 	return ret;
32103e9bd8dSGuenter Roeck }
32203e9bd8dSGuenter Roeck 
lm25056_read_word_data(struct i2c_client * client,int page,int phase,int reg)32343f33b6eSGuenter Roeck static int lm25056_read_word_data(struct i2c_client *client, int page,
32443f33b6eSGuenter Roeck 				  int phase, int reg)
32558615a94SGuenter Roeck {
32658615a94SGuenter Roeck 	int ret;
32758615a94SGuenter Roeck 
32858615a94SGuenter Roeck 	switch (reg) {
32958615a94SGuenter Roeck 	case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
33043f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
33158615a94SGuenter Roeck 					   LM25056_VAUX_UV_WARN_LIMIT);
33258615a94SGuenter Roeck 		if (ret < 0)
33358615a94SGuenter Roeck 			break;
33458615a94SGuenter Roeck 		/* Adjust returned value to match VIN coefficients */
33558615a94SGuenter Roeck 		ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
33658615a94SGuenter Roeck 		break;
33758615a94SGuenter Roeck 	case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
33843f33b6eSGuenter Roeck 		ret = pmbus_read_word_data(client, 0, 0xff,
33958615a94SGuenter Roeck 					   LM25056_VAUX_OV_WARN_LIMIT);
34058615a94SGuenter Roeck 		if (ret < 0)
34158615a94SGuenter Roeck 			break;
34258615a94SGuenter Roeck 		/* Adjust returned value to match VIN coefficients */
34358615a94SGuenter Roeck 		ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
34458615a94SGuenter Roeck 		break;
34558615a94SGuenter Roeck 	default:
34643f33b6eSGuenter Roeck 		ret = lm25066_read_word_data(client, page, phase, reg);
34758615a94SGuenter Roeck 		break;
34858615a94SGuenter Roeck 	}
34958615a94SGuenter Roeck 	return ret;
35058615a94SGuenter Roeck }
35158615a94SGuenter Roeck 
lm25056_read_byte_data(struct i2c_client * client,int page,int reg)35258615a94SGuenter Roeck static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
35358615a94SGuenter Roeck {
35458615a94SGuenter Roeck 	int ret, s;
35558615a94SGuenter Roeck 
35658615a94SGuenter Roeck 	switch (reg) {
35758615a94SGuenter Roeck 	case PMBUS_VIRT_STATUS_VMON:
35858615a94SGuenter Roeck 		ret = pmbus_read_byte_data(client, 0,
35958615a94SGuenter Roeck 					   PMBUS_STATUS_MFR_SPECIFIC);
36058615a94SGuenter Roeck 		if (ret < 0)
36158615a94SGuenter Roeck 			break;
36258615a94SGuenter Roeck 		s = 0;
36358615a94SGuenter Roeck 		if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
36458615a94SGuenter Roeck 			s |= PB_VOLTAGE_UV_WARNING;
36558615a94SGuenter Roeck 		if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
36658615a94SGuenter Roeck 			s |= PB_VOLTAGE_OV_WARNING;
36758615a94SGuenter Roeck 		ret = s;
36858615a94SGuenter Roeck 		break;
36958615a94SGuenter Roeck 	default:
37058615a94SGuenter Roeck 		ret = -ENODATA;
37158615a94SGuenter Roeck 		break;
37258615a94SGuenter Roeck 	}
37358615a94SGuenter Roeck 	return ret;
37458615a94SGuenter Roeck }
37558615a94SGuenter Roeck 
lm25066_write_word_data(struct i2c_client * client,int page,int reg,u16 word)37603e9bd8dSGuenter Roeck static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
37703e9bd8dSGuenter Roeck 				   u16 word)
37803e9bd8dSGuenter Roeck {
379c8ccab7aSGuenter Roeck 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
380c8ccab7aSGuenter Roeck 	const struct lm25066_data *data = to_lm25066_data(info);
38103e9bd8dSGuenter Roeck 	int ret;
38203e9bd8dSGuenter Roeck 
38303e9bd8dSGuenter Roeck 	switch (reg) {
384c8ccab7aSGuenter Roeck 	case PMBUS_POUT_OP_FAULT_LIMIT:
385c8ccab7aSGuenter Roeck 	case PMBUS_POUT_OP_WARN_LIMIT:
3862507abb3SGuenter Roeck 	case PMBUS_VOUT_UV_WARN_LIMIT:
3872507abb3SGuenter Roeck 	case PMBUS_OT_FAULT_LIMIT:
3882507abb3SGuenter Roeck 	case PMBUS_OT_WARN_LIMIT:
389c8ccab7aSGuenter Roeck 	case PMBUS_IIN_OC_FAULT_LIMIT:
3902507abb3SGuenter Roeck 	case PMBUS_VIN_UV_WARN_LIMIT:
391c8ccab7aSGuenter Roeck 	case PMBUS_VIN_UV_FAULT_LIMIT:
392c8ccab7aSGuenter Roeck 	case PMBUS_VIN_OV_FAULT_LIMIT:
3932507abb3SGuenter Roeck 	case PMBUS_VIN_OV_WARN_LIMIT:
394c8ccab7aSGuenter Roeck 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
3952507abb3SGuenter Roeck 		ret = pmbus_write_word_data(client, 0, reg, word);
3962507abb3SGuenter Roeck 		break;
39703e9bd8dSGuenter Roeck 	case PMBUS_IIN_OC_WARN_LIMIT:
398c8ccab7aSGuenter Roeck 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
39903e9bd8dSGuenter Roeck 		ret = pmbus_write_word_data(client, 0,
40003e9bd8dSGuenter Roeck 					    LM25066_MFR_IIN_OC_WARN_LIMIT,
40103e9bd8dSGuenter Roeck 					    word);
40203e9bd8dSGuenter Roeck 		break;
40303e9bd8dSGuenter Roeck 	case PMBUS_PIN_OP_WARN_LIMIT:
404c8ccab7aSGuenter Roeck 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
40503e9bd8dSGuenter Roeck 		ret = pmbus_write_word_data(client, 0,
40603e9bd8dSGuenter Roeck 					    LM25066_MFR_PIN_OP_WARN_LIMIT,
40703e9bd8dSGuenter Roeck 					    word);
40803e9bd8dSGuenter Roeck 		break;
40958615a94SGuenter Roeck 	case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
41058615a94SGuenter Roeck 		/* Adjust from VIN coefficients (for LM25056) */
41158615a94SGuenter Roeck 		word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
412c8ccab7aSGuenter Roeck 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
41358615a94SGuenter Roeck 		ret = pmbus_write_word_data(client, 0,
41458615a94SGuenter Roeck 					    LM25056_VAUX_UV_WARN_LIMIT, word);
41558615a94SGuenter Roeck 		break;
41658615a94SGuenter Roeck 	case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
41758615a94SGuenter Roeck 		/* Adjust from VIN coefficients (for LM25056) */
41858615a94SGuenter Roeck 		word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
419c8ccab7aSGuenter Roeck 		word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
42058615a94SGuenter Roeck 		ret = pmbus_write_word_data(client, 0,
42158615a94SGuenter Roeck 					    LM25056_VAUX_OV_WARN_LIMIT, word);
42258615a94SGuenter Roeck 		break;
42303e9bd8dSGuenter Roeck 	case PMBUS_VIRT_RESET_PIN_HISTORY:
42403e9bd8dSGuenter Roeck 		ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
42503e9bd8dSGuenter Roeck 		break;
4265d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 	case PMBUS_VIRT_SAMPLES:
4275d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		word = clamp_val(word, 1, LM25066_SAMPLES_FOR_AVG_MAX);
4285d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		ret = pmbus_write_byte_data(client, 0, LM25066_SAMPLES_FOR_AVG,
4295d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 					    ilog2(word));
4305d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 		break;
43103e9bd8dSGuenter Roeck 	default:
43203e9bd8dSGuenter Roeck 		ret = -ENODATA;
43303e9bd8dSGuenter Roeck 		break;
43403e9bd8dSGuenter Roeck 	}
43503e9bd8dSGuenter Roeck 	return ret;
43603e9bd8dSGuenter Roeck }
43703e9bd8dSGuenter Roeck 
438da78ad2bSZev Weiss #if IS_ENABLED(CONFIG_SENSORS_LM25066_REGULATOR)
439da78ad2bSZev Weiss static const struct regulator_desc lm25066_reg_desc[] = {
4408f832d23SGuenter Roeck 	PMBUS_REGULATOR_ONE("vout"),
441da78ad2bSZev Weiss };
442da78ad2bSZev Weiss #endif
443da78ad2bSZev Weiss 
4446d2ff184SZev Weiss static const struct i2c_device_id lm25066_id[] = {
4456d2ff184SZev Weiss 	{"lm25056", lm25056},
4466d2ff184SZev Weiss 	{"lm25066", lm25066},
4476d2ff184SZev Weiss 	{"lm5064", lm5064},
4486d2ff184SZev Weiss 	{"lm5066", lm5066},
4496d2ff184SZev Weiss 	{"lm5066i", lm5066i},
4506d2ff184SZev Weiss 	{ }
4516d2ff184SZev Weiss };
4526d2ff184SZev Weiss MODULE_DEVICE_TABLE(i2c, lm25066_id);
4536d2ff184SZev Weiss 
454b7792f3eSZev Weiss static const struct of_device_id __maybe_unused lm25066_of_match[] = {
455b7792f3eSZev Weiss 	{ .compatible = "ti,lm25056", .data = (void *)lm25056, },
456b7792f3eSZev Weiss 	{ .compatible = "ti,lm25066", .data = (void *)lm25066, },
457b7792f3eSZev Weiss 	{ .compatible = "ti,lm5064",  .data = (void *)lm5064,  },
458b7792f3eSZev Weiss 	{ .compatible = "ti,lm5066",  .data = (void *)lm5066,  },
459b7792f3eSZev Weiss 	{ .compatible = "ti,lm5066i", .data = (void *)lm5066i, },
460b7792f3eSZev Weiss 	{ },
461b7792f3eSZev Weiss };
462b7792f3eSZev Weiss MODULE_DEVICE_TABLE(of, lm25066_of_match);
463b7792f3eSZev Weiss 
lm25066_probe(struct i2c_client * client)464dd431939SStephen Kitt static int lm25066_probe(struct i2c_client *client)
46503e9bd8dSGuenter Roeck {
46603e9bd8dSGuenter Roeck 	int config;
46794ee5fccSZev Weiss 	u32 shunt;
46803e9bd8dSGuenter Roeck 	struct lm25066_data *data;
46903e9bd8dSGuenter Roeck 	struct pmbus_driver_info *info;
470df60a5daSZev Weiss 	const struct __coeff *coeff;
47103e9bd8dSGuenter Roeck 
47203e9bd8dSGuenter Roeck 	if (!i2c_check_functionality(client->adapter,
47303e9bd8dSGuenter Roeck 				     I2C_FUNC_SMBUS_READ_BYTE_DATA))
47403e9bd8dSGuenter Roeck 		return -ENODEV;
47503e9bd8dSGuenter Roeck 
4768b313ca7SGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
4778b313ca7SGuenter Roeck 			    GFP_KERNEL);
47803e9bd8dSGuenter Roeck 	if (!data)
47903e9bd8dSGuenter Roeck 		return -ENOMEM;
48003e9bd8dSGuenter Roeck 
48103e9bd8dSGuenter Roeck 	config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
4828b313ca7SGuenter Roeck 	if (config < 0)
4838b313ca7SGuenter Roeck 		return config;
48403e9bd8dSGuenter Roeck 
485ac0c26baSRob Herring 	data->id = (enum chips)(unsigned long)i2c_get_match_data(client);
486b7792f3eSZev Weiss 
48703e9bd8dSGuenter Roeck 	info = &data->info;
48803e9bd8dSGuenter Roeck 
489a7c69118SGuenter Roeck 	info->pages = 1;
49003e9bd8dSGuenter Roeck 	info->format[PSC_VOLTAGE_IN] = direct;
49103e9bd8dSGuenter Roeck 	info->format[PSC_VOLTAGE_OUT] = direct;
49203e9bd8dSGuenter Roeck 	info->format[PSC_CURRENT_IN] = direct;
49303e9bd8dSGuenter Roeck 	info->format[PSC_TEMPERATURE] = direct;
49403e9bd8dSGuenter Roeck 	info->format[PSC_POWER] = direct;
49503e9bd8dSGuenter Roeck 
49658615a94SGuenter Roeck 	info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
49758615a94SGuenter Roeck 	  | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
4985d9e8b3fSAdamski, Krzysztof (Nokia - PL/Wroclaw) 	  | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_SAMPLES;
49903e9bd8dSGuenter Roeck 
50058615a94SGuenter Roeck 	if (data->id == lm25056) {
50158615a94SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_STATUS_VMON;
50258615a94SGuenter Roeck 		info->read_word_data = lm25056_read_word_data;
50358615a94SGuenter Roeck 		info->read_byte_data = lm25056_read_byte_data;
504c8ccab7aSGuenter Roeck 		data->rlimit = 0x0fff;
50558615a94SGuenter Roeck 	} else {
50658615a94SGuenter Roeck 		info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
50703e9bd8dSGuenter Roeck 		info->read_word_data = lm25066_read_word_data;
508c8ccab7aSGuenter Roeck 		data->rlimit = 0x0fff;
50958615a94SGuenter Roeck 	}
51003e9bd8dSGuenter Roeck 	info->write_word_data = lm25066_write_word_data;
51103e9bd8dSGuenter Roeck 
512e53e6497SGuenter Roeck 	coeff = &lm25066_coeff[data->id][0];
513e53e6497SGuenter Roeck 	info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
514e53e6497SGuenter Roeck 	info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
515e53e6497SGuenter Roeck 	info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
516e53e6497SGuenter Roeck 	info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
517e53e6497SGuenter Roeck 	info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
518e53e6497SGuenter Roeck 	info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
519e53e6497SGuenter Roeck 	info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
520e53e6497SGuenter Roeck 	info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
521e53e6497SGuenter Roeck 	info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
522e53e6497SGuenter Roeck 	info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
523e53e6497SGuenter Roeck 	info->R[PSC_POWER] = coeff[PSC_POWER].R;
52403e9bd8dSGuenter Roeck 	if (config & LM25066_DEV_SETUP_CL) {
525e53e6497SGuenter Roeck 		info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
526ee983171SXo Wang 		info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
527e53e6497SGuenter Roeck 		info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
528ee983171SXo Wang 		info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
52903e9bd8dSGuenter Roeck 	} else {
530e53e6497SGuenter Roeck 		info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
531ee983171SXo Wang 		info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
532e53e6497SGuenter Roeck 		info->m[PSC_POWER] = coeff[PSC_POWER].m;
533ee983171SXo Wang 		info->b[PSC_POWER] = coeff[PSC_POWER].b;
53403e9bd8dSGuenter Roeck 	}
53503e9bd8dSGuenter Roeck 
53694ee5fccSZev Weiss 	/*
53794ee5fccSZev Weiss 	 * Values in the TI datasheets are normalized for a 1mOhm sense
53894ee5fccSZev Weiss 	 * resistor; assume that unless DT specifies a value explicitly.
53994ee5fccSZev Weiss 	 */
54094ee5fccSZev Weiss 	if (of_property_read_u32(client->dev.of_node, "shunt-resistor-micro-ohms", &shunt))
54194ee5fccSZev Weiss 		shunt = 1000;
54294ee5fccSZev Weiss 
54394ee5fccSZev Weiss 	info->m[PSC_CURRENT_IN] = info->m[PSC_CURRENT_IN] * shunt / 1000;
54494ee5fccSZev Weiss 	info->m[PSC_POWER] = info->m[PSC_POWER] * shunt / 1000;
54594ee5fccSZev Weiss 
546da78ad2bSZev Weiss #if IS_ENABLED(CONFIG_SENSORS_LM25066_REGULATOR)
547da78ad2bSZev Weiss 	/* LM25056 doesn't support OPERATION */
548da78ad2bSZev Weiss 	if (data->id != lm25056) {
549da78ad2bSZev Weiss 		info->num_regulators = ARRAY_SIZE(lm25066_reg_desc);
550da78ad2bSZev Weiss 		info->reg_desc = lm25066_reg_desc;
551da78ad2bSZev Weiss 	}
552da78ad2bSZev Weiss #endif
553da78ad2bSZev Weiss 
554dd431939SStephen Kitt 	return pmbus_do_probe(client, info);
55503e9bd8dSGuenter Roeck }
55603e9bd8dSGuenter Roeck 
55703e9bd8dSGuenter Roeck /* This is the driver that will be inserted */
55803e9bd8dSGuenter Roeck static struct i2c_driver lm25066_driver = {
55903e9bd8dSGuenter Roeck 	.driver = {
56003e9bd8dSGuenter Roeck 		   .name = "lm25066",
561b7792f3eSZev Weiss 		   .of_match_table = of_match_ptr(lm25066_of_match),
56203e9bd8dSGuenter Roeck 	},
5631975d167SUwe Kleine-König 	.probe = lm25066_probe,
56403e9bd8dSGuenter Roeck 	.id_table = lm25066_id,
56503e9bd8dSGuenter Roeck };
56603e9bd8dSGuenter Roeck 
567f0967eeaSAxel Lin module_i2c_driver(lm25066_driver);
56803e9bd8dSGuenter Roeck 
56903e9bd8dSGuenter Roeck MODULE_AUTHOR("Guenter Roeck");
570c8ccab7aSGuenter Roeck MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
57103e9bd8dSGuenter Roeck MODULE_LICENSE("GPL");
572b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS);
573