1 /* 2 * Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved. 3 * Copyright (c) 2016-2018 Vadim Pasternak <vadimp@mellanox.com> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the names of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * Alternatively, this software may be distributed under the terms of the 18 * GNU General Public License ("GPL") version 2 as published by the Free 19 * Software Foundation. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <linux/bitops.h> 35 #include <linux/device.h> 36 #include <linux/hwmon.h> 37 #include <linux/hwmon-sysfs.h> 38 #include <linux/i2c.h> 39 #include <linux/interrupt.h> 40 #include <linux/module.h> 41 #include <linux/of_device.h> 42 #include <linux/platform_data/mlxreg.h> 43 #include <linux/platform_device.h> 44 #include <linux/spinlock.h> 45 #include <linux/regmap.h> 46 #include <linux/workqueue.h> 47 48 /* Offset of event and mask registers from status register. */ 49 #define MLXREG_HOTPLUG_EVENT_OFF 1 50 #define MLXREG_HOTPLUG_MASK_OFF 2 51 #define MLXREG_HOTPLUG_AGGR_MASK_OFF 1 52 53 /* ASIC health parameters. */ 54 #define MLXREG_HOTPLUG_HEALTH_MASK 0x02 55 #define MLXREG_HOTPLUG_RST_CNTR 3 56 57 #define MLXREG_HOTPLUG_ATTRS_MAX 24 58 59 /** 60 * struct mlxreg_hotplug_priv_data - platform private data: 61 * @irq: platform device interrupt number; 62 * @pdev: platform device; 63 * @plat: platform data; 64 * @dwork: delayed work template; 65 * @lock: spin lock; 66 * @hwmon: hwmon device; 67 * @mlxreg_hotplug_attr: sysfs attributes array; 68 * @mlxreg_hotplug_dev_attr: sysfs sensor device attribute array; 69 * @group: sysfs attribute group; 70 * @groups: list of sysfs attribute group for hwmon registration; 71 * @cell: location of top aggregation interrupt register; 72 * @mask: top aggregation interrupt common mask; 73 * @aggr_cache: last value of aggregation register status; 74 */ 75 struct mlxreg_hotplug_priv_data { 76 int irq; 77 struct device *dev; 78 struct platform_device *pdev; 79 struct mlxreg_hotplug_platform_data *plat; 80 struct regmap *regmap; 81 struct delayed_work dwork_irq; 82 struct delayed_work dwork; 83 spinlock_t lock; /* sync with interrupt */ 84 struct device *hwmon; 85 struct attribute *mlxreg_hotplug_attr[MLXREG_HOTPLUG_ATTRS_MAX + 1]; 86 struct sensor_device_attribute_2 87 mlxreg_hotplug_dev_attr[MLXREG_HOTPLUG_ATTRS_MAX]; 88 struct attribute_group group; 89 const struct attribute_group *groups[2]; 90 u32 cell; 91 u32 mask; 92 u32 aggr_cache; 93 bool after_probe; 94 }; 95 96 static int mlxreg_hotplug_device_create(struct mlxreg_hotplug_priv_data *priv, 97 struct mlxreg_core_data *data) 98 { 99 struct mlxreg_core_hotplug_platform_data *pdata; 100 101 /* 102 * Return if adapter number is negative. It could be in case hotplug 103 * event is not associated with hotplug device. 104 */ 105 if (data->hpdev.nr < 0) 106 return 0; 107 108 pdata = dev_get_platdata(&priv->pdev->dev); 109 data->hpdev.adapter = i2c_get_adapter(data->hpdev.nr + 110 pdata->shift_nr); 111 if (!data->hpdev.adapter) { 112 dev_err(priv->dev, "Failed to get adapter for bus %d\n", 113 data->hpdev.nr + pdata->shift_nr); 114 return -EFAULT; 115 } 116 117 data->hpdev.client = i2c_new_device(data->hpdev.adapter, 118 data->hpdev.brdinfo); 119 if (!data->hpdev.client) { 120 dev_err(priv->dev, "Failed to create client %s at bus %d at addr 0x%02x\n", 121 data->hpdev.brdinfo->type, data->hpdev.nr + 122 pdata->shift_nr, data->hpdev.brdinfo->addr); 123 124 i2c_put_adapter(data->hpdev.adapter); 125 data->hpdev.adapter = NULL; 126 return -EFAULT; 127 } 128 129 return 0; 130 } 131 132 static void mlxreg_hotplug_device_destroy(struct mlxreg_core_data *data) 133 { 134 if (data->hpdev.client) { 135 i2c_unregister_device(data->hpdev.client); 136 data->hpdev.client = NULL; 137 } 138 139 if (data->hpdev.adapter) { 140 i2c_put_adapter(data->hpdev.adapter); 141 data->hpdev.adapter = NULL; 142 } 143 } 144 145 static ssize_t mlxreg_hotplug_attr_show(struct device *dev, 146 struct device_attribute *attr, 147 char *buf) 148 { 149 struct mlxreg_hotplug_priv_data *priv = dev_get_drvdata(dev); 150 struct mlxreg_core_hotplug_platform_data *pdata; 151 int index = to_sensor_dev_attr_2(attr)->index; 152 int nr = to_sensor_dev_attr_2(attr)->nr; 153 struct mlxreg_core_item *item; 154 struct mlxreg_core_data *data; 155 u32 regval; 156 int ret; 157 158 pdata = dev_get_platdata(&priv->pdev->dev); 159 item = pdata->items + nr; 160 data = item->data + index; 161 162 ret = regmap_read(priv->regmap, data->reg, ®val); 163 if (ret) 164 return ret; 165 166 if (item->health) { 167 regval &= data->mask; 168 } else { 169 /* Bit = 0 : functional if item->inversed is true. */ 170 if (item->inversed) 171 regval = !(regval & data->mask); 172 else 173 regval = !!(regval & data->mask); 174 } 175 176 return sprintf(buf, "%u\n", regval); 177 } 178 179 #define PRIV_ATTR(i) priv->mlxreg_hotplug_attr[i] 180 #define PRIV_DEV_ATTR(i) priv->mlxreg_hotplug_dev_attr[i] 181 182 static int mlxreg_hotplug_attr_init(struct mlxreg_hotplug_priv_data *priv) 183 { 184 struct mlxreg_core_hotplug_platform_data *pdata; 185 struct mlxreg_core_item *item; 186 struct mlxreg_core_data *data; 187 int num_attrs = 0, id = 0, i, j; 188 189 pdata = dev_get_platdata(&priv->pdev->dev); 190 item = pdata->items; 191 192 /* Go over all kinds of items - psu, pwr, fan. */ 193 for (i = 0; i < pdata->counter; i++, item++) { 194 num_attrs += item->count; 195 data = item->data; 196 /* Go over all units within the item. */ 197 for (j = 0; j < item->count; j++, data++, id++) { 198 PRIV_ATTR(id) = &PRIV_DEV_ATTR(id).dev_attr.attr; 199 PRIV_ATTR(id)->name = devm_kasprintf(&priv->pdev->dev, 200 GFP_KERNEL, 201 data->label); 202 203 if (!PRIV_ATTR(id)->name) { 204 dev_err(priv->dev, "Memory allocation failed for attr %d.\n", 205 id); 206 return -ENOMEM; 207 } 208 209 PRIV_DEV_ATTR(id).dev_attr.attr.name = 210 PRIV_ATTR(id)->name; 211 PRIV_DEV_ATTR(id).dev_attr.attr.mode = 0444; 212 PRIV_DEV_ATTR(id).dev_attr.show = 213 mlxreg_hotplug_attr_show; 214 PRIV_DEV_ATTR(id).nr = i; 215 PRIV_DEV_ATTR(id).index = j; 216 sysfs_attr_init(&PRIV_DEV_ATTR(id).dev_attr.attr); 217 } 218 } 219 220 priv->group.attrs = devm_kzalloc(&priv->pdev->dev, num_attrs * 221 sizeof(struct attribute *), 222 GFP_KERNEL); 223 if (!priv->group.attrs) 224 return -ENOMEM; 225 226 priv->group.attrs = priv->mlxreg_hotplug_attr; 227 priv->groups[0] = &priv->group; 228 priv->groups[1] = NULL; 229 230 return 0; 231 } 232 233 static void 234 mlxreg_hotplug_work_helper(struct mlxreg_hotplug_priv_data *priv, 235 struct mlxreg_core_item *item) 236 { 237 struct mlxreg_core_data *data; 238 u32 asserted, regval, bit; 239 int ret; 240 241 /* 242 * Validate if item related to received signal type is valid. 243 * It should never happen, excepted the situation when some 244 * piece of hardware is broken. In such situation just produce 245 * error message and return. Caller must continue to handle the 246 * signals from other devices if any. 247 */ 248 if (unlikely(!item)) { 249 dev_err(priv->dev, "False signal: at offset:mask 0x%02x:0x%02x.\n", 250 item->reg, item->mask); 251 252 return; 253 } 254 255 /* Mask event. */ 256 ret = regmap_write(priv->regmap, item->reg + MLXREG_HOTPLUG_MASK_OFF, 257 0); 258 if (ret) 259 goto out; 260 261 /* Read status. */ 262 ret = regmap_read(priv->regmap, item->reg, ®val); 263 if (ret) 264 goto out; 265 266 /* Set asserted bits and save last status. */ 267 regval &= item->mask; 268 asserted = item->cache ^ regval; 269 item->cache = regval; 270 271 for_each_set_bit(bit, (unsigned long *)&asserted, 8) { 272 data = item->data + bit; 273 if (regval & BIT(bit)) { 274 if (item->inversed) 275 mlxreg_hotplug_device_destroy(data); 276 else 277 mlxreg_hotplug_device_create(priv, data); 278 } else { 279 if (item->inversed) 280 mlxreg_hotplug_device_create(priv, data); 281 else 282 mlxreg_hotplug_device_destroy(data); 283 } 284 } 285 286 /* Acknowledge event. */ 287 ret = regmap_write(priv->regmap, item->reg + MLXREG_HOTPLUG_EVENT_OFF, 288 0); 289 if (ret) 290 goto out; 291 292 /* Unmask event. */ 293 ret = regmap_write(priv->regmap, item->reg + MLXREG_HOTPLUG_MASK_OFF, 294 item->mask); 295 296 out: 297 if (ret) 298 dev_err(priv->dev, "Failed to complete workqueue.\n"); 299 } 300 301 static void 302 mlxreg_hotplug_health_work_helper(struct mlxreg_hotplug_priv_data *priv, 303 struct mlxreg_core_item *item) 304 { 305 struct mlxreg_core_data *data = item->data; 306 u32 regval; 307 int i, ret = 0; 308 309 for (i = 0; i < item->count; i++, data++) { 310 /* Mask event. */ 311 ret = regmap_write(priv->regmap, data->reg + 312 MLXREG_HOTPLUG_MASK_OFF, 0); 313 if (ret) 314 goto out; 315 316 /* Read status. */ 317 ret = regmap_read(priv->regmap, data->reg, ®val); 318 if (ret) 319 goto out; 320 321 regval &= data->mask; 322 item->cache = regval; 323 if (regval == MLXREG_HOTPLUG_HEALTH_MASK) { 324 if ((data->health_cntr++ == MLXREG_HOTPLUG_RST_CNTR) || 325 !priv->after_probe) { 326 mlxreg_hotplug_device_create(priv, data); 327 data->attached = true; 328 } 329 } else { 330 if (data->attached) { 331 mlxreg_hotplug_device_destroy(data); 332 data->attached = false; 333 data->health_cntr = 0; 334 } 335 } 336 337 /* Acknowledge event. */ 338 ret = regmap_write(priv->regmap, data->reg + 339 MLXREG_HOTPLUG_EVENT_OFF, 0); 340 if (ret) 341 goto out; 342 343 /* Unmask event. */ 344 ret = regmap_write(priv->regmap, data->reg + 345 MLXREG_HOTPLUG_MASK_OFF, data->mask); 346 if (ret) 347 goto out; 348 } 349 350 out: 351 if (ret) 352 dev_err(priv->dev, "Failed to complete workqueue.\n"); 353 } 354 355 /* 356 * mlxreg_hotplug_work_handler - performs traversing of device interrupt 357 * registers according to the below hierarchy schema: 358 * 359 * Aggregation registers (status/mask) 360 * PSU registers: *---* 361 * *-----------------* | | 362 * |status/event/mask|-----> | * | 363 * *-----------------* | | 364 * Power registers: | | 365 * *-----------------* | | 366 * |status/event/mask|-----> | * | 367 * *-----------------* | | 368 * FAN registers: | |--> CPU 369 * *-----------------* | | 370 * |status/event/mask|-----> | * | 371 * *-----------------* | | 372 * ASIC registers: | | 373 * *-----------------* | | 374 * |status/event/mask|-----> | * | 375 * *-----------------* | | 376 * *---* 377 * 378 * In case some system changed are detected: FAN in/out, PSU in/out, power 379 * cable attached/detached, ASIC health good/bad, relevant device is created 380 * or destroyed. 381 */ 382 static void mlxreg_hotplug_work_handler(struct work_struct *work) 383 { 384 struct mlxreg_core_hotplug_platform_data *pdata; 385 struct mlxreg_hotplug_priv_data *priv; 386 struct mlxreg_core_item *item; 387 u32 regval, aggr_asserted; 388 unsigned long flags; 389 int i, ret; 390 391 priv = container_of(work, struct mlxreg_hotplug_priv_data, 392 dwork_irq.work); 393 pdata = dev_get_platdata(&priv->pdev->dev); 394 item = pdata->items; 395 396 /* Mask aggregation event. */ 397 ret = regmap_write(priv->regmap, pdata->cell + 398 MLXREG_HOTPLUG_AGGR_MASK_OFF, 0); 399 if (ret < 0) 400 goto out; 401 402 /* Read aggregation status. */ 403 ret = regmap_read(priv->regmap, pdata->cell, ®val); 404 if (ret) 405 goto out; 406 407 regval &= pdata->mask; 408 aggr_asserted = priv->aggr_cache ^ regval; 409 priv->aggr_cache = regval; 410 411 /* Handle topology and health configuration changes. */ 412 for (i = 0; i < pdata->counter; i++, item++) { 413 if (aggr_asserted & item->aggr_mask) { 414 if (item->health) 415 mlxreg_hotplug_health_work_helper(priv, item); 416 else 417 mlxreg_hotplug_work_helper(priv, item); 418 } 419 } 420 421 if (aggr_asserted) { 422 spin_lock_irqsave(&priv->lock, flags); 423 424 /* 425 * It is possible, that some signals have been inserted, while 426 * interrupt has been masked by mlxreg_hotplug_work_handler. 427 * In this case such signals will be missed. In order to handle 428 * these signals delayed work is canceled and work task 429 * re-scheduled for immediate execution. It allows to handle 430 * missed signals, if any. In other case work handler just 431 * validates that no new signals have been received during 432 * masking. 433 */ 434 cancel_delayed_work(&priv->dwork_irq); 435 schedule_delayed_work(&priv->dwork_irq, 0); 436 437 spin_unlock_irqrestore(&priv->lock, flags); 438 439 return; 440 } 441 442 /* Unmask aggregation event (no need acknowledge). */ 443 ret = regmap_write(priv->regmap, pdata->cell + 444 MLXREG_HOTPLUG_AGGR_MASK_OFF, pdata->mask); 445 446 out: 447 if (ret) 448 dev_err(priv->dev, "Failed to complete workqueue.\n"); 449 } 450 451 static int mlxreg_hotplug_set_irq(struct mlxreg_hotplug_priv_data *priv) 452 { 453 struct mlxreg_core_hotplug_platform_data *pdata; 454 struct mlxreg_core_item *item; 455 int i, ret; 456 457 pdata = dev_get_platdata(&priv->pdev->dev); 458 item = pdata->items; 459 460 for (i = 0; i < pdata->counter; i++, item++) { 461 /* Clear group presense event. */ 462 ret = regmap_write(priv->regmap, item->reg + 463 MLXREG_HOTPLUG_EVENT_OFF, 0); 464 if (ret) 465 goto out; 466 467 /* Set group initial status as mask and unmask group event. */ 468 if (item->inversed) { 469 item->cache = item->mask; 470 ret = regmap_write(priv->regmap, item->reg + 471 MLXREG_HOTPLUG_MASK_OFF, 472 item->mask); 473 if (ret) 474 goto out; 475 } 476 } 477 478 /* Keep aggregation initial status as zero and unmask events. */ 479 ret = regmap_write(priv->regmap, pdata->cell + 480 MLXREG_HOTPLUG_AGGR_MASK_OFF, pdata->mask); 481 if (ret) 482 goto out; 483 484 /* Keep low aggregation initial status as zero and unmask events. */ 485 if (pdata->cell_low) { 486 ret = regmap_write(priv->regmap, pdata->cell_low + 487 MLXREG_HOTPLUG_AGGR_MASK_OFF, 488 pdata->mask_low); 489 if (ret) 490 goto out; 491 } 492 493 /* Invoke work handler for initializing hot plug devices setting. */ 494 mlxreg_hotplug_work_handler(&priv->dwork_irq.work); 495 496 out: 497 if (ret) 498 dev_err(priv->dev, "Failed to set interrupts.\n"); 499 enable_irq(priv->irq); 500 return ret; 501 } 502 503 static void mlxreg_hotplug_unset_irq(struct mlxreg_hotplug_priv_data *priv) 504 { 505 struct mlxreg_core_hotplug_platform_data *pdata; 506 struct mlxreg_core_item *item; 507 struct mlxreg_core_data *data; 508 int count, i, j; 509 510 pdata = dev_get_platdata(&priv->pdev->dev); 511 item = pdata->items; 512 disable_irq(priv->irq); 513 cancel_delayed_work_sync(&priv->dwork_irq); 514 515 /* Mask low aggregation event, if defined. */ 516 if (pdata->cell_low) 517 regmap_write(priv->regmap, pdata->cell_low + 518 MLXREG_HOTPLUG_AGGR_MASK_OFF, 0); 519 520 /* Mask aggregation event. */ 521 regmap_write(priv->regmap, pdata->cell + MLXREG_HOTPLUG_AGGR_MASK_OFF, 522 0); 523 524 /* Clear topology configurations. */ 525 for (i = 0; i < pdata->counter; i++, item++) { 526 data = item->data; 527 /* Mask group presense event. */ 528 regmap_write(priv->regmap, data->reg + MLXREG_HOTPLUG_MASK_OFF, 529 0); 530 /* Clear group presense event. */ 531 regmap_write(priv->regmap, data->reg + 532 MLXREG_HOTPLUG_EVENT_OFF, 0); 533 534 /* Remove all the attached devices in group. */ 535 count = item->count; 536 for (j = 0; j < count; j++, data++) 537 mlxreg_hotplug_device_destroy(data); 538 } 539 } 540 541 static irqreturn_t mlxreg_hotplug_irq_handler(int irq, void *dev) 542 { 543 struct mlxreg_hotplug_priv_data *priv; 544 545 priv = (struct mlxreg_hotplug_priv_data *)dev; 546 547 /* Schedule work task for immediate execution.*/ 548 schedule_delayed_work(&priv->dwork_irq, 0); 549 550 return IRQ_HANDLED; 551 } 552 553 static int mlxreg_hotplug_probe(struct platform_device *pdev) 554 { 555 struct mlxreg_core_hotplug_platform_data *pdata; 556 struct mlxreg_hotplug_priv_data *priv; 557 struct i2c_adapter *deferred_adap; 558 int err; 559 560 pdata = dev_get_platdata(&pdev->dev); 561 if (!pdata) { 562 dev_err(&pdev->dev, "Failed to get platform data.\n"); 563 return -EINVAL; 564 } 565 566 /* Defer probing if the necessary adapter is not configured yet. */ 567 deferred_adap = i2c_get_adapter(pdata->deferred_nr); 568 if (!deferred_adap) 569 return -EPROBE_DEFER; 570 i2c_put_adapter(deferred_adap); 571 572 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 573 if (!priv) 574 return -ENOMEM; 575 576 if (pdata->irq) { 577 priv->irq = pdata->irq; 578 } else { 579 priv->irq = platform_get_irq(pdev, 0); 580 if (priv->irq < 0) { 581 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", 582 priv->irq); 583 return priv->irq; 584 } 585 } 586 587 priv->regmap = pdata->regmap; 588 priv->dev = pdev->dev.parent; 589 priv->pdev = pdev; 590 591 err = devm_request_irq(&pdev->dev, priv->irq, 592 mlxreg_hotplug_irq_handler, IRQF_TRIGGER_FALLING 593 | IRQF_SHARED, "mlxreg-hotplug", priv); 594 if (err) { 595 dev_err(&pdev->dev, "Failed to request irq: %d\n", err); 596 return err; 597 } 598 599 disable_irq(priv->irq); 600 spin_lock_init(&priv->lock); 601 INIT_DELAYED_WORK(&priv->dwork_irq, mlxreg_hotplug_work_handler); 602 /* Perform initial interrupts setup. */ 603 mlxreg_hotplug_set_irq(priv); 604 605 priv->after_probe = true; 606 dev_set_drvdata(&pdev->dev, priv); 607 608 err = mlxreg_hotplug_attr_init(priv); 609 if (err) { 610 dev_err(&pdev->dev, "Failed to allocate attributes: %d\n", 611 err); 612 return err; 613 } 614 615 priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, 616 "mlxreg_hotplug", priv, priv->groups); 617 if (IS_ERR(priv->hwmon)) { 618 dev_err(&pdev->dev, "Failed to register hwmon device %ld\n", 619 PTR_ERR(priv->hwmon)); 620 return PTR_ERR(priv->hwmon); 621 } 622 623 return 0; 624 } 625 626 static int mlxreg_hotplug_remove(struct platform_device *pdev) 627 { 628 struct mlxreg_hotplug_priv_data *priv = dev_get_drvdata(&pdev->dev); 629 630 /* Clean interrupts setup. */ 631 mlxreg_hotplug_unset_irq(priv); 632 633 return 0; 634 } 635 636 static struct platform_driver mlxreg_hotplug_driver = { 637 .driver = { 638 .name = "mlxreg-hotplug", 639 }, 640 .probe = mlxreg_hotplug_probe, 641 .remove = mlxreg_hotplug_remove, 642 }; 643 644 module_platform_driver(mlxreg_hotplug_driver); 645 646 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>"); 647 MODULE_DESCRIPTION("Mellanox regmap hotplug platform driver"); 648 MODULE_LICENSE("Dual BSD/GPL"); 649 MODULE_ALIAS("platform:mlxreg-hotplug"); 650