blob: 82052b6611c9d6b260cef33b7a6d806494c07101 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Guenter Roeck83f76492011-03-17 13:16:01 -07002/*
3 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
4 * and Digital Power Monitor
5 *
6 * Copyright (c) 2011 Ericsson AB.
Guenter Roeck4ff0ce22018-03-10 18:59:04 -08007 * Copyright (c) 2018 Guenter Roeck
Guenter Roeck83f76492011-03-17 13:16:01 -07008 */
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>
Guenter Roeck99b41602015-07-04 09:19:48 -070016#include <linux/bitops.h>
Guenter Roeck83f76492011-03-17 13:16:01 -070017#include "pmbus.h"
18
Guenter Roeck4ff0ce22018-03-10 18:59:04 -080019enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
Guenter Roeck68a40382015-03-17 13:19:51 -070020
21#define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0)
22#define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
23#define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
Guenter Roeck5cf231a2011-07-14 11:55:35 -070024
Guenter Roeckc576e302011-07-09 11:17:33 -070025#define ADM1275_PEAK_IOUT 0xd0
26#define ADM1275_PEAK_VIN 0xd1
27#define ADM1275_PEAK_VOUT 0xd2
Guenter Roeck83f76492011-03-17 13:16:01 -070028#define ADM1275_PMON_CONFIG 0xd4
29
Guenter Roeck99b41602015-07-04 09:19:48 -070030#define ADM1275_VIN_VOUT_SELECT BIT(6)
31#define ADM1275_VRANGE BIT(5)
32#define ADM1075_IRANGE_50 BIT(4)
33#define ADM1075_IRANGE_25 BIT(3)
34#define ADM1075_IRANGE_MASK (BIT(3) | BIT(4))
Guenter Roeck83f76492011-03-17 13:16:01 -070035
Guenter Roeck4ff0ce22018-03-10 18:59:04 -080036#define ADM1272_IRANGE BIT(0)
37
Guenter Roeck709066ac2015-07-05 11:04:56 -070038#define ADM1278_TEMP1_EN BIT(3)
39#define ADM1278_VIN_EN BIT(2)
40#define ADM1278_VOUT_EN BIT(1)
41
Guenter Roeck68a40382015-03-17 13:19:51 -070042#define ADM1293_IRANGE_25 0
43#define ADM1293_IRANGE_50 BIT(6)
44#define ADM1293_IRANGE_100 BIT(7)
45#define ADM1293_IRANGE_200 (BIT(6) | BIT(7))
46#define ADM1293_IRANGE_MASK (BIT(6) | BIT(7))
47
48#define ADM1293_VIN_SEL_012 BIT(2)
49#define ADM1293_VIN_SEL_074 BIT(3)
50#define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3))
51#define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3))
52
53#define ADM1293_VAUX_EN BIT(1)
54
Guenter Roeck709066ac2015-07-05 11:04:56 -070055#define ADM1278_PEAK_TEMP 0xd7
Guenter Roeckc5e67632011-08-02 11:08:57 -070056#define ADM1275_IOUT_WARN2_LIMIT 0xd7
57#define ADM1275_DEVICE_CONFIG 0xd8
58
Guenter Roeck99b41602015-07-04 09:19:48 -070059#define ADM1275_IOUT_WARN2_SELECT BIT(4)
Guenter Roeckc5e67632011-08-02 11:08:57 -070060
Guenter Roeck5cf231a2011-07-14 11:55:35 -070061#define ADM1276_PEAK_PIN 0xda
Guenter Roeck92711262012-02-24 03:40:53 -080062#define ADM1075_READ_VAUX 0xdd
63#define ADM1075_VAUX_OV_WARN_LIMIT 0xde
64#define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
Guenter Roeck68a40382015-03-17 13:19:51 -070065#define ADM1293_IOUT_MIN 0xe3
66#define ADM1293_PIN_MIN 0xe4
Guenter Roeck92711262012-02-24 03:40:53 -080067#define ADM1075_VAUX_STATUS 0xf6
68
Guenter Roeck99b41602015-07-04 09:19:48 -070069#define ADM1075_VAUX_OV_WARN BIT(7)
70#define ADM1075_VAUX_UV_WARN BIT(6)
Guenter Roeck92711262012-02-24 03:40:53 -080071
Guenter Roeckc5e67632011-08-02 11:08:57 -070072struct adm1275_data {
Guenter Roeck5cf231a2011-07-14 11:55:35 -070073 int id;
Guenter Roeckc5e67632011-08-02 11:08:57 -070074 bool have_oc_fault;
Guenter Roeck90485392015-07-04 10:09:54 -070075 bool have_uc_fault;
76 bool have_vout;
77 bool have_vaux_status;
Guenter Roeck68a40382015-03-17 13:19:51 -070078 bool have_mfr_vaux_status;
79 bool have_iout_min;
80 bool have_pin_min;
Guenter Roeck90485392015-07-04 10:09:54 -070081 bool have_pin_max;
Guenter Roeck709066ac2015-07-05 11:04:56 -070082 bool have_temp_max;
Guenter Roeckc5e67632011-08-02 11:08:57 -070083 struct pmbus_driver_info info;
84};
85
86#define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
87
Guenter Roeck904b2962015-07-04 08:39:18 -070088struct coefficients {
89 s16 m;
90 s16 b;
91 s16 R;
92};
93
94static const struct coefficients adm1075_coefficients[] = {
95 [0] = { 27169, 0, -1 }, /* voltage */
96 [1] = { 806, 20475, -1 }, /* current, irange25 */
97 [2] = { 404, 20475, -1 }, /* current, irange50 */
Shikhar Dogra6faecba2017-03-27 16:16:44 -070098 [3] = { 8549, 0, -1 }, /* power, irange25 */
99 [4] = { 4279, 0, -1 }, /* power, irange50 */
Guenter Roeck904b2962015-07-04 08:39:18 -0700100};
101
Guenter Roeck4ff0ce22018-03-10 18:59:04 -0800102static const struct coefficients adm1272_coefficients[] = {
103 [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */
104 [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */
105 [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */
106 [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */
107 [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */
108 [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */
109 [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */
110 [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */
111 [8] = { 42, 31871, -1 }, /* temperature */
112
113};
114
Guenter Roeck904b2962015-07-04 08:39:18 -0700115static const struct coefficients adm1275_coefficients[] = {
116 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
117 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
118 [2] = { 807, 20475, -1 }, /* current */
119};
120
121static const struct coefficients adm1276_coefficients[] = {
122 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
123 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
124 [2] = { 807, 20475, -1 }, /* current */
125 [3] = { 6043, 0, -2 }, /* power, vrange set */
126 [4] = { 2115, 0, -1 }, /* power, vrange not set */
127};
128
Guenter Roeck709066ac2015-07-05 11:04:56 -0700129static const struct coefficients adm1278_coefficients[] = {
130 [0] = { 19599, 0, -2 }, /* voltage */
131 [1] = { 800, 20475, -1 }, /* current */
132 [2] = { 6123, 0, -2 }, /* power */
133 [3] = { 42, 31880, -1 }, /* temperature */
134};
135
Guenter Roeck68a40382015-03-17 13:19:51 -0700136static const struct coefficients adm1293_coefficients[] = {
137 [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */
138 [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */
139 [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */
140 [3] = { 8000, -100, -2 }, /* current, irange25 */
141 [4] = { 4000, -100, -2 }, /* current, irange50 */
142 [5] = { 20000, -1000, -3 }, /* current, irange100 */
143 [6] = { 10000, -1000, -3 }, /* current, irange200 */
144 [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */
145 [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */
146 [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */
147 [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */
148 [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */
149 [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */
150 [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */
151 [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */
152 [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */
153 [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */
154 [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */
155 [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
156};
157
Guenter Roeckc576e302011-07-09 11:17:33 -0700158static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
159{
Guenter Roeckc5e67632011-08-02 11:08:57 -0700160 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
161 const struct adm1275_data *data = to_adm1275_data(info);
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700162 int ret = 0;
Guenter Roeckc576e302011-07-09 11:17:33 -0700163
Guenter Roeckecb29ab2018-03-10 17:55:47 -0800164 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700165 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700166
167 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700168 case PMBUS_IOUT_UC_FAULT_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700169 if (!data->have_uc_fault)
170 return -ENXIO;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700171 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
172 break;
173 case PMBUS_IOUT_OC_FAULT_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700174 if (!data->have_oc_fault)
175 return -ENXIO;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700176 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
177 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800178 case PMBUS_VOUT_OV_WARN_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700179 if (data->have_vout)
180 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800181 ret = pmbus_read_word_data(client, 0,
182 ADM1075_VAUX_OV_WARN_LIMIT);
183 break;
184 case PMBUS_VOUT_UV_WARN_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700185 if (data->have_vout)
186 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800187 ret = pmbus_read_word_data(client, 0,
188 ADM1075_VAUX_UV_WARN_LIMIT);
189 break;
190 case PMBUS_READ_VOUT:
Guenter Roeck90485392015-07-04 10:09:54 -0700191 if (data->have_vout)
192 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800193 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
194 break;
Guenter Roeck68a40382015-03-17 13:19:51 -0700195 case PMBUS_VIRT_READ_IOUT_MIN:
196 if (!data->have_iout_min)
197 return -ENXIO;
198 ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN);
199 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700200 case PMBUS_VIRT_READ_IOUT_MAX:
201 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
202 break;
203 case PMBUS_VIRT_READ_VOUT_MAX:
204 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
205 break;
206 case PMBUS_VIRT_READ_VIN_MAX:
207 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
208 break;
Guenter Roeck68a40382015-03-17 13:19:51 -0700209 case PMBUS_VIRT_READ_PIN_MIN:
210 if (!data->have_pin_min)
211 return -ENXIO;
212 ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN);
213 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700214 case PMBUS_VIRT_READ_PIN_MAX:
Guenter Roeck90485392015-07-04 10:09:54 -0700215 if (!data->have_pin_max)
216 return -ENXIO;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700217 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
218 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700219 case PMBUS_VIRT_READ_TEMP_MAX:
220 if (!data->have_temp_max)
221 return -ENXIO;
222 ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP);
223 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700224 case PMBUS_VIRT_RESET_IOUT_HISTORY:
225 case PMBUS_VIRT_RESET_VOUT_HISTORY:
226 case PMBUS_VIRT_RESET_VIN_HISTORY:
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700227 break;
228 case PMBUS_VIRT_RESET_PIN_HISTORY:
Guenter Roeck90485392015-07-04 10:09:54 -0700229 if (!data->have_pin_max)
230 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700231 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700232 case PMBUS_VIRT_RESET_TEMP_HISTORY:
233 if (!data->have_temp_max)
234 return -ENXIO;
235 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700236 default:
237 ret = -ENODATA;
238 break;
239 }
240 return ret;
241}
242
243static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
244 u16 word)
245{
Guenter Roeck68a40382015-03-17 13:19:51 -0700246 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
247 const struct adm1275_data *data = to_adm1275_data(info);
Guenter Roeckc576e302011-07-09 11:17:33 -0700248 int ret;
249
Guenter Roeckecb29ab2018-03-10 17:55:47 -0800250 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700251 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700252
253 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700254 case PMBUS_IOUT_UC_FAULT_LIMIT:
255 case PMBUS_IOUT_OC_FAULT_LIMIT:
256 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
257 word);
258 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700259 case PMBUS_VIRT_RESET_IOUT_HISTORY:
260 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
Guenter Roeck68a40382015-03-17 13:19:51 -0700261 if (!ret && data->have_iout_min)
262 ret = pmbus_write_word_data(client, 0,
263 ADM1293_IOUT_MIN, 0);
Guenter Roeckc576e302011-07-09 11:17:33 -0700264 break;
265 case PMBUS_VIRT_RESET_VOUT_HISTORY:
266 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
267 break;
268 case PMBUS_VIRT_RESET_VIN_HISTORY:
269 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
270 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700271 case PMBUS_VIRT_RESET_PIN_HISTORY:
272 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
Guenter Roeck68a40382015-03-17 13:19:51 -0700273 if (!ret && data->have_pin_min)
274 ret = pmbus_write_word_data(client, 0,
275 ADM1293_PIN_MIN, 0);
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700276 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700277 case PMBUS_VIRT_RESET_TEMP_HISTORY:
278 ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
279 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700280 default:
281 ret = -ENODATA;
282 break;
283 }
284 return ret;
285}
286
Guenter Roeckc5e67632011-08-02 11:08:57 -0700287static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
288{
289 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
290 const struct adm1275_data *data = to_adm1275_data(info);
291 int mfr_status, ret;
292
Guenter Roeckda8e48a2011-07-29 22:19:39 -0700293 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700294 return -ENXIO;
295
296 switch (reg) {
297 case PMBUS_STATUS_IOUT:
298 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
299 if (ret < 0)
300 break;
Guenter Roeck90485392015-07-04 10:09:54 -0700301 if (!data->have_oc_fault && !data->have_uc_fault)
302 break;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700303 mfr_status = pmbus_read_byte_data(client, page,
304 PMBUS_STATUS_MFR_SPECIFIC);
Guenter Roeck90485392015-07-04 10:09:54 -0700305 if (mfr_status < 0)
306 return mfr_status;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700307 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
308 ret |= data->have_oc_fault ?
309 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
310 }
311 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800312 case PMBUS_STATUS_VOUT:
Guenter Roeck90485392015-07-04 10:09:54 -0700313 if (data->have_vout)
314 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800315 ret = 0;
Guenter Roeck90485392015-07-04 10:09:54 -0700316 if (data->have_vaux_status) {
317 mfr_status = pmbus_read_byte_data(client, 0,
318 ADM1075_VAUX_STATUS);
319 if (mfr_status < 0)
320 return mfr_status;
321 if (mfr_status & ADM1075_VAUX_OV_WARN)
322 ret |= PB_VOLTAGE_OV_WARNING;
323 if (mfr_status & ADM1075_VAUX_UV_WARN)
324 ret |= PB_VOLTAGE_UV_WARNING;
Guenter Roeck68a40382015-03-17 13:19:51 -0700325 } else if (data->have_mfr_vaux_status) {
326 mfr_status = pmbus_read_byte_data(client, page,
327 PMBUS_STATUS_MFR_SPECIFIC);
328 if (mfr_status < 0)
329 return mfr_status;
330 if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
331 ret |= PB_VOLTAGE_OV_WARNING;
332 if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
333 ret |= PB_VOLTAGE_UV_WARNING;
Guenter Roeck90485392015-07-04 10:09:54 -0700334 }
Guenter Roeck92711262012-02-24 03:40:53 -0800335 break;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700336 default:
337 ret = -ENODATA;
338 break;
339 }
340 return ret;
341}
342
Guenter Roeck87102802011-09-30 11:53:25 -0700343static const struct i2c_device_id adm1275_id[] = {
Guenter Roeck92711262012-02-24 03:40:53 -0800344 { "adm1075", adm1075 },
Guenter Roeck4ff0ce22018-03-10 18:59:04 -0800345 { "adm1272", adm1272 },
Guenter Roeck87102802011-09-30 11:53:25 -0700346 { "adm1275", adm1275 },
347 { "adm1276", adm1276 },
Guenter Roeck709066ac2015-07-05 11:04:56 -0700348 { "adm1278", adm1278 },
Guenter Roeck68a40382015-03-17 13:19:51 -0700349 { "adm1293", adm1293 },
350 { "adm1294", adm1294 },
Guenter Roeck87102802011-09-30 11:53:25 -0700351 { }
352};
353MODULE_DEVICE_TABLE(i2c, adm1275_id);
354
Guenter Roeck83f76492011-03-17 13:16:01 -0700355static int adm1275_probe(struct i2c_client *client,
356 const struct i2c_device_id *id)
357{
Guenter Roeck87102802011-09-30 11:53:25 -0700358 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
Guenter Roeckc5e67632011-08-02 11:08:57 -0700359 int config, device_config;
Guenter Roeck3b33ca42011-06-30 02:30:03 -0700360 int ret;
Guenter Roeck83f76492011-03-17 13:16:01 -0700361 struct pmbus_driver_info *info;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700362 struct adm1275_data *data;
Guenter Roeck87102802011-09-30 11:53:25 -0700363 const struct i2c_device_id *mid;
Guenter Roeck904b2962015-07-04 08:39:18 -0700364 const struct coefficients *coefficients;
Guenter Roeck68a40382015-03-17 13:19:51 -0700365 int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700366 int tindex = -1;
Kun Yi6e5c06a2018-10-17 15:26:39 -0700367 u32 shunt;
Guenter Roeck83f76492011-03-17 13:16:01 -0700368
369 if (!i2c_check_functionality(client->adapter,
Guenter Roeck87102802011-09-30 11:53:25 -0700370 I2C_FUNC_SMBUS_READ_BYTE_DATA
371 | I2C_FUNC_SMBUS_BLOCK_DATA))
Guenter Roeck83f76492011-03-17 13:16:01 -0700372 return -ENODEV;
373
Guenter Roeck87102802011-09-30 11:53:25 -0700374 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
375 if (ret < 0) {
376 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
377 return ret;
378 }
379 if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
380 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
381 return -ENODEV;
382 }
383
384 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
385 if (ret < 0) {
386 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
387 return ret;
388 }
389 for (mid = adm1275_id; mid->name[0]; mid++) {
390 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
391 break;
392 }
393 if (!mid->name[0]) {
394 dev_err(&client->dev, "Unsupported device\n");
395 return -ENODEV;
396 }
397
398 if (id->driver_data != mid->driver_data)
399 dev_notice(&client->dev,
400 "Device mismatch: Configured %s, detected %s\n",
401 id->name, mid->name);
402
403 config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
404 if (config < 0)
405 return config;
406
407 device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
408 if (device_config < 0)
409 return device_config;
410
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800411 data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
412 GFP_KERNEL);
Guenter Roeckc5e67632011-08-02 11:08:57 -0700413 if (!data)
Guenter Roeck83f76492011-03-17 13:16:01 -0700414 return -ENOMEM;
415
Kun Yi6e5c06a2018-10-17 15:26:39 -0700416 if (of_property_read_u32(client->dev.of_node,
417 "shunt-resistor-micro-ohms", &shunt))
418 shunt = 1000; /* 1 mOhm if not set via DT */
419
420 if (shunt == 0)
421 return -EINVAL;
422
Guenter Roeck87102802011-09-30 11:53:25 -0700423 data->id = mid->driver_data;
Guenter Roeck83f76492011-03-17 13:16:01 -0700424
Guenter Roeckc5e67632011-08-02 11:08:57 -0700425 info = &data->info;
426
Guenter Roeck83f76492011-03-17 13:16:01 -0700427 info->pages = 1;
Guenter Roeck1061d852011-06-25 11:21:49 -0700428 info->format[PSC_VOLTAGE_IN] = direct;
429 info->format[PSC_VOLTAGE_OUT] = direct;
430 info->format[PSC_CURRENT_OUT] = direct;
Guenter Roeck904b2962015-07-04 08:39:18 -0700431 info->format[PSC_POWER] = direct;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700432 info->format[PSC_TEMPERATURE] = direct;
Guenter Roeck83f76492011-03-17 13:16:01 -0700433 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
434
Guenter Roeckc576e302011-07-09 11:17:33 -0700435 info->read_word_data = adm1275_read_word_data;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700436 info->read_byte_data = adm1275_read_byte_data;
Guenter Roeckc576e302011-07-09 11:17:33 -0700437 info->write_word_data = adm1275_write_word_data;
438
Guenter Roeck87102802011-09-30 11:53:25 -0700439 switch (data->id) {
Guenter Roeck92711262012-02-24 03:40:53 -0800440 case adm1075:
Guenter Roeck90485392015-07-04 10:09:54 -0700441 if (device_config & ADM1275_IOUT_WARN2_SELECT)
442 data->have_oc_fault = true;
443 else
444 data->have_uc_fault = true;
445 data->have_pin_max = true;
446 data->have_vaux_status = true;
447
Guenter Roeck904b2962015-07-04 08:39:18 -0700448 coefficients = adm1075_coefficients;
449 vindex = 0;
Guenter Roeck92711262012-02-24 03:40:53 -0800450 switch (config & ADM1075_IRANGE_MASK) {
451 case ADM1075_IRANGE_25:
Guenter Roeck904b2962015-07-04 08:39:18 -0700452 cindex = 1;
453 pindex = 3;
Guenter Roeck92711262012-02-24 03:40:53 -0800454 break;
455 case ADM1075_IRANGE_50:
Guenter Roeck904b2962015-07-04 08:39:18 -0700456 cindex = 2;
457 pindex = 4;
Guenter Roeck92711262012-02-24 03:40:53 -0800458 break;
459 default:
460 dev_err(&client->dev, "Invalid input current range");
Guenter Roeck92711262012-02-24 03:40:53 -0800461 break;
462 }
Guenter Roeck904b2962015-07-04 08:39:18 -0700463
Guenter Roeck92711262012-02-24 03:40:53 -0800464 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
465 | PMBUS_HAVE_STATUS_INPUT;
466 if (config & ADM1275_VIN_VOUT_SELECT)
467 info->func[0] |=
468 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
469 break;
Guenter Roeck4ff0ce22018-03-10 18:59:04 -0800470 case adm1272:
471 data->have_vout = true;
472 data->have_pin_max = true;
473 data->have_temp_max = true;
474
475 coefficients = adm1272_coefficients;
476 vindex = (config & ADM1275_VRANGE) ? 1 : 0;
477 cindex = (config & ADM1272_IRANGE) ? 3 : 2;
478 /* pindex depends on the combination of the above */
479 switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
480 case 0:
481 default:
482 pindex = 4;
483 break;
484 case ADM1275_VRANGE:
485 pindex = 5;
486 break;
487 case ADM1272_IRANGE:
488 pindex = 6;
489 break;
490 case ADM1275_VRANGE | ADM1272_IRANGE:
491 pindex = 7;
492 break;
493 }
494 tindex = 8;
495
496 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
497 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
498
499 /* Enable VOUT if not enabled (it is disabled by default) */
500 if (!(config & ADM1278_VOUT_EN)) {
501 config |= ADM1278_VOUT_EN;
502 ret = i2c_smbus_write_byte_data(client,
503 ADM1275_PMON_CONFIG,
504 config);
505 if (ret < 0) {
506 dev_err(&client->dev,
507 "Failed to enable VOUT monitoring\n");
508 return -ENODEV;
509 }
510 }
511
512 if (config & ADM1278_TEMP1_EN)
513 info->func[0] |=
514 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
515 if (config & ADM1278_VIN_EN)
516 info->func[0] |= PMBUS_HAVE_VIN;
517 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700518 case adm1275:
Guenter Roeck90485392015-07-04 10:09:54 -0700519 if (device_config & ADM1275_IOUT_WARN2_SELECT)
520 data->have_oc_fault = true;
521 else
522 data->have_uc_fault = true;
523 data->have_vout = true;
524
Guenter Roeck904b2962015-07-04 08:39:18 -0700525 coefficients = adm1275_coefficients;
526 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
527 cindex = 2;
528
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700529 if (config & ADM1275_VIN_VOUT_SELECT)
530 info->func[0] |=
531 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
532 else
533 info->func[0] |=
534 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
535 break;
536 case adm1276:
Guenter Roeck90485392015-07-04 10:09:54 -0700537 if (device_config & ADM1275_IOUT_WARN2_SELECT)
538 data->have_oc_fault = true;
539 else
540 data->have_uc_fault = true;
541 data->have_vout = true;
542 data->have_pin_max = true;
543
Guenter Roeck904b2962015-07-04 08:39:18 -0700544 coefficients = adm1276_coefficients;
545 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
546 cindex = 2;
547 pindex = (config & ADM1275_VRANGE) ? 3 : 4;
548
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700549 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
550 | PMBUS_HAVE_STATUS_INPUT;
551 if (config & ADM1275_VIN_VOUT_SELECT)
552 info->func[0] |=
553 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700554 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700555 case adm1278:
556 data->have_vout = true;
557 data->have_pin_max = true;
558 data->have_temp_max = true;
559
560 coefficients = adm1278_coefficients;
561 vindex = 0;
562 cindex = 1;
563 pindex = 2;
564 tindex = 3;
565
Yi Li2b3d0c12016-10-17 18:38:53 +1030566 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
567 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
568
569 /* Enable VOUT if not enabled (it is disabled by default) */
570 if (!(config & ADM1278_VOUT_EN)) {
571 config |= ADM1278_VOUT_EN;
572 ret = i2c_smbus_write_byte_data(client,
573 ADM1275_PMON_CONFIG,
574 config);
575 if (ret < 0) {
576 dev_err(&client->dev,
577 "Failed to enable VOUT monitoring\n");
578 return -ENODEV;
579 }
580 }
581
Guenter Roeck709066ac2015-07-05 11:04:56 -0700582 if (config & ADM1278_TEMP1_EN)
583 info->func[0] |=
584 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
585 if (config & ADM1278_VIN_EN)
586 info->func[0] |= PMBUS_HAVE_VIN;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700587 break;
Guenter Roeck68a40382015-03-17 13:19:51 -0700588 case adm1293:
589 case adm1294:
590 data->have_iout_min = true;
591 data->have_pin_min = true;
592 data->have_pin_max = true;
593 data->have_mfr_vaux_status = true;
594
595 coefficients = adm1293_coefficients;
596
597 voindex = 0;
598 switch (config & ADM1293_VIN_SEL_MASK) {
599 case ADM1293_VIN_SEL_012: /* 1.2V */
600 vindex = 0;
601 break;
602 case ADM1293_VIN_SEL_074: /* 7.4V */
603 vindex = 1;
604 break;
605 case ADM1293_VIN_SEL_210: /* 21V */
606 vindex = 2;
607 break;
608 default: /* disabled */
609 break;
610 }
611
612 switch (config & ADM1293_IRANGE_MASK) {
613 case ADM1293_IRANGE_25:
614 cindex = 3;
615 break;
616 case ADM1293_IRANGE_50:
617 cindex = 4;
618 break;
619 case ADM1293_IRANGE_100:
620 cindex = 5;
621 break;
622 case ADM1293_IRANGE_200:
623 cindex = 6;
624 break;
625 }
626
627 if (vindex >= 0)
628 pindex = 7 + vindex * 4 + (cindex - 3);
629
630 if (config & ADM1293_VAUX_EN)
631 info->func[0] |=
632 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
633
634 info->func[0] |= PMBUS_HAVE_PIN |
635 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
636
637 break;
Guenter Roeck904b2962015-07-04 08:39:18 -0700638 default:
639 dev_err(&client->dev, "Unsupported device\n");
640 return -ENODEV;
641 }
Guenter Roeck68a40382015-03-17 13:19:51 -0700642
643 if (voindex < 0)
644 voindex = vindex;
Guenter Roeck904b2962015-07-04 08:39:18 -0700645 if (vindex >= 0) {
646 info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
647 info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
648 info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
Guenter Roeck68a40382015-03-17 13:19:51 -0700649 }
650 if (voindex >= 0) {
651 info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
652 info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
653 info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
Guenter Roeck904b2962015-07-04 08:39:18 -0700654 }
655 if (cindex >= 0) {
Kun Yi6e5c06a2018-10-17 15:26:39 -0700656 /* Scale current with sense resistor value */
657 info->m[PSC_CURRENT_OUT] =
658 coefficients[cindex].m * shunt / 1000;
Guenter Roeck904b2962015-07-04 08:39:18 -0700659 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
660 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
661 }
662 if (pindex >= 0) {
Kun Yi6e5c06a2018-10-17 15:26:39 -0700663 info->m[PSC_POWER] =
664 coefficients[pindex].m * shunt / 1000;
Guenter Roeck904b2962015-07-04 08:39:18 -0700665 info->b[PSC_POWER] = coefficients[pindex].b;
666 info->R[PSC_POWER] = coefficients[pindex].R;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700667 }
Guenter Roeck709066ac2015-07-05 11:04:56 -0700668 if (tindex >= 0) {
669 info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
670 info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
671 info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
672 }
Guenter Roeck83f76492011-03-17 13:16:01 -0700673
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800674 return pmbus_do_probe(client, id, info);
Guenter Roeck83f76492011-03-17 13:16:01 -0700675}
676
Guenter Roeck83f76492011-03-17 13:16:01 -0700677static struct i2c_driver adm1275_driver = {
678 .driver = {
679 .name = "adm1275",
680 },
681 .probe = adm1275_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800682 .remove = pmbus_do_remove,
Guenter Roeck83f76492011-03-17 13:16:01 -0700683 .id_table = adm1275_id,
684};
685
Axel Linf0967ee2012-01-20 15:38:18 +0800686module_i2c_driver(adm1275_driver);
Guenter Roeck83f76492011-03-17 13:16:01 -0700687
688MODULE_AUTHOR("Guenter Roeck");
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700689MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
Guenter Roeck83f76492011-03-17 13:16:01 -0700690MODULE_LICENSE("GPL");