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