blob: 60aad9570f016987d82acd6d7f6104e94de3c12c [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>
24#include "pmbus.h"
25
Guenter Roeck92711262012-02-24 03:40:53 -080026enum chips { adm1075, adm1275, adm1276 };
Guenter Roeck5cf231a2011-07-14 11:55:35 -070027
Guenter Roeckc576e302011-07-09 11:17:33 -070028#define ADM1275_PEAK_IOUT 0xd0
29#define ADM1275_PEAK_VIN 0xd1
30#define ADM1275_PEAK_VOUT 0xd2
Guenter Roeck83f76492011-03-17 13:16:01 -070031#define ADM1275_PMON_CONFIG 0xd4
32
33#define ADM1275_VIN_VOUT_SELECT (1 << 6)
34#define ADM1275_VRANGE (1 << 5)
Guenter Roeck92711262012-02-24 03:40:53 -080035#define ADM1075_IRANGE_50 (1 << 4)
36#define ADM1075_IRANGE_25 (1 << 3)
37#define ADM1075_IRANGE_MASK ((1 << 3) | (1 << 4))
Guenter Roeck83f76492011-03-17 13:16:01 -070038
Guenter Roeckc5e67632011-08-02 11:08:57 -070039#define ADM1275_IOUT_WARN2_LIMIT 0xd7
40#define ADM1275_DEVICE_CONFIG 0xd8
41
42#define ADM1275_IOUT_WARN2_SELECT (1 << 4)
43
Guenter Roeck5cf231a2011-07-14 11:55:35 -070044#define ADM1276_PEAK_PIN 0xda
45
Guenter Roeckc5e67632011-08-02 11:08:57 -070046#define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0)
47
Guenter Roeck92711262012-02-24 03:40:53 -080048#define ADM1075_READ_VAUX 0xdd
49#define ADM1075_VAUX_OV_WARN_LIMIT 0xde
50#define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
51#define ADM1075_VAUX_STATUS 0xf6
52
53#define ADM1075_VAUX_OV_WARN (1<<7)
54#define ADM1075_VAUX_UV_WARN (1<<6)
55
Guenter Roeckc5e67632011-08-02 11:08:57 -070056struct adm1275_data {
Guenter Roeck5cf231a2011-07-14 11:55:35 -070057 int id;
Guenter Roeckc5e67632011-08-02 11:08:57 -070058 bool have_oc_fault;
59 struct pmbus_driver_info info;
60};
61
62#define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
63
Guenter Roeckc576e302011-07-09 11:17:33 -070064static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
65{
Guenter Roeckc5e67632011-08-02 11:08:57 -070066 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
67 const struct adm1275_data *data = to_adm1275_data(info);
Guenter Roeck5cf231a2011-07-14 11:55:35 -070068 int ret = 0;
Guenter Roeckc576e302011-07-09 11:17:33 -070069
70 if (page)
Guenter Roeckc5e67632011-08-02 11:08:57 -070071 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -070072
73 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -070074 case PMBUS_IOUT_UC_FAULT_LIMIT:
75 if (data->have_oc_fault) {
76 ret = -ENXIO;
77 break;
78 }
79 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
80 break;
81 case PMBUS_IOUT_OC_FAULT_LIMIT:
82 if (!data->have_oc_fault) {
83 ret = -ENXIO;
84 break;
85 }
86 ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
87 break;
Guenter Roeck92711262012-02-24 03:40:53 -080088 case PMBUS_VOUT_OV_WARN_LIMIT:
89 if (data->id != adm1075) {
90 ret = -ENODATA;
91 break;
92 }
93 ret = pmbus_read_word_data(client, 0,
94 ADM1075_VAUX_OV_WARN_LIMIT);
95 break;
96 case PMBUS_VOUT_UV_WARN_LIMIT:
97 if (data->id != adm1075) {
98 ret = -ENODATA;
99 break;
100 }
101 ret = pmbus_read_word_data(client, 0,
102 ADM1075_VAUX_UV_WARN_LIMIT);
103 break;
104 case PMBUS_READ_VOUT:
105 if (data->id != adm1075) {
106 ret = -ENODATA;
107 break;
108 }
109 ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
110 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700111 case PMBUS_VIRT_READ_IOUT_MAX:
112 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
113 break;
114 case PMBUS_VIRT_READ_VOUT_MAX:
115 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
116 break;
117 case PMBUS_VIRT_READ_VIN_MAX:
118 ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
119 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700120 case PMBUS_VIRT_READ_PIN_MAX:
Guenter Roeck92711262012-02-24 03:40:53 -0800121 if (data->id == adm1275) {
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700122 ret = -ENXIO;
123 break;
124 }
125 ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
126 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700127 case PMBUS_VIRT_RESET_IOUT_HISTORY:
128 case PMBUS_VIRT_RESET_VOUT_HISTORY:
129 case PMBUS_VIRT_RESET_VIN_HISTORY:
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700130 break;
131 case PMBUS_VIRT_RESET_PIN_HISTORY:
Guenter Roeck92711262012-02-24 03:40:53 -0800132 if (data->id == adm1275)
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700133 ret = -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700134 break;
135 default:
136 ret = -ENODATA;
137 break;
138 }
139 return ret;
140}
141
142static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
143 u16 word)
144{
145 int ret;
146
147 if (page)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700148 return -ENXIO;
Guenter Roeckc576e302011-07-09 11:17:33 -0700149
150 switch (reg) {
Guenter Roeckc5e67632011-08-02 11:08:57 -0700151 case PMBUS_IOUT_UC_FAULT_LIMIT:
152 case PMBUS_IOUT_OC_FAULT_LIMIT:
153 ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
154 word);
155 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700156 case PMBUS_VIRT_RESET_IOUT_HISTORY:
157 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
158 break;
159 case PMBUS_VIRT_RESET_VOUT_HISTORY:
160 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
161 break;
162 case PMBUS_VIRT_RESET_VIN_HISTORY:
163 ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
164 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700165 case PMBUS_VIRT_RESET_PIN_HISTORY:
166 ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
167 break;
Guenter Roeckc576e302011-07-09 11:17:33 -0700168 default:
169 ret = -ENODATA;
170 break;
171 }
172 return ret;
173}
174
Guenter Roeckc5e67632011-08-02 11:08:57 -0700175static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
176{
177 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
178 const struct adm1275_data *data = to_adm1275_data(info);
179 int mfr_status, ret;
180
Guenter Roeckda8e48a2011-07-29 22:19:39 -0700181 if (page > 0)
Guenter Roeckc5e67632011-08-02 11:08:57 -0700182 return -ENXIO;
183
184 switch (reg) {
185 case PMBUS_STATUS_IOUT:
186 ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
187 if (ret < 0)
188 break;
189 mfr_status = pmbus_read_byte_data(client, page,
190 PMBUS_STATUS_MFR_SPECIFIC);
191 if (mfr_status < 0) {
192 ret = mfr_status;
193 break;
194 }
195 if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
196 ret |= data->have_oc_fault ?
197 PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
198 }
199 break;
Guenter Roeck92711262012-02-24 03:40:53 -0800200 case PMBUS_STATUS_VOUT:
201 if (data->id != adm1075) {
202 ret = -ENODATA;
203 break;
204 }
205 ret = 0;
206 mfr_status = pmbus_read_byte_data(client, 0,
207 ADM1075_VAUX_STATUS);
208 if (mfr_status & ADM1075_VAUX_OV_WARN)
209 ret |= PB_VOLTAGE_OV_WARNING;
210 if (mfr_status & ADM1075_VAUX_UV_WARN)
211 ret |= PB_VOLTAGE_UV_WARNING;
212 break;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700213 default:
214 ret = -ENODATA;
215 break;
216 }
217 return ret;
218}
219
Guenter Roeck87102802011-09-30 11:53:25 -0700220static const struct i2c_device_id adm1275_id[] = {
Guenter Roeck92711262012-02-24 03:40:53 -0800221 { "adm1075", adm1075 },
Guenter Roeck87102802011-09-30 11:53:25 -0700222 { "adm1275", adm1275 },
223 { "adm1276", adm1276 },
224 { }
225};
226MODULE_DEVICE_TABLE(i2c, adm1275_id);
227
Guenter Roeck83f76492011-03-17 13:16:01 -0700228static int adm1275_probe(struct i2c_client *client,
229 const struct i2c_device_id *id)
230{
Guenter Roeck87102802011-09-30 11:53:25 -0700231 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
Guenter Roeckc5e67632011-08-02 11:08:57 -0700232 int config, device_config;
Guenter Roeck3b33ca42011-06-30 02:30:03 -0700233 int ret;
Guenter Roeck83f76492011-03-17 13:16:01 -0700234 struct pmbus_driver_info *info;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700235 struct adm1275_data *data;
Guenter Roeck87102802011-09-30 11:53:25 -0700236 const struct i2c_device_id *mid;
Guenter Roeck83f76492011-03-17 13:16:01 -0700237
238 if (!i2c_check_functionality(client->adapter,
Guenter Roeck87102802011-09-30 11:53:25 -0700239 I2C_FUNC_SMBUS_READ_BYTE_DATA
240 | I2C_FUNC_SMBUS_BLOCK_DATA))
Guenter Roeck83f76492011-03-17 13:16:01 -0700241 return -ENODEV;
242
Guenter Roeck87102802011-09-30 11:53:25 -0700243 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
244 if (ret < 0) {
245 dev_err(&client->dev, "Failed to read Manufacturer ID\n");
246 return ret;
247 }
248 if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
249 dev_err(&client->dev, "Unsupported Manufacturer ID\n");
250 return -ENODEV;
251 }
252
253 ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
254 if (ret < 0) {
255 dev_err(&client->dev, "Failed to read Manufacturer Model\n");
256 return ret;
257 }
258 for (mid = adm1275_id; mid->name[0]; mid++) {
259 if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
260 break;
261 }
262 if (!mid->name[0]) {
263 dev_err(&client->dev, "Unsupported device\n");
264 return -ENODEV;
265 }
266
267 if (id->driver_data != mid->driver_data)
268 dev_notice(&client->dev,
269 "Device mismatch: Configured %s, detected %s\n",
270 id->name, mid->name);
271
272 config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
273 if (config < 0)
274 return config;
275
276 device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
277 if (device_config < 0)
278 return device_config;
279
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800280 data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
281 GFP_KERNEL);
Guenter Roeckc5e67632011-08-02 11:08:57 -0700282 if (!data)
Guenter Roeck83f76492011-03-17 13:16:01 -0700283 return -ENOMEM;
284
Guenter Roeck87102802011-09-30 11:53:25 -0700285 data->id = mid->driver_data;
Guenter Roeck83f76492011-03-17 13:16:01 -0700286
Guenter Roeckc5e67632011-08-02 11:08:57 -0700287 info = &data->info;
288
Guenter Roeck83f76492011-03-17 13:16:01 -0700289 info->pages = 1;
Guenter Roeck1061d852011-06-25 11:21:49 -0700290 info->format[PSC_VOLTAGE_IN] = direct;
291 info->format[PSC_VOLTAGE_OUT] = direct;
292 info->format[PSC_CURRENT_OUT] = direct;
Guenter Roeck7e97bbb2011-07-14 14:18:03 -0700293 info->m[PSC_CURRENT_OUT] = 807;
Guenter Roeck83f76492011-03-17 13:16:01 -0700294 info->b[PSC_CURRENT_OUT] = 20475;
295 info->R[PSC_CURRENT_OUT] = -1;
296 info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
297
Guenter Roeckc576e302011-07-09 11:17:33 -0700298 info->read_word_data = adm1275_read_word_data;
Guenter Roeckc5e67632011-08-02 11:08:57 -0700299 info->read_byte_data = adm1275_read_byte_data;
Guenter Roeckc576e302011-07-09 11:17:33 -0700300 info->write_word_data = adm1275_write_word_data;
301
Guenter Roeck92711262012-02-24 03:40:53 -0800302 if (data->id == adm1075) {
303 info->m[PSC_VOLTAGE_IN] = 27169;
304 info->b[PSC_VOLTAGE_IN] = 0;
305 info->R[PSC_VOLTAGE_IN] = -1;
306 info->m[PSC_VOLTAGE_OUT] = 27169;
307 info->b[PSC_VOLTAGE_OUT] = 0;
308 info->R[PSC_VOLTAGE_OUT] = -1;
309 } else if (config & ADM1275_VRANGE) {
Guenter Roeck7e97bbb2011-07-14 14:18:03 -0700310 info->m[PSC_VOLTAGE_IN] = 19199;
Guenter Roeck83f76492011-03-17 13:16:01 -0700311 info->b[PSC_VOLTAGE_IN] = 0;
312 info->R[PSC_VOLTAGE_IN] = -2;
Guenter Roeck7e97bbb2011-07-14 14:18:03 -0700313 info->m[PSC_VOLTAGE_OUT] = 19199;
Guenter Roeck83f76492011-03-17 13:16:01 -0700314 info->b[PSC_VOLTAGE_OUT] = 0;
315 info->R[PSC_VOLTAGE_OUT] = -2;
316 } else {
Guenter Roeck7e97bbb2011-07-14 14:18:03 -0700317 info->m[PSC_VOLTAGE_IN] = 6720;
Guenter Roeck83f76492011-03-17 13:16:01 -0700318 info->b[PSC_VOLTAGE_IN] = 0;
319 info->R[PSC_VOLTAGE_IN] = -1;
Guenter Roeck7e97bbb2011-07-14 14:18:03 -0700320 info->m[PSC_VOLTAGE_OUT] = 6720;
Guenter Roeck83f76492011-03-17 13:16:01 -0700321 info->b[PSC_VOLTAGE_OUT] = 0;
322 info->R[PSC_VOLTAGE_OUT] = -1;
323 }
324
Guenter Roeckc5e67632011-08-02 11:08:57 -0700325 if (device_config & ADM1275_IOUT_WARN2_SELECT)
326 data->have_oc_fault = true;
327
Guenter Roeck87102802011-09-30 11:53:25 -0700328 switch (data->id) {
Guenter Roeck92711262012-02-24 03:40:53 -0800329 case adm1075:
330 info->format[PSC_POWER] = direct;
331 info->b[PSC_POWER] = 0;
332 info->R[PSC_POWER] = -1;
333 switch (config & ADM1075_IRANGE_MASK) {
334 case ADM1075_IRANGE_25:
335 info->m[PSC_POWER] = 8549;
336 info->m[PSC_CURRENT_OUT] = 806;
337 break;
338 case ADM1075_IRANGE_50:
339 info->m[PSC_POWER] = 4279;
340 info->m[PSC_CURRENT_OUT] = 404;
341 break;
342 default:
343 dev_err(&client->dev, "Invalid input current range");
344 info->m[PSC_POWER] = 0;
345 info->m[PSC_CURRENT_OUT] = 0;
346 break;
347 }
348 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
349 | PMBUS_HAVE_STATUS_INPUT;
350 if (config & ADM1275_VIN_VOUT_SELECT)
351 info->func[0] |=
352 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
353 break;
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700354 case adm1275:
355 if (config & ADM1275_VIN_VOUT_SELECT)
356 info->func[0] |=
357 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
358 else
359 info->func[0] |=
360 PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
361 break;
362 case adm1276:
363 info->format[PSC_POWER] = direct;
364 info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
365 | PMBUS_HAVE_STATUS_INPUT;
366 if (config & ADM1275_VIN_VOUT_SELECT)
367 info->func[0] |=
368 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
369 if (config & ADM1275_VRANGE) {
370 info->m[PSC_POWER] = 6043;
371 info->b[PSC_POWER] = 0;
372 info->R[PSC_POWER] = -2;
373 } else {
374 info->m[PSC_POWER] = 2115;
375 info->b[PSC_POWER] = 0;
376 info->R[PSC_POWER] = -1;
377 }
378 break;
379 }
Guenter Roeck83f76492011-03-17 13:16:01 -0700380
Guenter Roeck8b313ca2012-02-22 08:56:43 -0800381 return pmbus_do_probe(client, id, info);
Guenter Roeck83f76492011-03-17 13:16:01 -0700382}
383
Guenter Roeck83f76492011-03-17 13:16:01 -0700384static struct i2c_driver adm1275_driver = {
385 .driver = {
386 .name = "adm1275",
387 },
388 .probe = adm1275_probe,
Guenter Roeckdd285ad2012-02-22 08:56:44 -0800389 .remove = pmbus_do_remove,
Guenter Roeck83f76492011-03-17 13:16:01 -0700390 .id_table = adm1275_id,
391};
392
Axel Linf0967ee2012-01-20 15:38:18 +0800393module_i2c_driver(adm1275_driver);
Guenter Roeck83f76492011-03-17 13:16:01 -0700394
395MODULE_AUTHOR("Guenter Roeck");
Guenter Roeck5cf231a2011-07-14 11:55:35 -0700396MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
Guenter Roeck83f76492011-03-17 13:16:01 -0700397MODULE_LICENSE("GPL");