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 <wenst@chromium.org>
Reviewed-by: Douglas Anderson <dianders@chromium.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Andrey Skvortsov <andrej.skvortzov@gmail.com>
Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

authored by Chen-Yu Tsai and committed by Wolfram Sang 39b415f8 89726114

+119 -2
+98 -2
drivers/i2c/i2c-core-of-prober.c
··· 10 #include <linux/device.h> 11 #include <linux/dev_printk.h> 12 #include <linux/err.h> 13 #include <linux/i2c.h> 14 #include <linux/i2c-of-prober.h> 15 #include <linux/module.h> ··· 32 * address responds. 33 * 34 * TODO: 35 - * - Support handling common GPIOs. 36 * - Support I2C muxes 37 */ 38 ··· 246 regulator_disable(ctx->supply); 247 } 248 249 /** 250 * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources 251 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages ··· 309 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 310 * 311 * If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply. 312 * If a regulator supply was found, enable that regulator. 313 * 314 * Return: %0 on success or no-op, or a negative error number on failure. 315 */ ··· 342 if (ret) 343 goto out_put_node; 344 345 - ret = i2c_of_probe_simple_enable_regulator(dev, ctx); 346 if (ret) 347 goto out_put_supply; 348 349 return 0; 350 351 out_put_supply: 352 i2c_of_probe_simple_put_supply(ctx); 353 out_put_node: ··· 369 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); 370 371 /** 372 * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers 373 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages 374 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 375 * 376 * * If a regulator supply was found, disable that regulator and release it. 377 */ 378 void i2c_of_probe_simple_cleanup(struct device *dev, void *data) 379 { 380 struct i2c_of_probe_simple_ctx *ctx = data; 381 382 i2c_of_probe_simple_disable_regulator(dev, ctx); 383 i2c_of_probe_simple_put_supply(ctx); ··· 409 410 struct i2c_of_probe_ops i2c_of_probe_simple_ops = { 411 .enable = i2c_of_probe_simple_enable, 412 .cleanup = i2c_of_probe_simple_cleanup, 413 }; 414 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER);
··· 10 #include <linux/device.h> 11 #include <linux/dev_printk.h> 12 #include <linux/err.h> 13 + #include <linux/gpio/consumer.h> 14 #include <linux/i2c.h> 15 #include <linux/i2c-of-prober.h> 16 #include <linux/module.h> ··· 31 * address responds. 32 * 33 * TODO: 34 * - Support I2C muxes 35 */ 36 ··· 246 regulator_disable(ctx->supply); 247 } 248 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 + 305 /** 306 * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources 307 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages ··· 253 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 254 * 255 * If &i2c_of_probe_simple_opts->supply_name is given, request the named regulator supply. 256 + * If &i2c_of_probe_simple_opts->gpio_name is given, request the named GPIO. Or if it is 257 + * the empty string, request the unnamed GPIO. 258 * If a regulator supply was found, enable that regulator. 259 + * If a GPIO line was found, configure the GPIO line to output and set value 260 + * according to given options. 261 * 262 * Return: %0 on success or no-op, or a negative error number on failure. 263 */ ··· 282 if (ret) 283 goto out_put_node; 284 285 + ret = i2c_of_probe_simple_get_gpiod(dev, node, ctx); 286 if (ret) 287 goto out_put_supply; 288 289 + ret = i2c_of_probe_simple_enable_regulator(dev, ctx); 290 + if (ret) 291 + goto out_put_gpiod; 292 + 293 + ret = i2c_of_probe_simple_set_gpio(dev, ctx); 294 + if (ret) 295 + goto out_disable_regulator; 296 + 297 return 0; 298 299 + out_disable_regulator: 300 + i2c_of_probe_simple_disable_regulator(dev, ctx); 301 + out_put_gpiod: 302 + i2c_of_probe_simple_put_gpiod(ctx); 303 out_put_supply: 304 i2c_of_probe_simple_put_supply(ctx); 305 out_put_node: ··· 297 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); 298 299 /** 300 + * i2c_of_probe_simple_cleanup_early - \ 301 + * Simple helper for I2C OF prober to release GPIOs before component is enabled 302 + * @dev: Pointer to the &struct device of the caller; unused. 303 + * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 304 + * 305 + * GPIO descriptors are exclusive and have to be released before the 306 + * actual driver probes so that the latter can acquire them. 307 + */ 308 + void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data) 309 + { 310 + struct i2c_of_probe_simple_ctx *ctx = data; 311 + 312 + i2c_of_probe_simple_put_gpiod(ctx); 313 + } 314 + EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_cleanup_early, I2C_OF_PROBER); 315 + 316 + /** 317 * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers 318 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages 319 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 320 * 321 + * * If a GPIO line was found and not yet released, set its value to the opposite of that 322 + * set in i2c_of_probe_simple_enable() and release it. 323 * * If a regulator supply was found, disable that regulator and release it. 324 */ 325 void i2c_of_probe_simple_cleanup(struct device *dev, void *data) 326 { 327 struct i2c_of_probe_simple_ctx *ctx = data; 328 + 329 + /* GPIO operations here are no-ops if i2c_of_probe_simple_cleanup_early was called. */ 330 + i2c_of_probe_simple_disable_gpio(dev, ctx); 331 + i2c_of_probe_simple_put_gpiod(ctx); 332 333 i2c_of_probe_simple_disable_regulator(dev, ctx); 334 i2c_of_probe_simple_put_supply(ctx); ··· 314 315 struct i2c_of_probe_ops i2c_of_probe_simple_ops = { 316 .enable = i2c_of_probe_simple_enable, 317 + .cleanup_early = i2c_of_probe_simple_cleanup_early, 318 .cleanup = i2c_of_probe_simple_cleanup, 319 }; 320 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER);
+21
include/linux/i2c-of-prober.h
··· 9 #define _LINUX_I2C_OF_PROBER_H 10 11 #include <linux/kconfig.h> 12 13 struct device; 14 struct device_node; ··· 86 * 87 * The following helpers are provided: 88 * * i2c_of_probe_simple_enable() 89 * * i2c_of_probe_simple_cleanup() 90 */ 91 ··· 94 * struct i2c_of_probe_simple_opts - Options for simple I2C component prober callbacks 95 * @res_node_compatible: Compatible string of device node to retrieve resources from. 96 * @supply_name: Name of regulator supply. 97 * @post_power_on_delay_ms: Delay after regulators are powered on. Passed to msleep(). 98 */ 99 struct i2c_of_probe_simple_opts { 100 const char *res_node_compatible; 101 const char *supply_name; 102 unsigned int post_power_on_delay_ms; 103 }; 104 105 struct regulator; 106 107 struct i2c_of_probe_simple_ctx { ··· 126 const struct i2c_of_probe_simple_opts *opts; 127 /* private: internal fields for helpers. */ 128 struct regulator *supply; 129 }; 130 131 int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data); 132 void i2c_of_probe_simple_cleanup(struct device *dev, void *data); 133 134 extern struct i2c_of_probe_ops i2c_of_probe_simple_ops;
··· 9 #define _LINUX_I2C_OF_PROBER_H 10 11 #include <linux/kconfig.h> 12 + #include <linux/types.h> 13 14 struct device; 15 struct device_node; ··· 85 * 86 * The following helpers are provided: 87 * * i2c_of_probe_simple_enable() 88 + * * i2c_of_probe_simple_cleanup_early() 89 * * i2c_of_probe_simple_cleanup() 90 */ 91 ··· 92 * struct i2c_of_probe_simple_opts - Options for simple I2C component prober callbacks 93 * @res_node_compatible: Compatible string of device node to retrieve resources from. 94 * @supply_name: Name of regulator supply. 95 + * @gpio_name: Name of GPIO. NULL if no GPIO line is used. Empty string ("") if GPIO 96 + * line is unnamed. 97 * @post_power_on_delay_ms: Delay after regulators are powered on. Passed to msleep(). 98 + * @post_gpio_config_delay_ms: Delay after GPIO is configured. Passed to msleep(). 99 + * @gpio_assert_to_enable: %true if GPIO should be asserted, i.e. set to logical high, 100 + * to enable the component. 101 + * 102 + * This describes power sequences common for the class of components supported by the 103 + * simple component prober: 104 + * * @gpio_name is configured to the non-active setting according to @gpio_assert_to_enable. 105 + * * @supply_name regulator supply is enabled. 106 + * * Wait for @post_power_on_delay_ms to pass. 107 + * * @gpio_name is configured to the active setting according to @gpio_assert_to_enable. 108 + * * Wait for @post_gpio_config_delay_ms to pass. 109 */ 110 struct i2c_of_probe_simple_opts { 111 const char *res_node_compatible; 112 const char *supply_name; 113 + const char *gpio_name; 114 unsigned int post_power_on_delay_ms; 115 + unsigned int post_gpio_config_delay_ms; 116 + bool gpio_assert_to_enable; 117 }; 118 119 + struct gpio_desc; 120 struct regulator; 121 122 struct i2c_of_probe_simple_ctx { ··· 107 const struct i2c_of_probe_simple_opts *opts; 108 /* private: internal fields for helpers. */ 109 struct regulator *supply; 110 + struct gpio_desc *gpiod; 111 }; 112 113 int i2c_of_probe_simple_enable(struct device *dev, struct device_node *bus_node, void *data); 114 + void i2c_of_probe_simple_cleanup_early(struct device *dev, void *data); 115 void i2c_of_probe_simple_cleanup(struct device *dev, void *data); 116 117 extern struct i2c_of_probe_ops i2c_of_probe_simple_ops;