Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Hardware monitoring driver for UCD90xxx Sequencer and System Health |
| 4 | * Controller series |
| 5 | * |
| 6 | * Copyright (C) 2011 Ericsson AB. |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 9 | #include <linux/debugfs.h> |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 12 | #include <linux/of_device.h> |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/i2c.h> |
Wolfram Sang | 4ba1bb1 | 2017-05-21 22:34:43 +0200 | [diff] [blame] | 17 | #include <linux/pmbus.h> |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 18 | #include <linux/gpio/driver.h> |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 19 | #include "pmbus.h" |
| 20 | |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 21 | enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd90320, ucd9090, |
| 22 | ucd90910 }; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 23 | |
| 24 | #define UCD9000_MONITOR_CONFIG 0xd5 |
| 25 | #define UCD9000_NUM_PAGES 0xd6 |
| 26 | #define UCD9000_FAN_CONFIG_INDEX 0xe7 |
| 27 | #define UCD9000_FAN_CONFIG 0xe8 |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 28 | #define UCD9000_MFR_STATUS 0xf3 |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 29 | #define UCD9000_GPIO_SELECT 0xfa |
| 30 | #define UCD9000_GPIO_CONFIG 0xfb |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 31 | #define UCD9000_DEVICE_ID 0xfd |
| 32 | |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 33 | /* GPIO CONFIG bits */ |
| 34 | #define UCD9000_GPIO_CONFIG_ENABLE BIT(0) |
| 35 | #define UCD9000_GPIO_CONFIG_OUT_ENABLE BIT(1) |
| 36 | #define UCD9000_GPIO_CONFIG_OUT_VALUE BIT(2) |
| 37 | #define UCD9000_GPIO_CONFIG_STATUS BIT(3) |
| 38 | #define UCD9000_GPIO_INPUT 0 |
| 39 | #define UCD9000_GPIO_OUTPUT 1 |
| 40 | |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 41 | #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07) |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 42 | #define UCD9000_MON_PAGE(x) ((x) & 0x1f) |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 43 | |
| 44 | #define UCD9000_MON_VOLTAGE 1 |
| 45 | #define UCD9000_MON_TEMPERATURE 2 |
| 46 | #define UCD9000_MON_CURRENT 3 |
| 47 | #define UCD9000_MON_VOLTAGE_HW 4 |
| 48 | |
| 49 | #define UCD9000_NUM_FAN 4 |
| 50 | |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 51 | #define UCD9000_GPIO_NAME_LEN 16 |
| 52 | #define UCD9090_NUM_GPIOS 23 |
| 53 | #define UCD901XX_NUM_GPIOS 26 |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 54 | #define UCD90320_NUM_GPIOS 84 |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 55 | #define UCD90910_NUM_GPIOS 26 |
| 56 | |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 57 | #define UCD9000_DEBUGFS_NAME_LEN 24 |
| 58 | #define UCD9000_GPI_COUNT 8 |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 59 | #define UCD90320_GPI_COUNT 32 |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 60 | |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 61 | struct ucd9000_data { |
| 62 | u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX]; |
| 63 | struct pmbus_driver_info info; |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 64 | #ifdef CONFIG_GPIOLIB |
| 65 | struct gpio_chip gpio; |
| 66 | #endif |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 67 | struct dentry *debugfs; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 68 | }; |
| 69 | #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info) |
| 70 | |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 71 | struct ucd9000_debugfs_entry { |
| 72 | struct i2c_client *client; |
| 73 | u8 index; |
| 74 | }; |
| 75 | |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 76 | static int ucd9000_get_fan_config(struct i2c_client *client, int fan) |
| 77 | { |
| 78 | int fan_config = 0; |
| 79 | struct ucd9000_data *data |
| 80 | = to_ucd9000_data(pmbus_get_driver_info(client)); |
| 81 | |
| 82 | if (data->fan_data[fan][3] & 1) |
| 83 | fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */ |
| 84 | |
| 85 | /* Pulses/revolution */ |
| 86 | fan_config |= (data->fan_data[fan][3] & 0x06) >> 1; |
| 87 | |
| 88 | return fan_config; |
| 89 | } |
| 90 | |
| 91 | static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg) |
| 92 | { |
| 93 | int ret = 0; |
| 94 | int fan_config; |
| 95 | |
| 96 | switch (reg) { |
| 97 | case PMBUS_FAN_CONFIG_12: |
Guenter Roeck | da8e48a | 2011-07-29 22:19:39 -0700 | [diff] [blame] | 98 | if (page > 0) |
Guenter Roeck | 179144a | 2011-09-01 08:34:31 -0700 | [diff] [blame] | 99 | return -ENXIO; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 100 | |
| 101 | ret = ucd9000_get_fan_config(client, 0); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | fan_config = ret << 4; |
| 105 | ret = ucd9000_get_fan_config(client, 1); |
| 106 | if (ret < 0) |
| 107 | return ret; |
| 108 | fan_config |= ret; |
| 109 | ret = fan_config; |
| 110 | break; |
| 111 | case PMBUS_FAN_CONFIG_34: |
Guenter Roeck | da8e48a | 2011-07-29 22:19:39 -0700 | [diff] [blame] | 112 | if (page > 0) |
Guenter Roeck | 179144a | 2011-09-01 08:34:31 -0700 | [diff] [blame] | 113 | return -ENXIO; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 114 | |
| 115 | ret = ucd9000_get_fan_config(client, 2); |
| 116 | if (ret < 0) |
| 117 | return ret; |
| 118 | fan_config = ret << 4; |
| 119 | ret = ucd9000_get_fan_config(client, 3); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | fan_config |= ret; |
| 123 | ret = fan_config; |
| 124 | break; |
| 125 | default: |
| 126 | ret = -ENODATA; |
| 127 | break; |
| 128 | } |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static const struct i2c_device_id ucd9000_id[] = { |
| 133 | {"ucd9000", ucd9000}, |
| 134 | {"ucd90120", ucd90120}, |
| 135 | {"ucd90124", ucd90124}, |
Matt Weber | 50b2b02 | 2016-08-19 21:08:45 -0500 | [diff] [blame] | 136 | {"ucd90160", ucd90160}, |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 137 | {"ucd90320", ucd90320}, |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 138 | {"ucd9090", ucd9090}, |
| 139 | {"ucd90910", ucd90910}, |
| 140 | {} |
| 141 | }; |
| 142 | MODULE_DEVICE_TABLE(i2c, ucd9000_id); |
| 143 | |
Guenter Roeck | 64e5116 | 2019-04-04 06:17:49 -0700 | [diff] [blame] | 144 | static const struct of_device_id __maybe_unused ucd9000_of_match[] = { |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 145 | { |
| 146 | .compatible = "ti,ucd9000", |
| 147 | .data = (void *)ucd9000 |
| 148 | }, |
| 149 | { |
| 150 | .compatible = "ti,ucd90120", |
| 151 | .data = (void *)ucd90120 |
| 152 | }, |
| 153 | { |
| 154 | .compatible = "ti,ucd90124", |
| 155 | .data = (void *)ucd90124 |
| 156 | }, |
| 157 | { |
| 158 | .compatible = "ti,ucd90160", |
| 159 | .data = (void *)ucd90160 |
| 160 | }, |
| 161 | { |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 162 | .compatible = "ti,ucd90320", |
| 163 | .data = (void *)ucd90320 |
| 164 | }, |
| 165 | { |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 166 | .compatible = "ti,ucd9090", |
| 167 | .data = (void *)ucd9090 |
| 168 | }, |
| 169 | { |
| 170 | .compatible = "ti,ucd90910", |
| 171 | .data = (void *)ucd90910 |
| 172 | }, |
| 173 | { }, |
| 174 | }; |
| 175 | MODULE_DEVICE_TABLE(of, ucd9000_of_match); |
| 176 | |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 177 | #ifdef CONFIG_GPIOLIB |
| 178 | static int ucd9000_gpio_read_config(struct i2c_client *client, |
| 179 | unsigned int offset) |
| 180 | { |
| 181 | int ret; |
| 182 | |
| 183 | /* No page set required */ |
| 184 | ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_SELECT, offset); |
| 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | |
| 188 | return i2c_smbus_read_byte_data(client, UCD9000_GPIO_CONFIG); |
| 189 | } |
| 190 | |
| 191 | static int ucd9000_gpio_get(struct gpio_chip *gc, unsigned int offset) |
| 192 | { |
| 193 | struct i2c_client *client = gpiochip_get_data(gc); |
| 194 | int ret; |
| 195 | |
| 196 | ret = ucd9000_gpio_read_config(client, offset); |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | |
| 200 | return !!(ret & UCD9000_GPIO_CONFIG_STATUS); |
| 201 | } |
| 202 | |
| 203 | static void ucd9000_gpio_set(struct gpio_chip *gc, unsigned int offset, |
| 204 | int value) |
| 205 | { |
| 206 | struct i2c_client *client = gpiochip_get_data(gc); |
| 207 | int ret; |
| 208 | |
| 209 | ret = ucd9000_gpio_read_config(client, offset); |
| 210 | if (ret < 0) { |
| 211 | dev_dbg(&client->dev, "failed to read GPIO %d config: %d\n", |
| 212 | offset, ret); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | if (value) { |
| 217 | if (ret & UCD9000_GPIO_CONFIG_STATUS) |
| 218 | return; |
| 219 | |
| 220 | ret |= UCD9000_GPIO_CONFIG_STATUS; |
| 221 | } else { |
| 222 | if (!(ret & UCD9000_GPIO_CONFIG_STATUS)) |
| 223 | return; |
| 224 | |
| 225 | ret &= ~UCD9000_GPIO_CONFIG_STATUS; |
| 226 | } |
| 227 | |
| 228 | ret |= UCD9000_GPIO_CONFIG_ENABLE; |
| 229 | |
| 230 | /* Page set not required */ |
| 231 | ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret); |
| 232 | if (ret < 0) { |
| 233 | dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n", |
| 234 | offset, ret); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | ret &= ~UCD9000_GPIO_CONFIG_ENABLE; |
| 239 | |
| 240 | ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, ret); |
| 241 | if (ret < 0) |
| 242 | dev_dbg(&client->dev, "Failed to write GPIO %d config: %d\n", |
| 243 | offset, ret); |
| 244 | } |
| 245 | |
| 246 | static int ucd9000_gpio_get_direction(struct gpio_chip *gc, |
| 247 | unsigned int offset) |
| 248 | { |
| 249 | struct i2c_client *client = gpiochip_get_data(gc); |
| 250 | int ret; |
| 251 | |
| 252 | ret = ucd9000_gpio_read_config(client, offset); |
| 253 | if (ret < 0) |
| 254 | return ret; |
| 255 | |
| 256 | return !(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE); |
| 257 | } |
| 258 | |
| 259 | static int ucd9000_gpio_set_direction(struct gpio_chip *gc, |
| 260 | unsigned int offset, bool direction_out, |
| 261 | int requested_out) |
| 262 | { |
| 263 | struct i2c_client *client = gpiochip_get_data(gc); |
| 264 | int ret, config, out_val; |
| 265 | |
| 266 | ret = ucd9000_gpio_read_config(client, offset); |
| 267 | if (ret < 0) |
| 268 | return ret; |
| 269 | |
| 270 | if (direction_out) { |
| 271 | out_val = requested_out ? UCD9000_GPIO_CONFIG_OUT_VALUE : 0; |
| 272 | |
| 273 | if (ret & UCD9000_GPIO_CONFIG_OUT_ENABLE) { |
| 274 | if ((ret & UCD9000_GPIO_CONFIG_OUT_VALUE) == out_val) |
| 275 | return 0; |
| 276 | } else { |
| 277 | ret |= UCD9000_GPIO_CONFIG_OUT_ENABLE; |
| 278 | } |
| 279 | |
| 280 | if (out_val) |
| 281 | ret |= UCD9000_GPIO_CONFIG_OUT_VALUE; |
| 282 | else |
| 283 | ret &= ~UCD9000_GPIO_CONFIG_OUT_VALUE; |
| 284 | |
| 285 | } else { |
| 286 | if (!(ret & UCD9000_GPIO_CONFIG_OUT_ENABLE)) |
| 287 | return 0; |
| 288 | |
| 289 | ret &= ~UCD9000_GPIO_CONFIG_OUT_ENABLE; |
| 290 | } |
| 291 | |
| 292 | ret |= UCD9000_GPIO_CONFIG_ENABLE; |
| 293 | config = ret; |
| 294 | |
| 295 | /* Page set not required */ |
| 296 | ret = i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config); |
| 297 | if (ret < 0) |
| 298 | return ret; |
| 299 | |
| 300 | config &= ~UCD9000_GPIO_CONFIG_ENABLE; |
| 301 | |
| 302 | return i2c_smbus_write_byte_data(client, UCD9000_GPIO_CONFIG, config); |
| 303 | } |
| 304 | |
| 305 | static int ucd9000_gpio_direction_input(struct gpio_chip *gc, |
| 306 | unsigned int offset) |
| 307 | { |
| 308 | return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_INPUT, 0); |
| 309 | } |
| 310 | |
| 311 | static int ucd9000_gpio_direction_output(struct gpio_chip *gc, |
| 312 | unsigned int offset, int val) |
| 313 | { |
| 314 | return ucd9000_gpio_set_direction(gc, offset, UCD9000_GPIO_OUTPUT, |
| 315 | val); |
| 316 | } |
| 317 | |
| 318 | static void ucd9000_probe_gpio(struct i2c_client *client, |
| 319 | const struct i2c_device_id *mid, |
| 320 | struct ucd9000_data *data) |
| 321 | { |
| 322 | int rc; |
| 323 | |
| 324 | switch (mid->driver_data) { |
| 325 | case ucd9090: |
| 326 | data->gpio.ngpio = UCD9090_NUM_GPIOS; |
| 327 | break; |
| 328 | case ucd90120: |
| 329 | case ucd90124: |
| 330 | case ucd90160: |
| 331 | data->gpio.ngpio = UCD901XX_NUM_GPIOS; |
| 332 | break; |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 333 | case ucd90320: |
| 334 | data->gpio.ngpio = UCD90320_NUM_GPIOS; |
| 335 | break; |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 336 | case ucd90910: |
| 337 | data->gpio.ngpio = UCD90910_NUM_GPIOS; |
| 338 | break; |
| 339 | default: |
| 340 | return; /* GPIO support is optional. */ |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Pinmux support has not been added to the new gpio_chip. |
| 345 | * This support should be added when possible given the mux |
| 346 | * behavior of these IO devices. |
| 347 | */ |
| 348 | data->gpio.label = client->name; |
| 349 | data->gpio.get_direction = ucd9000_gpio_get_direction; |
| 350 | data->gpio.direction_input = ucd9000_gpio_direction_input; |
| 351 | data->gpio.direction_output = ucd9000_gpio_direction_output; |
| 352 | data->gpio.get = ucd9000_gpio_get; |
| 353 | data->gpio.set = ucd9000_gpio_set; |
| 354 | data->gpio.can_sleep = true; |
| 355 | data->gpio.base = -1; |
| 356 | data->gpio.parent = &client->dev; |
| 357 | |
| 358 | rc = devm_gpiochip_add_data(&client->dev, &data->gpio, client); |
| 359 | if (rc) |
| 360 | dev_warn(&client->dev, "Could not add gpiochip: %d\n", rc); |
| 361 | } |
| 362 | #else |
| 363 | static void ucd9000_probe_gpio(struct i2c_client *client, |
| 364 | const struct i2c_device_id *mid, |
| 365 | struct ucd9000_data *data) |
| 366 | { |
| 367 | } |
| 368 | #endif /* CONFIG_GPIOLIB */ |
| 369 | |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 370 | #ifdef CONFIG_DEBUG_FS |
| 371 | static int ucd9000_get_mfr_status(struct i2c_client *client, u8 *buffer) |
| 372 | { |
Guenter Roeck | 43f33b6 | 2020-01-14 09:49:27 -0800 | [diff] [blame] | 373 | int ret = pmbus_set_page(client, 0, 0xff); |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 374 | |
| 375 | if (ret < 0) |
| 376 | return ret; |
| 377 | |
| 378 | return i2c_smbus_read_block_data(client, UCD9000_MFR_STATUS, buffer); |
| 379 | } |
| 380 | |
| 381 | static int ucd9000_debugfs_show_mfr_status_bit(void *data, u64 *val) |
| 382 | { |
| 383 | struct ucd9000_debugfs_entry *entry = data; |
| 384 | struct i2c_client *client = entry->client; |
| 385 | u8 buffer[I2C_SMBUS_BLOCK_MAX]; |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 386 | int ret, i; |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 387 | |
| 388 | ret = ucd9000_get_mfr_status(client, buffer); |
| 389 | if (ret < 0) |
| 390 | return ret; |
| 391 | |
| 392 | /* |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 393 | * GPI fault bits are in sets of 8, two bytes from end of response. |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 394 | */ |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 395 | i = ret - 3 - entry->index / 8; |
| 396 | if (i >= 0) |
| 397 | *val = !!(buffer[i] & BIT(entry->index % 8)); |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | DEFINE_DEBUGFS_ATTRIBUTE(ucd9000_debugfs_mfr_status_bit, |
| 402 | ucd9000_debugfs_show_mfr_status_bit, NULL, "%1lld\n"); |
| 403 | |
| 404 | static ssize_t ucd9000_debugfs_read_mfr_status(struct file *file, |
| 405 | char __user *buf, size_t count, |
| 406 | loff_t *ppos) |
| 407 | { |
| 408 | struct i2c_client *client = file->private_data; |
| 409 | u8 buffer[I2C_SMBUS_BLOCK_MAX]; |
| 410 | char str[(I2C_SMBUS_BLOCK_MAX * 2) + 2]; |
| 411 | char *res; |
| 412 | int rc; |
| 413 | |
| 414 | rc = ucd9000_get_mfr_status(client, buffer); |
| 415 | if (rc < 0) |
| 416 | return rc; |
| 417 | |
| 418 | res = bin2hex(str, buffer, min(rc, I2C_SMBUS_BLOCK_MAX)); |
| 419 | *res++ = '\n'; |
| 420 | *res = 0; |
| 421 | |
| 422 | return simple_read_from_buffer(buf, count, ppos, str, res - str); |
| 423 | } |
| 424 | |
| 425 | static const struct file_operations ucd9000_debugfs_show_mfr_status_fops = { |
| 426 | .llseek = noop_llseek, |
| 427 | .read = ucd9000_debugfs_read_mfr_status, |
| 428 | .open = simple_open, |
| 429 | }; |
| 430 | |
| 431 | static int ucd9000_init_debugfs(struct i2c_client *client, |
| 432 | const struct i2c_device_id *mid, |
| 433 | struct ucd9000_data *data) |
| 434 | { |
| 435 | struct dentry *debugfs; |
| 436 | struct ucd9000_debugfs_entry *entries; |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 437 | int i, gpi_count; |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 438 | char name[UCD9000_DEBUGFS_NAME_LEN]; |
| 439 | |
| 440 | debugfs = pmbus_get_debugfs_dir(client); |
| 441 | if (!debugfs) |
| 442 | return -ENOENT; |
| 443 | |
| 444 | data->debugfs = debugfs_create_dir(client->name, debugfs); |
| 445 | if (!data->debugfs) |
| 446 | return -ENOENT; |
| 447 | |
| 448 | /* |
| 449 | * Of the chips this driver supports, only the UCD9090, UCD90160, |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 450 | * UCD90320, and UCD90910 report GPI faults in their MFR_STATUS |
| 451 | * register, so only create the GPI fault debugfs attributes for those |
| 452 | * chips. |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 453 | */ |
| 454 | if (mid->driver_data == ucd9090 || mid->driver_data == ucd90160 || |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 455 | mid->driver_data == ucd90320 || mid->driver_data == ucd90910) { |
| 456 | gpi_count = mid->driver_data == ucd90320 ? UCD90320_GPI_COUNT |
| 457 | : UCD9000_GPI_COUNT; |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 458 | entries = devm_kcalloc(&client->dev, |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 459 | gpi_count, sizeof(*entries), |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 460 | GFP_KERNEL); |
| 461 | if (!entries) |
| 462 | return -ENOMEM; |
| 463 | |
Jim Wright | a470f11 | 2019-12-05 17:24:11 -0600 | [diff] [blame] | 464 | for (i = 0; i < gpi_count; i++) { |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 465 | entries[i].client = client; |
| 466 | entries[i].index = i; |
| 467 | scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, |
| 468 | "gpi%d_alarm", i + 1); |
| 469 | debugfs_create_file(name, 0444, data->debugfs, |
| 470 | &entries[i], |
| 471 | &ucd9000_debugfs_mfr_status_bit); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | scnprintf(name, UCD9000_DEBUGFS_NAME_LEN, "mfr_status"); |
| 476 | debugfs_create_file(name, 0444, data->debugfs, client, |
| 477 | &ucd9000_debugfs_show_mfr_status_fops); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | #else |
| 482 | static int ucd9000_init_debugfs(struct i2c_client *client, |
| 483 | const struct i2c_device_id *mid, |
| 484 | struct ucd9000_data *data) |
| 485 | { |
| 486 | return 0; |
| 487 | } |
| 488 | #endif /* CONFIG_DEBUG_FS */ |
| 489 | |
Stephen Kitt | dd43193 | 2020-08-08 23:00:04 +0200 | [diff] [blame] | 490 | static int ucd9000_probe(struct i2c_client *client) |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 491 | { |
| 492 | u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1]; |
| 493 | struct ucd9000_data *data; |
| 494 | struct pmbus_driver_info *info; |
| 495 | const struct i2c_device_id *mid; |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 496 | enum chips chip; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 497 | int i, ret; |
| 498 | |
| 499 | if (!i2c_check_functionality(client->adapter, |
| 500 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 501 | I2C_FUNC_SMBUS_BLOCK_DATA)) |
| 502 | return -ENODEV; |
| 503 | |
| 504 | ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID, |
| 505 | block_buffer); |
| 506 | if (ret < 0) { |
| 507 | dev_err(&client->dev, "Failed to read device ID\n"); |
| 508 | return ret; |
| 509 | } |
| 510 | block_buffer[ret] = '\0'; |
| 511 | dev_info(&client->dev, "Device ID %s\n", block_buffer); |
| 512 | |
Jean Delvare | f020b00 | 2011-08-31 11:53:41 -0400 | [diff] [blame] | 513 | for (mid = ucd9000_id; mid->name[0]; mid++) { |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 514 | if (!strncasecmp(mid->name, block_buffer, strlen(mid->name))) |
| 515 | break; |
| 516 | } |
Jean Delvare | f020b00 | 2011-08-31 11:53:41 -0400 | [diff] [blame] | 517 | if (!mid->name[0]) { |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 518 | dev_err(&client->dev, "Unsupported device\n"); |
| 519 | return -ENODEV; |
| 520 | } |
| 521 | |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 522 | if (client->dev.of_node) |
| 523 | chip = (enum chips)of_device_get_match_data(&client->dev); |
| 524 | else |
Stephen Kitt | dd43193 | 2020-08-08 23:00:04 +0200 | [diff] [blame] | 525 | chip = mid->driver_data; |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 526 | |
Stephen Kitt | dd43193 | 2020-08-08 23:00:04 +0200 | [diff] [blame] | 527 | if (chip != ucd9000 && strcmp(client->name, mid->name) != 0) |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 528 | dev_notice(&client->dev, |
| 529 | "Device mismatch: Configured %s, detected %s\n", |
Stephen Kitt | dd43193 | 2020-08-08 23:00:04 +0200 | [diff] [blame] | 530 | client->name, mid->name); |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 531 | |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 532 | data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data), |
| 533 | GFP_KERNEL); |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 534 | if (!data) |
| 535 | return -ENOMEM; |
| 536 | info = &data->info; |
| 537 | |
| 538 | ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES); |
| 539 | if (ret < 0) { |
| 540 | dev_err(&client->dev, |
| 541 | "Failed to read number of active pages\n"); |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 542 | return ret; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 543 | } |
| 544 | info->pages = ret; |
| 545 | if (!info->pages) { |
| 546 | dev_err(&client->dev, "No pages configured\n"); |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 547 | return -ENODEV; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* The internal temperature sensor is always active */ |
| 551 | info->func[0] = PMBUS_HAVE_TEMP; |
| 552 | |
| 553 | /* Everything else is configurable */ |
| 554 | ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG, |
| 555 | block_buffer); |
| 556 | if (ret <= 0) { |
| 557 | dev_err(&client->dev, "Failed to read configuration data\n"); |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 558 | return -ENODEV; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 559 | } |
| 560 | for (i = 0; i < ret; i++) { |
| 561 | int page = UCD9000_MON_PAGE(block_buffer[i]); |
| 562 | |
| 563 | if (page >= info->pages) |
| 564 | continue; |
| 565 | |
| 566 | switch (UCD9000_MON_TYPE(block_buffer[i])) { |
| 567 | case UCD9000_MON_VOLTAGE: |
| 568 | case UCD9000_MON_VOLTAGE_HW: |
| 569 | info->func[page] |= PMBUS_HAVE_VOUT |
| 570 | | PMBUS_HAVE_STATUS_VOUT; |
| 571 | break; |
| 572 | case UCD9000_MON_TEMPERATURE: |
| 573 | info->func[page] |= PMBUS_HAVE_TEMP2 |
| 574 | | PMBUS_HAVE_STATUS_TEMP; |
| 575 | break; |
| 576 | case UCD9000_MON_CURRENT: |
| 577 | info->func[page] |= PMBUS_HAVE_IOUT |
| 578 | | PMBUS_HAVE_STATUS_IOUT; |
| 579 | break; |
| 580 | default: |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* Fan configuration */ |
| 586 | if (mid->driver_data == ucd90124) { |
| 587 | for (i = 0; i < UCD9000_NUM_FAN; i++) { |
| 588 | i2c_smbus_write_byte_data(client, |
| 589 | UCD9000_FAN_CONFIG_INDEX, i); |
| 590 | ret = i2c_smbus_read_block_data(client, |
| 591 | UCD9000_FAN_CONFIG, |
| 592 | data->fan_data[i]); |
| 593 | if (ret < 0) |
Guenter Roeck | 8b313ca | 2012-02-22 08:56:43 -0800 | [diff] [blame] | 594 | return ret; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 595 | } |
| 596 | i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0); |
| 597 | |
| 598 | info->read_byte_data = ucd9000_read_byte_data; |
| 599 | info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 |
| 600 | | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34; |
| 601 | } |
| 602 | |
Christopher Bostic | ca781fb | 2018-03-16 16:22:05 -0500 | [diff] [blame] | 603 | ucd9000_probe_gpio(client, mid, data); |
| 604 | |
Stephen Kitt | dd43193 | 2020-08-08 23:00:04 +0200 | [diff] [blame] | 605 | ret = pmbus_do_probe(client, info); |
Christopher Bostic | 72816cb | 2018-03-16 16:22:06 -0500 | [diff] [blame] | 606 | if (ret) |
| 607 | return ret; |
| 608 | |
| 609 | ret = ucd9000_init_debugfs(client, mid, data); |
| 610 | if (ret) |
| 611 | dev_warn(&client->dev, "Failed to register debugfs: %d\n", |
| 612 | ret); |
| 613 | |
| 614 | return 0; |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 617 | /* This is the driver that will be inserted */ |
| 618 | static struct i2c_driver ucd9000_driver = { |
| 619 | .driver = { |
| 620 | .name = "ucd9000", |
Javier Martinez Canillas | 8881a19 | 2017-02-24 10:13:07 -0300 | [diff] [blame] | 621 | .of_match_table = of_match_ptr(ucd9000_of_match), |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 622 | }, |
Stephen Kitt | dd43193 | 2020-08-08 23:00:04 +0200 | [diff] [blame] | 623 | .probe_new = ucd9000_probe, |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 624 | .id_table = ucd9000_id, |
| 625 | }; |
| 626 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 627 | module_i2c_driver(ucd9000_driver); |
Guenter Roeck | 0c0a061 | 2011-03-08 22:01:39 -0800 | [diff] [blame] | 628 | |
| 629 | MODULE_AUTHOR("Guenter Roeck"); |
| 630 | MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx"); |
| 631 | MODULE_LICENSE("GPL"); |