Skip to content

Commit 39b415f

Browse files
wensWolfram Sang
authored andcommitted
i2c: of-prober: Add GPIO support to simple helpers
Add GPIO support to the simple helpers for the I2C OF component prober. Components that the prober intends to probe likely require their regulator supplies be enabled, and GPIOs be toggled to enable them or bring them out of reset before they will respond to probe attempts. Regulator supplies were handled in the previous patch. The assumption is that the same class of components to be probed are always connected in the same fashion with the same regulator supply and GPIO. The names may vary due to binding differences, but the physical layout does not change. This supports at most one GPIO pin. The user must specify the GPIO name, the polarity, and the amount of time to wait after the GPIO is toggled. Devices with more than one GPIO pin likely require specific power sequencing beyond what generic code can easily support. Signed-off-by: Chen-Yu Tsai <[email protected]> Reviewed-by: Douglas Anderson <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Tested-by: Andrey Skvortsov <[email protected]> Reviewed-by: AngeloGioacchino Del Regno <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 8972611 commit 39b415f

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

drivers/i2c/i2c-core-of-prober.c

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/device.h>
1111
#include <linux/dev_printk.h>
1212
#include <linux/err.h>
13+
#include <linux/gpio/consumer.h>
1314
#include <linux/i2c.h>
1415
#include <linux/i2c-of-prober.h>
1516
#include <linux/module.h>
@@ -31,7 +32,6 @@
3132
* address responds.
3233
*
3334
* TODO:
34-
* - Support handling common GPIOs.
3535
* - Support I2C muxes
3636
*/
3737

@@ -246,14 +246,74 @@ static void i2c_of_probe_simple_disable_regulator(struct device *dev, struct i2c
246246
regulator_disable(ctx->supply);
247247
}
248248

249+
static int i2c_of_probe_simple_get_gpiod(struct device *dev, struct device_node *node,
250+
struct i2c_of_probe_simple_ctx *ctx)
251+
{
252+
struct fwnode_handle *fwnode = of_fwnode_handle(node);
253+
struct gpio_desc *gpiod;
254+
const char *con_id;
255+
256+
/* NULL signals no GPIO needed */
257+
if (!ctx->opts->gpio_name)
258+
return 0;
259+
260+
/* An empty string signals an unnamed GPIO */
261+
if (!ctx->opts->gpio_name[0])
262+
con_id = NULL;
263+
else
264+
con_id = ctx->opts->gpio_name;
265+
266+
gpiod = fwnode_gpiod_get_index(fwnode, con_id, 0, GPIOD_ASIS, "i2c-of-prober");
267+
if (IS_ERR(gpiod))
268+
return PTR_ERR(gpiod);
269+
270+
ctx->gpiod = gpiod;
271+
272+
return 0;
273+
}
274+
275+
static void i2c_of_probe_simple_put_gpiod(struct i2c_of_probe_simple_ctx *ctx)
276+
{
277+
gpiod_put(ctx->gpiod);
278+
ctx->gpiod = NULL;
279+
}
280+
281+
static int i2c_of_probe_simple_set_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
282+
{
283+
int ret;
284+
285+
if (!ctx->gpiod)
286+
return 0;
287+
288+
dev_dbg(dev, "Configuring GPIO\n");
289+
290+
ret = gpiod_direction_output(ctx->gpiod, ctx->opts->gpio_assert_to_enable);
291+
if (ret)
292+
return ret;
293+
294+
if (ctx->opts->post_gpio_config_delay_ms)
295+
msleep(ctx->opts->post_gpio_config_delay_ms);
296+
297+
return 0;
298+
}
299+
300+
static void i2c_of_probe_simple_disable_gpio(struct device *dev, struct i2c_of_probe_simple_ctx *ctx)
301+
{
302+
gpiod_set_value(ctx->gpiod, !ctx->opts->gpio_assert_to_enable);
303+
}
304+
249305
/**
250306
* i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources
251307
* @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
252308
* @bus_node: Pointer to the &struct device_node of the I2C adapter.
253309
* @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
254310
*
255311
* If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply.
312+
* If &i2c_of_probe_simple_opts->gpio_name is given, request the named GPIO. Or if it is
313+
* the empty string, request the unnamed GPIO.
256314
* If a regulator supply was found, enable that regulator.
315+
* If a GPIO line was found, configure the GPIO line to output and set value
316+
* according to given options.
257317
*
258318
* Return: %0 on success or no-op, or a negative error number on failure.
259319
*/
@@ -282,12 +342,24 @@ int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node,
282342
if (ret)
283343
goto out_put_node;
284344

285-
ret = i2c_of_probe_simple_enable_regulator(dev, ctx);
345+
ret = i2c_of_probe_simple_get_gpiod(dev, node, ctx);
286346
if (ret)
287347
goto out_put_supply;
288348

349+
ret = i2c_of_probe_simple_enable_regulator(dev, ctx);
350+
if (ret)
351+
goto out_put_gpiod;
352+
353+
ret = i2c_of_probe_simple_set_gpio(dev, ctx);
354+
if (ret)
355+
goto out_disable_regulator;
356+
289357
return 0;
290358

359+
out_disable_regulator:
360+
i2c_of_probe_simple_disable_regulator(dev, ctx);
361+
out_put_gpiod:
362+
i2c_of_probe_simple_put_gpiod(ctx);
291363
out_put_supply:
292364
i2c_of_probe_simple_put_supply(ctx);
293365
out_put_node:
@@ -296,24 +368,48 @@ int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node,
296368
}
297369
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER);
298370

371+
/**
372+
* i2c_of_probe_simple_cleanup_early - \
373+
* Simple helper for I2C OF prober to release GPIOs before component is enabled
374+
* @dev: Pointer to the &struct device of the caller; unused.
375+
* @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
376+
*
377+
* GPIO descriptors are exclusive and have to be released before the
378+
* actual driver probes so that the latter can acquire them.
379+
*/
380+
void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data)
381+
{
382+
struct i2c_of_probe_simple_ctx *ctx = data;
383+
384+
i2c_of_probe_simple_put_gpiod(ctx);
385+
}
386+
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup_early, I2C_OF_PROBER);
387+
299388
/**
300389
* i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers
301390
* @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages
302391
* @data: Pointer to &struct i2c_of_probe_simple_ctx helper context.
303392
*
393+
* * If a GPIO line was found and not yet released, set its value to the opposite of that
394+
* set in i2c_of_probe_simple_enable() and release it.
304395
* * If a regulator supply was found, disable that regulator and release it.
305396
*/
306397
void i2c_of_probe_simple_cleanup(struct device *dev, void *data)
307398
{
308399
struct i2c_of_probe_simple_ctx *ctx = data;
309400

401+
/* GPIO operations here are no-ops if i2c_of_probe_simple_cleanup_early was called. */
402+
i2c_of_probe_simple_disable_gpio(dev, ctx);
403+
i2c_of_probe_simple_put_gpiod(ctx);
404+
310405
i2c_of_probe_simple_disable_regulator(dev, ctx);
311406
i2c_of_probe_simple_put_supply(ctx);
312407
}
313408
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup, I2C_OF_PROBER);
314409

315410
struct i2c_of_probe_ops i2c_of_probe_simple_ops = {
316411
.enable = i2c_of_probe_simple_enable,
412+
.cleanup_early = i2c_of_probe_simple_cleanup_early,
317413
.cleanup = i2c_of_probe_simple_cleanup,
318414
};
319415
EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER);

include/linux/i2c-of-prober.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define _LINUX_I2C_OF_PROBER_H
1010

1111
#include <linux/kconfig.h>
12+
#include <linux/types.h>
1213

1314
struct device;
1415
struct device_node;
@@ -85,31 +86,51 @@ int i2c_of_probe_component(struct device *dev, const struct i2c_of_probe_cfg *cf
8586
*
8687
* The following helpers are provided:
8788
* * i2c_of_probe_simple_enable()
89+
* * i2c_of_probe_simple_cleanup_early()
8890
* * i2c_of_probe_simple_cleanup()
8991
*/
9092

9193
/**
9294
* struct i2c_of_probe_simple_opts - Options for simple I2C component prober callbacks
9395
* @res_node_compatible: Compatible string of device node to retrieve resources from.
9496
* @supply_name: Name of regulator supply.
97+
* @gpio_name: Name of GPIO. NULL if no GPIO line is used. Empty string ("") if GPIO
98+
* line is unnamed.
9599
* @post_power_on_delay_ms: Delay after regulators are powered on. Passed to msleep().
100+
* @post_gpio_config_delay_ms: Delay after GPIO is configured. Passed to msleep().
101+
* @gpio_assert_to_enable: %true if GPIO should be asserted, i.e. set to logical high,
102+
* to enable the component.
103+
*
104+
* This describes power sequences common for the class of components supported by the
105+
* simple component prober:
106+
* * @gpio_name is configured to the non-active setting according to @gpio_assert_to_enable.
107+
* * @supply_name regulator supply is enabled.
108+
* * Wait for @post_power_on_delay_ms to pass.
109+
* * @gpio_name is configured to the active setting according to @gpio_assert_to_enable.
110+
* * Wait for @post_gpio_config_delay_ms to pass.
96111
*/
97112
struct i2c_of_probe_simple_opts {
98113
const char *res_node_compatible;
99114
const char *supply_name;
115+
const char *gpio_name;
100116
unsigned int post_power_on_delay_ms;
117+
unsigned int post_gpio_config_delay_ms;
118+
bool gpio_assert_to_enable;
101119
};
102120

121+
struct gpio_desc;
103122
struct regulator;
104123

105124
struct i2c_of_probe_simple_ctx {
106125
/* public: provided by user before helpers are used. */
107126
const struct i2c_of_probe_simple_opts *opts;
108127
/* private: internal fields for helpers. */
109128
struct regulator *supply;
129+
struct gpio_desc *gpiod;
110130
};
111131

112132
int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data);
133+
void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data);
113134
void i2c_of_probe_simple_cleanup(struct device *dev, void *data);
114135

115136
extern struct i2c_of_probe_ops i2c_of_probe_simple_ops;

0 commit comments

Comments
 (0)