Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Hardware monitoring driver for PMBus devices |
| 4 | * |
| 5 | * Copyright (c) 2010, 2011 Ericsson AB. |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/i2c.h> |
Wolfram Sang | 4ba1bb1 | 2017-05-21 22:34:43 +0200 | [diff] [blame] | 15 | #include <linux/pmbus.h> |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 16 | #include "pmbus.h" |
| 17 | |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 18 | struct pmbus_device_info { |
| 19 | int pages; |
| 20 | u32 flags; |
| 21 | }; |
| 22 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 23 | /* |
| 24 | * Find sensor groups and status registers on each page. |
| 25 | */ |
| 26 | static void pmbus_find_sensor_groups(struct i2c_client *client, |
| 27 | struct pmbus_driver_info *info) |
| 28 | { |
| 29 | int page; |
| 30 | |
| 31 | /* Sensors detected on page 0 only */ |
| 32 | if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN)) |
| 33 | info->func[0] |= PMBUS_HAVE_VIN; |
| 34 | if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP)) |
| 35 | info->func[0] |= PMBUS_HAVE_VCAP; |
| 36 | if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN)) |
| 37 | info->func[0] |= PMBUS_HAVE_IIN; |
| 38 | if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN)) |
| 39 | info->func[0] |= PMBUS_HAVE_PIN; |
| 40 | if (info->func[0] |
| 41 | && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT)) |
| 42 | info->func[0] |= PMBUS_HAVE_STATUS_INPUT; |
Guenter Roeck | 81ae681 | 2011-06-30 06:54:05 -0700 | [diff] [blame] | 43 | if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) && |
| 44 | pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 45 | info->func[0] |= PMBUS_HAVE_FAN12; |
| 46 | if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12)) |
| 47 | info->func[0] |= PMBUS_HAVE_STATUS_FAN12; |
| 48 | } |
Guenter Roeck | 81ae681 | 2011-06-30 06:54:05 -0700 | [diff] [blame] | 49 | if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) && |
| 50 | pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 51 | info->func[0] |= PMBUS_HAVE_FAN34; |
| 52 | if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34)) |
| 53 | info->func[0] |= PMBUS_HAVE_STATUS_FAN34; |
| 54 | } |
Guenter Roeck | 22e6b23 | 2011-07-03 13:08:03 -0700 | [diff] [blame] | 55 | if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1)) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 56 | info->func[0] |= PMBUS_HAVE_TEMP; |
Guenter Roeck | 0e502ec | 2011-06-30 06:57:41 -0700 | [diff] [blame] | 57 | if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2)) |
| 58 | info->func[0] |= PMBUS_HAVE_TEMP2; |
| 59 | if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3)) |
| 60 | info->func[0] |= PMBUS_HAVE_TEMP3; |
Guenter Roeck | 22e6b23 | 2011-07-03 13:08:03 -0700 | [diff] [blame] | 61 | if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
| 62 | | PMBUS_HAVE_TEMP3) |
| 63 | && pmbus_check_byte_register(client, 0, |
| 64 | PMBUS_STATUS_TEMPERATURE)) |
| 65 | info->func[0] |= PMBUS_HAVE_STATUS_TEMP; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 66 | |
| 67 | /* Sensors detected on all pages */ |
| 68 | for (page = 0; page < info->pages; page++) { |
| 69 | if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) { |
| 70 | info->func[page] |= PMBUS_HAVE_VOUT; |
| 71 | if (pmbus_check_byte_register(client, page, |
| 72 | PMBUS_STATUS_VOUT)) |
| 73 | info->func[page] |= PMBUS_HAVE_STATUS_VOUT; |
| 74 | } |
| 75 | if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) { |
| 76 | info->func[page] |= PMBUS_HAVE_IOUT; |
| 77 | if (pmbus_check_byte_register(client, 0, |
| 78 | PMBUS_STATUS_IOUT)) |
| 79 | info->func[page] |= PMBUS_HAVE_STATUS_IOUT; |
| 80 | } |
| 81 | if (pmbus_check_word_register(client, page, PMBUS_READ_POUT)) |
| 82 | info->func[page] |= PMBUS_HAVE_POUT; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Identify chip parameters. |
| 88 | */ |
| 89 | static int pmbus_identify(struct i2c_client *client, |
| 90 | struct pmbus_driver_info *info) |
| 91 | { |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 92 | int ret = 0; |
| 93 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 94 | if (!info->pages) { |
| 95 | /* |
| 96 | * Check if the PAGE command is supported. If it is, |
| 97 | * keep setting the page number until it fails or until the |
| 98 | * maximum number of pages has been reached. Assume that |
| 99 | * this is the number of pages supported by the chip. |
| 100 | */ |
| 101 | if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) { |
| 102 | int page; |
| 103 | |
| 104 | for (page = 1; page < PMBUS_PAGES; page++) { |
Guenter Roeck | 43f33b6 | 2020-01-14 09:49:27 -0800 | [diff] [blame] | 105 | if (pmbus_set_page(client, page, 0xff) < 0) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 106 | break; |
| 107 | } |
Guenter Roeck | 43f33b6 | 2020-01-14 09:49:27 -0800 | [diff] [blame] | 108 | pmbus_set_page(client, 0, 0xff); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 109 | info->pages = page; |
| 110 | } else { |
| 111 | info->pages = 1; |
| 112 | } |
Dmitry Bazhenov | e7c6a55 | 2018-10-15 14:21:22 +0500 | [diff] [blame] | 113 | |
| 114 | pmbus_clear_faults(client); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 117 | if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) { |
Vadim Pasternak | b9fa0a3 | 2020-01-13 15:08:36 +0000 | [diff] [blame] | 118 | int vout_mode, i; |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 119 | |
| 120 | vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE); |
| 121 | if (vout_mode >= 0 && vout_mode != 0xff) { |
| 122 | switch (vout_mode >> 5) { |
| 123 | case 0: |
| 124 | break; |
| 125 | case 1: |
| 126 | info->format[PSC_VOLTAGE_OUT] = vid; |
Vadim Pasternak | b9fa0a3 | 2020-01-13 15:08:36 +0000 | [diff] [blame] | 127 | for (i = 0; i < info->pages; i++) |
| 128 | info->vrm_version[i] = vr11; |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 129 | break; |
| 130 | case 2: |
| 131 | info->format[PSC_VOLTAGE_OUT] = direct; |
| 132 | break; |
| 133 | default: |
| 134 | ret = -ENODEV; |
| 135 | goto abort; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 140 | /* |
| 141 | * We should check if the COEFFICIENTS register is supported. |
| 142 | * If it is, and the chip is configured for direct mode, we can read |
| 143 | * the coefficients from the chip, one set per group of sensor |
| 144 | * registers. |
| 145 | * |
| 146 | * To do this, we will need access to a chip which actually supports the |
| 147 | * COEFFICIENTS command, since the command is too complex to implement |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 148 | * without testing it. Until then, abort if a chip configured for direct |
| 149 | * mode was detected. |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 150 | */ |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 151 | if (info->format[PSC_VOLTAGE_OUT] == direct) { |
| 152 | ret = -ENODEV; |
| 153 | goto abort; |
| 154 | } |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 155 | |
| 156 | /* Try to find sensor groups */ |
| 157 | pmbus_find_sensor_groups(client, info); |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 158 | abort: |
| 159 | return ret; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int pmbus_probe(struct i2c_client *client, |
| 163 | const struct i2c_device_id *id) |
| 164 | { |
| 165 | struct pmbus_driver_info *info; |
Vadim Pasternak | cc00dec | 2016-07-25 10:55:53 +0000 | [diff] [blame] | 166 | struct pmbus_platform_data *pdata = NULL; |
| 167 | struct device *dev = &client->dev; |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 168 | struct pmbus_device_info *device_info; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 169 | |
Vadim Pasternak | cc00dec | 2016-07-25 10:55:53 +0000 | [diff] [blame] | 170 | info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 171 | if (!info) |
| 172 | return -ENOMEM; |
| 173 | |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 174 | device_info = (struct pmbus_device_info *)id->driver_data; |
| 175 | if (device_info->flags & PMBUS_SKIP_STATUS_CHECK) { |
Vadim Pasternak | cc00dec | 2016-07-25 10:55:53 +0000 | [diff] [blame] | 176 | pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data), |
| 177 | GFP_KERNEL); |
| 178 | if (!pdata) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | pdata->flags = PMBUS_SKIP_STATUS_CHECK; |
| 182 | } |
| 183 | |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 184 | info->pages = device_info->pages; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 185 | info->identify = pmbus_identify; |
Vadim Pasternak | cc00dec | 2016-07-25 10:55:53 +0000 | [diff] [blame] | 186 | dev->platform_data = pdata; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 187 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 188 | return pmbus_do_probe(client, id, info); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 191 | static const struct pmbus_device_info pmbus_info_one = { |
| 192 | .pages = 1, |
| 193 | .flags = 0 |
| 194 | }; |
| 195 | static const struct pmbus_device_info pmbus_info_zero = { |
| 196 | .pages = 0, |
| 197 | .flags = 0 |
| 198 | }; |
| 199 | static const struct pmbus_device_info pmbus_info_one_skip = { |
| 200 | .pages = 1, |
| 201 | .flags = PMBUS_SKIP_STATUS_CHECK |
| 202 | }; |
| 203 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 204 | /* |
| 205 | * Use driver_data to set the number of pages supported by the chip. |
| 206 | */ |
| 207 | static const struct i2c_device_id pmbus_id[] = { |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 208 | {"adp4000", (kernel_ulong_t)&pmbus_info_one}, |
| 209 | {"bmr453", (kernel_ulong_t)&pmbus_info_one}, |
| 210 | {"bmr454", (kernel_ulong_t)&pmbus_info_one}, |
| 211 | {"dps460", (kernel_ulong_t)&pmbus_info_one_skip}, |
Xiaoting Liu | 705f2c8 | 2019-01-10 10:31:44 +0800 | [diff] [blame] | 212 | {"dps650ab", (kernel_ulong_t)&pmbus_info_one_skip}, |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 213 | {"dps800", (kernel_ulong_t)&pmbus_info_one_skip}, |
Guenter Roeck | 1a1ea12 | 2019-12-13 13:36:36 -0800 | [diff] [blame] | 214 | {"max20796", (kernel_ulong_t)&pmbus_info_one}, |
Xiaoting Liu | 6f4a46f | 2019-01-10 10:18:20 +0800 | [diff] [blame] | 215 | {"mdt040", (kernel_ulong_t)&pmbus_info_one}, |
| 216 | {"ncp4200", (kernel_ulong_t)&pmbus_info_one}, |
| 217 | {"ncp4208", (kernel_ulong_t)&pmbus_info_one}, |
| 218 | {"pdt003", (kernel_ulong_t)&pmbus_info_one}, |
| 219 | {"pdt006", (kernel_ulong_t)&pmbus_info_one}, |
| 220 | {"pdt012", (kernel_ulong_t)&pmbus_info_one}, |
| 221 | {"pmbus", (kernel_ulong_t)&pmbus_info_zero}, |
| 222 | {"sgd009", (kernel_ulong_t)&pmbus_info_one_skip}, |
| 223 | {"tps40400", (kernel_ulong_t)&pmbus_info_one}, |
| 224 | {"tps544b20", (kernel_ulong_t)&pmbus_info_one}, |
| 225 | {"tps544b25", (kernel_ulong_t)&pmbus_info_one}, |
| 226 | {"tps544c20", (kernel_ulong_t)&pmbus_info_one}, |
| 227 | {"tps544c25", (kernel_ulong_t)&pmbus_info_one}, |
| 228 | {"udt020", (kernel_ulong_t)&pmbus_info_one}, |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 229 | {} |
| 230 | }; |
| 231 | |
| 232 | MODULE_DEVICE_TABLE(i2c, pmbus_id); |
| 233 | |
| 234 | /* This is the driver that will be inserted */ |
| 235 | static struct i2c_driver pmbus_driver = { |
| 236 | .driver = { |
| 237 | .name = "pmbus", |
| 238 | }, |
| 239 | .probe = pmbus_probe, |
Guenter Roeck | dd285ad | 2012-02-22 08:56:44 -0800 | [diff] [blame] | 240 | .remove = pmbus_do_remove, |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 241 | .id_table = pmbus_id, |
| 242 | }; |
| 243 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 244 | module_i2c_driver(pmbus_driver); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 245 | |
| 246 | MODULE_AUTHOR("Guenter Roeck"); |
| 247 | MODULE_DESCRIPTION("Generic PMBus driver"); |
| 248 | MODULE_LICENSE("GPL"); |