drivers/input/misc/gpio-vibra.c

Source file repositories/reference/linux-study-clean/drivers/input/misc/gpio-vibra.c

File Facts

System
Linux kernel
Corpus path
drivers/input/misc/gpio-vibra.c
Extension
.c
Size
4880 bytes
Lines
200
Domain
Driver Families
Bucket
drivers/input
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

struct gpio_vibrator {
	struct input_dev *input;
	struct gpio_desc *gpio;
	struct regulator *vcc;

	struct work_struct play_work;
	bool running;
	bool vcc_on;
};

static int gpio_vibrator_start(struct gpio_vibrator *vibrator)
{
	struct device *pdev = vibrator->input->dev.parent;
	int err;

	if (!vibrator->vcc_on) {
		err = regulator_enable(vibrator->vcc);
		if (err) {
			dev_err(pdev, "failed to enable regulator: %d\n", err);
			return err;
		}
		vibrator->vcc_on = true;
	}

	gpiod_set_value_cansleep(vibrator->gpio, 1);

	return 0;
}

static void gpio_vibrator_stop(struct gpio_vibrator *vibrator)
{
	gpiod_set_value_cansleep(vibrator->gpio, 0);

	if (vibrator->vcc_on) {
		regulator_disable(vibrator->vcc);
		vibrator->vcc_on = false;
	}
}

static void gpio_vibrator_play_work(struct work_struct *work)
{
	struct gpio_vibrator *vibrator =
		container_of(work, struct gpio_vibrator, play_work);

	if (vibrator->running)
		gpio_vibrator_start(vibrator);
	else
		gpio_vibrator_stop(vibrator);
}

static int gpio_vibrator_play_effect(struct input_dev *dev, void *data,
				     struct ff_effect *effect)
{
	struct gpio_vibrator *vibrator = input_get_drvdata(dev);
	int level;

	level = effect->u.rumble.strong_magnitude;
	if (!level)
		level = effect->u.rumble.weak_magnitude;

	vibrator->running = level;
	schedule_work(&vibrator->play_work);

	return 0;
}

static void gpio_vibrator_close(struct input_dev *input)
{
	struct gpio_vibrator *vibrator = input_get_drvdata(input);

	cancel_work_sync(&vibrator->play_work);
	gpio_vibrator_stop(vibrator);
	vibrator->running = false;
}

static int gpio_vibrator_probe(struct platform_device *pdev)
{
	struct gpio_vibrator *vibrator;
	int err;

	vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL);
	if (!vibrator)
		return -ENOMEM;

	vibrator->input = devm_input_allocate_device(&pdev->dev);
	if (!vibrator->input)
		return -ENOMEM;

	vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc");
	if (IS_ERR(vibrator->vcc))

Annotation

Implementation Notes