summaryrefslogtreecommitdiff
path: root/include/linux/i2c-of-prober.h
blob: df95aa6ad90ef65d68162e4a4df0793769508671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Definitions for the Linux I2C OF component prober
 *
 * Copyright (C) 2024 Google LLC
 */

#ifndef _LINUX_I2C_OF_PROBER_H
#define _LINUX_I2C_OF_PROBER_H

#include <linux/kconfig.h>

struct device;
struct device_node;

/**
 * struct i2c_of_probe_ops - I2C OF component prober callbacks
 *
 * A set of callbacks to be used by i2c_of_probe_component().
 *
 * All callbacks are optional. Callbacks are called only once per run, and are
 * used in the order they are defined in this structure.
 *
 * All callbacks that have return values shall return %0 on success,
 * or a negative error number on failure.
 *
 * The @dev parameter passed to the callbacks is the same as @dev passed to
 * i2c_of_probe_component(). It should only be used for dev_printk() calls
 * and nothing else, especially not managed device resource (devres) APIs.
 */
struct i2c_of_probe_ops {
	/**
	 * @enable: Retrieve and enable resources so that the components respond to probes.
	 *
	 * It is OK for this callback to return -EPROBE_DEFER since the intended use includes
	 * retrieving resources and enables them. Resources should be reverted to their initial
	 * state and released before returning if this fails.
	 */
	int (*enable)(struct device *dev, struct device_node *bus_node, void *data);

	/**
	 * @cleanup_early: Release exclusive resources prior to calling probe() on a
	 *		   detected component.
	 *
	 * Only called if a matching component is actually found. If none are found,
	 * resources that would have been released in this callback should be released in
	 * @free_resourcs_late instead.
	 */
	void (*cleanup_early)(struct device *dev, void *data);

	/**
	 * @cleanup: Opposite of @enable to balance refcounts and free resources after probing.
	 *
	 * Should check if resources were already freed by @cleanup_early.
	 */
	void (*cleanup)(struct device *dev, void *data);
};

/**
 * struct i2c_of_probe_cfg - I2C OF component prober configuration
 * @ops: Callbacks for the prober to use.
 * @type: A string to match the device node name prefix to probe for.
 */
struct i2c_of_probe_cfg {
	const struct i2c_of_probe_ops *ops;
	const char *type;
};

#if IS_ENABLED(CONFIG_OF_DYNAMIC)

int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cfg, void *ctx);

/**
 * DOC: I2C OF component prober simple helpers
 *
 * Components such as trackpads are commonly connected to a devices baseboard
 * with a 6-pin ribbon cable. That gives at most one voltage supply and one
 * GPIO (commonly a "enable" or "reset" line) besides the I2C bus, interrupt
 * pin, and common ground. Touchscreens, while integrated into the display
 * panel's connection, typically have the same set of connections.
 *
 * A simple set of helpers are provided here for use with the I2C OF component
 * prober. This implementation targets such components, allowing for at most
 * one regulator supply.
 *
 * The following helpers are provided:
 * * i2c_of_probe_simple_enable()
 * * i2c_of_probe_simple_cleanup()
 */

/**
 * struct i2c_of_probe_simple_opts - Options for simple I2C component prober callbacks
 * @res_node_compatible: Compatible string of device node to retrieve resources from.
 * @supply_name: Name of regulator supply.
 * @post_power_on_delay_ms: Delay after regulators are powered on. Passed to msleep().
 */
struct i2c_of_probe_simple_opts {
	const char *res_node_compatible;
	const char *supply_name;
	unsigned int post_power_on_delay_ms;
};

struct regulator;

struct i2c_of_probe_simple_ctx {
	/* public: provided by user before helpers are used. */
	const struct i2c_of_probe_simple_opts *opts;
	/* private: internal fields for helpers. */
	struct regulator *supply;
};

int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data);
void i2c_of_probe_simple_cleanup(struct device *dev, void *data);

extern struct i2c_of_probe_ops i2c_of_probe_simple_ops;

#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */

#endif /* _LINUX_I2C_OF_PROBER_H */