blob: 3a8d69b6c9a646b9e1267d94bdb85672d5a19c7d [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.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/slab.h>
23#include <linux/i2c.h>
Guenter Roeck99b41602015-07-04 09:19:48 -070024#include <linux/bitops.h>
Guenter Roeck83f76492011-03-17 13:16:01 -070025#include "pmbus.h"
26
Guenter Roeck92711262012-02-24 03:40:53 -080027enum chips { adm1075, adm1275, adm1276 };
Guenter Roeck5cf231a2011-07-14 11:55:35 -070028
Guenter Roeckc576e302011-07-09 11:17:33 -070029#define ADM1275_PEAK_IOUT 0xd0
30#define ADM1275_PEAK_VIN 0xd1
31#define ADM1275_PEAK_VOUT 0xd2
Guenter Roeck83f76492011-03-17 13:16:01 -070032#define ADM1275_PMON_CONFIG 0xd4
33
Guenter Roeck99b41602015-07-04 09:19:48 -070034#define ADM1275_VIN_VOUT_SELECT BIT(6)
35#define ADM1275_VRANGE BIT(5)
36#define ADM1075_IRANGE_50 BIT(4)
37#define ADM1075_IRANGE_25 BIT(3)
38#define ADM1075_IRANGE_MASK (BIT(3) | BIT(4))
Guenter Roeck83f76492011-03-17 13:16:01 -070039
Guenter Roeckc5e67632011-08-02 11:08:57 -070040#define ADM1275_IOUT_WARN2_LIMIT 0xd7
41#define ADM1275_DEVICE_CONFIG 0xd8
42
Guenter Roeck99b41602015-07-04 09:19:48 -070043#define ADM1275_IOUT_WARN2_SELECT BIT(4)
Guenter Roeckc5e67632011-08-02 11:08:57 -070044
Guenter Roeck5cf231a2011-07-14 11:55:35 -070045#define ADM1276_PEAK_PIN 0xda
46
Guenter Roeck99b41602015-07-04 09:19:48 -070047#define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0)
Guenter Roeckc5e67632011-08-02 11:08:57 -070048
Guenter Roeck92711262012-02-24 03:40:53 -080049#define ADM1075_READ_VAUX 0xdd
50#define ADM1075_VAUX_OV_WARN_LIMIT 0xde
51#define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
52#define ADM1075_VAUX_STATUS 0xf6
53
Guenter Roeck99b41602015-07-04 09:19:48 -070054#define ADM1075_VAUX_OV_WARN BIT(7)
55#define ADM1075_VAUX_UV_WARN BIT(6)
Guenter Roeck92711262012-02-24 03:40:53 -080056
Guenter Roeckc5e67632011-08-02 11:08:57 -070057struct adm1275_data {
Guenter Roeck5cf231a2011-07-14 11:55:35 -070058 int id;
Guenter Roeckc5e67632011-08-02 11:08:57 -070059 bool have_oc_fault;
60 struct pmbus_driver_info info;
61};
62
63#define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
64
Guenter Roeck904b2962015-07-04 08:39:18 -070065struct coefficients {
66 s16 m;
67 s16 b;
68 s16 R;
69};
70
71static const struct coefficients adm1075_coefficients[] = {
72 [0] = { 27169, 0, -1 }, /* voltage */
73 [1] = { 806, 20475, -1 }, /* current, irange25 */
74 [2] = { 404, 20475, -1 }, /* current, irange50 */
75 [3] = { 0, -1, 8549 }, /* power, irange25 */
76 [4] = { 0, -1, 4279 }, /* power, irange50 */
77};
78
79static const struct coefficients adm1275_coefficients[] = {
80 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
81 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
82 [2] = { 807, 20475, -1 }, /* current */
83};
84
85static const struct coefficients adm1276_coefficients[] = {
86 [0] = { 19199, 0, -2 }, /* voltage, vrange set */
87 [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
88 [2] = { 807, 20475, -1 }, /* current */
89 [3] = { 6043, 0, -2 }, /* power, vrange set */
90 [4] = { 2115, 0, -1 }, /* power, vrange not set */
91};
92
Guenter Roeckc576e302011-07-09 11:17:33 -070093static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
94{
Guenter Roeckc5e67632011-08-02 11:08:57 -070095 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
96 const struct adm1275_data *data = to_adm1275_data(info);
Guenter Roeck5cf231a2011-07-14 11:55:35 -070097 int ret = 0;
Guenter Roeckc576e302011-07-09 11:17:33 -070098
99 if (page)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700100 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700101
102 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700103 case PMBUS_IOUT_UC_FAULT_LIMIT:
104 if (data->have_oc_fault) {
105 ret = -ENXIO;
106 break;
107 }
108 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
109 break;
110 case PMBUS_IOUT_OC_FAULT_LIMIT:
111 if (!data->have_oc_fault) {
112 ret = -ENXIO;
113 break;
114 }
115 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
116 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800117 case PMBUS_VOUT_OV_WARN_LIMIT:
118 if (data->id != adm1075) {
119 ret = -ENODATA;
120 break;
121 }
122 ret = pmbus_read_word_data(client, 0,
123 ADM1075_VAUX_OV_WARN_LIMIT);
124 break;
125 case PMBUS_VOUT_UV_WARN_LIMIT:
126 if (data->id != adm1075) {
127 ret = -ENODATA;
128 break;
129 }
130 ret = pmbus_read_word_data(client, 0,
131 ADM1075_VAUX_UV_WARN_LIMIT);
132 break;
133 case PMBUS_READ_VOUT:
134 if (data->id != adm1075) {
135 ret = -ENODATA;
136 break;
137 }
138 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
139 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700140 case PMBUS_VIRT_READ_IOUT_MAX:
141 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
142 break;
143 case PMBUS_VIRT_READ_VOUT_MAX:
144 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
145 break;
146 case PMBUS_VIRT_READ_VIN_MAX:
147 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
148 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700149 case PMBUS_VIRT_READ_PIN_MAX:
Guenter Roeck92711262012-02-24 03:40:53 -0800150 if (data->id == adm1275) {
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700151 ret = -ENXIO;
152 break;
153 }
154 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
155 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700156 case PMBUS_VIRT_RESET_IOUT_HISTORY:
157 case PMBUS_VIRT_RESET_VOUT_HISTORY:
158 case PMBUS_VIRT_RESET_VIN_HISTORY:
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700159 break;
160 case PMBUS_VIRT_RESET_PIN_HISTORY:
Guenter Roeck92711262012-02-24 03:40:53 -0800161 if (data->id == adm1275)
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700162 ret = -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700163 break;
164 default:
165 ret = -ENODATA;
166 break;
167 }
168 return ret;
169}
170
171static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
172 u16 word)
173{
174 int ret;
175
176 if (page)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700177 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700178
179 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700180 case PMBUS_IOUT_UC_FAULT_LIMIT:
181 case PMBUS_IOUT_OC_FAULT_LIMIT:
182 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
183 word);
184 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700185 case PMBUS_VIRT_RESET_IOUT_HISTORY:
186 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
187 break;
188 case PMBUS_VIRT_RESET_VOUT_HISTORY:
189 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
190 break;
191 case PMBUS_VIRT_RESET_VIN_HISTORY:
192 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
193 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700194 case PMBUS_VIRT_RESET_PIN_HISTORY:
195 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
196 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700197 default:
198 ret = -ENODATA;
199 break;
200 }
201 return ret;
202}
203
Guenter Roeckc5e67632011-08-02 11:08:57 -0700204static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
205{
206 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
207 const struct adm1275_data *data = to_adm1275_data(info);
208 int mfr_status, ret;
209
Guenter Roeckda8e48a2011-07-29 22:19:39 -0700210 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700211 return -ENXIO;
212
213 switch (reg) {
214 case PMBUS_STATUS_IOUT:
215 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
216 if (ret < 0)
217 break;
218 mfr_status = pmbus_read_byte_data(client, page,
219 PMBUS_STATUS_MFR_SPECIFIC);
220 if (mfr_status < 0) {
221 ret = mfr_status;
222 break;
223 }
224 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
225 ret |= data->have_oc_fault ?
226 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
227 }
228 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800229 case PMBUS_STATUS_VOUT:
230 if (data->id != adm1075) {
231 ret = -ENODATA;
232 break;
233 }
234 ret = 0;
235 mfr_status = pmbus_read_byte_data(client, 0,
236 ADM1075_VAUX_STATUS);
237 if (mfr_status & ADM1075_VAUX_OV_WARN)
238 ret |= PB_VOLTAGE_OV_WARNING;
239 if (mfr_status & ADM1075_VAUX_UV_WARN)
240 ret |= PB_VOLTAGE_UV_WARNING;
241 break;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700242 default:
243 ret = -ENODATA;
244 break;
245 }
246 return ret;
247}
248
Guenter Roeck87102802011-09-30 11:53:25 -0700249static const struct i2c_device_id adm1275_id[] = {
Guenter Roeck92711262012-02-24 03:40:53 -0800250 { "adm1075", adm1075 },
Guenter Roeck87102802011-09-30 11:53:25 -0700251 { "adm1275", adm1275 },
252 { "adm1276", adm1276 },
253 { }
254};
255MODULE_DEVICE_TABLE(i2c, adm1275_id);
256
Guenter Roeck83f76492011-03-17 13:16:01 -0700257static int adm1275_probe(struct i2c_client *client,
258 const struct i2c_device_id *id)
259{
Guenter Roeck87102802011-09-30 11:53:25 -0700260 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
Guenter Roeckc5e67632011-08-02 11:08:57 -0700261 int config, device_config;
Guenter Roeck3b33ca42011-06-30 02:30:03 -0700262 int ret;
Guenter Roeck83f76492011-03-17 13:16:01 -0700263 struct pmbus_driver_info *info;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700264 struct adm1275_data *data;
Guenter Roeck87102802011-09-30 11:53:25 -0700265 const struct i2c_device_id *mid;
Guenter Roeck904b2962015-07-04 08:39:18 -0700266 const struct coefficients *coefficients;
267 int vindex = -1, cindex = -1, pindex = -1;
Guenter Roeck83f76492011-03-17 13:16:01 -0700268
269 if (!i2c_check_functionality(client->adapter,
Guenter Roeck87102802011-09-30 11:53:25 -0700270 I2C_FUNC_SMBUS_READ_BYTE_DATA
271 | I2C_FUNC_SMBUS_BLOCK_DATA))
Guenter Roeck83f76492011-03-17 13:16:01 -0700272 return -ENODEV;
273
Guenter Roeck87102802011-09-30 11:53:25 -0700274 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
275 if (ret < 0) {
276 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
277 return ret;
278 }
279 if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
280 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
281 return -ENODEV;
282 }
283
284 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
285 if (ret < 0) {
286 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
287 return ret;
288 }
289 for (mid = adm1275_id; mid->name[0]; mid++) {
290 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
291 break;
292 }
293 if (!mid->name[0]) {
294 dev_err(&client->dev, "Unsupported device\n");
295 return -ENODEV;
296 }
297
298 if (id->driver_data != mid->driver_data)
299 dev_notice(&client->dev,
300 "Device mismatch: Configured %s, detected %s\n",
301 id->name, mid->name);
302
303 config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
304 if (config < 0)
305 return config;
306
307 device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
308 if (device_config < 0)
309 return device_config;
310
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800311 data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
312 GFP_KERNEL);
Guenter Roeckc5e67632011-08-02 11:08:57 -0700313 if (!data)
Guenter Roeck83f76492011-03-17 13:16:01 -0700314 return -ENOMEM;
315
Guenter Roeck87102802011-09-30 11:53:25 -0700316 data->id = mid->driver_data;
Guenter Roeck83f76492011-03-17 13:16:01 -0700317
Guenter Roeckc5e67632011-08-02 11:08:57 -0700318 info = &data->info;
319
Guenter Roeck83f76492011-03-17 13:16:01 -0700320 info->pages = 1;
Guenter Roeck1061d852011-06-25 11:21:49 -0700321 info->format[PSC_VOLTAGE_IN] = direct;
322 info->format[PSC_VOLTAGE_OUT] = direct;
323 info->format[PSC_CURRENT_OUT] = direct;
Guenter Roeck904b2962015-07-04 08:39:18 -0700324 info->format[PSC_POWER] = direct;
Guenter Roeck83f76492011-03-17 13:16:01 -0700325 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
326
Guenter Roeckc576e302011-07-09 11:17:33 -0700327 info->read_word_data = adm1275_read_word_data;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700328 info->read_byte_data = adm1275_read_byte_data;
Guenter Roeckc576e302011-07-09 11:17:33 -0700329 info->write_word_data = adm1275_write_word_data;
330
Guenter Roeckc5e67632011-08-02 11:08:57 -0700331 if (device_config & ADM1275_IOUT_WARN2_SELECT)
332 data->have_oc_fault = true;
333
Guenter Roeck87102802011-09-30 11:53:25 -0700334 switch (data->id) {
Guenter Roeck92711262012-02-24 03:40:53 -0800335 case adm1075:
Guenter Roeck904b2962015-07-04 08:39:18 -0700336 coefficients = adm1075_coefficients;
337 vindex = 0;
Guenter Roeck92711262012-02-24 03:40:53 -0800338 switch (config & ADM1075_IRANGE_MASK) {
339 case ADM1075_IRANGE_25:
Guenter Roeck904b2962015-07-04 08:39:18 -0700340 cindex = 1;
341 pindex = 3;
Guenter Roeck92711262012-02-24 03:40:53 -0800342 break;
343 case ADM1075_IRANGE_50:
Guenter Roeck904b2962015-07-04 08:39:18 -0700344 cindex = 2;
345 pindex = 4;
Guenter Roeck92711262012-02-24 03:40:53 -0800346 break;
347 default:
348 dev_err(&client->dev, "Invalid input current range");
Guenter Roeck92711262012-02-24 03:40:53 -0800349 break;
350 }
Guenter Roeck904b2962015-07-04 08:39:18 -0700351
Guenter Roeck92711262012-02-24 03:40:53 -0800352 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
353 | PMBUS_HAVE_STATUS_INPUT;
354 if (config & ADM1275_VIN_VOUT_SELECT)
355 info->func[0] |=
356 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
357 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700358 case adm1275:
Guenter Roeck904b2962015-07-04 08:39:18 -0700359 coefficients = adm1275_coefficients;
360 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
361 cindex = 2;
362
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700363 if (config & ADM1275_VIN_VOUT_SELECT)
364 info->func[0] |=
365 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
366 else
367 info->func[0] |=
368 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
369 break;
370 case adm1276:
Guenter Roeck904b2962015-07-04 08:39:18 -0700371 coefficients = adm1276_coefficients;
372 vindex = (config & ADM1275_VRANGE) ? 0 : 1;
373 cindex = 2;
374 pindex = (config & ADM1275_VRANGE) ? 3 : 4;
375
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700376 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
377 | PMBUS_HAVE_STATUS_INPUT;
378 if (config & ADM1275_VIN_VOUT_SELECT)
379 info->func[0] |=
380 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700381 break;
Guenter Roeck904b2962015-07-04 08:39:18 -0700382 default:
383 dev_err(&client->dev, "Unsupported device\n");
384 return -ENODEV;
385 }
386 if (vindex >= 0) {
387 info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
388 info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
389 info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
390 info->m[PSC_VOLTAGE_OUT] = coefficients[vindex].m;
391 info->b[PSC_VOLTAGE_OUT] = coefficients[vindex].b;
392 info->R[PSC_VOLTAGE_OUT] = coefficients[vindex].R;
393 }
394 if (cindex >= 0) {
395 info->m[PSC_CURRENT_OUT] = coefficients[cindex].m;
396 info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
397 info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
398 }
399 if (pindex >= 0) {
400 info->m[PSC_POWER] = coefficients[pindex].m;
401 info->b[PSC_POWER] = coefficients[pindex].b;
402 info->R[PSC_POWER] = coefficients[pindex].R;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700403 }
Guenter Roeck83f76492011-03-17 13:16:01 -0700404
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800405 return pmbus_do_probe(client, id, info);
Guenter Roeck83f76492011-03-17 13:16:01 -0700406}
407
Guenter Roeck83f76492011-03-17 13:16:01 -0700408static struct i2c_driver adm1275_driver = {
409 .driver = {
410 .name = "adm1275",
411 },
412 .probe = adm1275_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800413 .remove = pmbus_do_remove,
Guenter Roeck83f76492011-03-17 13:16:01 -0700414 .id_table = adm1275_id,
415};
416
Axel Linf0967ee2012-01-20 15:38:18 +0800417module_i2c_driver(adm1275_driver);
Guenter Roeck83f76492011-03-17 13:16:01 -0700418
419MODULE_AUTHOR("Guenter Roeck");
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700420MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
Guenter Roeck83f76492011-03-17 13:16:01 -0700421MODULE_LICENSE("GPL");