xref: /linux/drivers/hwmon/pmbus/adm1275.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
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, adm1281, 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 	{ "adm1281", adm1281 },
486 	{ "adm1293", adm1293 },
487 	{ "adm1294", adm1294 },
488 	{ }
489 };
490 MODULE_DEVICE_TABLE(i2c, adm1275_id);
491 
492 /* Enable VOUT & TEMP1 if not enabled (disabled by default) */
adm1275_enable_vout_temp(struct adm1275_data * data,struct i2c_client * client,int config)493 static int adm1275_enable_vout_temp(struct adm1275_data *data,
494 				    struct i2c_client *client, int config)
495 {
496 	int ret;
497 
498 	if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) {
499 		config |= ADM1278_PMON_DEFCONFIG;
500 		ret = adm1275_write_pmon_config(data, client, config);
501 		if (ret < 0) {
502 			dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n");
503 			return ret;
504 		}
505 	}
506 	return 0;
507 }
508 
adm1275_probe(struct i2c_client * client)509 static int adm1275_probe(struct i2c_client *client)
510 {
511 	s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
512 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
513 	int config, device_config;
514 	int ret;
515 	struct pmbus_driver_info *info;
516 	struct adm1275_data *data;
517 	const struct i2c_device_id *mid;
518 	const struct coefficients *coefficients;
519 	int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
520 	int tindex = -1;
521 	u32 shunt;
522 	u32 avg;
523 
524 	if (!i2c_check_functionality(client->adapter,
525 				     I2C_FUNC_SMBUS_READ_BYTE_DATA
526 				     | I2C_FUNC_SMBUS_BLOCK_DATA))
527 		return -ENODEV;
528 
529 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
530 	if (ret < 0) {
531 		dev_err(&client->dev, "Failed to read Manufacturer ID\n");
532 		return ret;
533 	}
534 	if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
535 		dev_err(&client->dev, "Unsupported Manufacturer ID\n");
536 		return -ENODEV;
537 	}
538 
539 	ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
540 	if (ret < 0) {
541 		dev_err(&client->dev, "Failed to read Manufacturer Model\n");
542 		return ret;
543 	}
544 	for (mid = adm1275_id; mid->name[0]; mid++) {
545 		if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
546 			break;
547 	}
548 	if (!mid->name[0]) {
549 		dev_err(&client->dev, "Unsupported device\n");
550 		return -ENODEV;
551 	}
552 
553 	if (strcmp(client->name, mid->name) != 0)
554 		dev_notice(&client->dev,
555 			   "Device mismatch: Configured %s, detected %s\n",
556 			   client->name, mid->name);
557 
558 	if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
559 	    mid->driver_data == adm1281 || mid->driver_data == adm1293 ||
560 	    mid->driver_data == adm1294)
561 		config_read_fn = i2c_smbus_read_word_data;
562 	else
563 		config_read_fn = i2c_smbus_read_byte_data;
564 	config = config_read_fn(client, ADM1275_PMON_CONFIG);
565 	if (config < 0)
566 		return config;
567 
568 	device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG);
569 	if (device_config < 0)
570 		return device_config;
571 
572 	data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
573 			    GFP_KERNEL);
574 	if (!data)
575 		return -ENOMEM;
576 
577 	if (of_property_read_u32(client->dev.of_node,
578 				 "shunt-resistor-micro-ohms", &shunt))
579 		shunt = 1000; /* 1 mOhm if not set via DT */
580 
581 	if (shunt == 0)
582 		return -EINVAL;
583 
584 	data->id = mid->driver_data;
585 
586 	info = &data->info;
587 
588 	info->pages = 1;
589 	info->format[PSC_VOLTAGE_IN] = direct;
590 	info->format[PSC_VOLTAGE_OUT] = direct;
591 	info->format[PSC_CURRENT_OUT] = direct;
592 	info->format[PSC_POWER] = direct;
593 	info->format[PSC_TEMPERATURE] = direct;
594 	info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
595 			PMBUS_HAVE_SAMPLES;
596 
597 	info->read_word_data = adm1275_read_word_data;
598 	info->read_byte_data = adm1275_read_byte_data;
599 	info->write_word_data = adm1275_write_word_data;
600 
601 	switch (data->id) {
602 	case adm1075:
603 		if (device_config & ADM1275_IOUT_WARN2_SELECT)
604 			data->have_oc_fault = true;
605 		else
606 			data->have_uc_fault = true;
607 		data->have_pin_max = true;
608 		data->have_vaux_status = true;
609 
610 		coefficients = adm1075_coefficients;
611 		vindex = 0;
612 		switch (config & ADM1075_IRANGE_MASK) {
613 		case ADM1075_IRANGE_25:
614 			cindex = 1;
615 			pindex = 3;
616 			break;
617 		case ADM1075_IRANGE_50:
618 			cindex = 2;
619 			pindex = 4;
620 			break;
621 		default:
622 			dev_err(&client->dev, "Invalid input current range");
623 			break;
624 		}
625 
626 		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
627 		  | PMBUS_HAVE_STATUS_INPUT;
628 		if (config & ADM1275_VIN_VOUT_SELECT)
629 			info->func[0] |=
630 			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
631 		break;
632 	case adm1272:
633 		data->have_vout = true;
634 		data->have_pin_max = true;
635 		data->have_temp_max = true;
636 		data->have_power_sampling = true;
637 
638 		coefficients = adm1272_coefficients;
639 		vindex = (config & ADM1275_VRANGE) ? 1 : 0;
640 		cindex = (config & ADM1272_IRANGE) ? 3 : 2;
641 		/* pindex depends on the combination of the above */
642 		switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
643 		case 0:
644 		default:
645 			pindex = 4;
646 			break;
647 		case ADM1275_VRANGE:
648 			pindex = 5;
649 			break;
650 		case ADM1272_IRANGE:
651 			pindex = 6;
652 			break;
653 		case ADM1275_VRANGE | ADM1272_IRANGE:
654 			pindex = 7;
655 			break;
656 		}
657 		tindex = 8;
658 
659 		info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
660 			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
661 			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
662 
663 		ret = adm1275_enable_vout_temp(data, client, config);
664 		if (ret)
665 			return ret;
666 
667 		if (config & ADM1278_VIN_EN)
668 			info->func[0] |= PMBUS_HAVE_VIN;
669 		break;
670 	case adm1275:
671 		if (device_config & ADM1275_IOUT_WARN2_SELECT)
672 			data->have_oc_fault = true;
673 		else
674 			data->have_uc_fault = true;
675 		data->have_vout = true;
676 
677 		coefficients = adm1275_coefficients;
678 		vindex = (config & ADM1275_VRANGE) ? 0 : 1;
679 		cindex = 2;
680 
681 		if (config & ADM1275_VIN_VOUT_SELECT)
682 			info->func[0] |=
683 			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
684 		else
685 			info->func[0] |=
686 			  PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
687 		break;
688 	case adm1276:
689 		if (device_config & ADM1275_IOUT_WARN2_SELECT)
690 			data->have_oc_fault = true;
691 		else
692 			data->have_uc_fault = true;
693 		data->have_vout = true;
694 		data->have_pin_max = true;
695 
696 		coefficients = adm1276_coefficients;
697 		vindex = (config & ADM1275_VRANGE) ? 0 : 1;
698 		cindex = 2;
699 		pindex = (config & ADM1275_VRANGE) ? 3 : 4;
700 
701 		info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
702 		  | PMBUS_HAVE_STATUS_INPUT;
703 		if (config & ADM1275_VIN_VOUT_SELECT)
704 			info->func[0] |=
705 			  PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
706 		break;
707 	case adm1278:
708 	case adm1281:
709 		data->have_vout = true;
710 		data->have_pin_max = true;
711 		data->have_temp_max = true;
712 		data->have_power_sampling = true;
713 
714 		coefficients = adm1278_coefficients;
715 		vindex = 0;
716 		cindex = 1;
717 		pindex = 2;
718 		tindex = 3;
719 
720 		info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
721 			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
722 			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
723 
724 		ret = adm1275_enable_vout_temp(data, client, config);
725 		if (ret)
726 			return ret;
727 
728 		if (config & ADM1278_VIN_EN)
729 			info->func[0] |= PMBUS_HAVE_VIN;
730 		break;
731 	case adm1293:
732 	case adm1294:
733 		data->have_iout_min = true;
734 		data->have_pin_min = true;
735 		data->have_pin_max = true;
736 		data->have_mfr_vaux_status = true;
737 		data->have_power_sampling = true;
738 
739 		coefficients = adm1293_coefficients;
740 
741 		voindex = 0;
742 		switch (config & ADM1293_VIN_SEL_MASK) {
743 		case ADM1293_VIN_SEL_012:	/* 1.2V */
744 			vindex = 0;
745 			break;
746 		case ADM1293_VIN_SEL_074:	/* 7.4V */
747 			vindex = 1;
748 			break;
749 		case ADM1293_VIN_SEL_210:	/* 21V */
750 			vindex = 2;
751 			break;
752 		default:			/* disabled */
753 			break;
754 		}
755 
756 		switch (config & ADM1293_IRANGE_MASK) {
757 		case ADM1293_IRANGE_25:
758 			cindex = 3;
759 			break;
760 		case ADM1293_IRANGE_50:
761 			cindex = 4;
762 			break;
763 		case ADM1293_IRANGE_100:
764 			cindex = 5;
765 			break;
766 		case ADM1293_IRANGE_200:
767 			cindex = 6;
768 			break;
769 		}
770 
771 		if (vindex >= 0)
772 			pindex = 7 + vindex * 4 + (cindex - 3);
773 
774 		if (config & ADM1293_VAUX_EN)
775 			info->func[0] |=
776 				PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
777 
778 		info->func[0] |= PMBUS_HAVE_PIN |
779 			PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
780 
781 		break;
782 	default:
783 		dev_err(&client->dev, "Unsupported device\n");
784 		return -ENODEV;
785 	}
786 
787 	if (data->have_power_sampling &&
788 	    of_property_read_u32(client->dev.of_node,
789 				 "adi,power-sample-average", &avg) == 0) {
790 		if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
791 		    BIT(__fls(avg)) != avg) {
792 			dev_err(&client->dev,
793 				"Invalid number of power samples");
794 			return -EINVAL;
795 		}
796 		ret = adm1275_write_samples(data, client, true, ilog2(avg));
797 		if (ret < 0) {
798 			dev_err(&client->dev,
799 				"Setting power sample averaging failed with error %d",
800 				ret);
801 			return ret;
802 		}
803 	}
804 
805 	if (of_property_read_u32(client->dev.of_node,
806 				"adi,volt-curr-sample-average", &avg) == 0) {
807 		if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
808 		    BIT(__fls(avg)) != avg) {
809 			dev_err(&client->dev,
810 				"Invalid number of voltage/current samples");
811 			return -EINVAL;
812 		}
813 		ret = adm1275_write_samples(data, client, false, ilog2(avg));
814 		if (ret < 0) {
815 			dev_err(&client->dev,
816 				"Setting voltage and current sample averaging failed with error %d",
817 				ret);
818 			return ret;
819 		}
820 	}
821 
822 	if (voindex < 0)
823 		voindex = vindex;
824 	if (vindex >= 0) {
825 		info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
826 		info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
827 		info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
828 	}
829 	if (voindex >= 0) {
830 		info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
831 		info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
832 		info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
833 	}
834 	if (cindex >= 0) {
835 		/* Scale current with sense resistor value */
836 		info->m[PSC_CURRENT_OUT] =
837 			coefficients[cindex].m * shunt / 1000;
838 		info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
839 		info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
840 	}
841 	if (pindex >= 0) {
842 		info->m[PSC_POWER] =
843 			coefficients[pindex].m * shunt / 1000;
844 		info->b[PSC_POWER] = coefficients[pindex].b;
845 		info->R[PSC_POWER] = coefficients[pindex].R;
846 	}
847 	if (tindex >= 0) {
848 		info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
849 		info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
850 		info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
851 	}
852 
853 	return pmbus_do_probe(client, info);
854 }
855 
856 static struct i2c_driver adm1275_driver = {
857 	.driver = {
858 		   .name = "adm1275",
859 		   },
860 	.probe = adm1275_probe,
861 	.id_table = adm1275_id,
862 };
863 
864 module_i2c_driver(adm1275_driver);
865 
866 MODULE_AUTHOR("Guenter Roeck");
867 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
868 MODULE_LICENSE("GPL");
869 MODULE_IMPORT_NS(PMBUS);
870