mlxreg-hotplug.c 21.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * Mellanox hotplug driver
4
 *
5
 * Copyright (C) 2016-2020 Mellanox Technologies
6 7 8 9 10 11 12 13 14
 */

#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/module.h>
15
#include <linux/platform_data/mlxreg.h>
16 17
#include <linux/platform_device.h>
#include <linux/spinlock.h>
18
#include <linux/string_helpers.h>
19
#include <linux/regmap.h>
20 21
#include <linux/workqueue.h>

22
/* Offset of event and mask registers from status register. */
23
#define MLXREG_HOTPLUG_EVENT_OFF	1
24
#define MLXREG_HOTPLUG_MASK_OFF		2
25
#define MLXREG_HOTPLUG_AGGR_MASK_OFF	1
26

27 28
/* ASIC good health mask. */
#define MLXREG_HOTPLUG_GOOD_HEALTH_MASK	0x02
29

30
#define MLXREG_HOTPLUG_ATTRS_MAX	128
31
#define MLXREG_HOTPLUG_NOT_ASSERT	3
32 33

/**
34
 * struct mlxreg_hotplug_priv_data - platform private data:
35
 * @irq: platform device interrupt number;
36
 * @dev: basic device;
37 38
 * @pdev: platform device;
 * @plat: platform data;
39 40
 * @regmap: register map handle;
 * @dwork_irq: delayed work template;
41
 * @lock: spin lock;
42
 * @hwmon: hwmon device;
43 44
 * @mlxreg_hotplug_attr: sysfs attributes array;
 * @mlxreg_hotplug_dev_attr: sysfs sensor device attribute array;
45 46
 * @group: sysfs attribute group;
 * @groups: list of sysfs attribute group for hwmon registration;
47 48
 * @cell: location of top aggregation interrupt register;
 * @mask: top aggregation interrupt common mask;
49
 * @aggr_cache: last value of aggregation register status;
50
 * @after_probe: flag indication probing completion;
51
 * @not_asserted: number of entries in workqueue with no signal assertion;
52
 */
53
struct mlxreg_hotplug_priv_data {
54
	int irq;
55
	struct device *dev;
56
	struct platform_device *pdev;
57
	struct mlxreg_hotplug_platform_data *plat;
58 59 60
	struct regmap *regmap;
	struct delayed_work dwork_irq;
	spinlock_t lock; /* sync with interrupt */
61
	struct device *hwmon;
62
	struct attribute *mlxreg_hotplug_attr[MLXREG_HOTPLUG_ATTRS_MAX + 1];
63
	struct sensor_device_attribute_2
64
			mlxreg_hotplug_dev_attr[MLXREG_HOTPLUG_ATTRS_MAX];
65 66
	struct attribute_group group;
	const struct attribute_group *groups[2];
67 68 69 70
	u32 cell;
	u32 mask;
	u32 aggr_cache;
	bool after_probe;
71
	u8 not_asserted;
72 73
};

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/* Environment variables array for udev. */
static char *mlxreg_hotplug_udev_envp[] = { NULL, NULL };

static int
mlxreg_hotplug_udev_event_send(struct kobject *kobj,
			       struct mlxreg_core_data *data, bool action)
{
	char event_str[MLXREG_CORE_LABEL_MAX_SIZE + 2];
	char label[MLXREG_CORE_LABEL_MAX_SIZE] = { 0 };

	mlxreg_hotplug_udev_envp[0] = event_str;
	string_upper(label, data->label);
	snprintf(event_str, MLXREG_CORE_LABEL_MAX_SIZE, "%s=%d", label, !!action);

	return kobject_uevent_env(kobj, KOBJ_CHANGE, mlxreg_hotplug_udev_envp);
}

91 92 93 94 95 96 97 98 99
static void
mlxreg_hotplug_pdata_export(void *pdata, void *regmap)
{
	struct mlxreg_core_hotplug_platform_data *dev_pdata = pdata;

	/* Export regmap to underlying device. */
	dev_pdata->regmap = regmap;
}

100
static int mlxreg_hotplug_device_create(struct mlxreg_hotplug_priv_data *priv,
101 102
					struct mlxreg_core_data *data,
					enum mlxreg_hotplug_kind kind)
103
{
104
	struct i2c_board_info *brdinfo = data->hpdev.brdinfo;
105
	struct mlxreg_core_hotplug_platform_data *pdata;
106
	struct i2c_client *client;
107

108
	/* Notify user by sending hwmon uevent. */
109
	mlxreg_hotplug_udev_event_send(&priv->hwmon->kobj, data, true);
110

111 112 113 114
	/*
	 * Return if adapter number is negative. It could be in case hotplug
	 * event is not associated with hotplug device.
	 */
115
	if (data->hpdev.nr < 0 && data->hpdev.action != MLXREG_HOTPLUG_DEVICE_NO_ACTION)
116 117
		return 0;

118
	pdata = dev_get_platdata(&priv->pdev->dev);
119 120 121 122 123 124 125 126 127
	switch (data->hpdev.action) {
	case MLXREG_HOTPLUG_DEVICE_DEFAULT_ACTION:
		data->hpdev.adapter = i2c_get_adapter(data->hpdev.nr +
						      pdata->shift_nr);
		if (!data->hpdev.adapter) {
			dev_err(priv->dev, "Failed to get adapter for bus %d\n",
				data->hpdev.nr + pdata->shift_nr);
			return -EFAULT;
		}
128

129 130 131
		/* Export platform data to underlying device. */
		if (brdinfo->platform_data)
			mlxreg_hotplug_pdata_export(brdinfo->platform_data, pdata->regmap);
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		client = i2c_new_client_device(data->hpdev.adapter,
					       brdinfo);
		if (IS_ERR(client)) {
			dev_err(priv->dev, "Failed to create client %s at bus %d at addr 0x%02x\n",
				brdinfo->type, data->hpdev.nr +
				pdata->shift_nr, brdinfo->addr);

			i2c_put_adapter(data->hpdev.adapter);
			data->hpdev.adapter = NULL;
			return PTR_ERR(client);
		}

		data->hpdev.client = client;
		break;
	case MLXREG_HOTPLUG_DEVICE_PLATFORM_ACTION:
		/* Export platform data to underlying device. */
		if (data->hpdev.brdinfo && data->hpdev.brdinfo->platform_data)
			mlxreg_hotplug_pdata_export(data->hpdev.brdinfo->platform_data,
						    pdata->regmap);
		/* Pass parent hotplug device handle to underlying device. */
		data->notifier = data->hpdev.notifier;
		data->hpdev.pdev = platform_device_register_resndata(&priv->pdev->dev,
								     brdinfo->type,
								     data->hpdev.nr,
								     NULL, 0, data,
								     sizeof(*data));
		if (IS_ERR(data->hpdev.pdev))
			return PTR_ERR(data->hpdev.pdev);

		break;
	default:
		break;
165 166
	}

167 168
	if (data->hpdev.notifier && data->hpdev.notifier->user_handler)
		return data->hpdev.notifier->user_handler(data->hpdev.notifier->handle, kind, 1);
169

170 171 172
	return 0;
}

173 174
static void
mlxreg_hotplug_device_destroy(struct mlxreg_hotplug_priv_data *priv,
175 176
			      struct mlxreg_core_data *data,
			      enum mlxreg_hotplug_kind kind)
177
{
178
	/* Notify user by sending hwmon uevent. */
179
	mlxreg_hotplug_udev_event_send(&priv->hwmon->kobj, data, false);
180 181 182 183 184 185 186 187 188
	if (data->hpdev.notifier && data->hpdev.notifier->user_handler)
		data->hpdev.notifier->user_handler(data->hpdev.notifier->handle, kind, 0);

	switch (data->hpdev.action) {
	case MLXREG_HOTPLUG_DEVICE_DEFAULT_ACTION:
		if (data->hpdev.client) {
			i2c_unregister_device(data->hpdev.client);
			data->hpdev.client = NULL;
		}
189

190 191 192 193 194 195 196 197 198 199 200
		if (data->hpdev.adapter) {
			i2c_put_adapter(data->hpdev.adapter);
			data->hpdev.adapter = NULL;
		}
		break;
	case MLXREG_HOTPLUG_DEVICE_PLATFORM_ACTION:
		if (data->hpdev.pdev)
			platform_device_unregister(data->hpdev.pdev);
		break;
	default:
		break;
201 202 203
	}
}

204 205 206
static ssize_t mlxreg_hotplug_attr_show(struct device *dev,
					struct device_attribute *attr,
					char *buf)
207
{
208 209
	struct mlxreg_hotplug_priv_data *priv = dev_get_drvdata(dev);
	struct mlxreg_core_hotplug_platform_data *pdata;
210 211
	int index = to_sensor_dev_attr_2(attr)->index;
	int nr = to_sensor_dev_attr_2(attr)->nr;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	struct mlxreg_core_item *item;
	struct mlxreg_core_data *data;
	u32 regval;
	int ret;

	pdata = dev_get_platdata(&priv->pdev->dev);
	item = pdata->items + nr;
	data = item->data + index;

	ret = regmap_read(priv->regmap, data->reg, &regval);
	if (ret)
		return ret;

	if (item->health) {
		regval &= data->mask;
	} else {
		/* Bit = 0 : functional if item->inversed is true. */
		if (item->inversed)
			regval = !(regval & data->mask);
		else
			regval = !!(regval & data->mask);
233 234
	}

235
	return sprintf(buf, "%u\n", regval);
236 237
}

238 239
#define PRIV_ATTR(i) priv->mlxreg_hotplug_attr[i]
#define PRIV_DEV_ATTR(i) priv->mlxreg_hotplug_dev_attr[i]
240

241 242 243 244 245 246 247 248 249 250 251
static int mlxreg_hotplug_item_label_index_get(u32 mask, u32 bit)
{
	int i, j;

	for (i = 0, j = -1; i <= bit; i++) {
		if (mask & BIT(i))
			j++;
	}
	return j;
}

252
static int mlxreg_hotplug_attr_init(struct mlxreg_hotplug_priv_data *priv)
253
{
254 255 256
	struct mlxreg_core_hotplug_platform_data *pdata;
	struct mlxreg_core_item *item;
	struct mlxreg_core_data *data;
257 258
	unsigned long mask;
	u32 regval;
259
	int num_attrs = 0, id = 0, i, j, k, count, ret;
260 261 262 263 264 265

	pdata = dev_get_platdata(&priv->pdev->dev);
	item = pdata->items;

	/* Go over all kinds of items - psu, pwr, fan. */
	for (i = 0; i < pdata->counter; i++, item++) {
266 267 268 269 270 271 272 273 274 275 276 277 278 279
		if (item->capability) {
			/*
			 * Read group capability register to get actual number
			 * of interrupt capable components and set group mask
			 * accordingly.
			 */
			ret = regmap_read(priv->regmap, item->capability,
					  &regval);
			if (ret)
				return ret;

			item->mask = GENMASK((regval & item->mask) - 1, 0);
		}

280
		data = item->data;
281 282 283 284

		/* Go over all unmasked units within item. */
		mask = item->mask;
		k = 0;
285 286
		count = item->ind ? item->ind : item->count;
		for_each_set_bit(j, &mask, count) {
287 288 289 290 291 292 293 294 295
			if (data->capability) {
				/*
				 * Read capability register and skip non
				 * relevant attributes.
				 */
				ret = regmap_read(priv->regmap,
						  data->capability, &regval);
				if (ret)
					return ret;
296

297 298 299 300 301
				if (!(regval & data->bit)) {
					data++;
					continue;
				}
			}
302

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
			PRIV_ATTR(id) = &PRIV_DEV_ATTR(id).dev_attr.attr;
			PRIV_ATTR(id)->name = devm_kasprintf(&priv->pdev->dev,
							     GFP_KERNEL,
							     data->label);
			if (!PRIV_ATTR(id)->name) {
				dev_err(priv->dev, "Memory allocation failed for attr %d.\n",
					id);
				return -ENOMEM;
			}

			PRIV_DEV_ATTR(id).dev_attr.attr.name =
							PRIV_ATTR(id)->name;
			PRIV_DEV_ATTR(id).dev_attr.attr.mode = 0444;
			PRIV_DEV_ATTR(id).dev_attr.show =
						mlxreg_hotplug_attr_show;
			PRIV_DEV_ATTR(id).nr = i;
319
			PRIV_DEV_ATTR(id).index = k;
320
			sysfs_attr_init(&PRIV_DEV_ATTR(id).dev_attr.attr);
321 322 323
			data++;
			id++;
			k++;
324
		}
325
		num_attrs += k;
326
	}
327

328 329
	priv->group.attrs = devm_kcalloc(&priv->pdev->dev,
					 num_attrs,
330 331 332 333 334
					 sizeof(struct attribute *),
					 GFP_KERNEL);
	if (!priv->group.attrs)
		return -ENOMEM;

335
	priv->group.attrs = priv->mlxreg_hotplug_attr;
336 337 338 339 340 341
	priv->groups[0] = &priv->group;
	priv->groups[1] = NULL;

	return 0;
}

342 343 344
static void
mlxreg_hotplug_work_helper(struct mlxreg_hotplug_priv_data *priv,
			   struct mlxreg_core_item *item)
345
{
346
	struct mlxreg_core_data *data;
347 348
	unsigned long asserted;
	u32 regval, bit;
349
	int ret;
350

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	/* Mask event. */
	ret = regmap_write(priv->regmap, item->reg + MLXREG_HOTPLUG_MASK_OFF,
			   0);
	if (ret)
		goto out;

	/* Read status. */
	ret = regmap_read(priv->regmap, item->reg, &regval);
	if (ret)
		goto out;

	/* Set asserted bits and save last status. */
	regval &= item->mask;
	asserted = item->cache ^ regval;
	item->cache = regval;
366
	for_each_set_bit(bit, &asserted, 8) {
367 368 369 370 371 372 373
		int pos;

		pos = mlxreg_hotplug_item_label_index_get(item->mask, bit);
		if (pos < 0)
			goto out;

		data = item->data + pos;
374 375
		if (regval & BIT(bit)) {
			if (item->inversed)
376
				mlxreg_hotplug_device_destroy(priv, data, item->kind);
377
			else
378
				mlxreg_hotplug_device_create(priv, data, item->kind);
379
		} else {
380
			if (item->inversed)
381
				mlxreg_hotplug_device_create(priv, data, item->kind);
382
			else
383
				mlxreg_hotplug_device_destroy(priv, data, item->kind);
384 385 386 387
		}
	}

	/* Acknowledge event. */
388 389 390 391 392
	ret = regmap_write(priv->regmap, item->reg + MLXREG_HOTPLUG_EVENT_OFF,
			   0);
	if (ret)
		goto out;

393
	/* Unmask event. */
394 395 396 397 398 399 400 401 402 403 404 405 406 407
	ret = regmap_write(priv->regmap, item->reg + MLXREG_HOTPLUG_MASK_OFF,
			   item->mask);

 out:
	if (ret)
		dev_err(priv->dev, "Failed to complete workqueue.\n");
}

static void
mlxreg_hotplug_health_work_helper(struct mlxreg_hotplug_priv_data *priv,
				  struct mlxreg_core_item *item)
{
	struct mlxreg_core_data *data = item->data;
	u32 regval;
408
	int i, ret = 0;
409 410 411 412 413 414 415 416 417 418 419 420 421 422

	for (i = 0; i < item->count; i++, data++) {
		/* Mask event. */
		ret = regmap_write(priv->regmap, data->reg +
				   MLXREG_HOTPLUG_MASK_OFF, 0);
		if (ret)
			goto out;

		/* Read status. */
		ret = regmap_read(priv->regmap, data->reg, &regval);
		if (ret)
			goto out;

		regval &= data->mask;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

		if (item->cache == regval)
			goto ack_event;

		/*
		 * ASIC health indication is provided through two bits. Bits
		 * value 0x2 indicates that ASIC reached the good health, value
		 * 0x0 indicates ASIC the bad health or dormant state and value
		 * 0x3 indicates the booting state. During ASIC reset it should
		 * pass the following states: dormant -> booting -> good.
		 */
		if (regval == MLXREG_HOTPLUG_GOOD_HEALTH_MASK) {
			if (!data->attached) {
				/*
				 * ASIC is in steady state. Connect associated
				 * device, if configured.
				 */
440
				mlxreg_hotplug_device_create(priv, data, item->kind);
441 442 443 444
				data->attached = true;
			}
		} else {
			if (data->attached) {
445 446 447 448 449
				/*
				 * ASIC health is failed after ASIC has been
				 * in steady state. Disconnect associated
				 * device, if it has been connected.
				 */
450
				mlxreg_hotplug_device_destroy(priv, data, item->kind);
451 452 453 454
				data->attached = false;
				data->health_cntr = 0;
			}
		}
455 456
		item->cache = regval;
ack_event:
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		/* Acknowledge event. */
		ret = regmap_write(priv->regmap, data->reg +
				   MLXREG_HOTPLUG_EVENT_OFF, 0);
		if (ret)
			goto out;

		/* Unmask event. */
		ret = regmap_write(priv->regmap, data->reg +
				   MLXREG_HOTPLUG_MASK_OFF, data->mask);
		if (ret)
			goto out;
	}

 out:
	if (ret)
		dev_err(priv->dev, "Failed to complete workqueue.\n");
473 474 475
}

/*
476
 * mlxreg_hotplug_work_handler - performs traversing of device interrupt
477 478
 * registers according to the below hierarchy schema:
 *
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
 *				Aggregation registers (status/mask)
 * PSU registers:		*---*
 * *-----------------*		|   |
 * |status/event/mask|----->    | * |
 * *-----------------*		|   |
 * Power registers:		|   |
 * *-----------------*		|   |
 * |status/event/mask|----->    | * |
 * *-----------------*		|   |
 * FAN registers:		|   |--> CPU
 * *-----------------*		|   |
 * |status/event/mask|----->    | * |
 * *-----------------*		|   |
 * ASIC registers:		|   |
 * *-----------------*		|   |
 * |status/event/mask|----->    | * |
 * *-----------------*		|   |
 *				*---*
 *
498
 * In case some system changed are detected: FAN in/out, PSU in/out, power
499 500
 * cable attached/detached, ASIC health good/bad, relevant device is created
 * or destroyed.
501
 */
502
static void mlxreg_hotplug_work_handler(struct work_struct *work)
503
{
504 505 506 507
	struct mlxreg_core_hotplug_platform_data *pdata;
	struct mlxreg_hotplug_priv_data *priv;
	struct mlxreg_core_item *item;
	u32 regval, aggr_asserted;
508
	unsigned long flags;
509 510 511 512 513 514
	int i, ret;

	priv = container_of(work, struct mlxreg_hotplug_priv_data,
			    dwork_irq.work);
	pdata = dev_get_platdata(&priv->pdev->dev);
	item = pdata->items;
515 516

	/* Mask aggregation event. */
517 518 519 520 521
	ret = regmap_write(priv->regmap, pdata->cell +
			   MLXREG_HOTPLUG_AGGR_MASK_OFF, 0);
	if (ret < 0)
		goto out;

522
	/* Read aggregation status. */
523 524 525 526 527 528 529 530
	ret = regmap_read(priv->regmap, pdata->cell, &regval);
	if (ret)
		goto out;

	regval &= pdata->mask;
	aggr_asserted = priv->aggr_cache ^ regval;
	priv->aggr_cache = regval;

531 532 533 534 535 536 537 538 539 540 541 542
	/*
	 * Handler is invoked, but no assertion is detected at top aggregation
	 * status level. Set aggr_asserted to mask value to allow handler extra
	 * run over all relevant signals to recover any missed signal.
	 */
	if (priv->not_asserted == MLXREG_HOTPLUG_NOT_ASSERT) {
		priv->not_asserted = 0;
		aggr_asserted = pdata->mask;
	}
	if (!aggr_asserted)
		goto unmask_event;

543 544 545 546 547 548 549 550 551
	/* Handle topology and health configuration changes. */
	for (i = 0; i < pdata->counter; i++, item++) {
		if (aggr_asserted & item->aggr_mask) {
			if (item->health)
				mlxreg_hotplug_health_work_helper(priv, item);
			else
				mlxreg_hotplug_work_helper(priv, item);
		}
	}
552

553
	spin_lock_irqsave(&priv->lock, flags);
554

555 556 557 558 559 560 561 562 563 564 565
	/*
	 * It is possible, that some signals have been inserted, while
	 * interrupt has been masked by mlxreg_hotplug_work_handler. In this
	 * case such signals will be missed. In order to handle these signals
	 * delayed work is canceled and work task re-scheduled for immediate
	 * execution. It allows to handle missed signals, if any. In other case
	 * work handler just validates that no new signals have been received
	 * during masking.
	 */
	cancel_delayed_work(&priv->dwork_irq);
	schedule_delayed_work(&priv->dwork_irq, 0);
566

567
	spin_unlock_irqrestore(&priv->lock, flags);
568

569
	return;
570

571 572
unmask_event:
	priv->not_asserted++;
573
	/* Unmask aggregation event (no need acknowledge). */
574 575 576 577 578 579
	ret = regmap_write(priv->regmap, pdata->cell +
			   MLXREG_HOTPLUG_AGGR_MASK_OFF, pdata->mask);

 out:
	if (ret)
		dev_err(priv->dev, "Failed to complete workqueue.\n");
580 581
}

582
static int mlxreg_hotplug_set_irq(struct mlxreg_hotplug_priv_data *priv)
583
{
584 585
	struct mlxreg_core_hotplug_platform_data *pdata;
	struct mlxreg_core_item *item;
586 587 588
	struct mlxreg_core_data *data;
	u32 regval;
	int i, j, ret;
589 590 591 592 593 594 595 596 597 598 599

	pdata = dev_get_platdata(&priv->pdev->dev);
	item = pdata->items;

	for (i = 0; i < pdata->counter; i++, item++) {
		/* Clear group presense event. */
		ret = regmap_write(priv->regmap, item->reg +
				   MLXREG_HOTPLUG_EVENT_OFF, 0);
		if (ret)
			goto out;

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		/*
		 * Verify if hardware configuration requires to disable
		 * interrupt capability for some of components.
		 */
		data = item->data;
		for (j = 0; j < item->count; j++, data++) {
			/* Verify if the attribute has capability register. */
			if (data->capability) {
				/* Read capability register. */
				ret = regmap_read(priv->regmap,
						  data->capability, &regval);
				if (ret)
					goto out;

				if (!(regval & data->bit))
					item->mask &= ~BIT(j);
			}
		}

619 620 621 622 623 624 625 626 627 628
		/* Set group initial status as mask and unmask group event. */
		if (item->inversed) {
			item->cache = item->mask;
			ret = regmap_write(priv->regmap, item->reg +
					   MLXREG_HOTPLUG_MASK_OFF,
					   item->mask);
			if (ret)
				goto out;
		}
	}
629 630

	/* Keep aggregation initial status as zero and unmask events. */
631 632 633 634 635 636 637 638 639 640 641 642 643
	ret = regmap_write(priv->regmap, pdata->cell +
			   MLXREG_HOTPLUG_AGGR_MASK_OFF, pdata->mask);
	if (ret)
		goto out;

	/* Keep low aggregation initial status as zero and unmask events. */
	if (pdata->cell_low) {
		ret = regmap_write(priv->regmap, pdata->cell_low +
				   MLXREG_HOTPLUG_AGGR_MASK_OFF,
				   pdata->mask_low);
		if (ret)
			goto out;
	}
644 645

	/* Invoke work handler for initializing hot plug devices setting. */
646
	mlxreg_hotplug_work_handler(&priv->dwork_irq.work);
647

648 649 650
 out:
	if (ret)
		dev_err(priv->dev, "Failed to set interrupts.\n");
651
	enable_irq(priv->irq);
652
	return ret;
653 654
}

655
static void mlxreg_hotplug_unset_irq(struct mlxreg_hotplug_priv_data *priv)
656
{
657 658 659 660
	struct mlxreg_core_hotplug_platform_data *pdata;
	struct mlxreg_core_item *item;
	struct mlxreg_core_data *data;
	int count, i, j;
661

662 663
	pdata = dev_get_platdata(&priv->pdev->dev);
	item = pdata->items;
664
	disable_irq(priv->irq);
665
	cancel_delayed_work_sync(&priv->dwork_irq);
666

667 668 669 670
	/* Mask low aggregation event, if defined. */
	if (pdata->cell_low)
		regmap_write(priv->regmap, pdata->cell_low +
			     MLXREG_HOTPLUG_AGGR_MASK_OFF, 0);
671

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	/* Mask aggregation event. */
	regmap_write(priv->regmap, pdata->cell + MLXREG_HOTPLUG_AGGR_MASK_OFF,
		     0);

	/* Clear topology configurations. */
	for (i = 0; i < pdata->counter; i++, item++) {
		data = item->data;
		/* Mask group presense event. */
		regmap_write(priv->regmap, data->reg + MLXREG_HOTPLUG_MASK_OFF,
			     0);
		/* Clear group presense event. */
		regmap_write(priv->regmap, data->reg +
			     MLXREG_HOTPLUG_EVENT_OFF, 0);

		/* Remove all the attached devices in group. */
		count = item->count;
		for (j = 0; j < count; j++, data++)
689
			mlxreg_hotplug_device_destroy(priv, data, item->kind);
690
	}
691 692
}

693
static irqreturn_t mlxreg_hotplug_irq_handler(int irq, void *dev)
694
{
695 696 697
	struct mlxreg_hotplug_priv_data *priv;

	priv = (struct mlxreg_hotplug_priv_data *)dev;
698 699

	/* Schedule work task for immediate execution.*/
700
	schedule_delayed_work(&priv->dwork_irq, 0);
701 702 703 704

	return IRQ_HANDLED;
}

705
static int mlxreg_hotplug_probe(struct platform_device *pdev)
706
{
707
	struct mlxreg_core_hotplug_platform_data *pdata;
708
	struct mlxreg_hotplug_priv_data *priv;
709
	struct i2c_adapter *deferred_adap;
710 711 712 713 714 715 716 717
	int err;

	pdata = dev_get_platdata(&pdev->dev);
	if (!pdata) {
		dev_err(&pdev->dev, "Failed to get platform data.\n");
		return -EINVAL;
	}

718 719 720 721 722 723
	/* Defer probing if the necessary adapter is not configured yet. */
	deferred_adap = i2c_get_adapter(pdata->deferred_nr);
	if (!deferred_adap)
		return -EPROBE_DEFER;
	i2c_put_adapter(deferred_adap);

724 725 726 727
	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

728 729 730 731
	if (pdata->irq) {
		priv->irq = pdata->irq;
	} else {
		priv->irq = platform_get_irq(pdev, 0);
732
		if (priv->irq < 0)
733
			return priv->irq;
734 735
	}

736 737 738 739
	priv->regmap = pdata->regmap;
	priv->dev = pdev->dev.parent;
	priv->pdev = pdev;

740
	err = devm_request_irq(&pdev->dev, priv->irq,
741
			       mlxreg_hotplug_irq_handler, IRQF_TRIGGER_FALLING
742
			       | IRQF_SHARED, "mlxreg-hotplug", priv);
743 744 745 746 747
	if (err) {
		dev_err(&pdev->dev, "Failed to request irq: %d\n", err);
		return err;
	}

748
	disable_irq(priv->irq);
749
	spin_lock_init(&priv->lock);
750 751
	INIT_DELAYED_WORK(&priv->dwork_irq, mlxreg_hotplug_work_handler);
	dev_set_drvdata(&pdev->dev, priv);
752

753
	err = mlxreg_hotplug_attr_init(priv);
754
	if (err) {
755 756
		dev_err(&pdev->dev, "Failed to allocate attributes: %d\n",
			err);
757 758 759 760
		return err;
	}

	priv->hwmon = devm_hwmon_device_register_with_groups(&pdev->dev,
761
					"mlxreg_hotplug", priv, priv->groups);
762 763 764 765 766 767
	if (IS_ERR(priv->hwmon)) {
		dev_err(&pdev->dev, "Failed to register hwmon device %ld\n",
			PTR_ERR(priv->hwmon));
		return PTR_ERR(priv->hwmon);
	}

768 769 770 771
	/* Perform initial interrupts setup. */
	mlxreg_hotplug_set_irq(priv);
	priv->after_probe = true;

772 773 774
	return 0;
}

775
static void mlxreg_hotplug_remove(struct platform_device *pdev)
776
{
777
	struct mlxreg_hotplug_priv_data *priv = dev_get_drvdata(&pdev->dev);
778 779

	/* Clean interrupts setup. */
780
	mlxreg_hotplug_unset_irq(priv);
781
	devm_free_irq(&pdev->dev, priv->irq, priv);
782 783
}

784
static struct platform_driver mlxreg_hotplug_driver = {
785
	.driver = {
786
		.name = "mlxreg-hotplug",
787
	},
788
	.probe = mlxreg_hotplug_probe,
789
	.remove_new = mlxreg_hotplug_remove,
790 791
};

792
module_platform_driver(mlxreg_hotplug_driver);
793 794

MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
795
MODULE_DESCRIPTION("Mellanox regmap hotplug platform driver");
796
MODULE_LICENSE("Dual BSD/GPL");
797
MODULE_ALIAS("platform:mlxreg-hotplug");