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> |
| 28 | #include "pmbus.h" |
| 29 | |
| 30 | /* |
| 31 | * Find sensor groups and status registers on each page. |
| 32 | */ |
| 33 | static void pmbus_find_sensor_groups(struct i2c_client *client, |
| 34 | struct pmbus_driver_info *info) |
| 35 | { |
| 36 | int page; |
| 37 | |
| 38 | /* Sensors detected on page 0 only */ |
| 39 | if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN)) |
| 40 | info->func[0] |= PMBUS_HAVE_VIN; |
| 41 | if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP)) |
| 42 | info->func[0] |= PMBUS_HAVE_VCAP; |
| 43 | if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN)) |
| 44 | info->func[0] |= PMBUS_HAVE_IIN; |
| 45 | if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN)) |
| 46 | info->func[0] |= PMBUS_HAVE_PIN; |
| 47 | if (info->func[0] |
| 48 | && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT)) |
| 49 | info->func[0] |= PMBUS_HAVE_STATUS_INPUT; |
Guenter Roeck | 81ae681 | 2011-06-30 06:54:05 -0700 | [diff] [blame] | 50 | if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) && |
| 51 | pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 52 | info->func[0] |= PMBUS_HAVE_FAN12; |
| 53 | if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12)) |
| 54 | info->func[0] |= PMBUS_HAVE_STATUS_FAN12; |
| 55 | } |
Guenter Roeck | 81ae681 | 2011-06-30 06:54:05 -0700 | [diff] [blame] | 56 | if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) && |
| 57 | pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) { |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 58 | info->func[0] |= PMBUS_HAVE_FAN34; |
| 59 | if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34)) |
| 60 | info->func[0] |= PMBUS_HAVE_STATUS_FAN34; |
| 61 | } |
Guenter Roeck | 22e6b23 | 2011-07-03 13:08:03 -0700 | [diff] [blame] | 62 | if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1)) |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 63 | info->func[0] |= PMBUS_HAVE_TEMP; |
Guenter Roeck | 0e502ec | 2011-06-30 06:57:41 -0700 | [diff] [blame] | 64 | if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2)) |
| 65 | info->func[0] |= PMBUS_HAVE_TEMP2; |
| 66 | if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3)) |
| 67 | info->func[0] |= PMBUS_HAVE_TEMP3; |
Guenter Roeck | 22e6b23 | 2011-07-03 13:08:03 -0700 | [diff] [blame] | 68 | if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
| 69 | | PMBUS_HAVE_TEMP3) |
| 70 | && pmbus_check_byte_register(client, 0, |
| 71 | PMBUS_STATUS_TEMPERATURE)) |
| 72 | info->func[0] |= PMBUS_HAVE_STATUS_TEMP; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 73 | |
| 74 | /* Sensors detected on all pages */ |
| 75 | for (page = 0; page < info->pages; page++) { |
| 76 | if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) { |
| 77 | info->func[page] |= PMBUS_HAVE_VOUT; |
| 78 | if (pmbus_check_byte_register(client, page, |
| 79 | PMBUS_STATUS_VOUT)) |
| 80 | info->func[page] |= PMBUS_HAVE_STATUS_VOUT; |
| 81 | } |
| 82 | if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) { |
| 83 | info->func[page] |= PMBUS_HAVE_IOUT; |
| 84 | if (pmbus_check_byte_register(client, 0, |
| 85 | PMBUS_STATUS_IOUT)) |
| 86 | info->func[page] |= PMBUS_HAVE_STATUS_IOUT; |
| 87 | } |
| 88 | if (pmbus_check_word_register(client, page, PMBUS_READ_POUT)) |
| 89 | info->func[page] |= PMBUS_HAVE_POUT; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Identify chip parameters. |
| 95 | */ |
| 96 | static int pmbus_identify(struct i2c_client *client, |
| 97 | struct pmbus_driver_info *info) |
| 98 | { |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 99 | int ret = 0; |
| 100 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 101 | if (!info->pages) { |
| 102 | /* |
| 103 | * Check if the PAGE command is supported. If it is, |
| 104 | * keep setting the page number until it fails or until the |
| 105 | * maximum number of pages has been reached. Assume that |
| 106 | * this is the number of pages supported by the chip. |
| 107 | */ |
| 108 | if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) { |
| 109 | int page; |
| 110 | |
| 111 | for (page = 1; page < PMBUS_PAGES; page++) { |
| 112 | if (pmbus_set_page(client, page) < 0) |
| 113 | break; |
| 114 | } |
| 115 | pmbus_set_page(client, 0); |
| 116 | info->pages = page; |
| 117 | } else { |
| 118 | info->pages = 1; |
| 119 | } |
| 120 | } |
| 121 | |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 122 | if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) { |
| 123 | int vout_mode; |
| 124 | |
| 125 | vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE); |
| 126 | if (vout_mode >= 0 && vout_mode != 0xff) { |
| 127 | switch (vout_mode >> 5) { |
| 128 | case 0: |
| 129 | break; |
| 130 | case 1: |
| 131 | info->format[PSC_VOLTAGE_OUT] = vid; |
Guenter Roeck | 068c227 | 2015-07-20 09:47:33 -0700 | [diff] [blame] | 132 | info->vrm_version = vr11; |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 133 | break; |
| 134 | case 2: |
| 135 | info->format[PSC_VOLTAGE_OUT] = direct; |
| 136 | break; |
| 137 | default: |
| 138 | ret = -ENODEV; |
| 139 | goto abort; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 144 | /* |
| 145 | * We should check if the COEFFICIENTS register is supported. |
| 146 | * If it is, and the chip is configured for direct mode, we can read |
| 147 | * the coefficients from the chip, one set per group of sensor |
| 148 | * registers. |
| 149 | * |
| 150 | * To do this, we will need access to a chip which actually supports the |
| 151 | * COEFFICIENTS command, since the command is too complex to implement |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 152 | * without testing it. Until then, abort if a chip configured for direct |
| 153 | * mode was detected. |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 154 | */ |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 155 | if (info->format[PSC_VOLTAGE_OUT] == direct) { |
| 156 | ret = -ENODEV; |
| 157 | goto abort; |
| 158 | } |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 159 | |
| 160 | /* Try to find sensor groups */ |
| 161 | pmbus_find_sensor_groups(client, info); |
Guenter Roeck | 1061d85 | 2011-06-25 11:21:49 -0700 | [diff] [blame] | 162 | abort: |
| 163 | return ret; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static int pmbus_probe(struct i2c_client *client, |
| 167 | const struct i2c_device_id *id) |
| 168 | { |
| 169 | struct pmbus_driver_info *info; |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 170 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 171 | info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info), |
| 172 | GFP_KERNEL); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 173 | if (!info) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | info->pages = id->driver_data; |
| 177 | info->identify = pmbus_identify; |
| 178 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 179 | return pmbus_do_probe(client, id, info); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 182 | /* |
| 183 | * Use driver_data to set the number of pages supported by the chip. |
| 184 | */ |
| 185 | static const struct i2c_device_id pmbus_id[] = { |
Guenter Roeck | e0455e3 | 2011-06-25 15:13:44 -0700 | [diff] [blame] | 186 | {"adp4000", 1}, |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 187 | {"bmr453", 1}, |
| 188 | {"bmr454", 1}, |
Guenter Roeck | 2163340 | 2012-02-28 11:00:54 -0800 | [diff] [blame] | 189 | {"mdt040", 1}, |
Guenter Roeck | e0455e3 | 2011-06-25 15:13:44 -0700 | [diff] [blame] | 190 | {"ncp4200", 1}, |
| 191 | {"ncp4208", 1}, |
Guenter Roeck | bc365a7 | 2011-09-15 10:43:40 -0700 | [diff] [blame] | 192 | {"pdt003", 1}, |
| 193 | {"pdt006", 1}, |
| 194 | {"pdt012", 1}, |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 195 | {"pmbus", 0}, |
Guenter Roeck | c5f35c9 | 2012-02-28 10:24:54 -0800 | [diff] [blame] | 196 | {"tps40400", 1}, |
Guenter Roeck | cfca378 | 2015-08-07 20:55:59 -0700 | [diff] [blame] | 197 | {"tps544b20", 1}, |
| 198 | {"tps544b25", 1}, |
| 199 | {"tps544c20", 1}, |
| 200 | {"tps544c25", 1}, |
Guenter Roeck | bc365a7 | 2011-09-15 10:43:40 -0700 | [diff] [blame] | 201 | {"udt020", 1}, |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 202 | {} |
| 203 | }; |
| 204 | |
| 205 | MODULE_DEVICE_TABLE(i2c, pmbus_id); |
| 206 | |
| 207 | /* This is the driver that will be inserted */ |
| 208 | static struct i2c_driver pmbus_driver = { |
| 209 | .driver = { |
| 210 | .name = "pmbus", |
| 211 | }, |
| 212 | .probe = pmbus_probe, |
Guenter Roeck | dd285ad | 2012-02-22 08:56:44 -0800 | [diff] [blame] | 213 | .remove = pmbus_do_remove, |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 214 | .id_table = pmbus_id, |
| 215 | }; |
| 216 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 217 | module_i2c_driver(pmbus_driver); |
Guenter Roeck | 442aba7 | 2011-01-26 20:09:02 -0800 | [diff] [blame] | 218 | |
| 219 | MODULE_AUTHOR("Guenter Roeck"); |
| 220 | MODULE_DESCRIPTION("Generic PMBus driver"); |
| 221 | MODULE_LICENSE("GPL"); |