2 * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
3 * and Digital Power Monitor
5 * Copyright (c) 2011 Ericsson AB.
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.
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.
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>
26 enum chips
{ adm1275
, adm1276
};
28 #define ADM1275_PEAK_IOUT 0xd0
29 #define ADM1275_PEAK_VIN 0xd1
30 #define ADM1275_PEAK_VOUT 0xd2
31 #define ADM1275_PMON_CONFIG 0xd4
33 #define ADM1275_VIN_VOUT_SELECT (1 << 6)
34 #define ADM1275_VRANGE (1 << 5)
36 #define ADM1275_IOUT_WARN2_LIMIT 0xd7
37 #define ADM1275_DEVICE_CONFIG 0xd8
39 #define ADM1275_IOUT_WARN2_SELECT (1 << 4)
41 #define ADM1276_PEAK_PIN 0xda
43 #define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0)
48 struct pmbus_driver_info info
;
51 #define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
53 static int adm1275_read_word_data(struct i2c_client
*client
, int page
, int reg
)
55 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
56 const struct adm1275_data
*data
= to_adm1275_data(info
);
63 case PMBUS_IOUT_UC_FAULT_LIMIT
:
64 if (data
->have_oc_fault
) {
68 ret
= pmbus_read_word_data(client
, 0, ADM1275_IOUT_WARN2_LIMIT
);
70 case PMBUS_IOUT_OC_FAULT_LIMIT
:
71 if (!data
->have_oc_fault
) {
75 ret
= pmbus_read_word_data(client
, 0, ADM1275_IOUT_WARN2_LIMIT
);
77 case PMBUS_VIRT_READ_IOUT_MAX
:
78 ret
= pmbus_read_word_data(client
, 0, ADM1275_PEAK_IOUT
);
80 case PMBUS_VIRT_READ_VOUT_MAX
:
81 ret
= pmbus_read_word_data(client
, 0, ADM1275_PEAK_VOUT
);
83 case PMBUS_VIRT_READ_VIN_MAX
:
84 ret
= pmbus_read_word_data(client
, 0, ADM1275_PEAK_VIN
);
86 case PMBUS_VIRT_READ_PIN_MAX
:
87 if (data
->id
!= adm1276
) {
91 ret
= pmbus_read_word_data(client
, 0, ADM1276_PEAK_PIN
);
93 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
94 case PMBUS_VIRT_RESET_VOUT_HISTORY
:
95 case PMBUS_VIRT_RESET_VIN_HISTORY
:
97 case PMBUS_VIRT_RESET_PIN_HISTORY
:
98 if (data
->id
!= adm1276
)
108 static int adm1275_write_word_data(struct i2c_client
*client
, int page
, int reg
,
117 case PMBUS_IOUT_UC_FAULT_LIMIT
:
118 case PMBUS_IOUT_OC_FAULT_LIMIT
:
119 ret
= pmbus_write_word_data(client
, 0, ADM1275_IOUT_WARN2_LIMIT
,
122 case PMBUS_VIRT_RESET_IOUT_HISTORY
:
123 ret
= pmbus_write_word_data(client
, 0, ADM1275_PEAK_IOUT
, 0);
125 case PMBUS_VIRT_RESET_VOUT_HISTORY
:
126 ret
= pmbus_write_word_data(client
, 0, ADM1275_PEAK_VOUT
, 0);
128 case PMBUS_VIRT_RESET_VIN_HISTORY
:
129 ret
= pmbus_write_word_data(client
, 0, ADM1275_PEAK_VIN
, 0);
131 case PMBUS_VIRT_RESET_PIN_HISTORY
:
132 ret
= pmbus_write_word_data(client
, 0, ADM1276_PEAK_PIN
, 0);
141 static int adm1275_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
143 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
144 const struct adm1275_data
*data
= to_adm1275_data(info
);
151 case PMBUS_STATUS_IOUT
:
152 ret
= pmbus_read_byte_data(client
, page
, PMBUS_STATUS_IOUT
);
155 mfr_status
= pmbus_read_byte_data(client
, page
,
156 PMBUS_STATUS_MFR_SPECIFIC
);
157 if (mfr_status
< 0) {
161 if (mfr_status
& ADM1275_MFR_STATUS_IOUT_WARN2
) {
162 ret
|= data
->have_oc_fault
?
163 PB_IOUT_OC_FAULT
: PB_IOUT_UC_FAULT
;
173 static const struct i2c_device_id adm1275_id
[] = {
174 { "adm1275", adm1275
},
175 { "adm1276", adm1276
},
178 MODULE_DEVICE_TABLE(i2c
, adm1275_id
);
180 static int adm1275_probe(struct i2c_client
*client
,
181 const struct i2c_device_id
*id
)
183 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1];
184 int config
, device_config
;
186 struct pmbus_driver_info
*info
;
187 struct adm1275_data
*data
;
188 const struct i2c_device_id
*mid
;
190 if (!i2c_check_functionality(client
->adapter
,
191 I2C_FUNC_SMBUS_READ_BYTE_DATA
192 | I2C_FUNC_SMBUS_BLOCK_DATA
))
195 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_ID
, block_buffer
);
197 dev_err(&client
->dev
, "Failed to read Manufacturer ID\n");
200 if (ret
!= 3 || strncmp(block_buffer
, "ADI", 3)) {
201 dev_err(&client
->dev
, "Unsupported Manufacturer ID\n");
205 ret
= i2c_smbus_read_block_data(client
, PMBUS_MFR_MODEL
, block_buffer
);
207 dev_err(&client
->dev
, "Failed to read Manufacturer Model\n");
210 for (mid
= adm1275_id
; mid
->name
[0]; mid
++) {
211 if (!strncasecmp(mid
->name
, block_buffer
, strlen(mid
->name
)))
215 dev_err(&client
->dev
, "Unsupported device\n");
219 if (id
->driver_data
!= mid
->driver_data
)
220 dev_notice(&client
->dev
,
221 "Device mismatch: Configured %s, detected %s\n",
222 id
->name
, mid
->name
);
224 config
= i2c_smbus_read_byte_data(client
, ADM1275_PMON_CONFIG
);
228 device_config
= i2c_smbus_read_byte_data(client
, ADM1275_DEVICE_CONFIG
);
229 if (device_config
< 0)
230 return device_config
;
232 data
= kzalloc(sizeof(struct adm1275_data
), GFP_KERNEL
);
236 data
->id
= mid
->driver_data
;
241 info
->format
[PSC_VOLTAGE_IN
] = direct
;
242 info
->format
[PSC_VOLTAGE_OUT
] = direct
;
243 info
->format
[PSC_CURRENT_OUT
] = direct
;
244 info
->m
[PSC_CURRENT_OUT
] = 807;
245 info
->b
[PSC_CURRENT_OUT
] = 20475;
246 info
->R
[PSC_CURRENT_OUT
] = -1;
247 info
->func
[0] = PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
;
249 info
->read_word_data
= adm1275_read_word_data
;
250 info
->read_byte_data
= adm1275_read_byte_data
;
251 info
->write_word_data
= adm1275_write_word_data
;
253 if (config
& ADM1275_VRANGE
) {
254 info
->m
[PSC_VOLTAGE_IN
] = 19199;
255 info
->b
[PSC_VOLTAGE_IN
] = 0;
256 info
->R
[PSC_VOLTAGE_IN
] = -2;
257 info
->m
[PSC_VOLTAGE_OUT
] = 19199;
258 info
->b
[PSC_VOLTAGE_OUT
] = 0;
259 info
->R
[PSC_VOLTAGE_OUT
] = -2;
261 info
->m
[PSC_VOLTAGE_IN
] = 6720;
262 info
->b
[PSC_VOLTAGE_IN
] = 0;
263 info
->R
[PSC_VOLTAGE_IN
] = -1;
264 info
->m
[PSC_VOLTAGE_OUT
] = 6720;
265 info
->b
[PSC_VOLTAGE_OUT
] = 0;
266 info
->R
[PSC_VOLTAGE_OUT
] = -1;
269 if (device_config
& ADM1275_IOUT_WARN2_SELECT
)
270 data
->have_oc_fault
= true;
274 if (config
& ADM1275_VIN_VOUT_SELECT
)
276 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
279 PMBUS_HAVE_VIN
| PMBUS_HAVE_STATUS_INPUT
;
282 info
->format
[PSC_POWER
] = direct
;
283 info
->func
[0] |= PMBUS_HAVE_VIN
| PMBUS_HAVE_PIN
284 | PMBUS_HAVE_STATUS_INPUT
;
285 if (config
& ADM1275_VIN_VOUT_SELECT
)
287 PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
;
288 if (config
& ADM1275_VRANGE
) {
289 info
->m
[PSC_POWER
] = 6043;
290 info
->b
[PSC_POWER
] = 0;
291 info
->R
[PSC_POWER
] = -2;
293 info
->m
[PSC_POWER
] = 2115;
294 info
->b
[PSC_POWER
] = 0;
295 info
->R
[PSC_POWER
] = -1;
300 ret
= pmbus_do_probe(client
, id
, info
);
310 static int adm1275_remove(struct i2c_client
*client
)
312 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
313 const struct adm1275_data
*data
= to_adm1275_data(info
);
315 pmbus_do_remove(client
);
320 static struct i2c_driver adm1275_driver
= {
324 .probe
= adm1275_probe
,
325 .remove
= adm1275_remove
,
326 .id_table
= adm1275_id
,
329 static int __init
adm1275_init(void)
331 return i2c_add_driver(&adm1275_driver
);
334 static void __exit
adm1275_exit(void)
336 i2c_del_driver(&adm1275_driver
);
339 MODULE_AUTHOR("Guenter Roeck");
340 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
341 MODULE_LICENSE("GPL");
342 module_init(adm1275_init
);
343 module_exit(adm1275_exit
);