Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 26 | enum chips { adm1275, adm1276 }; |
| 27 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 28 | #define ADM1275_PEAK_IOUT 0xd0 |
| 29 | #define ADM1275_PEAK_VIN 0xd1 |
| 30 | #define ADM1275_PEAK_VOUT 0xd2 |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 31 | #define ADM1275_PMON_CONFIG 0xd4 |
| 32 | |
| 33 | #define ADM1275_VIN_VOUT_SELECT (1 << 6) |
| 34 | #define ADM1275_VRANGE (1 << 5) |
| 35 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 36 | #define ADM1275_IOUT_WARN2_LIMIT 0xd7 |
| 37 | #define ADM1275_DEVICE_CONFIG 0xd8 |
| 38 | |
| 39 | #define ADM1275_IOUT_WARN2_SELECT (1 << 4) |
| 40 | |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 41 | #define ADM1276_PEAK_PIN 0xda |
| 42 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 43 | #define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0) |
| 44 | |
| 45 | struct adm1275_data { |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 46 | int id; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 47 | bool have_oc_fault; |
| 48 | struct pmbus_driver_info info; |
| 49 | }; |
| 50 | |
| 51 | #define to_adm1275_data(x) container_of(x, struct adm1275_data, info) |
| 52 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 53 | static int adm1275_read_word_data(struct i2c_client *client, int page, int reg) |
| 54 | { |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 55 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
| 56 | const struct adm1275_data *data = to_adm1275_data(info); |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 57 | int ret = 0; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 58 | |
| 59 | if (page) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 60 | return -ENXIO; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 61 | |
| 62 | switch (reg) { |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 63 | case PMBUS_IOUT_UC_FAULT_LIMIT: |
| 64 | if (data->have_oc_fault) { |
| 65 | ret = -ENXIO; |
| 66 | break; |
| 67 | } |
| 68 | ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT); |
| 69 | break; |
| 70 | case PMBUS_IOUT_OC_FAULT_LIMIT: |
| 71 | if (!data->have_oc_fault) { |
| 72 | ret = -ENXIO; |
| 73 | break; |
| 74 | } |
| 75 | ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT); |
| 76 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 77 | case PMBUS_VIRT_READ_IOUT_MAX: |
| 78 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT); |
| 79 | break; |
| 80 | case PMBUS_VIRT_READ_VOUT_MAX: |
| 81 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT); |
| 82 | break; |
| 83 | case PMBUS_VIRT_READ_VIN_MAX: |
| 84 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN); |
| 85 | break; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 86 | case PMBUS_VIRT_READ_PIN_MAX: |
| 87 | if (data->id != adm1276) { |
| 88 | ret = -ENXIO; |
| 89 | break; |
| 90 | } |
| 91 | ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN); |
| 92 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 93 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 94 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 95 | case PMBUS_VIRT_RESET_VIN_HISTORY: |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 96 | break; |
| 97 | case PMBUS_VIRT_RESET_PIN_HISTORY: |
| 98 | if (data->id != adm1276) |
| 99 | ret = -ENXIO; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | ret = -ENODATA; |
| 103 | break; |
| 104 | } |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | static int adm1275_write_word_data(struct i2c_client *client, int page, int reg, |
| 109 | u16 word) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | if (page) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 114 | return -ENXIO; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 115 | |
| 116 | switch (reg) { |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 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, |
| 120 | word); |
| 121 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 122 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 123 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0); |
| 124 | break; |
| 125 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 126 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0); |
| 127 | break; |
| 128 | case PMBUS_VIRT_RESET_VIN_HISTORY: |
| 129 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0); |
| 130 | break; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 131 | case PMBUS_VIRT_RESET_PIN_HISTORY: |
| 132 | ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0); |
| 133 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 134 | default: |
| 135 | ret = -ENODATA; |
| 136 | break; |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 141 | static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg) |
| 142 | { |
| 143 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
| 144 | const struct adm1275_data *data = to_adm1275_data(info); |
| 145 | int mfr_status, ret; |
| 146 | |
Guenter Roeck | da8e48a | 2011-07-29 22:19:39 -0700 | [diff] [blame] | 147 | if (page > 0) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 148 | return -ENXIO; |
| 149 | |
| 150 | switch (reg) { |
| 151 | case PMBUS_STATUS_IOUT: |
| 152 | ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT); |
| 153 | if (ret < 0) |
| 154 | break; |
| 155 | mfr_status = pmbus_read_byte_data(client, page, |
| 156 | PMBUS_STATUS_MFR_SPECIFIC); |
| 157 | if (mfr_status < 0) { |
| 158 | ret = mfr_status; |
| 159 | break; |
| 160 | } |
| 161 | if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) { |
| 162 | ret |= data->have_oc_fault ? |
| 163 | PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT; |
| 164 | } |
| 165 | break; |
| 166 | default: |
| 167 | ret = -ENODATA; |
| 168 | break; |
| 169 | } |
| 170 | return ret; |
| 171 | } |
| 172 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 173 | static const struct i2c_device_id adm1275_id[] = { |
| 174 | { "adm1275", adm1275 }, |
| 175 | { "adm1276", adm1276 }, |
| 176 | { } |
| 177 | }; |
| 178 | MODULE_DEVICE_TABLE(i2c, adm1275_id); |
| 179 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 180 | static int adm1275_probe(struct i2c_client *client, |
| 181 | const struct i2c_device_id *id) |
| 182 | { |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 183 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 184 | int config, device_config; |
Guenter Roeck | 3b33ca4 | 2011-06-30 02:30:03 -0700 | [diff] [blame] | 185 | int ret; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 186 | struct pmbus_driver_info *info; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 187 | struct adm1275_data *data; |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 188 | const struct i2c_device_id *mid; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 189 | |
| 190 | if (!i2c_check_functionality(client->adapter, |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 191 | I2C_FUNC_SMBUS_READ_BYTE_DATA |
| 192 | | I2C_FUNC_SMBUS_BLOCK_DATA)) |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 193 | return -ENODEV; |
| 194 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 195 | ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer); |
| 196 | if (ret < 0) { |
| 197 | dev_err(&client->dev, "Failed to read Manufacturer ID\n"); |
| 198 | return ret; |
| 199 | } |
| 200 | if (ret != 3 || strncmp(block_buffer, "ADI", 3)) { |
| 201 | dev_err(&client->dev, "Unsupported Manufacturer ID\n"); |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer); |
| 206 | if (ret < 0) { |
| 207 | dev_err(&client->dev, "Failed to read Manufacturer Model\n"); |
| 208 | return ret; |
| 209 | } |
| 210 | for (mid = adm1275_id; mid->name[0]; mid++) { |
| 211 | if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) |
| 212 | break; |
| 213 | } |
| 214 | if (!mid->name[0]) { |
| 215 | dev_err(&client->dev, "Unsupported device\n"); |
| 216 | return -ENODEV; |
| 217 | } |
| 218 | |
| 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); |
| 223 | |
| 224 | config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); |
| 225 | if (config < 0) |
| 226 | return config; |
| 227 | |
| 228 | device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG); |
| 229 | if (device_config < 0) |
| 230 | return device_config; |
| 231 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame^] | 232 | data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data), |
| 233 | GFP_KERNEL); |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 234 | if (!data) |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 235 | return -ENOMEM; |
| 236 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 237 | data->id = mid->driver_data; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 238 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 239 | info = &data->info; |
| 240 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 241 | info->pages = 1; |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 242 | info->format[PSC_VOLTAGE_IN] = direct; |
| 243 | info->format[PSC_VOLTAGE_OUT] = direct; |
| 244 | info->format[PSC_CURRENT_OUT] = direct; |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 245 | info->m[PSC_CURRENT_OUT] = 807; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 246 | info->b[PSC_CURRENT_OUT] = 20475; |
| 247 | info->R[PSC_CURRENT_OUT] = -1; |
| 248 | info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT; |
| 249 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 250 | info->read_word_data = adm1275_read_word_data; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 251 | info->read_byte_data = adm1275_read_byte_data; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 252 | info->write_word_data = adm1275_write_word_data; |
| 253 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 254 | if (config & ADM1275_VRANGE) { |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 255 | info->m[PSC_VOLTAGE_IN] = 19199; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 256 | info->b[PSC_VOLTAGE_IN] = 0; |
| 257 | info->R[PSC_VOLTAGE_IN] = -2; |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 258 | info->m[PSC_VOLTAGE_OUT] = 19199; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 259 | info->b[PSC_VOLTAGE_OUT] = 0; |
| 260 | info->R[PSC_VOLTAGE_OUT] = -2; |
| 261 | } else { |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 262 | info->m[PSC_VOLTAGE_IN] = 6720; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 263 | info->b[PSC_VOLTAGE_IN] = 0; |
| 264 | info->R[PSC_VOLTAGE_IN] = -1; |
Guenter Roeck | 7e97bbb | 2011-07-14 14:18:03 -0700 | [diff] [blame] | 265 | info->m[PSC_VOLTAGE_OUT] = 6720; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 266 | info->b[PSC_VOLTAGE_OUT] = 0; |
| 267 | info->R[PSC_VOLTAGE_OUT] = -1; |
| 268 | } |
| 269 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 270 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
| 271 | data->have_oc_fault = true; |
| 272 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 273 | switch (data->id) { |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 274 | case adm1275: |
| 275 | if (config & ADM1275_VIN_VOUT_SELECT) |
| 276 | info->func[0] |= |
| 277 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 278 | else |
| 279 | info->func[0] |= |
| 280 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; |
| 281 | break; |
| 282 | case adm1276: |
| 283 | info->format[PSC_POWER] = direct; |
| 284 | info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN |
| 285 | | PMBUS_HAVE_STATUS_INPUT; |
| 286 | if (config & ADM1275_VIN_VOUT_SELECT) |
| 287 | info->func[0] |= |
| 288 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 289 | if (config & ADM1275_VRANGE) { |
| 290 | info->m[PSC_POWER] = 6043; |
| 291 | info->b[PSC_POWER] = 0; |
| 292 | info->R[PSC_POWER] = -2; |
| 293 | } else { |
| 294 | info->m[PSC_POWER] = 2115; |
| 295 | info->b[PSC_POWER] = 0; |
| 296 | info->R[PSC_POWER] = -1; |
| 297 | } |
| 298 | break; |
| 299 | } |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 300 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame^] | 301 | return pmbus_do_probe(client, id, info); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int adm1275_remove(struct i2c_client *client) |
| 305 | { |
Guenter Roeck | 866cf12 | 2011-08-26 08:12:38 -0700 | [diff] [blame] | 306 | pmbus_do_remove(client); |
Guenter Roeck | 866cf12 | 2011-08-26 08:12:38 -0700 | [diff] [blame] | 307 | return 0; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 310 | static struct i2c_driver adm1275_driver = { |
| 311 | .driver = { |
| 312 | .name = "adm1275", |
| 313 | }, |
| 314 | .probe = adm1275_probe, |
| 315 | .remove = adm1275_remove, |
| 316 | .id_table = adm1275_id, |
| 317 | }; |
| 318 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 319 | module_i2c_driver(adm1275_driver); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 320 | |
| 321 | MODULE_AUTHOR("Guenter Roeck"); |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 322 | MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles"); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 323 | MODULE_LICENSE("GPL"); |