xref: /openbmc/linux/drivers/hwmon/pmbus/adm1275.c (revision dd5219ce)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
4  * and Digital Power Monitor
5  *
6  * Copyright (c) 2011 Ericsson AB.
7  * Copyright (c) 2018 Guenter Roeck
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/bitops.h>
17 #include <linux/bitfield.h>
18 #include <linux/log2.h>
19 #include "pmbus.h"
20 
21 enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
22 
23 #define ADM1275_MFR_STATUS_IOUT_WARN2	BIT(0)
24 #define ADM1293_MFR_STATUS_VAUX_UV_WARN	BIT(5)
25 #define ADM1293_MFR_STATUS_VAUX_OV_WARN	BIT(6)
26 
27 #define ADM1275_PEAK_IOUT		0xd0
28 #define ADM1275_PEAK_VIN		0xd1
29 #define ADM1275_PEAK_VOUT		0xd2
30 #define ADM1275_PMON_CONTROL		0xd3
31 #define ADM1275_PMON_CONFIG		0xd4
32 
33 #define ADM1275_CONVERT_EN		BIT(0)
34 
35 #define ADM1275_VIN_VOUT_SELECT		BIT(6)
36 #define ADM1275_VRANGE			BIT(5)
37 #define ADM1075_IRANGE_50		BIT(4)
38 #define ADM1075_IRANGE_25		BIT(3)
39 #define ADM1075_IRANGE_MASK		(BIT(3) | BIT(4))
40 
41 #define ADM1272_IRANGE			BIT(0)
42 
43 #define ADM1278_TSFILT			BIT(15)
44 #define ADM1278_TEMP1_EN		BIT(3)
45 #define ADM1278_VIN_EN			BIT(2)
46 #define ADM1278_VOUT_EN			BIT(1)
47 
48 #define ADM1278_PMON_DEFCONFIG		(ADM1278_VOUT_EN | ADM1278_TEMP1_EN | ADM1278_TSFILT)
49 
50 #define ADM1293_IRANGE_25		0
51 #define ADM1293_IRANGE_50		BIT(6)
52 #define ADM1293_IRANGE_100		BIT(7)
53 #define ADM1293_IRANGE_200		(BIT(6) | BIT(7))
54 #define ADM1293_IRANGE_MASK		(BIT(6) | BIT(7))
55 
56 #define ADM1293_VIN_SEL_012		BIT(2)
57 #define ADM1293_VIN_SEL_074		BIT(3)
58 #define ADM1293_VIN_SEL_210		(BIT(2) | BIT(3))
59 #define ADM1293_VIN_SEL_MASK		(BIT(2) | BIT(3))
60 
61 #define ADM1293_VAUX_EN			BIT(1)
62 
63 #define ADM1278_PEAK_TEMP		0xd7
64 #define ADM1275_IOUT_WARN2_LIMIT	0xd7
65 #define ADM1275_DEVICE_CONFIG		0xd8
66 
67 #define ADM1275_IOUT_WARN2_SELECT	BIT(4)
68 
69 #define ADM1276_PEAK_PIN		0xda
70 #define ADM1075_READ_VAUX		0xdd
71 #define ADM1075_VAUX_OV_WARN_LIMIT	0xde
72 #define ADM1075_VAUX_UV_WARN_LIMIT	0xdf
73 #define ADM1293_IOUT_MIN		0xe3
74 #define ADM1293_PIN_MIN			0xe4
75 #define ADM1075_VAUX_STATUS		0xf6
76 
77 #define ADM1075_VAUX_OV_WARN		BIT(7)
78 #define ADM1075_VAUX_UV_WARN		BIT(6)
79 
80 #define ADM1275_VI_AVG_SHIFT		0
81 #define ADM1275_VI_AVG_MASK		GENMASK(ADM1275_VI_AVG_SHIFT + 2, \
82 						ADM1275_VI_AVG_SHIFT)
83 #define ADM1275_SAMPLES_AVG_MAX		128
84 
85 #define ADM1278_PWR_AVG_SHIFT		11
86 #define ADM1278_PWR_AVG_MASK		GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \
87 						ADM1278_PWR_AVG_SHIFT)
88 #define ADM1278_VI_AVG_SHIFT		8
89 #define ADM1278_VI_AVG_MASK		GENMASK(ADM1278_VI_AVG_SHIFT + 2, \
90 						ADM1278_VI_AVG_SHIFT)
91 
92 struct adm1275_data {
93 	int id;
94 	bool have_oc_fault;
95 	bool have_uc_fault;
96 	bool have_vout;
97 	bool have_vaux_status;
98 	bool have_mfr_vaux_status;
99 	bool have_iout_min;
100 	bool have_pin_min;
101 	bool have_pin_max;
102 	bool have_temp_max;
103 	bool have_power_sampling;
104 	struct pmbus_driver_info info;
105 };
106 
107 #define to_adm1275_data(x)  container_of(x, struct adm1275_data, info)
108 
109 struct coefficients {
110 	s16 m;
111 	s16 b;
112 	s16 R;
113 };
114 
115 static const struct coefficients adm1075_coefficients[] = {
116 	[0] = { 27169, 0, -1 },		/* voltage */
117 	[1] = { 806, 20475, -1 },	/* current, irange25 */
118 	[2] = { 404, 20475, -1 },	/* current, irange50 */
119 	[3] = { 8549, 0, -1 },		/* power, irange25 */
120 	[4] = { 4279, 0, -1 },		/* power, irange50 */
121 };
122 
123 static const struct coefficients adm1272_coefficients[] = {
124 	[0] = { 6770, 0, -2 },		/* voltage, vrange 60V */
125 	[1] = { 4062, 0, -2 },		/* voltage, vrange 100V */
126 	[2] = { 1326, 20480, -1 },	/* current, vsense range 15mV */
127 	[3] = { 663, 20480, -1 },	/* current, vsense range 30mV */
128 	[4] = { 3512, 0, -2 },		/* power, vrange 60V, irange 15mV */
129 	[5] = { 21071, 0, -3 },		/* power, vrange 100V, irange 15mV */
130 	[6] = { 17561, 0, -3 },		/* power, vrange 60V, irange 30mV */
131 	[7] = { 10535, 0, -3 },		/* power, vrange 100V, irange 30mV */
132 	[8] = { 42, 31871, -1 },	/* temperature */
133 
134 };
135 
136 static const struct coefficients adm1275_coefficients[] = {
137 	[0] = { 19199, 0, -2 },		/* voltage, vrange set */
138 	[1] = { 6720, 0, -1 },		/* voltage, vrange not set */
139 	[2] = { 807, 20475, -1 },	/* current */
140 };
141 
142 static const struct coefficients adm1276_coefficients[] = {
143 	[0] = { 19199, 0, -2 },		/* voltage, vrange set */
144 	[1] = { 6720, 0, -1 },		/* voltage, vrange not set */
145 	[2] = { 807, 20475, -1 },	/* current */
146 	[3] = { 6043, 0, -2 },		/* power, vrange set */
147 	[4] = { 2115, 0, -1 },		/* power, vrange not set */
148 };
149 
150 static const struct coefficients adm1278_coefficients[] = {
151 	[0] = { 19599, 0, -2 },		/* voltage */
152 	[1] = { 800, 20475, -1 },	/* current */
153 	[2] = { 6123, 0, -2 },		/* power */
154 	[3] = { 42, 31880, -1 },	/* temperature */
155 };
156 
157 static const struct coefficients adm1293_coefficients[] = {
158 	[0] = { 3333, -1, 0 },		/* voltage, vrange 1.2V */
159 	[1] = { 5552, -5, -1 },		/* voltage, vrange 7.4V */
160 	[2] = { 19604, -50, -2 },	/* voltage, vrange 21V */
161 	[3] = { 8000, -100, -2 },	/* current, irange25 */
162 	[4] = { 4000, -100, -2 },	/* current, irange50 */
163 	[5] = { 20000, -1000, -3 },	/* current, irange100 */
164 	[6] = { 10000, -1000, -3 },	/* current, irange200 */
165 	[7] = { 10417, 0, -1 },		/* power, 1.2V, irange25 */
166 	[8] = { 5208, 0, -1 },		/* power, 1.2V, irange50 */
167 	[9] = { 26042, 0, -2 },		/* power, 1.2V, irange100 */
168 	[10] = { 13021, 0, -2 },	/* power, 1.2V, irange200 */
169 	[11] = { 17351, 0, -2 },	/* power, 7.4V, irange25 */
170 	[12] = { 8676, 0, -2 },		/* power, 7.4V, irange50 */
171 	[13] = { 4338, 0, -2 },		/* power, 7.4V, irange100 */
172 	[14] = { 21689, 0, -3 },	/* power, 7.4V, irange200 */
173 	[15] = { 6126, 0, -2 },		/* power, 21V, irange25 */
174 	[16] = { 30631, 0, -3 },	/* power, 21V, irange50 */
175 	[17] = { 15316, 0, -3 },	/* power, 21V, irange100 */
176 	[18] = { 7658, 0, -3 },		/* power, 21V, irange200 */
177 };
178 
adm1275_read_samples(const struct adm1275_data * data,struct i2c_client * client,bool is_power)179 static int adm1275_read_samples(const struct adm1275_data *data,
180 				struct i2c_client *client, bool is_power)
181 {
182 	int shift, ret;
183 	u16 mask;
184 
185 	/*
186 	 * The PMON configuration register is a 16-bit register only on chips
187 	 * supporting power average sampling. On other chips it is an 8-bit
188 	 * register.
189 	 */
190 	if (data->have_power_sampling) {
191 		ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
192 		mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
193 		shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
194 	} else {
195 		ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
196 		mask = ADM1275_VI_AVG_MASK;
197 		shift = ADM1275_VI_AVG_SHIFT;
198 	}
199 	if (ret < 0)
200 		return ret;
201 
202 	return (ret & mask) >> shift;
203 }
204 
adm1275_write_pmon_config(const struct adm1275_data * data,struct i2c_client * client,u16 word)205 static int adm1275_write_pmon_config(const struct adm1275_data *data,
206 				     struct i2c_client *client, u16 word)
207 {
208 	int ret, ret2;
209 
210 	ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONTROL, 0);
211 	if (ret)
212 		return ret;
213 
214 	if (data->have_power_sampling)
215 		ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
216 						word);
217 	else
218 		ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
219 						word);
220 
221 	/*
222 	 * We still want to re-enable conversions if writing into
223 	 * ADM1275_PMON_CONFIG failed.
224 	 */
225 	ret2 = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONTROL,
226 					 ADM1275_CONVERT_EN);
227 	if (!ret)
228 		ret = ret2;
229 
230 	return ret;
231 }
232 
adm1275_write_samples(const struct adm1275_data * data,struct i2c_client * client,bool is_power,u16 word)233 static int adm1275_write_samples(const struct adm1275_data *data,
234 				 struct i2c_client *client,
235 				 bool is_power, u16 word)
236 {
237 	int shift, ret;
238 	u16 mask;
239 
240 	if (data->have_power_sampling) {
241 		ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
242 		mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
243 		shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
244 	} else {
245 		ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
246 		mask = ADM1275_VI_AVG_MASK;
247 		shift = ADM1275_VI_AVG_SHIFT;
248 	}
249 	if (ret < 0)
250 		return ret;
251 
252 	word = (ret & ~mask) | ((word << shift) & mask);
253 
254 	return adm1275_write_pmon_config(data, client, word);
255 }
256 
adm1275_read_word_data(struct i2c_client * client,int page,int phase,int reg)257 static int adm1275_read_word_data(struct i2c_client *client, int page,
258 				  int phase, int reg)
259 {
260 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
261 	const struct adm1275_data *data = to_adm1275_data(info);
262 	int ret = 0;
263 
264 	if (page > 0)
265 		return -ENXIO;
266 
267 	switch (reg) {
268 	case PMBUS_IOUT_UC_FAULT_LIMIT:
269 		if (!data->have_uc_fault)
270 			return -ENXIO;
271 		ret = pmbus_read_word_data(client, 0, 0xff,
272 					   ADM1275_IOUT_WARN2_LIMIT);
273 		break;
274 	case PMBUS_IOUT_OC_FAULT_LIMIT:
275 		if (!data->have_oc_fault)
276 			return -ENXIO;
277 		ret = pmbus_read_word_data(client, 0, 0xff,
278 					   ADM1275_IOUT_WARN2_LIMIT);
279 		break;
280 	case PMBUS_VOUT_OV_WARN_LIMIT:
281 		if (data->have_vout)
282 			return -ENODATA;
283 		ret = pmbus_read_word_data(client, 0, 0xff,
284 					   ADM1075_VAUX_OV_WARN_LIMIT);
285 		break;
286 	case PMBUS_VOUT_UV_WARN_LIMIT:
287 		if (data->have_vout)
288 			return -ENODATA;
289 		ret = pmbus_read_word_data(client, 0, 0xff,
290 					   ADM1075_VAUX_UV_WARN_LIMIT);
291 		break;
292 	case PMBUS_READ_VOUT:
293 		if (data->have_vout)
294 			return -ENODATA;
295 		ret = pmbus_read_word_data(client, 0, 0xff,
296 					   ADM1075_READ_VAUX);
297 		break;
298 	case PMBUS_VIRT_READ_IOUT_MIN:
299 		if (!data->have_iout_min)
300 			return -ENXIO;
301 		ret = pmbus_read_word_data(client, 0, 0xff,
302 					   ADM1293_IOUT_MIN);
303 		break;
304 	case PMBUS_VIRT_READ_IOUT_MAX:
305 		ret = pmbus_read_word_data(client, 0, 0xff,
306 					   ADM1275_PEAK_IOUT);
307 		break;
308 	case PMBUS_VIRT_READ_VOUT_MAX:
309 		ret = pmbus_read_word_data(client, 0, 0xff,
310 					   ADM1275_PEAK_VOUT);
311 		break;
312 	case PMBUS_VIRT_READ_VIN_MAX:
313 		ret = pmbus_read_word_data(client, 0, 0xff,
314 					   ADM1275_PEAK_VIN);
315 		break;
316 	case PMBUS_VIRT_READ_PIN_MIN:
317 		if (!data->have_pin_min)
318 			return -ENXIO;
319 		ret = pmbus_read_word_data(client, 0, 0xff,
320 					   ADM1293_PIN_MIN);
321 		break;
322 	case PMBUS_VIRT_READ_PIN_MAX:
323 		if (!data->have_pin_max)
324 			return -ENXIO;
325 		ret = pmbus_read_word_data(client, 0, 0xff,
326 					   ADM1276_PEAK_PIN);
327 		break;
328 	case PMBUS_VIRT_READ_TEMP_MAX:
329 		if (!data->have_temp_max)
330 			return -ENXIO;
331 		ret = pmbus_read_word_data(client, 0, 0xff,
332 					   ADM1278_PEAK_TEMP);
333 		break;
334 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
335 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
336 	case PMBUS_VIRT_RESET_VIN_HISTORY:
337 		break;
338 	case PMBUS_VIRT_RESET_PIN_HISTORY:
339 		if (!data->have_pin_max)
340 			return -ENXIO;
341 		break;
342 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
343 		if (!data->have_temp_max)
344 			return -ENXIO;
345 		break;
346 	case PMBUS_VIRT_POWER_SAMPLES:
347 		if (!data->have_power_sampling)
348 			return -ENXIO;
349 		ret = adm1275_read_samples(data, client, true);
350 		if (ret < 0)
351 			break;
352 		ret = BIT(ret);
353 		break;
354 	case PMBUS_VIRT_IN_SAMPLES:
355 	case PMBUS_VIRT_CURR_SAMPLES:
356 		ret = adm1275_read_samples(data, client, false);
357 		if (ret < 0)
358 			break;
359 		ret = BIT(ret);
360 		break;
361 	default:
362 		ret = -ENODATA;
363 		break;
364 	}
365 	return ret;
366 }
367 
adm1275_write_word_data(struct i2c_client * client,int page,int reg,u16 word)368 static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
369 				   u16 word)
370 {
371 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
372 	const struct adm1275_data *data = to_adm1275_data(info);
373 	int ret;
374 
375 	if (page > 0)
376 		return -ENXIO;
377 
378 	switch (reg) {
379 	case PMBUS_IOUT_UC_FAULT_LIMIT:
380 	case PMBUS_IOUT_OC_FAULT_LIMIT:
381 		ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
382 					    word);
383 		break;
384 	case PMBUS_VIRT_RESET_IOUT_HISTORY:
385 		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
386 		if (!ret && data->have_iout_min)
387 			ret = pmbus_write_word_data(client, 0,
388 						    ADM1293_IOUT_MIN, 0);
389 		break;
390 	case PMBUS_VIRT_RESET_VOUT_HISTORY:
391 		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
392 		break;
393 	case PMBUS_VIRT_RESET_VIN_HISTORY:
394 		ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
395 		break;
396 	case PMBUS_VIRT_RESET_PIN_HISTORY:
397 		ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
398 		if (!ret && data->have_pin_min)
399 			ret = pmbus_write_word_data(client, 0,
400 						    ADM1293_PIN_MIN, 0);
401 		break;
402 	case PMBUS_VIRT_RESET_TEMP_HISTORY:
403 		ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
404 		break;
405 	case PMBUS_VIRT_POWER_SAMPLES:
406 		if (!data->have_power_sampling)
407 			return -ENXIO;
408 		word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
409 		ret = adm1275_write_samples(data, client, true, ilog2(word));
410 		break;
411 	case PMBUS_VIRT_IN_SAMPLES:
412 	case PMBUS_VIRT_CURR_SAMPLES:
413 		word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
414 		ret = adm1275_write_samples(data, client, false, ilog2(word));
415 		break;
416 	default:
417 		ret = -ENODATA;
418 		break;
419 	}
420 	return ret;
421 }
422 
adm1275_read_byte_data(struct i2c_client * client,int page,int reg)423 static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
424 {
425 	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
426 	const struct adm1275_data *data = to_adm1275_data(info);
427 	int mfr_status, ret;
428 
429 	if (page > 0)
430 		return -ENXIO;
431 
432 	switch (reg) {
433 	case PMBUS_STATUS_IOUT:
434 		ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
435 		if (ret < 0)
436 			break;
437 		if (!data->have_oc_fault && !data->have_uc_fault)
438 			break;
439 		mfr_status = pmbus_read_byte_data(client, page,
440 						  PMBUS_STATUS_MFR_SPECIFIC);
441 		if (mfr_status < 0)
442 			return mfr_status;
443 		if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
444 			ret |= data->have_oc_fault ?
445 			  PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
446 		}
447 		break;
448 	case PMBUS_STATUS_VOUT:
449 		if (data->have_vout)
450 			return -ENODATA;
451 		ret = 0;
452 		if (data->have_vaux_status) {
453 			mfr_status = pmbus_read_byte_data(client, 0,
454 							  ADM1075_VAUX_STATUS);
455 			if (mfr_status < 0)
456 				return mfr_status;
457 			if (mfr_status & ADM1075_VAUX_OV_WARN)
458 				ret |= PB_VOLTAGE_OV_WARNING;
459 			if (mfr_status & ADM1075_VAUX_UV_WARN)
460 				ret |= PB_VOLTAGE_UV_WARNING;
461 		} else if (data->have_mfr_vaux_status) {
462 			mfr_status = pmbus_read_byte_data(client, page,
463 						PMBUS_STATUS_MFR_SPECIFIC);
464 			if (mfr_status < 0)
465 				return mfr_status;
466 			if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
467 				ret |= PB_VOLTAGE_OV_WARNING;
468 			if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
469 				ret |= PB_VOLTAGE_UV_WARNING;
470 		}
471 		break;
472 	default:
473 		ret = -ENODATA;
474 		break;
475 	}
476 	return ret;
477 }
478 
479 static const struct i2c_device_id adm1275_id[] = {
480 	{ "adm1075", adm1075 },
481 	{ "adm1272", adm1272 },
482 	{ "adm1275", adm1275 },
483 	{ "adm1276", adm1276 },
484 	{ "adm1278", adm1278 },
485 	{ "adm1293", adm1293 },
486 	{ "adm1294", adm1294 },
487 	{ }
488 };
489 MODULE_DEVICE_TABLE(i2c, adm1275_id);
490 
491 /* Enable VOUT & TEMP1 if not enabled (disabled by default) */
adm1275_enable_vout_temp(struct adm1275_data * data,struct i2c_client * client,int config)492 static int adm1275_enable_vout_temp(struct adm1275_data *data,
493 				    struct i2c_client *client, int config)
494 {
495 	int ret;
496 
497 	if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) {
498 		config |= ADM1278_PMON_DEFCONFIG;
499 		ret = adm1275_write_pmon_config(data, client, config);
500 		if (ret < 0) {
501 			dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n");
502 			return ret;
503 		}
504 	}
505 	return 0;
506 }
507 
adm1275_probe(struct i2c_client * client)508 static int adm1275_probe(struct i2c_client *client)
509 {
510 	s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
511 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
512 	int config, device_config;
513 	int ret;
514 	struct pmbus_driver_info *info;
515 	struct adm1275_data *data;
516 	const struct i2c_device_id *mid;
517 	const struct coefficients *coefficients;
518 	int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
519 	int tindex = -1;
520 	u32 shunt;
521 	u32 avg;
522 
523 	if (!i2c_check_functionality(client->adapter,
524 				     I2C_FUNC_SMBUS_READ_BYTE_DATA
525 				     | I2C_FUNC_SMBUS_BLOCK_DATA))
526 		return -ENODEV;
527 
528 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
529 	if (ret < 0) {
530 		dev_err(&client->dev, "Failed to read Manufacturer ID\n");
531 		return ret;
532 	}
533 	if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
534 		dev_err(&client->dev, "Unsupported Manufacturer ID\n");
535 		return -ENODEV;
536 	}
537 
538 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
539 	if (ret < 0) {
540 		dev_err(&client->dev, "Failed to read Manufacturer Model\n");
541 		return ret;
542 	}
543 	for (mid = adm1275_id; mid->name[0]; mid++) {
544 		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
545 			break;
546 	}
547 	if (!mid->name[0]) {
548 		dev_err(&client->dev, "Unsupported device\n");
549 		return -ENODEV;
550 	}
551 
552 	if (strcmp(client->name, mid->name) != 0)
553 		dev_notice(&client->dev,
554 			   "Device mismatch: Configured %s, detected %s\n",
555 			   client->name, mid->name);
556 
557 	if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
558 	    mid->driver_data == adm1293 || mid->driver_data == adm1294)
559 		config_read_fn = i2c_smbus_read_word_data;
560 	else
561 		config_read_fn = i2c_smbus_read_byte_data;
562 	config = config_read_fn(client, ADM1275_PMON_CONFIG);
563 	if (config < 0)
564 		return config;
565 
566 	device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG);
567 	if (device_config < 0)
568 		return device_config;
569 
570 	data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
571 			    GFP_KERNEL);
572 	if (!data)
573 		return -ENOMEM;
574 
575 	if (of_property_read_u32(client->dev.of_node,
576 				 "shunt-resistor-micro-ohms", &shunt))
577 		shunt = 1000; /* 1 mOhm if not set via DT */
578 
579 	if (shunt == 0)
580 		return -EINVAL;
581 
582 	data->id = mid->driver_data;
583 
584 	info = &data->info;
585 
586 	info->pages = 1;
587 	info->format[PSC_VOLTAGE_IN] = direct;
588 	info->format[PSC_VOLTAGE_OUT] = direct;
589 	info->format[PSC_CURRENT_OUT] = direct;
590 	info->format[PSC_POWER] = direct;
591 	info->format[PSC_TEMPERATURE] = direct;
592 	info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
593 			PMBUS_HAVE_SAMPLES;
594 
595 	info->read_word_data = adm1275_read_word_data;
596 	info->read_byte_data = adm1275_read_byte_data;
597 	info->write_word_data = adm1275_write_word_data;
598 
599 	switch (data->id) {
600 	case adm1075:
601 		if (device_config & ADM1275_IOUT_WARN2_SELECT)
602 			data->have_oc_fault = true;
603 		else
604 			data->have_uc_fault = true;
605 		data->have_pin_max = true;
606 		data->have_vaux_status = true;
607 
608 		coefficients = adm1075_coefficients;
609 		vindex = 0;
610 		switch (config & ADM1075_IRANGE_MASK) {
611 		case ADM1075_IRANGE_25:
612 			cindex = 1;
613 			pindex = 3;
614 			break;
615 		case ADM1075_IRANGE_50:
616 			cindex = 2;
617 			pindex = 4;
618 			break;
619 		default:
620 			dev_err(&client->dev, "Invalid input current range");
621 			break;
622 		}
623 
624 		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
625 		  | PMBUS_HAVE_STATUS_INPUT;
626 		if (config & ADM1275_VIN_VOUT_SELECT)
627 			info->func[0] |=
628 			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
629 		break;
630 	case adm1272:
631 		data->have_vout = true;
632 		data->have_pin_max = true;
633 		data->have_temp_max = true;
634 		data->have_power_sampling = true;
635 
636 		coefficients = adm1272_coefficients;
637 		vindex = (config & ADM1275_VRANGE) ? 1 : 0;
638 		cindex = (config & ADM1272_IRANGE) ? 3 : 2;
639 		/* pindex depends on the combination of the above */
640 		switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
641 		case 0:
642 		default:
643 			pindex = 4;
644 			break;
645 		case ADM1275_VRANGE:
646 			pindex = 5;
647 			break;
648 		case ADM1272_IRANGE:
649 			pindex = 6;
650 			break;
651 		case ADM1275_VRANGE | ADM1272_IRANGE:
652 			pindex = 7;
653 			break;
654 		}
655 		tindex = 8;
656 
657 		info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
658 			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
659 			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
660 
661 		ret = adm1275_enable_vout_temp(data, client, config);
662 		if (ret)
663 			return ret;
664 
665 		if (config & ADM1278_VIN_EN)
666 			info->func[0] |= PMBUS_HAVE_VIN;
667 		break;
668 	case adm1275:
669 		if (device_config & ADM1275_IOUT_WARN2_SELECT)
670 			data->have_oc_fault = true;
671 		else
672 			data->have_uc_fault = true;
673 		data->have_vout = true;
674 
675 		coefficients = adm1275_coefficients;
676 		vindex = (config & ADM1275_VRANGE) ? 0 : 1;
677 		cindex = 2;
678 
679 		if (config & ADM1275_VIN_VOUT_SELECT)
680 			info->func[0] |=
681 			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
682 		else
683 			info->func[0] |=
684 			  PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
685 		break;
686 	case adm1276:
687 		if (device_config & ADM1275_IOUT_WARN2_SELECT)
688 			data->have_oc_fault = true;
689 		else
690 			data->have_uc_fault = true;
691 		data->have_vout = true;
692 		data->have_pin_max = true;
693 
694 		coefficients = adm1276_coefficients;
695 		vindex = (config & ADM1275_VRANGE) ? 0 : 1;
696 		cindex = 2;
697 		pindex = (config & ADM1275_VRANGE) ? 3 : 4;
698 
699 		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
700 		  | PMBUS_HAVE_STATUS_INPUT;
701 		if (config & ADM1275_VIN_VOUT_SELECT)
702 			info->func[0] |=
703 			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
704 		break;
705 	case adm1278:
706 		data->have_vout = true;
707 		data->have_pin_max = true;
708 		data->have_temp_max = true;
709 		data->have_power_sampling = true;
710 
711 		coefficients = adm1278_coefficients;
712 		vindex = 0;
713 		cindex = 1;
714 		pindex = 2;
715 		tindex = 3;
716 
717 		info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
718 			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
719 			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
720 
721 		ret = adm1275_enable_vout_temp(data, client, config);
722 		if (ret)
723 			return ret;
724 
725 		if (config & ADM1278_VIN_EN)
726 			info->func[0] |= PMBUS_HAVE_VIN;
727 		break;
728 	case adm1293:
729 	case adm1294:
730 		data->have_iout_min = true;
731 		data->have_pin_min = true;
732 		data->have_pin_max = true;
733 		data->have_mfr_vaux_status = true;
734 		data->have_power_sampling = true;
735 
736 		coefficients = adm1293_coefficients;
737 
738 		voindex = 0;
739 		switch (config & ADM1293_VIN_SEL_MASK) {
740 		case ADM1293_VIN_SEL_012:	/* 1.2V */
741 			vindex = 0;
742 			break;
743 		case ADM1293_VIN_SEL_074:	/* 7.4V */
744 			vindex = 1;
745 			break;
746 		case ADM1293_VIN_SEL_210:	/* 21V */
747 			vindex = 2;
748 			break;
749 		default:			/* disabled */
750 			break;
751 		}
752 
753 		switch (config & ADM1293_IRANGE_MASK) {
754 		case ADM1293_IRANGE_25:
755 			cindex = 3;
756 			break;
757 		case ADM1293_IRANGE_50:
758 			cindex = 4;
759 			break;
760 		case ADM1293_IRANGE_100:
761 			cindex = 5;
762 			break;
763 		case ADM1293_IRANGE_200:
764 			cindex = 6;
765 			break;
766 		}
767 
768 		if (vindex >= 0)
769 			pindex = 7 + vindex * 4 + (cindex - 3);
770 
771 		if (config & ADM1293_VAUX_EN)
772 			info->func[0] |=
773 				PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
774 
775 		info->func[0] |= PMBUS_HAVE_PIN |
776 			PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
777 
778 		break;
779 	default:
780 		dev_err(&client->dev, "Unsupported device\n");
781 		return -ENODEV;
782 	}
783 
784 	if (data->have_power_sampling &&
785 	    of_property_read_u32(client->dev.of_node,
786 				 "adi,power-sample-average", &avg) == 0) {
787 		if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
788 		    BIT(__fls(avg)) != avg) {
789 			dev_err(&client->dev,
790 				"Invalid number of power samples");
791 			return -EINVAL;
792 		}
793 		ret = adm1275_write_samples(data, client, true, ilog2(avg));
794 		if (ret < 0) {
795 			dev_err(&client->dev,
796 				"Setting power sample averaging failed with error %d",
797 				ret);
798 			return ret;
799 		}
800 	}
801 
802 	if (of_property_read_u32(client->dev.of_node,
803 				"adi,volt-curr-sample-average", &avg) == 0) {
804 		if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
805 		    BIT(__fls(avg)) != avg) {
806 			dev_err(&client->dev,
807 				"Invalid number of voltage/current samples");
808 			return -EINVAL;
809 		}
810 		ret = adm1275_write_samples(data, client, false, ilog2(avg));
811 		if (ret < 0) {
812 			dev_err(&client->dev,
813 				"Setting voltage and current sample averaging failed with error %d",
814 				ret);
815 			return ret;
816 		}
817 	}
818 
819 	if (voindex < 0)
820 		voindex = vindex;
821 	if (vindex >= 0) {
822 		info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
823 		info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
824 		info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
825 	}
826 	if (voindex >= 0) {
827 		info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
828 		info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
829 		info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
830 	}
831 	if (cindex >= 0) {
832 		/* Scale current with sense resistor value */
833 		info->m[PSC_CURRENT_OUT] =
834 			coefficients[cindex].m * shunt / 1000;
835 		info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
836 		info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
837 	}
838 	if (pindex >= 0) {
839 		info->m[PSC_POWER] =
840 			coefficients[pindex].m * shunt / 1000;
841 		info->b[PSC_POWER] = coefficients[pindex].b;
842 		info->R[PSC_POWER] = coefficients[pindex].R;
843 	}
844 	if (tindex >= 0) {
845 		info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
846 		info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
847 		info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
848 	}
849 
850 	return pmbus_do_probe(client, info);
851 }
852 
853 static struct i2c_driver adm1275_driver = {
854 	.driver = {
855 		   .name = "adm1275",
856 		   },
857 	.probe = adm1275_probe,
858 	.id_table = adm1275_id,
859 };
860 
861 module_i2c_driver(adm1275_driver);
862 
863 MODULE_AUTHOR("Guenter Roeck");
864 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
865 MODULE_LICENSE("GPL");
866 MODULE_IMPORT_NS(PMBUS);
867