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> |
Guenter Roeck | 99b4160 | 2015-07-04 09:19:48 -0700 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 25 | #include "pmbus.h" |
| 26 | |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 27 | enum chips { adm1075, adm1275, adm1276, adm1278, adm1293, adm1294 }; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 28 | |
| 29 | #define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0) |
| 30 | #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5) |
| 31 | #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6) |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 32 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 33 | #define ADM1275_PEAK_IOUT 0xd0 |
| 34 | #define ADM1275_PEAK_VIN 0xd1 |
| 35 | #define ADM1275_PEAK_VOUT 0xd2 |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 36 | #define ADM1275_PMON_CONFIG 0xd4 |
| 37 | |
Guenter Roeck | 99b4160 | 2015-07-04 09:19:48 -0700 | [diff] [blame] | 38 | #define ADM1275_VIN_VOUT_SELECT BIT(6) |
| 39 | #define ADM1275_VRANGE BIT(5) |
| 40 | #define ADM1075_IRANGE_50 BIT(4) |
| 41 | #define ADM1075_IRANGE_25 BIT(3) |
| 42 | #define ADM1075_IRANGE_MASK (BIT(3) | BIT(4)) |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 43 | |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 44 | #define ADM1278_TEMP1_EN BIT(3) |
| 45 | #define ADM1278_VIN_EN BIT(2) |
| 46 | #define ADM1278_VOUT_EN BIT(1) |
| 47 | |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 48 | #define ADM1293_IRANGE_25 0 |
| 49 | #define ADM1293_IRANGE_50 BIT(6) |
| 50 | #define ADM1293_IRANGE_100 BIT(7) |
| 51 | #define ADM1293_IRANGE_200 (BIT(6) | BIT(7)) |
| 52 | #define ADM1293_IRANGE_MASK (BIT(6) | BIT(7)) |
| 53 | |
| 54 | #define ADM1293_VIN_SEL_012 BIT(2) |
| 55 | #define ADM1293_VIN_SEL_074 BIT(3) |
| 56 | #define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3)) |
| 57 | #define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3)) |
| 58 | |
| 59 | #define ADM1293_VAUX_EN BIT(1) |
| 60 | |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 61 | #define ADM1278_PEAK_TEMP 0xd7 |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 62 | #define ADM1275_IOUT_WARN2_LIMIT 0xd7 |
| 63 | #define ADM1275_DEVICE_CONFIG 0xd8 |
| 64 | |
Guenter Roeck | 99b4160 | 2015-07-04 09:19:48 -0700 | [diff] [blame] | 65 | #define ADM1275_IOUT_WARN2_SELECT BIT(4) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 66 | |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 67 | #define ADM1276_PEAK_PIN 0xda |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 68 | #define ADM1075_READ_VAUX 0xdd |
| 69 | #define ADM1075_VAUX_OV_WARN_LIMIT 0xde |
| 70 | #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 71 | #define ADM1293_IOUT_MIN 0xe3 |
| 72 | #define ADM1293_PIN_MIN 0xe4 |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 73 | #define ADM1075_VAUX_STATUS 0xf6 |
| 74 | |
Guenter Roeck | 99b4160 | 2015-07-04 09:19:48 -0700 | [diff] [blame] | 75 | #define ADM1075_VAUX_OV_WARN BIT(7) |
| 76 | #define ADM1075_VAUX_UV_WARN BIT(6) |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 77 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 78 | struct adm1275_data { |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 79 | int id; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 80 | bool have_oc_fault; |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 81 | bool have_uc_fault; |
| 82 | bool have_vout; |
| 83 | bool have_vaux_status; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 84 | bool have_mfr_vaux_status; |
| 85 | bool have_iout_min; |
| 86 | bool have_pin_min; |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 87 | bool have_pin_max; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 88 | bool have_temp_max; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 89 | struct pmbus_driver_info info; |
| 90 | }; |
| 91 | |
| 92 | #define to_adm1275_data(x) container_of(x, struct adm1275_data, info) |
| 93 | |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 94 | struct coefficients { |
| 95 | s16 m; |
| 96 | s16 b; |
| 97 | s16 R; |
| 98 | }; |
| 99 | |
| 100 | static const struct coefficients adm1075_coefficients[] = { |
| 101 | [0] = { 27169, 0, -1 }, /* voltage */ |
| 102 | [1] = { 806, 20475, -1 }, /* current, irange25 */ |
| 103 | [2] = { 404, 20475, -1 }, /* current, irange50 */ |
| 104 | [3] = { 0, -1, 8549 }, /* power, irange25 */ |
| 105 | [4] = { 0, -1, 4279 }, /* power, irange50 */ |
| 106 | }; |
| 107 | |
| 108 | static const struct coefficients adm1275_coefficients[] = { |
| 109 | [0] = { 19199, 0, -2 }, /* voltage, vrange set */ |
| 110 | [1] = { 6720, 0, -1 }, /* voltage, vrange not set */ |
| 111 | [2] = { 807, 20475, -1 }, /* current */ |
| 112 | }; |
| 113 | |
| 114 | static const struct coefficients adm1276_coefficients[] = { |
| 115 | [0] = { 19199, 0, -2 }, /* voltage, vrange set */ |
| 116 | [1] = { 6720, 0, -1 }, /* voltage, vrange not set */ |
| 117 | [2] = { 807, 20475, -1 }, /* current */ |
| 118 | [3] = { 6043, 0, -2 }, /* power, vrange set */ |
| 119 | [4] = { 2115, 0, -1 }, /* power, vrange not set */ |
| 120 | }; |
| 121 | |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 122 | static const struct coefficients adm1278_coefficients[] = { |
| 123 | [0] = { 19599, 0, -2 }, /* voltage */ |
| 124 | [1] = { 800, 20475, -1 }, /* current */ |
| 125 | [2] = { 6123, 0, -2 }, /* power */ |
| 126 | [3] = { 42, 31880, -1 }, /* temperature */ |
| 127 | }; |
| 128 | |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 129 | static const struct coefficients adm1293_coefficients[] = { |
| 130 | [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */ |
| 131 | [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */ |
| 132 | [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */ |
| 133 | [3] = { 8000, -100, -2 }, /* current, irange25 */ |
| 134 | [4] = { 4000, -100, -2 }, /* current, irange50 */ |
| 135 | [5] = { 20000, -1000, -3 }, /* current, irange100 */ |
| 136 | [6] = { 10000, -1000, -3 }, /* current, irange200 */ |
| 137 | [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */ |
| 138 | [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */ |
| 139 | [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */ |
| 140 | [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */ |
| 141 | [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */ |
| 142 | [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */ |
| 143 | [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */ |
| 144 | [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */ |
| 145 | [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */ |
| 146 | [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */ |
| 147 | [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */ |
| 148 | [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */ |
| 149 | }; |
| 150 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 151 | static int adm1275_read_word_data(struct i2c_client *client, int page, int reg) |
| 152 | { |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 153 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
| 154 | const struct adm1275_data *data = to_adm1275_data(info); |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 155 | int ret = 0; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 156 | |
| 157 | if (page) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 158 | return -ENXIO; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 159 | |
| 160 | switch (reg) { |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 161 | case PMBUS_IOUT_UC_FAULT_LIMIT: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 162 | if (!data->have_uc_fault) |
| 163 | return -ENXIO; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 164 | ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT); |
| 165 | break; |
| 166 | case PMBUS_IOUT_OC_FAULT_LIMIT: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 167 | if (!data->have_oc_fault) |
| 168 | return -ENXIO; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 169 | ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT); |
| 170 | break; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 171 | case PMBUS_VOUT_OV_WARN_LIMIT: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 172 | if (data->have_vout) |
| 173 | return -ENODATA; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 174 | ret = pmbus_read_word_data(client, 0, |
| 175 | ADM1075_VAUX_OV_WARN_LIMIT); |
| 176 | break; |
| 177 | case PMBUS_VOUT_UV_WARN_LIMIT: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 178 | if (data->have_vout) |
| 179 | return -ENODATA; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 180 | ret = pmbus_read_word_data(client, 0, |
| 181 | ADM1075_VAUX_UV_WARN_LIMIT); |
| 182 | break; |
| 183 | case PMBUS_READ_VOUT: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 184 | if (data->have_vout) |
| 185 | return -ENODATA; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 186 | ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX); |
| 187 | break; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 188 | case PMBUS_VIRT_READ_IOUT_MIN: |
| 189 | if (!data->have_iout_min) |
| 190 | return -ENXIO; |
| 191 | ret = pmbus_read_word_data(client, 0, ADM1293_IOUT_MIN); |
| 192 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 193 | case PMBUS_VIRT_READ_IOUT_MAX: |
| 194 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT); |
| 195 | break; |
| 196 | case PMBUS_VIRT_READ_VOUT_MAX: |
| 197 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT); |
| 198 | break; |
| 199 | case PMBUS_VIRT_READ_VIN_MAX: |
| 200 | ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN); |
| 201 | break; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 202 | case PMBUS_VIRT_READ_PIN_MIN: |
| 203 | if (!data->have_pin_min) |
| 204 | return -ENXIO; |
| 205 | ret = pmbus_read_word_data(client, 0, ADM1293_PIN_MIN); |
| 206 | break; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 207 | case PMBUS_VIRT_READ_PIN_MAX: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 208 | if (!data->have_pin_max) |
| 209 | return -ENXIO; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 210 | ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN); |
| 211 | break; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 212 | case PMBUS_VIRT_READ_TEMP_MAX: |
| 213 | if (!data->have_temp_max) |
| 214 | return -ENXIO; |
| 215 | ret = pmbus_read_word_data(client, 0, ADM1278_PEAK_TEMP); |
| 216 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 217 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 218 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 219 | case PMBUS_VIRT_RESET_VIN_HISTORY: |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 220 | break; |
| 221 | case PMBUS_VIRT_RESET_PIN_HISTORY: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 222 | if (!data->have_pin_max) |
| 223 | return -ENXIO; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 224 | break; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 225 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
| 226 | if (!data->have_temp_max) |
| 227 | return -ENXIO; |
| 228 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 229 | default: |
| 230 | ret = -ENODATA; |
| 231 | break; |
| 232 | } |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static int adm1275_write_word_data(struct i2c_client *client, int page, int reg, |
| 237 | u16 word) |
| 238 | { |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 239 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
| 240 | const struct adm1275_data *data = to_adm1275_data(info); |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 241 | int ret; |
| 242 | |
| 243 | if (page) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 244 | return -ENXIO; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 245 | |
| 246 | switch (reg) { |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 247 | case PMBUS_IOUT_UC_FAULT_LIMIT: |
| 248 | case PMBUS_IOUT_OC_FAULT_LIMIT: |
| 249 | ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT, |
| 250 | word); |
| 251 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 252 | case PMBUS_VIRT_RESET_IOUT_HISTORY: |
| 253 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0); |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 254 | if (!ret && data->have_iout_min) |
| 255 | ret = pmbus_write_word_data(client, 0, |
| 256 | ADM1293_IOUT_MIN, 0); |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 257 | break; |
| 258 | case PMBUS_VIRT_RESET_VOUT_HISTORY: |
| 259 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0); |
| 260 | break; |
| 261 | case PMBUS_VIRT_RESET_VIN_HISTORY: |
| 262 | ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0); |
| 263 | break; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 264 | case PMBUS_VIRT_RESET_PIN_HISTORY: |
| 265 | ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0); |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 266 | if (!ret && data->have_pin_min) |
| 267 | ret = pmbus_write_word_data(client, 0, |
| 268 | ADM1293_PIN_MIN, 0); |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 269 | break; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 270 | case PMBUS_VIRT_RESET_TEMP_HISTORY: |
| 271 | ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0); |
| 272 | break; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 273 | default: |
| 274 | ret = -ENODATA; |
| 275 | break; |
| 276 | } |
| 277 | return ret; |
| 278 | } |
| 279 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 280 | static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg) |
| 281 | { |
| 282 | const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
| 283 | const struct adm1275_data *data = to_adm1275_data(info); |
| 284 | int mfr_status, ret; |
| 285 | |
Guenter Roeck | da8e48a | 2011-07-29 22:19:39 -0700 | [diff] [blame] | 286 | if (page > 0) |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 287 | return -ENXIO; |
| 288 | |
| 289 | switch (reg) { |
| 290 | case PMBUS_STATUS_IOUT: |
| 291 | ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT); |
| 292 | if (ret < 0) |
| 293 | break; |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 294 | if (!data->have_oc_fault && !data->have_uc_fault) |
| 295 | break; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 296 | mfr_status = pmbus_read_byte_data(client, page, |
| 297 | PMBUS_STATUS_MFR_SPECIFIC); |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 298 | if (mfr_status < 0) |
| 299 | return mfr_status; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 300 | if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) { |
| 301 | ret |= data->have_oc_fault ? |
| 302 | PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT; |
| 303 | } |
| 304 | break; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 305 | case PMBUS_STATUS_VOUT: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 306 | if (data->have_vout) |
| 307 | return -ENODATA; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 308 | ret = 0; |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 309 | if (data->have_vaux_status) { |
| 310 | mfr_status = pmbus_read_byte_data(client, 0, |
| 311 | ADM1075_VAUX_STATUS); |
| 312 | if (mfr_status < 0) |
| 313 | return mfr_status; |
| 314 | if (mfr_status & ADM1075_VAUX_OV_WARN) |
| 315 | ret |= PB_VOLTAGE_OV_WARNING; |
| 316 | if (mfr_status & ADM1075_VAUX_UV_WARN) |
| 317 | ret |= PB_VOLTAGE_UV_WARNING; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 318 | } else if (data->have_mfr_vaux_status) { |
| 319 | mfr_status = pmbus_read_byte_data(client, page, |
| 320 | PMBUS_STATUS_MFR_SPECIFIC); |
| 321 | if (mfr_status < 0) |
| 322 | return mfr_status; |
| 323 | if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN) |
| 324 | ret |= PB_VOLTAGE_OV_WARNING; |
| 325 | if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN) |
| 326 | ret |= PB_VOLTAGE_UV_WARNING; |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 327 | } |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 328 | break; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 329 | default: |
| 330 | ret = -ENODATA; |
| 331 | break; |
| 332 | } |
| 333 | return ret; |
| 334 | } |
| 335 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 336 | static const struct i2c_device_id adm1275_id[] = { |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 337 | { "adm1075", adm1075 }, |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 338 | { "adm1275", adm1275 }, |
| 339 | { "adm1276", adm1276 }, |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 340 | { "adm1278", adm1278 }, |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 341 | { "adm1293", adm1293 }, |
| 342 | { "adm1294", adm1294 }, |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 343 | { } |
| 344 | }; |
| 345 | MODULE_DEVICE_TABLE(i2c, adm1275_id); |
| 346 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 347 | static int adm1275_probe(struct i2c_client *client, |
| 348 | const struct i2c_device_id *id) |
| 349 | { |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 350 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 351 | int config, device_config; |
Guenter Roeck | 3b33ca4 | 2011-06-30 02:30:03 -0700 | [diff] [blame] | 352 | int ret; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 353 | struct pmbus_driver_info *info; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 354 | struct adm1275_data *data; |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 355 | const struct i2c_device_id *mid; |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 356 | const struct coefficients *coefficients; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 357 | int vindex = -1, voindex = -1, cindex = -1, pindex = -1; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 358 | int tindex = -1; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 359 | |
| 360 | if (!i2c_check_functionality(client->adapter, |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 361 | I2C_FUNC_SMBUS_READ_BYTE_DATA |
| 362 | | I2C_FUNC_SMBUS_BLOCK_DATA)) |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 363 | return -ENODEV; |
| 364 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 365 | ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer); |
| 366 | if (ret < 0) { |
| 367 | dev_err(&client->dev, "Failed to read Manufacturer ID\n"); |
| 368 | return ret; |
| 369 | } |
| 370 | if (ret != 3 || strncmp(block_buffer, "ADI", 3)) { |
| 371 | dev_err(&client->dev, "Unsupported Manufacturer ID\n"); |
| 372 | return -ENODEV; |
| 373 | } |
| 374 | |
| 375 | ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer); |
| 376 | if (ret < 0) { |
| 377 | dev_err(&client->dev, "Failed to read Manufacturer Model\n"); |
| 378 | return ret; |
| 379 | } |
| 380 | for (mid = adm1275_id; mid->name[0]; mid++) { |
| 381 | if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) |
| 382 | break; |
| 383 | } |
| 384 | if (!mid->name[0]) { |
| 385 | dev_err(&client->dev, "Unsupported device\n"); |
| 386 | return -ENODEV; |
| 387 | } |
| 388 | |
| 389 | if (id->driver_data != mid->driver_data) |
| 390 | dev_notice(&client->dev, |
| 391 | "Device mismatch: Configured %s, detected %s\n", |
| 392 | id->name, mid->name); |
| 393 | |
| 394 | config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG); |
| 395 | if (config < 0) |
| 396 | return config; |
| 397 | |
| 398 | device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG); |
| 399 | if (device_config < 0) |
| 400 | return device_config; |
| 401 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 402 | data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data), |
| 403 | GFP_KERNEL); |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 404 | if (!data) |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 405 | return -ENOMEM; |
| 406 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 407 | data->id = mid->driver_data; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 408 | |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 409 | info = &data->info; |
| 410 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 411 | info->pages = 1; |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 412 | info->format[PSC_VOLTAGE_IN] = direct; |
| 413 | info->format[PSC_VOLTAGE_OUT] = direct; |
| 414 | info->format[PSC_CURRENT_OUT] = direct; |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 415 | info->format[PSC_POWER] = direct; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 416 | info->format[PSC_TEMPERATURE] = direct; |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 417 | info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT; |
| 418 | |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 419 | info->read_word_data = adm1275_read_word_data; |
Guenter Roeck | c5e6763 | 2011-08-02 11:08:57 -0700 | [diff] [blame] | 420 | info->read_byte_data = adm1275_read_byte_data; |
Guenter Roeck | c576e30 | 2011-07-09 11:17:33 -0700 | [diff] [blame] | 421 | info->write_word_data = adm1275_write_word_data; |
| 422 | |
Guenter Roeck | 8710280 | 2011-09-30 11:53:25 -0700 | [diff] [blame] | 423 | switch (data->id) { |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 424 | case adm1075: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 425 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
| 426 | data->have_oc_fault = true; |
| 427 | else |
| 428 | data->have_uc_fault = true; |
| 429 | data->have_pin_max = true; |
| 430 | data->have_vaux_status = true; |
| 431 | |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 432 | coefficients = adm1075_coefficients; |
| 433 | vindex = 0; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 434 | switch (config & ADM1075_IRANGE_MASK) { |
| 435 | case ADM1075_IRANGE_25: |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 436 | cindex = 1; |
| 437 | pindex = 3; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 438 | break; |
| 439 | case ADM1075_IRANGE_50: |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 440 | cindex = 2; |
| 441 | pindex = 4; |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 442 | break; |
| 443 | default: |
| 444 | dev_err(&client->dev, "Invalid input current range"); |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 445 | break; |
| 446 | } |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 447 | |
Guenter Roeck | 9271126 | 2012-02-24 03:40:53 -0800 | [diff] [blame] | 448 | info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN |
| 449 | | PMBUS_HAVE_STATUS_INPUT; |
| 450 | if (config & ADM1275_VIN_VOUT_SELECT) |
| 451 | info->func[0] |= |
| 452 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 453 | break; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 454 | case adm1275: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 455 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
| 456 | data->have_oc_fault = true; |
| 457 | else |
| 458 | data->have_uc_fault = true; |
| 459 | data->have_vout = true; |
| 460 | |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 461 | coefficients = adm1275_coefficients; |
| 462 | vindex = (config & ADM1275_VRANGE) ? 0 : 1; |
| 463 | cindex = 2; |
| 464 | |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 465 | if (config & ADM1275_VIN_VOUT_SELECT) |
| 466 | info->func[0] |= |
| 467 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 468 | else |
| 469 | info->func[0] |= |
| 470 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; |
| 471 | break; |
| 472 | case adm1276: |
Guenter Roeck | 9048539 | 2015-07-04 10:09:54 -0700 | [diff] [blame] | 473 | if (device_config & ADM1275_IOUT_WARN2_SELECT) |
| 474 | data->have_oc_fault = true; |
| 475 | else |
| 476 | data->have_uc_fault = true; |
| 477 | data->have_vout = true; |
| 478 | data->have_pin_max = true; |
| 479 | |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 480 | coefficients = adm1276_coefficients; |
| 481 | vindex = (config & ADM1275_VRANGE) ? 0 : 1; |
| 482 | cindex = 2; |
| 483 | pindex = (config & ADM1275_VRANGE) ? 3 : 4; |
| 484 | |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 485 | info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN |
| 486 | | PMBUS_HAVE_STATUS_INPUT; |
| 487 | if (config & ADM1275_VIN_VOUT_SELECT) |
| 488 | info->func[0] |= |
| 489 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 490 | break; |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 491 | case adm1278: |
| 492 | data->have_vout = true; |
| 493 | data->have_pin_max = true; |
| 494 | data->have_temp_max = true; |
| 495 | |
| 496 | coefficients = adm1278_coefficients; |
| 497 | vindex = 0; |
| 498 | cindex = 1; |
| 499 | pindex = 2; |
| 500 | tindex = 3; |
| 501 | |
| 502 | info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT; |
| 503 | if (config & ADM1278_TEMP1_EN) |
| 504 | info->func[0] |= |
| 505 | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP; |
| 506 | if (config & ADM1278_VIN_EN) |
| 507 | info->func[0] |= PMBUS_HAVE_VIN; |
| 508 | if (config & ADM1278_VOUT_EN) |
| 509 | info->func[0] |= |
| 510 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 511 | break; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 512 | case adm1293: |
| 513 | case adm1294: |
| 514 | data->have_iout_min = true; |
| 515 | data->have_pin_min = true; |
| 516 | data->have_pin_max = true; |
| 517 | data->have_mfr_vaux_status = true; |
| 518 | |
| 519 | coefficients = adm1293_coefficients; |
| 520 | |
| 521 | voindex = 0; |
| 522 | switch (config & ADM1293_VIN_SEL_MASK) { |
| 523 | case ADM1293_VIN_SEL_012: /* 1.2V */ |
| 524 | vindex = 0; |
| 525 | break; |
| 526 | case ADM1293_VIN_SEL_074: /* 7.4V */ |
| 527 | vindex = 1; |
| 528 | break; |
| 529 | case ADM1293_VIN_SEL_210: /* 21V */ |
| 530 | vindex = 2; |
| 531 | break; |
| 532 | default: /* disabled */ |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | switch (config & ADM1293_IRANGE_MASK) { |
| 537 | case ADM1293_IRANGE_25: |
| 538 | cindex = 3; |
| 539 | break; |
| 540 | case ADM1293_IRANGE_50: |
| 541 | cindex = 4; |
| 542 | break; |
| 543 | case ADM1293_IRANGE_100: |
| 544 | cindex = 5; |
| 545 | break; |
| 546 | case ADM1293_IRANGE_200: |
| 547 | cindex = 6; |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | if (vindex >= 0) |
| 552 | pindex = 7 + vindex * 4 + (cindex - 3); |
| 553 | |
| 554 | if (config & ADM1293_VAUX_EN) |
| 555 | info->func[0] |= |
| 556 | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT; |
| 557 | |
| 558 | info->func[0] |= PMBUS_HAVE_PIN | |
| 559 | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT; |
| 560 | |
| 561 | break; |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 562 | default: |
| 563 | dev_err(&client->dev, "Unsupported device\n"); |
| 564 | return -ENODEV; |
| 565 | } |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 566 | |
| 567 | if (voindex < 0) |
| 568 | voindex = vindex; |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 569 | if (vindex >= 0) { |
| 570 | info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m; |
| 571 | info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b; |
| 572 | info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R; |
Guenter Roeck | 68a4038 | 2015-03-17 13:19:51 -0700 | [diff] [blame] | 573 | } |
| 574 | if (voindex >= 0) { |
| 575 | info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m; |
| 576 | info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b; |
| 577 | info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R; |
Guenter Roeck | 904b296 | 2015-07-04 08:39:18 -0700 | [diff] [blame] | 578 | } |
| 579 | if (cindex >= 0) { |
| 580 | info->m[PSC_CURRENT_OUT] = coefficients[cindex].m; |
| 581 | info->b[PSC_CURRENT_OUT] = coefficients[cindex].b; |
| 582 | info->R[PSC_CURRENT_OUT] = coefficients[cindex].R; |
| 583 | } |
| 584 | if (pindex >= 0) { |
| 585 | info->m[PSC_POWER] = coefficients[pindex].m; |
| 586 | info->b[PSC_POWER] = coefficients[pindex].b; |
| 587 | info->R[PSC_POWER] = coefficients[pindex].R; |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 588 | } |
Guenter Roeck | 709066ac | 2015-07-05 11:04:56 -0700 | [diff] [blame] | 589 | if (tindex >= 0) { |
| 590 | info->m[PSC_TEMPERATURE] = coefficients[tindex].m; |
| 591 | info->b[PSC_TEMPERATURE] = coefficients[tindex].b; |
| 592 | info->R[PSC_TEMPERATURE] = coefficients[tindex].R; |
| 593 | } |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 594 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 595 | return pmbus_do_probe(client, id, info); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 598 | static struct i2c_driver adm1275_driver = { |
| 599 | .driver = { |
| 600 | .name = "adm1275", |
| 601 | }, |
| 602 | .probe = adm1275_probe, |
Guenter Roeck | dd285ad | 2012-02-22 08:56:44 -0800 | [diff] [blame] | 603 | .remove = pmbus_do_remove, |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 604 | .id_table = adm1275_id, |
| 605 | }; |
| 606 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 607 | module_i2c_driver(adm1275_driver); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 608 | |
| 609 | MODULE_AUTHOR("Guenter Roeck"); |
Guenter Roeck | 5cf231a | 2011-07-14 11:55:35 -0700 | [diff] [blame] | 610 | MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles"); |
Guenter Roeck | 83f7649 | 2011-03-17 13:16:01 -0700 | [diff] [blame] | 611 | MODULE_LICENSE("GPL"); |