blob: f569372c920426dcbad830761dc83cb6ee9af246 [file] [log] [blame]
Guenter Roeck83f76492011-03-17 13:16:01 -07001/*
2 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
3 * and Digital Power Monitor
4 *
5 * Copyright (c) 2011 Ericsson AB.
Guenter Roeck4ff0ce22018-03-10 18:59:04 -08006 * Copyright (c) 2018 Guenter Roeck
Guenter Roeck83f76492011-03-17 13:16:01 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/i2c.h>
Guenter Roeck99b41602015-07-04 09:19:48 -070025#include <linux/bitops.h>
Guenter Roeck83f76492011-03-17 13:16:01 -070026#include "pmbus.h"
27
Guenter Roeck4ff0ce22018-03-10 18:59:04 -080028enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
Guenter Roeck68a40382015-03-17 13:19:51 -070029
30#define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0)
31#define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
32#define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
Guenter Roeck5cf231a2011-07-14 11:55:35 -070033
Guenter Roeckc576e302011-07-09 11:17:33 -070034#define ADM1275_PEAK_IOUT 0xd0
35#define ADM1275_PEAK_VIN 0xd1
36#define ADM1275_PEAK_VOUT 0xd2
Guenter Roeck83f76492011-03-17 13:16:01 -070037#define ADM1275_PMON_CONFIG 0xd4
38
Guenter Roeck99b41602015-07-04 09:19:48 -070039#define ADM1275_VIN_VOUT_SELECT BIT(6)
40#define ADM1275_VRANGE BIT(5)
41#define ADM1075_IRANGE_50 BIT(4)
42#define ADM1075_IRANGE_25 BIT(3)
43#define ADM1075_IRANGE_MASK (BIT(3) | BIT(4))
Guenter Roeck83f76492011-03-17 13:16:01 -070044
Guenter Roeck4ff0ce22018-03-10 18:59:04 -080045#define ADM1272_IRANGE BIT(0)
46
Guenter Roeck709066ac2015-07-05 11:04:56 -070047#define ADM1278_TEMP1_EN BIT(3)
48#define ADM1278_VIN_EN BIT(2)
49#define ADM1278_VOUT_EN BIT(1)
50
Guenter Roeck68a40382015-03-17 13:19:51 -070051#define ADM1293_IRANGE_25 0
52#define ADM1293_IRANGE_50 BIT(6)
53#define ADM1293_IRANGE_100 BIT(7)
54#define ADM1293_IRANGE_200 (BIT(6) | BIT(7))
55#define ADM1293_IRANGE_MASK (BIT(6) | BIT(7))
56
57#define ADM1293_VIN_SEL_012 BIT(2)
58#define ADM1293_VIN_SEL_074 BIT(3)
59#define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3))
60#define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3))
61
62#define ADM1293_VAUX_EN BIT(1)
63
Guenter Roeck709066ac2015-07-05 11:04:56 -070064#define ADM1278_PEAK_TEMP 0xd7
Guenter Roeckc5e67632011-08-02 11:08:57 -070065#define ADM1275_IOUT_WARN2_LIMIT 0xd7
66#define ADM1275_DEVICE_CONFIG 0xd8
67
Guenter Roeck99b41602015-07-04 09:19:48 -070068#define ADM1275_IOUT_WARN2_SELECT BIT(4)
Guenter Roeckc5e67632011-08-02 11:08:57 -070069
Guenter Roeck5cf231a2011-07-14 11:55:35 -070070#define ADM1276_PEAK_PIN 0xda
Guenter Roeck92711262012-02-24 03:40:53 -080071#define ADM1075_READ_VAUX 0xdd
72#define ADM1075_VAUX_OV_WARN_LIMIT 0xde
73#define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
Guenter Roeck68a40382015-03-17 13:19:51 -070074#define ADM1293_IOUT_MIN 0xe3
75#define ADM1293_PIN_MIN 0xe4
Guenter Roeck92711262012-02-24 03:40:53 -080076#define ADM1075_VAUX_STATUS 0xf6
77
Guenter Roeck99b41602015-07-04 09:19:48 -070078#define ADM1075_VAUX_OV_WARN BIT(7)
79#define ADM1075_VAUX_UV_WARN BIT(6)
Guenter Roeck92711262012-02-24 03:40:53 -080080
Guenter Roeckc5e67632011-08-02 11:08:57 -070081struct adm1275_data {
Guenter Roeck5cf231a2011-07-14 11:55:35 -070082 int id;
Guenter Roeckc5e67632011-08-02 11:08:57 -070083 bool have_oc_fault;
Guenter Roeck90485392015-07-04 10:09:54 -070084 bool have_uc_fault;
85 bool have_vout;
86 bool have_vaux_status;
Guenter Roeck68a40382015-03-17 13:19:51 -070087 bool have_mfr_vaux_status;
88 bool have_iout_min;
89 bool have_pin_min;
Guenter Roeck90485392015-07-04 10:09:54 -070090 bool have_pin_max;
Guenter Roeck709066ac2015-07-05 11:04:56 -070091 bool have_temp_max;
Guenter Roeckc5e67632011-08-02 11:08:57 -070092 struct pmbus_driver_info info;
93};
94
95#define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
96
Guenter Roeck904b2962015-07-04 08:39:18 -070097struct coefficients {
98 s16 m;
99 s16 b;
100 s16 R;
101};
102
103static const struct coefficients adm1075_coefficients[] = {
104 [0] = { 27169, 0, -1 }, /* voltage */
105 [1] = { 806, 20475, -1 }, /* current, irange25 */
106 [2] = { 404, 20475, -1 }, /* current, irange50 */
Shikhar Dogra6faecba2017-03-27 16:16:44 -0700107 [3] = { 8549, 0, -1 }, /* power, irange25 */
108 [4] = { 4279, 0, -1 }, /* power, irange50 */
Guenter Roeck904b2962015-07-04 08:39:18 -0700109};
110
Guenter Roeck4ff0ce22018-03-10 18:59:04 -0800111static const struct coefficients adm1272_coefficients[] = {
112 [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */
113 [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */
114 [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */
115 [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */
116 [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */
117 [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */
118 [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */
119 [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */
120 [8] = { 42, 31871, -1 }, /* temperature */
121
122};
123
Guenter Roeck904b2962015-07-04 08:39:18 -0700124static const struct coefficients adm1275_coefficients[] = {
125 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
126 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
127 [2] = { 807, 20475, -1 }, /* current */
128};
129
130static const struct coefficients adm1276_coefficients[] = {
131 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
132 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
133 [2] = { 807, 20475, -1 }, /* current */
134 [3] = { 6043, 0, -2 }, /* power, vrange set */
135 [4] = { 2115, 0, -1 }, /* power, vrange not set */
136};
137
Guenter Roeck709066ac2015-07-05 11:04:56 -0700138static const struct coefficients adm1278_coefficients[] = {
139 [0] = { 19599, 0, -2 }, /* voltage */
140 [1] = { 800, 20475, -1 }, /* current */
141 [2] = { 6123, 0, -2 }, /* power */
142 [3] = { 42, 31880, -1 }, /* temperature */
143};
144
Guenter Roeck68a40382015-03-17 13:19:51 -0700145static const struct coefficients adm1293_coefficients[] = {
146 [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */
147 [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */
148 [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */
149 [3] = { 8000, -100, -2 }, /* current, irange25 */
150 [4] = { 4000, -100, -2 }, /* current, irange50 */
151 [5] = { 20000, -1000, -3 }, /* current, irange100 */
152 [6] = { 10000, -1000, -3 }, /* current, irange200 */
153 [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */
154 [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */
155 [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */
156 [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */
157 [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */
158 [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */
159 [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */
160 [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */
161 [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */
162 [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */
163 [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */
164 [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
165};
166
Guenter Roeckc576e302011-07-09 11:17:33 -0700167static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
168{
Guenter Roeckc5e67632011-08-02 11:08:57 -0700169 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
170 const struct adm1275_data *data = to_adm1275_data(info);
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700171 int ret = 0;
Guenter Roeckc576e302011-07-09 11:17:33 -0700172
Guenter Roeckecb29ab2018-03-10 17:55:47 -0800173 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700174 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700175
176 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700177 case PMBUS_IOUT_UC_FAULT_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700178 if (!data->have_uc_fault)
179 return -ENXIO;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700180 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
181 break;
182 case PMBUS_IOUT_OC_FAULT_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700183 if (!data->have_oc_fault)
184 return -ENXIO;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700185 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
186 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800187 case PMBUS_VOUT_OV_WARN_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700188 if (data->have_vout)
189 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800190 ret = pmbus_read_word_data(client, 0,
191 ADM1075_VAUX_OV_WARN_LIMIT);
192 break;
193 case PMBUS_VOUT_UV_WARN_LIMIT:
Guenter Roeck90485392015-07-04 10:09:54 -0700194 if (data->have_vout)
195 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800196 ret = pmbus_read_word_data(client, 0,
197 ADM1075_VAUX_UV_WARN_LIMIT);
198 break;
199 case PMBUS_READ_VOUT:
Guenter Roeck90485392015-07-04 10:09:54 -0700200 if (data->have_vout)
201 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800202 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
203 break;
Guenter Roeck68a40382015-03-17 13:19:51 -0700204 case PMBUS_VIRT_READ_IOUT_MIN:
205 if (!data->have_iout_min)
206 return -ENXIO;
207 ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN);
208 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700209 case PMBUS_VIRT_READ_IOUT_MAX:
210 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
211 break;
212 case PMBUS_VIRT_READ_VOUT_MAX:
213 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
214 break;
215 case PMBUS_VIRT_READ_VIN_MAX:
216 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
217 break;
Guenter Roeck68a40382015-03-17 13:19:51 -0700218 case PMBUS_VIRT_READ_PIN_MIN:
219 if (!data->have_pin_min)
220 return -ENXIO;
221 ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN);
222 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700223 case PMBUS_VIRT_READ_PIN_MAX:
Guenter Roeck90485392015-07-04 10:09:54 -0700224 if (!data->have_pin_max)
225 return -ENXIO;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700226 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
227 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700228 case PMBUS_VIRT_READ_TEMP_MAX:
229 if (!data->have_temp_max)
230 return -ENXIO;
231 ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP);
232 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700233 case PMBUS_VIRT_RESET_IOUT_HISTORY:
234 case PMBUS_VIRT_RESET_VOUT_HISTORY:
235 case PMBUS_VIRT_RESET_VIN_HISTORY:
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700236 break;
237 case PMBUS_VIRT_RESET_PIN_HISTORY:
Guenter Roeck90485392015-07-04 10:09:54 -0700238 if (!data->have_pin_max)
239 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700240 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700241 case PMBUS_VIRT_RESET_TEMP_HISTORY:
242 if (!data->have_temp_max)
243 return -ENXIO;
244 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700245 default:
246 ret = -ENODATA;
247 break;
248 }
249 return ret;
250}
251
252static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
253 u16 word)
254{
Guenter Roeck68a40382015-03-17 13:19:51 -0700255 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
256 const struct adm1275_data *data = to_adm1275_data(info);
Guenter Roeckc576e302011-07-09 11:17:33 -0700257 int ret;
258
Guenter Roeckecb29ab2018-03-10 17:55:47 -0800259 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700260 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700261
262 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700263 case PMBUS_IOUT_UC_FAULT_LIMIT:
264 case PMBUS_IOUT_OC_FAULT_LIMIT:
265 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
266 word);
267 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700268 case PMBUS_VIRT_RESET_IOUT_HISTORY:
269 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
Guenter Roeck68a40382015-03-17 13:19:51 -0700270 if (!ret && data->have_iout_min)
271 ret = pmbus_write_word_data(client, 0,
272 ADM1293_IOUT_MIN, 0);
Guenter Roeckc576e302011-07-09 11:17:33 -0700273 break;
274 case PMBUS_VIRT_RESET_VOUT_HISTORY:
275 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
276 break;
277 case PMBUS_VIRT_RESET_VIN_HISTORY:
278 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
279 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700280 case PMBUS_VIRT_RESET_PIN_HISTORY:
281 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
Guenter Roeck68a40382015-03-17 13:19:51 -0700282 if (!ret && data->have_pin_min)
283 ret = pmbus_write_word_data(client, 0,
284 ADM1293_PIN_MIN, 0);
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700285 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700286 case PMBUS_VIRT_RESET_TEMP_HISTORY:
287 ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
288 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700289 default:
290 ret = -ENODATA;
291 break;
292 }
293 return ret;
294}
295
Guenter Roeckc5e67632011-08-02 11:08:57 -0700296static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
297{
298 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
299 const struct adm1275_data *data = to_adm1275_data(info);
300 int mfr_status, ret;
301
Guenter Roeckda8e48a2011-07-29 22:19:39 -0700302 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700303 return -ENXIO;
304
305 switch (reg) {
306 case PMBUS_STATUS_IOUT:
307 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
308 if (ret < 0)
309 break;
Guenter Roeck90485392015-07-04 10:09:54 -0700310 if (!data->have_oc_fault && !data->have_uc_fault)
311 break;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700312 mfr_status = pmbus_read_byte_data(client, page,
313 PMBUS_STATUS_MFR_SPECIFIC);
Guenter Roeck90485392015-07-04 10:09:54 -0700314 if (mfr_status < 0)
315 return mfr_status;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700316 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
317 ret |= data->have_oc_fault ?
318 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
319 }
320 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800321 case PMBUS_STATUS_VOUT:
Guenter Roeck90485392015-07-04 10:09:54 -0700322 if (data->have_vout)
323 return -ENODATA;
Guenter Roeck92711262012-02-24 03:40:53 -0800324 ret = 0;
Guenter Roeck90485392015-07-04 10:09:54 -0700325 if (data->have_vaux_status) {
326 mfr_status = pmbus_read_byte_data(client, 0,
327 ADM1075_VAUX_STATUS);
328 if (mfr_status < 0)
329 return mfr_status;
330 if (mfr_status & ADM1075_VAUX_OV_WARN)
331 ret |= PB_VOLTAGE_OV_WARNING;
332 if (mfr_status & ADM1075_VAUX_UV_WARN)
333 ret |= PB_VOLTAGE_UV_WARNING;
Guenter Roeck68a40382015-03-17 13:19:51 -0700334 } else if (data->have_mfr_vaux_status) {
335 mfr_status = pmbus_read_byte_data(client, page,
336 PMBUS_STATUS_MFR_SPECIFIC);
337 if (mfr_status < 0)
338 return mfr_status;
339 if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
340 ret |= PB_VOLTAGE_OV_WARNING;
341 if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
342 ret |= PB_VOLTAGE_UV_WARNING;
Guenter Roeck90485392015-07-04 10:09:54 -0700343 }
Guenter Roeck92711262012-02-24 03:40:53 -0800344 break;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700345 default:
346 ret = -ENODATA;
347 break;
348 }
349 return ret;
350}
351
Guenter Roeck87102802011-09-30 11:53:25 -0700352static const struct i2c_device_id adm1275_id[] = {
Guenter Roeck92711262012-02-24 03:40:53 -0800353 { "adm1075", adm1075 },
Guenter Roeck4ff0ce22018-03-10 18:59:04 -0800354 { "adm1272", adm1272 },
Guenter Roeck87102802011-09-30 11:53:25 -0700355 { "adm1275", adm1275 },
356 { "adm1276", adm1276 },
Guenter Roeck709066ac2015-07-05 11:04:56 -0700357 { "adm1278", adm1278 },
Guenter Roeck68a40382015-03-17 13:19:51 -0700358 { "adm1293", adm1293 },
359 { "adm1294", adm1294 },
Guenter Roeck87102802011-09-30 11:53:25 -0700360 { }
361};
362MODULE_DEVICE_TABLE(i2c, adm1275_id);
363
Guenter Roeck83f76492011-03-17 13:16:01 -0700364static int adm1275_probe(struct i2c_client *client,
365 const struct i2c_device_id *id)
366{
Guenter Roeck87102802011-09-30 11:53:25 -0700367 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
Guenter Roeckc5e67632011-08-02 11:08:57 -0700368 int config, device_config;
Guenter Roeck3b33ca42011-06-30 02:30:03 -0700369 int ret;
Guenter Roeck83f76492011-03-17 13:16:01 -0700370 struct pmbus_driver_info *info;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700371 struct adm1275_data *data;
Guenter Roeck87102802011-09-30 11:53:25 -0700372 const struct i2c_device_id *mid;
Guenter Roeck904b2962015-07-04 08:39:18 -0700373 const struct coefficients *coefficients;
Guenter Roeck68a40382015-03-17 13:19:51 -0700374 int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700375 int tindex = -1;
Kun Yi6e5c06a2018-10-17 15:26:39 -0700376 u32 shunt;
Guenter Roeck83f76492011-03-17 13:16:01 -0700377
378 if (!i2c_check_functionality(client->adapter,
Guenter Roeck87102802011-09-30 11:53:25 -0700379 I2C_FUNC_SMBUS_READ_BYTE_DATA
380 | I2C_FUNC_SMBUS_BLOCK_DATA))
Guenter Roeck83f76492011-03-17 13:16:01 -0700381 return -ENODEV;
382
Guenter Roeck87102802011-09-30 11:53:25 -0700383 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
384 if (ret < 0) {
385 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
386 return ret;
387 }
388 if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
389 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
390 return -ENODEV;
391 }
392
393 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
394 if (ret < 0) {
395 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
396 return ret;
397 }
398 for (mid = adm1275_id; mid->name[0]; mid++) {
399 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
400 break;
401 }
402 if (!mid->name[0]) {
403 dev_err(&client->dev, "Unsupported device\n");
404 return -ENODEV;
405 }
406
407 if (id->driver_data != mid->driver_data)
408 dev_notice(&client->dev,
409 "Device mismatch: Configured %s, detected %s\n",
410 id->name, mid->name);
411
412 config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
413 if (config < 0)
414 return config;
415
416 device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
417 if (device_config < 0)
418 return device_config;
419
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800420 data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
421 GFP_KERNEL);
Guenter Roeckc5e67632011-08-02 11:08:57 -0700422 if (!data)
Guenter Roeck83f76492011-03-17 13:16:01 -0700423 return -ENOMEM;
424
Kun Yi6e5c06a2018-10-17 15:26:39 -0700425 if (of_property_read_u32(client->dev.of_node,
426 "shunt-resistor-micro-ohms", &shunt))
427 shunt = 1000; /* 1 mOhm if not set via DT */
428
429 if (shunt == 0)
430 return -EINVAL;
431
Guenter Roeck87102802011-09-30 11:53:25 -0700432 data->id = mid->driver_data;
Guenter Roeck83f76492011-03-17 13:16:01 -0700433
Guenter Roeckc5e67632011-08-02 11:08:57 -0700434 info = &data->info;
435
Guenter Roeck83f76492011-03-17 13:16:01 -0700436 info->pages = 1;
Guenter Roeck1061d852011-06-25 11:21:49 -0700437 info->format[PSC_VOLTAGE_IN] = direct;
438 info->format[PSC_VOLTAGE_OUT] = direct;
439 info->format[PSC_CURRENT_OUT] = direct;
Guenter Roeck904b2962015-07-04 08:39:18 -0700440 info->format[PSC_POWER] = direct;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700441 info->format[PSC_TEMPERATURE] = direct;
Guenter Roeck83f76492011-03-17 13:16:01 -0700442 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
443
Guenter Roeckc576e302011-07-09 11:17:33 -0700444 info->read_word_data = adm1275_read_word_data;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700445 info->read_byte_data = adm1275_read_byte_data;
Guenter Roeckc576e302011-07-09 11:17:33 -0700446 info->write_word_data = adm1275_write_word_data;
447
Guenter Roeck87102802011-09-30 11:53:25 -0700448 switch (data->id) {
Guenter Roeck92711262012-02-24 03:40:53 -0800449 case adm1075:
Guenter Roeck90485392015-07-04 10:09:54 -0700450 if (device_config & ADM1275_IOUT_WARN2_SELECT)
451 data->have_oc_fault = true;
452 else
453 data->have_uc_fault = true;
454 data->have_pin_max = true;
455 data->have_vaux_status = true;
456
Guenter Roeck904b2962015-07-04 08:39:18 -0700457 coefficients = adm1075_coefficients;
458 vindex = 0;
Guenter Roeck92711262012-02-24 03:40:53 -0800459 switch (config & ADM1075_IRANGE_MASK) {
460 case ADM1075_IRANGE_25:
Guenter Roeck904b2962015-07-04 08:39:18 -0700461 cindex = 1;
462 pindex = 3;
Guenter Roeck92711262012-02-24 03:40:53 -0800463 break;
464 case ADM1075_IRANGE_50:
Guenter Roeck904b2962015-07-04 08:39:18 -0700465 cindex = 2;
466 pindex = 4;
Guenter Roeck92711262012-02-24 03:40:53 -0800467 break;
468 default:
469 dev_err(&client->dev, "Invalid input current range");
Guenter Roeck92711262012-02-24 03:40:53 -0800470 break;
471 }
Guenter Roeck904b2962015-07-04 08:39:18 -0700472
Guenter Roeck92711262012-02-24 03:40:53 -0800473 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
474 | PMBUS_HAVE_STATUS_INPUT;
475 if (config & ADM1275_VIN_VOUT_SELECT)
476 info->func[0] |=
477 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
478 break;
Guenter Roeck4ff0ce22018-03-10 18:59:04 -0800479 case adm1272:
480 data->have_vout = true;
481 data->have_pin_max = true;
482 data->have_temp_max = true;
483
484 coefficients = adm1272_coefficients;
485 vindex = (config & ADM1275_VRANGE) ? 1 : 0;
486 cindex = (config & ADM1272_IRANGE) ? 3 : 2;
487 /* pindex depends on the combination of the above */
488 switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
489 case 0:
490 default:
491 pindex = 4;
492 break;
493 case ADM1275_VRANGE:
494 pindex = 5;
495 break;
496 case ADM1272_IRANGE:
497 pindex = 6;
498 break;
499 case ADM1275_VRANGE | ADM1272_IRANGE:
500 pindex = 7;
501 break;
502 }
503 tindex = 8;
504
505 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
506 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
507
508 /* Enable VOUT if not enabled (it is disabled by default) */
509 if (!(config & ADM1278_VOUT_EN)) {
510 config |= ADM1278_VOUT_EN;
511 ret = i2c_smbus_write_byte_data(client,
512 ADM1275_PMON_CONFIG,
513 config);
514 if (ret < 0) {
515 dev_err(&client->dev,
516 "Failed to enable VOUT monitoring\n");
517 return -ENODEV;
518 }
519 }
520
521 if (config & ADM1278_TEMP1_EN)
522 info->func[0] |=
523 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
524 if (config & ADM1278_VIN_EN)
525 info->func[0] |= PMBUS_HAVE_VIN;
526 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700527 case adm1275:
Guenter Roeck90485392015-07-04 10:09:54 -0700528 if (device_config & ADM1275_IOUT_WARN2_SELECT)
529 data->have_oc_fault = true;
530 else
531 data->have_uc_fault = true;
532 data->have_vout = true;
533
Guenter Roeck904b2962015-07-04 08:39:18 -0700534 coefficients = adm1275_coefficients;
535 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
536 cindex = 2;
537
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700538 if (config & ADM1275_VIN_VOUT_SELECT)
539 info->func[0] |=
540 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
541 else
542 info->func[0] |=
543 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
544 break;
545 case adm1276:
Guenter Roeck90485392015-07-04 10:09:54 -0700546 if (device_config & ADM1275_IOUT_WARN2_SELECT)
547 data->have_oc_fault = true;
548 else
549 data->have_uc_fault = true;
550 data->have_vout = true;
551 data->have_pin_max = true;
552
Guenter Roeck904b2962015-07-04 08:39:18 -0700553 coefficients = adm1276_coefficients;
554 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
555 cindex = 2;
556 pindex = (config & ADM1275_VRANGE) ? 3 : 4;
557
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700558 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
559 | PMBUS_HAVE_STATUS_INPUT;
560 if (config & ADM1275_VIN_VOUT_SELECT)
561 info->func[0] |=
562 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700563 break;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700564 case adm1278:
565 data->have_vout = true;
566 data->have_pin_max = true;
567 data->have_temp_max = true;
568
569 coefficients = adm1278_coefficients;
570 vindex = 0;
571 cindex = 1;
572 pindex = 2;
573 tindex = 3;
574
Yi Li2b3d0c12016-10-17 18:38:53 +1030575 info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
576 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
577
578 /* Enable VOUT if not enabled (it is disabled by default) */
579 if (!(config & ADM1278_VOUT_EN)) {
580 config |= ADM1278_VOUT_EN;
581 ret = i2c_smbus_write_byte_data(client,
582 ADM1275_PMON_CONFIG,
583 config);
584 if (ret < 0) {
585 dev_err(&client->dev,
586 "Failed to enable VOUT monitoring\n");
587 return -ENODEV;
588 }
589 }
590
Guenter Roeck709066ac2015-07-05 11:04:56 -0700591 if (config & ADM1278_TEMP1_EN)
592 info->func[0] |=
593 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
594 if (config & ADM1278_VIN_EN)
595 info->func[0] |= PMBUS_HAVE_VIN;
Guenter Roeck709066ac2015-07-05 11:04:56 -0700596 break;
Guenter Roeck68a40382015-03-17 13:19:51 -0700597 case adm1293:
598 case adm1294:
599 data->have_iout_min = true;
600 data->have_pin_min = true;
601 data->have_pin_max = true;
602 data->have_mfr_vaux_status = true;
603
604 coefficients = adm1293_coefficients;
605
606 voindex = 0;
607 switch (config & ADM1293_VIN_SEL_MASK) {
608 case ADM1293_VIN_SEL_012: /* 1.2V */
609 vindex = 0;
610 break;
611 case ADM1293_VIN_SEL_074: /* 7.4V */
612 vindex = 1;
613 break;
614 case ADM1293_VIN_SEL_210: /* 21V */
615 vindex = 2;
616 break;
617 default: /* disabled */
618 break;
619 }
620
621 switch (config & ADM1293_IRANGE_MASK) {
622 case ADM1293_IRANGE_25:
623 cindex = 3;
624 break;
625 case ADM1293_IRANGE_50:
626 cindex = 4;
627 break;
628 case ADM1293_IRANGE_100:
629 cindex = 5;
630 break;
631 case ADM1293_IRANGE_200:
632 cindex = 6;
633 break;
634 }
635
636 if (vindex >= 0)
637 pindex = 7 + vindex * 4 + (cindex - 3);
638
639 if (config & ADM1293_VAUX_EN)
640 info->func[0] |=
641 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
642
643 info->func[0] |= PMBUS_HAVE_PIN |
644 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
645
646 break;
Guenter Roeck904b2962015-07-04 08:39:18 -0700647 default:
648 dev_err(&client->dev, "Unsupported device\n");
649 return -ENODEV;
650 }
Guenter Roeck68a40382015-03-17 13:19:51 -0700651
652 if (voindex < 0)
653 voindex = vindex;
Guenter Roeck904b2962015-07-04 08:39:18 -0700654 if (vindex >= 0) {
655 info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
656 info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
657 info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
Guenter Roeck68a40382015-03-17 13:19:51 -0700658 }
659 if (voindex >= 0) {
660 info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
661 info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
662 info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
Guenter Roeck904b2962015-07-04 08:39:18 -0700663 }
664 if (cindex >= 0) {
Kun Yi6e5c06a2018-10-17 15:26:39 -0700665 /* Scale current with sense resistor value */
666 info->m[PSC_CURRENT_OUT] =
667 coefficients[cindex].m * shunt / 1000;
Guenter Roeck904b2962015-07-04 08:39:18 -0700668 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
669 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
670 }
671 if (pindex >= 0) {
Kun Yi6e5c06a2018-10-17 15:26:39 -0700672 info->m[PSC_POWER] =
673 coefficients[pindex].m * shunt / 1000;
Guenter Roeck904b2962015-07-04 08:39:18 -0700674 info->b[PSC_POWER] = coefficients[pindex].b;
675 info->R[PSC_POWER] = coefficients[pindex].R;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700676 }
Guenter Roeck709066ac2015-07-05 11:04:56 -0700677 if (tindex >= 0) {
678 info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
679 info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
680 info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
681 }
Guenter Roeck83f76492011-03-17 13:16:01 -0700682
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800683 return pmbus_do_probe(client, id, info);
Guenter Roeck83f76492011-03-17 13:16:01 -0700684}
685
Guenter Roeck83f76492011-03-17 13:16:01 -0700686static struct i2c_driver adm1275_driver = {
687 .driver = {
688 .name = "adm1275",
689 },
690 .probe = adm1275_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800691 .remove = pmbus_do_remove,
Guenter Roeck83f76492011-03-17 13:16:01 -0700692 .id_table = adm1275_id,
693};
694
Axel Linf0967ee2012-01-20 15:38:18 +0800695module_i2c_driver(adm1275_driver);
Guenter Roeck83f76492011-03-17 13:16:01 -0700696
697MODULE_AUTHOR("Guenter Roeck");
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700698MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
Guenter Roeck83f76492011-03-17 13:16:01 -0700699MODULE_LICENSE("GPL");