1 // SPDX-License-Identifier: GPL-2.0+
3 * Mellanox hotplug driver
5 * Copyright (C) 2016-2020 Mellanox Technologies
8 #include <linux/bitops.h>
9 #include <linux/device.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_data/mlxreg.h>
17 #include <linux/platform_device.h>
18 #include <linux/spinlock.h>
19 #include <linux/string_helpers.h>
20 #include <linux/regmap.h>
21 #include <linux/workqueue.h>
23 /* Offset of event and mask registers from status register. */
24 #define MLXREG_HOTPLUG_EVENT_OFF 1
25 #define MLXREG_HOTPLUG_MASK_OFF 2
26 #define MLXREG_HOTPLUG_AGGR_MASK_OFF 1
28 /* ASIC good health mask. */
29 #define MLXREG_HOTPLUG_GOOD_HEALTH_MASK 0x02
31 #define MLXREG_HOTPLUG_ATTRS_MAX 24
32 #define MLXREG_HOTPLUG_NOT_ASSERT 3
35 * struct mlxreg_hotplug_priv_data - platform private data:
36 * @irq: platform device interrupt number;
38 * @pdev: platform device;
39 * @plat: platform data;
40 * @regmap: register map handle;
41 * @dwork_irq: delayed work template;
43 * @hwmon: hwmon device;
44 * @mlxreg_hotplug_attr: sysfs attributes array;
45 * @mlxreg_hotplug_dev_attr: sysfs sensor device attribute array;
46 * @group: sysfs attribute group;
47 * @groups: list of sysfs attribute group for hwmon registration;
48 * @cell: location of top aggregation interrupt register;
49 * @mask: top aggregation interrupt common mask;
50 * @aggr_cache: last value of aggregation register status;
51 * @after_probe: flag indication probing completion;
52 * @not_asserted: number of entries in workqueue with no signal assertion;
54 struct mlxreg_hotplug_priv_data
{
57 struct platform_device
*pdev
;
58 struct mlxreg_hotplug_platform_data
*plat
;
59 struct regmap
*regmap
;
60 struct delayed_work dwork_irq
;
61 spinlock_t lock
; /* sync with interrupt */
63 struct attribute
*mlxreg_hotplug_attr
[MLXREG_HOTPLUG_ATTRS_MAX
+ 1];
64 struct sensor_device_attribute_2
65 mlxreg_hotplug_dev_attr
[MLXREG_HOTPLUG_ATTRS_MAX
];
66 struct attribute_group group
;
67 const struct attribute_group
*groups
[2];
75 /* Environment variables array for udev. */
76 static char *mlxreg_hotplug_udev_envp
[] = { NULL
, NULL
};
79 mlxreg_hotplug_udev_event_send(struct kobject
*kobj
,
80 struct mlxreg_core_data
*data
, bool action
)
82 char event_str
[MLXREG_CORE_LABEL_MAX_SIZE
+ 2];
83 char label
[MLXREG_CORE_LABEL_MAX_SIZE
] = { 0 };
85 mlxreg_hotplug_udev_envp
[0] = event_str
;
86 string_upper(label
, data
->label
);
87 snprintf(event_str
, MLXREG_CORE_LABEL_MAX_SIZE
, "%s=%d", label
, !!action
);
89 return kobject_uevent_env(kobj
, KOBJ_CHANGE
, mlxreg_hotplug_udev_envp
);
92 static int mlxreg_hotplug_device_create(struct mlxreg_hotplug_priv_data
*priv
,
93 struct mlxreg_core_data
*data
)
95 struct mlxreg_core_hotplug_platform_data
*pdata
;
96 struct i2c_client
*client
;
98 /* Notify user by sending hwmon uevent. */
99 mlxreg_hotplug_udev_event_send(&priv
->hwmon
->kobj
, data
, true);
102 * Return if adapter number is negative. It could be in case hotplug
103 * event is not associated with hotplug device.
105 if (data
->hpdev
.nr
< 0)
108 pdata
= dev_get_platdata(&priv
->pdev
->dev
);
109 data
->hpdev
.adapter
= i2c_get_adapter(data
->hpdev
.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
);
117 client
= i2c_new_client_device(data
->hpdev
.adapter
,
118 data
->hpdev
.brdinfo
);
119 if (IS_ERR(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
);
124 i2c_put_adapter(data
->hpdev
.adapter
);
125 data
->hpdev
.adapter
= NULL
;
126 return PTR_ERR(client
);
129 data
->hpdev
.client
= client
;
135 mlxreg_hotplug_device_destroy(struct mlxreg_hotplug_priv_data
*priv
,
136 struct mlxreg_core_data
*data
)
138 /* Notify user by sending hwmon uevent. */
139 mlxreg_hotplug_udev_event_send(&priv
->hwmon
->kobj
, data
, false);
141 if (data
->hpdev
.client
) {
142 i2c_unregister_device(data
->hpdev
.client
);
143 data
->hpdev
.client
= NULL
;
146 if (data
->hpdev
.adapter
) {
147 i2c_put_adapter(data
->hpdev
.adapter
);
148 data
->hpdev
.adapter
= NULL
;
152 static ssize_t
mlxreg_hotplug_attr_show(struct device
*dev
,
153 struct device_attribute
*attr
,
156 struct mlxreg_hotplug_priv_data
*priv
= dev_get_drvdata(dev
);
157 struct mlxreg_core_hotplug_platform_data
*pdata
;
158 int index
= to_sensor_dev_attr_2(attr
)->index
;
159 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
160 struct mlxreg_core_item
*item
;
161 struct mlxreg_core_data
*data
;
165 pdata
= dev_get_platdata(&priv
->pdev
->dev
);
166 item
= pdata
->items
+ nr
;
167 data
= item
->data
+ index
;
169 ret
= regmap_read(priv
->regmap
, data
->reg
, ®val
);
174 regval
&= data
->mask
;
176 /* Bit = 0 : functional if item->inversed is true. */
178 regval
= !(regval
& data
->mask
);
180 regval
= !!(regval
& data
->mask
);
183 return sprintf(buf
, "%u\n", regval
);
186 #define PRIV_ATTR(i) priv->mlxreg_hotplug_attr[i]
187 #define PRIV_DEV_ATTR(i) priv->mlxreg_hotplug_dev_attr[i]
189 static int mlxreg_hotplug_attr_init(struct mlxreg_hotplug_priv_data
*priv
)
191 struct mlxreg_core_hotplug_platform_data
*pdata
;
192 struct mlxreg_core_item
*item
;
193 struct mlxreg_core_data
*data
;
196 int num_attrs
= 0, id
= 0, i
, j
, k
, ret
;
198 pdata
= dev_get_platdata(&priv
->pdev
->dev
);
201 /* Go over all kinds of items - psu, pwr, fan. */
202 for (i
= 0; i
< pdata
->counter
; i
++, item
++) {
203 if (item
->capability
) {
205 * Read group capability register to get actual number
206 * of interrupt capable components and set group mask
209 ret
= regmap_read(priv
->regmap
, item
->capability
,
214 item
->mask
= GENMASK((regval
& item
->mask
) - 1, 0);
219 /* Go over all unmasked units within item. */
222 for_each_set_bit(j
, &mask
, item
->count
) {
223 if (data
->capability
) {
225 * Read capability register and skip non
226 * relevant attributes.
228 ret
= regmap_read(priv
->regmap
,
229 data
->capability
, ®val
);
232 if (!(regval
& data
->bit
)) {
237 PRIV_ATTR(id
) = &PRIV_DEV_ATTR(id
).dev_attr
.attr
;
238 PRIV_ATTR(id
)->name
= devm_kasprintf(&priv
->pdev
->dev
,
242 if (!PRIV_ATTR(id
)->name
) {
243 dev_err(priv
->dev
, "Memory allocation failed for attr %d.\n",
248 PRIV_DEV_ATTR(id
).dev_attr
.attr
.name
=
250 PRIV_DEV_ATTR(id
).dev_attr
.attr
.mode
= 0444;
251 PRIV_DEV_ATTR(id
).dev_attr
.show
=
252 mlxreg_hotplug_attr_show
;
253 PRIV_DEV_ATTR(id
).nr
= i
;
254 PRIV_DEV_ATTR(id
).index
= k
;
255 sysfs_attr_init(&PRIV_DEV_ATTR(id
).dev_attr
.attr
);
263 priv
->group
.attrs
= devm_kcalloc(&priv
->pdev
->dev
,
265 sizeof(struct attribute
*),
267 if (!priv
->group
.attrs
)
270 priv
->group
.attrs
= priv
->mlxreg_hotplug_attr
;
271 priv
->groups
[0] = &priv
->group
;
272 priv
->groups
[1] = NULL
;
278 mlxreg_hotplug_work_helper(struct mlxreg_hotplug_priv_data
*priv
,
279 struct mlxreg_core_item
*item
)
281 struct mlxreg_core_data
*data
;
282 unsigned long asserted
;
287 * Validate if item related to received signal type is valid.
288 * It should never happen, excepted the situation when some
289 * piece of hardware is broken. In such situation just produce
290 * error message and return. Caller must continue to handle the
291 * signals from other devices if any.
293 if (unlikely(!item
)) {
294 dev_err(priv
->dev
, "False signal: at offset:mask 0x%02x:0x%02x.\n",
295 item
->reg
, item
->mask
);
301 ret
= regmap_write(priv
->regmap
, item
->reg
+ MLXREG_HOTPLUG_MASK_OFF
,
307 ret
= regmap_read(priv
->regmap
, item
->reg
, ®val
);
311 /* Set asserted bits and save last status. */
312 regval
&= item
->mask
;
313 asserted
= item
->cache
^ regval
;
314 item
->cache
= regval
;
316 for_each_set_bit(bit
, &asserted
, 8) {
317 data
= item
->data
+ bit
;
318 if (regval
& BIT(bit
)) {
320 mlxreg_hotplug_device_destroy(priv
, data
);
322 mlxreg_hotplug_device_create(priv
, data
);
325 mlxreg_hotplug_device_create(priv
, data
);
327 mlxreg_hotplug_device_destroy(priv
, data
);
331 /* Acknowledge event. */
332 ret
= regmap_write(priv
->regmap
, item
->reg
+ MLXREG_HOTPLUG_EVENT_OFF
,
338 ret
= regmap_write(priv
->regmap
, item
->reg
+ MLXREG_HOTPLUG_MASK_OFF
,
343 dev_err(priv
->dev
, "Failed to complete workqueue.\n");
347 mlxreg_hotplug_health_work_helper(struct mlxreg_hotplug_priv_data
*priv
,
348 struct mlxreg_core_item
*item
)
350 struct mlxreg_core_data
*data
= item
->data
;
354 for (i
= 0; i
< item
->count
; i
++, data
++) {
356 ret
= regmap_write(priv
->regmap
, data
->reg
+
357 MLXREG_HOTPLUG_MASK_OFF
, 0);
362 ret
= regmap_read(priv
->regmap
, data
->reg
, ®val
);
366 regval
&= data
->mask
;
368 if (item
->cache
== regval
)
372 * ASIC health indication is provided through two bits. Bits
373 * value 0x2 indicates that ASIC reached the good health, value
374 * 0x0 indicates ASIC the bad health or dormant state and value
375 * 0x3 indicates the booting state. During ASIC reset it should
376 * pass the following states: dormant -> booting -> good.
378 if (regval
== MLXREG_HOTPLUG_GOOD_HEALTH_MASK
) {
379 if (!data
->attached
) {
381 * ASIC is in steady state. Connect associated
382 * device, if configured.
384 mlxreg_hotplug_device_create(priv
, data
);
385 data
->attached
= true;
388 if (data
->attached
) {
390 * ASIC health is failed after ASIC has been
391 * in steady state. Disconnect associated
392 * device, if it has been connected.
394 mlxreg_hotplug_device_destroy(priv
, data
);
395 data
->attached
= false;
396 data
->health_cntr
= 0;
399 item
->cache
= regval
;
401 /* Acknowledge event. */
402 ret
= regmap_write(priv
->regmap
, data
->reg
+
403 MLXREG_HOTPLUG_EVENT_OFF
, 0);
408 ret
= regmap_write(priv
->regmap
, data
->reg
+
409 MLXREG_HOTPLUG_MASK_OFF
, data
->mask
);
416 dev_err(priv
->dev
, "Failed to complete workqueue.\n");
420 * mlxreg_hotplug_work_handler - performs traversing of device interrupt
421 * registers according to the below hierarchy schema:
423 * Aggregation registers (status/mask)
424 * PSU registers: *---*
425 * *-----------------* | |
426 * |status/event/mask|-----> | * |
427 * *-----------------* | |
428 * Power registers: | |
429 * *-----------------* | |
430 * |status/event/mask|-----> | * |
431 * *-----------------* | |
432 * FAN registers: | |--> CPU
433 * *-----------------* | |
434 * |status/event/mask|-----> | * |
435 * *-----------------* | |
436 * ASIC registers: | |
437 * *-----------------* | |
438 * |status/event/mask|-----> | * |
439 * *-----------------* | |
442 * In case some system changed are detected: FAN in/out, PSU in/out, power
443 * cable attached/detached, ASIC health good/bad, relevant device is created
446 static void mlxreg_hotplug_work_handler(struct work_struct
*work
)
448 struct mlxreg_core_hotplug_platform_data
*pdata
;
449 struct mlxreg_hotplug_priv_data
*priv
;
450 struct mlxreg_core_item
*item
;
451 u32 regval
, aggr_asserted
;
455 priv
= container_of(work
, struct mlxreg_hotplug_priv_data
,
457 pdata
= dev_get_platdata(&priv
->pdev
->dev
);
460 /* Mask aggregation event. */
461 ret
= regmap_write(priv
->regmap
, pdata
->cell
+
462 MLXREG_HOTPLUG_AGGR_MASK_OFF
, 0);
466 /* Read aggregation status. */
467 ret
= regmap_read(priv
->regmap
, pdata
->cell
, ®val
);
471 regval
&= pdata
->mask
;
472 aggr_asserted
= priv
->aggr_cache
^ regval
;
473 priv
->aggr_cache
= regval
;
476 * Handler is invoked, but no assertion is detected at top aggregation
477 * status level. Set aggr_asserted to mask value to allow handler extra
478 * run over all relevant signals to recover any missed signal.
480 if (priv
->not_asserted
== MLXREG_HOTPLUG_NOT_ASSERT
) {
481 priv
->not_asserted
= 0;
482 aggr_asserted
= pdata
->mask
;
487 /* Handle topology and health configuration changes. */
488 for (i
= 0; i
< pdata
->counter
; i
++, item
++) {
489 if (aggr_asserted
& item
->aggr_mask
) {
491 mlxreg_hotplug_health_work_helper(priv
, item
);
493 mlxreg_hotplug_work_helper(priv
, item
);
497 spin_lock_irqsave(&priv
->lock
, flags
);
500 * It is possible, that some signals have been inserted, while
501 * interrupt has been masked by mlxreg_hotplug_work_handler. In this
502 * case such signals will be missed. In order to handle these signals
503 * delayed work is canceled and work task re-scheduled for immediate
504 * execution. It allows to handle missed signals, if any. In other case
505 * work handler just validates that no new signals have been received
508 cancel_delayed_work(&priv
->dwork_irq
);
509 schedule_delayed_work(&priv
->dwork_irq
, 0);
511 spin_unlock_irqrestore(&priv
->lock
, flags
);
516 priv
->not_asserted
++;
517 /* Unmask aggregation event (no need acknowledge). */
518 ret
= regmap_write(priv
->regmap
, pdata
->cell
+
519 MLXREG_HOTPLUG_AGGR_MASK_OFF
, pdata
->mask
);
523 dev_err(priv
->dev
, "Failed to complete workqueue.\n");
526 static int mlxreg_hotplug_set_irq(struct mlxreg_hotplug_priv_data
*priv
)
528 struct mlxreg_core_hotplug_platform_data
*pdata
;
529 struct mlxreg_core_item
*item
;
530 struct mlxreg_core_data
*data
;
534 pdata
= dev_get_platdata(&priv
->pdev
->dev
);
537 for (i
= 0; i
< pdata
->counter
; i
++, item
++) {
538 /* Clear group presense event. */
539 ret
= regmap_write(priv
->regmap
, item
->reg
+
540 MLXREG_HOTPLUG_EVENT_OFF
, 0);
545 * Verify if hardware configuration requires to disable
546 * interrupt capability for some of components.
549 for (j
= 0; j
< item
->count
; j
++, data
++) {
550 /* Verify if the attribute has capability register. */
551 if (data
->capability
) {
552 /* Read capability register. */
553 ret
= regmap_read(priv
->regmap
,
554 data
->capability
, ®val
);
558 if (!(regval
& data
->bit
))
559 item
->mask
&= ~BIT(j
);
563 /* Set group initial status as mask and unmask group event. */
564 if (item
->inversed
) {
565 item
->cache
= item
->mask
;
566 ret
= regmap_write(priv
->regmap
, item
->reg
+
567 MLXREG_HOTPLUG_MASK_OFF
,
574 /* Keep aggregation initial status as zero and unmask events. */
575 ret
= regmap_write(priv
->regmap
, pdata
->cell
+
576 MLXREG_HOTPLUG_AGGR_MASK_OFF
, pdata
->mask
);
580 /* Keep low aggregation initial status as zero and unmask events. */
581 if (pdata
->cell_low
) {
582 ret
= regmap_write(priv
->regmap
, pdata
->cell_low
+
583 MLXREG_HOTPLUG_AGGR_MASK_OFF
,
589 /* Invoke work handler for initializing hot plug devices setting. */
590 mlxreg_hotplug_work_handler(&priv
->dwork_irq
.work
);
594 dev_err(priv
->dev
, "Failed to set interrupts.\n");
595 enable_irq(priv
->irq
);
599 static void mlxreg_hotplug_unset_irq(struct mlxreg_hotplug_priv_data
*priv
)
601 struct mlxreg_core_hotplug_platform_data
*pdata
;
602 struct mlxreg_core_item
*item
;
603 struct mlxreg_core_data
*data
;
606 pdata
= dev_get_platdata(&priv
->pdev
->dev
);
608 disable_irq(priv
->irq
);
609 cancel_delayed_work_sync(&priv
->dwork_irq
);
611 /* Mask low aggregation event, if defined. */
613 regmap_write(priv
->regmap
, pdata
->cell_low
+
614 MLXREG_HOTPLUG_AGGR_MASK_OFF
, 0);
616 /* Mask aggregation event. */
617 regmap_write(priv
->regmap
, pdata
->cell
+ MLXREG_HOTPLUG_AGGR_MASK_OFF
,
620 /* Clear topology configurations. */
621 for (i
= 0; i
< pdata
->counter
; i
++, item
++) {
623 /* Mask group presense event. */
624 regmap_write(priv
->regmap
, data
->reg
+ MLXREG_HOTPLUG_MASK_OFF
,
626 /* Clear group presense event. */
627 regmap_write(priv
->regmap
, data
->reg
+
628 MLXREG_HOTPLUG_EVENT_OFF
, 0);
630 /* Remove all the attached devices in group. */
632 for (j
= 0; j
< count
; j
++, data
++)
633 mlxreg_hotplug_device_destroy(priv
, data
);
637 static irqreturn_t
mlxreg_hotplug_irq_handler(int irq
, void *dev
)
639 struct mlxreg_hotplug_priv_data
*priv
;
641 priv
= (struct mlxreg_hotplug_priv_data
*)dev
;
643 /* Schedule work task for immediate execution.*/
644 schedule_delayed_work(&priv
->dwork_irq
, 0);
649 static int mlxreg_hotplug_probe(struct platform_device
*pdev
)
651 struct mlxreg_core_hotplug_platform_data
*pdata
;
652 struct mlxreg_hotplug_priv_data
*priv
;
653 struct i2c_adapter
*deferred_adap
;
656 pdata
= dev_get_platdata(&pdev
->dev
);
658 dev_err(&pdev
->dev
, "Failed to get platform data.\n");
662 /* Defer probing if the necessary adapter is not configured yet. */
663 deferred_adap
= i2c_get_adapter(pdata
->deferred_nr
);
665 return -EPROBE_DEFER
;
666 i2c_put_adapter(deferred_adap
);
668 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
673 priv
->irq
= pdata
->irq
;
675 priv
->irq
= platform_get_irq(pdev
, 0);
680 priv
->regmap
= pdata
->regmap
;
681 priv
->dev
= pdev
->dev
.parent
;
684 err
= devm_request_irq(&pdev
->dev
, priv
->irq
,
685 mlxreg_hotplug_irq_handler
, IRQF_TRIGGER_FALLING
686 | IRQF_SHARED
, "mlxreg-hotplug", priv
);
688 dev_err(&pdev
->dev
, "Failed to request irq: %d\n", err
);
692 disable_irq(priv
->irq
);
693 spin_lock_init(&priv
->lock
);
694 INIT_DELAYED_WORK(&priv
->dwork_irq
, mlxreg_hotplug_work_handler
);
695 dev_set_drvdata(&pdev
->dev
, priv
);
697 err
= mlxreg_hotplug_attr_init(priv
);
699 dev_err(&pdev
->dev
, "Failed to allocate attributes: %d\n",
704 priv
->hwmon
= devm_hwmon_device_register_with_groups(&pdev
->dev
,
705 "mlxreg_hotplug", priv
, priv
->groups
);
706 if (IS_ERR(priv
->hwmon
)) {
707 dev_err(&pdev
->dev
, "Failed to register hwmon device %ld\n",
708 PTR_ERR(priv
->hwmon
));
709 return PTR_ERR(priv
->hwmon
);
712 /* Perform initial interrupts setup. */
713 mlxreg_hotplug_set_irq(priv
);
714 priv
->after_probe
= true;
719 static int mlxreg_hotplug_remove(struct platform_device
*pdev
)
721 struct mlxreg_hotplug_priv_data
*priv
= dev_get_drvdata(&pdev
->dev
);
723 /* Clean interrupts setup. */
724 mlxreg_hotplug_unset_irq(priv
);
725 devm_free_irq(&pdev
->dev
, priv
->irq
, priv
);
730 static struct platform_driver mlxreg_hotplug_driver
= {
732 .name
= "mlxreg-hotplug",
734 .probe
= mlxreg_hotplug_probe
,
735 .remove
= mlxreg_hotplug_remove
,
738 module_platform_driver(mlxreg_hotplug_driver
);
740 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
741 MODULE_DESCRIPTION("Mellanox regmap hotplug platform driver");
742 MODULE_LICENSE("Dual BSD/GPL");
743 MODULE_ALIAS("platform:mlxreg-hotplug");