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

Changed files
+119 -2
drivers
include
+98 -2
drivers/i2c/i2c-core-of-prober.c
··· 10 10 #include <linux/device.h> 11 11 #include <linux/dev_printk.h> 12 12 #include <linux/err.h> 13 + #include <linux/gpio/consumer.h> 13 14 #include <linux/i2c.h> 14 15 #include <linux/i2c-of-prober.h> 15 16 #include <linux/module.h> ··· 32 31 * address responds. 33 32 * 34 33 * TODO: 35 - * - Support handling common GPIOs. 36 34 * - Support I2C muxes 37 35 */ 38 36 ··· 246 246 regulator_disable(ctx->supply); 247 247 } 248 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 + 249 305 /** 250 306 * i2c_of_probe_simple_enable - Simple helper for I2C OF prober to get and enable resources 251 307 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages ··· 309 253 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 310 254 * 311 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. 312 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. 313 261 * 314 262 * Return: %0 on success or no-op, or a negative error number on failure. 315 263 */ ··· 342 282 if (ret) 343 283 goto out_put_node; 344 284 345 - ret = i2c_of_probe_simple_enable_regulator(dev, ctx); 285 + ret = i2c_of_probe_simple_get_gpiod(dev, node, ctx); 346 286 if (ret) 347 287 goto out_put_supply; 348 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 + 349 297 return 0; 350 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); 351 303 out_put_supply: 352 304 i2c_of_probe_simple_put_supply(ctx); 353 305 out_put_node: ··· 369 297 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_enable, I2C_OF_PROBER); 370 298 371 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 + /** 372 317 * i2c_of_probe_simple_cleanup - Clean up and release resources for I2C OF prober simple helpers 373 318 * @dev: Pointer to the &struct device of the caller, only used for dev_printk() messages 374 319 * @data: Pointer to &struct i2c_of_probe_simple_ctx helper context. 375 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. 376 323 * * If a regulator supply was found, disable that regulator and release it. 377 324 */ 378 325 void i2c_of_probe_simple_cleanup(struct device *dev, void *data) 379 326 { 380 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); 381 332 382 333 i2c_of_probe_simple_disable_regulator(dev, ctx); 383 334 i2c_of_probe_simple_put_supply(ctx); ··· 409 314 410 315 struct i2c_of_probe_ops i2c_of_probe_simple_ops = { 411 316 .enable = i2c_of_probe_simple_enable, 317 + .cleanup_early = i2c_of_probe_simple_cleanup_early, 412 318 .cleanup = i2c_of_probe_simple_cleanup, 413 319 }; 414 320 EXPORT_SYMBOL_NS_GPL(i2c_of_probe_simple_ops, I2C_OF_PROBER);
+21
include/linux/i2c-of-prober.h
··· 9 9 #define _LINUX_I2C_OF_PROBER_H 10 10 11 11 #include <linux/kconfig.h> 12 + #include <linux/types.h> 12 13 13 14 struct device; 14 15 struct device_node; ··· 86 85 * 87 86 * The following helpers are provided: 88 87 * * i2c_of_probe_simple_enable() 88 + * * i2c_of_probe_simple_cleanup_early() 89 89 * * i2c_of_probe_simple_cleanup() 90 90 */ 91 91 ··· 94 92 * struct i2c_of_probe_simple_opts - Options for simple I2C component prober callbacks 95 93 * @res_node_compatible: Compatible string of device node to retrieve resources from. 96 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 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. 98 109 */ 99 110 struct i2c_of_probe_simple_opts { 100 111 const char *res_node_compatible; 101 112 const char *supply_name; 113 + const char *gpio_name; 102 114 unsigned int post_power_on_delay_ms; 115 + unsigned int post_gpio_config_delay_ms; 116 + bool gpio_assert_to_enable; 103 117 }; 104 118 119 + struct gpio_desc; 105 120 struct regulator; 106 121 107 122 struct i2c_of_probe_simple_ctx { ··· 126 107 const struct i2c_of_probe_simple_opts *opts; 127 108 /* private: internal fields for helpers. */ 128 109 struct regulator *supply; 110 + struct gpio_desc *gpiod; 129 111 }; 130 112 131 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); 132 115 void i2c_of_probe_simple_cleanup(struct device *dev, void *data); 133 116 134 117 extern struct i2c_of_probe_ops i2c_of_probe_simple_ops;