You are an experienced linux developer that always uses best coding practices. Your task…
- User
You are an experienced linux developer that always uses best coding practices. Your task is porting an android driver to mainline linux, it often has deprecated/unavailable functions which you'll need to fix
Error: error: implicit declaration of function 'of_get_named_gpio_flags'; did you mean 'of_get_named_gpio'? [-Wimplicit-function-declaration] 1283 | pdata->reset_gpio = of_get_named_gpio_flags(np, "focaltech,reset-gpio", | ^~~~~~~~~~~~~~~~~~~~~~~ | of_get_named_gpio Affected code: static int fts_irq_registration(struct fts_ts_data *ts_data) { int ret = 0; struct fts_ts_platform_data pdata = ts_data->pdata; ts_data->irq = gpio_to_irq(pdata->irq_gpio); pdata->irq_gpio_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT; FTS_INFO("irq:%d, flag:%x", ts_data->irq, pdata->irq_gpio_flags); ret = request_threaded_irq(ts_data->irq, NULL, fts_irq_handler, pdata->irq_gpio_flags, FTS_DRIVER_NAME, ts_data); return ret; } / reset, irq gpio info */ pdata->reset_gpio = of_get_named_gpio_flags(np, "focaltech,reset-gpio", 0, &pdata->reset_gpio_flags); if (pdata->reset_gpio < 0) FTS_ERROR("Unable to get reset_gpio"); pdata->irq_gpio = of_get_named_gpio_flags(np, "focaltech,irq-gpio", 0, &pdata->irq_gpio_flags); if (pdata->irq_gpio < 0) FTS_ERROR("Unable to get irq_gpio");
- Assistant
To port your Android driver to mainline Linux and resolve the compilation error regarding the deprecated
of_get_named_gpio_flags, it's recommended to transition to the modern GPIO Descriptor (gpiod) API. Theof_get_named_gpio_flagsfunction has been deprecated in favor of thegpiodAPI, which provides a more flexible and robust framework for GPIO management in the Linux kernel.Here's a step-by-step guide to refactor your code accordingly:
1. Update Your Data Structures
Modify your
fts_ts_platform_datastructure to usestruct gpio_desc *instead of plain GPIO numbers. This allows you to use thegpiodAPI effectively.struct fts_ts_platform_data { struct gpio_desc *reset_gpio_desc; unsigned int reset_gpio_flags; struct gpio_desc *irq_gpio_desc; unsigned int irq_gpio_flags; // ... other members ... };2. Refactor GPIO Acquisition
Replace the deprecated
of_get_named_gpio_flagswithdevm_gpiod_get_flags_from_infoorgpiod_get_flags_from_of_nodedepending on your context. Here's how you can modify the GPIO acquisition:#include <linux/gpio/consumer.h> #include <linux/of_gpio.h> static int fts_parse_dt(struct device *dev, struct device_node *np, struct fts_ts_platform_data *pdata) { int ret; pdata->reset_gpio_desc = devm_gpiod_get_flags_from_of_node(dev, np, "focaltech,reset-gpio", 0, &pdata->reset_gpio_flags); if (IS_ERR(pdata->reset_gpio_desc)) { dev_err(dev, "Unable to get reset_gpio\n"); return PTR_ERR(pdata->reset_gpio_desc); } pdata->irq_gpio_desc = devm_gpiod_get_flags_from_of_node(dev, np, "focaltech,irq-gpio", 0, &pdata->irq_gpio_flags); if (IS_ERR(pdata->irq_gpio_desc)) { dev_err(dev, "Unable to get irq_gpio\n"); return PTR_ERR(pdata->irq_gpio_desc); } return 0; }3. Update IRQ Registration Function
Adjust the IRQ registration to use the GPIO descriptors obtained via the
gpiodAPI.static int fts_irq_registration(struct fts_ts_data *ts_data) { int ret; struct fts_ts_platform_data *pdata = ts_data->pdata; ts_data->irq = gpiod_to_irq(pdata->irq_gpio_desc); if (ts_data->irq < 0) { FTS_ERROR("Unable to convert irq GPIO to IRQ number"); return ts_data->irq; } pdata->irq_gpio_flags |= IRQF_TRIGGER_FALLING | IRQF_ONESHOT; FTS_INFO("irq:%d, flag:%x", ts_data->irq, pdata->irq_gpio_flags); ret = request_threaded_irq(ts_data->irq, NULL, fts_irq_handler, pdata->irq_gpio_flags, FTS_DRIVER_NAME, ts_data); if (ret) { FTS_ERROR("Failed to request threaded IRQ"); } return ret; }4. Handle GPIO Usage in the Driver
When you need to use the GPIO (e.g., setting it high or low), use the
gpiodfunctions:#include <linux/gpio/consumer.h> static void fts_reset_device(struct fts_ts_data *ts_data) { gpiod_set_value_cansleep(ts_data->pdata->reset_gpio_desc, 1); msleep(10); gpiod_set_value_cansleep(ts_data->pdata->reset_gpio_desc, 0); msleep(10); }5. Ensure Device Tree Compatibility
Make sure your Device Tree (DT) entries comply with the
gpiodexpectations. Typically, they should look like this:focaltech_device: focaltech@0 { compatible = "focaltech,driver"; focaltech,reset-gpio = <&gpio1 5 GPIO_ACTIVE_HIGH>; focaltech,irq-gpio = <&gpio1 6 GPIO_ACTIVE_LOW>; // ... other properties ... };6. Update Kbuild and Kconfig If Necessary
Ensure your
KbuildandKconfigfiles include the necessary dependencies for thegpiodAPI.# In your Kbuild file obj-$(CONFIG_FOCALTECH_DRIVER) += fts_driver.o# In your Kconfig file config FOCALTECH_DRIVER tristate "Focaltech Touchscreen Driver" depends on GPIO help Support for Focaltech touchscreen controller.7. Test the Changes
After refactoring, thoroughly test the driver to ensure that GPIOs are correctly managed and that the device functions as expected.
Summary of Changes
- Use
gpiodAPI: Transition fromof_get_named_gpio_flagstogpiodfunctions likedevm_gpiod_get_flags_from_of_nodeandgpiod_to_irq. - Update Data Structures: Use
struct gpio_desc *for GPIOs instead of plain integers. - Modify IRQ Handling: Convert GPIO descriptors to IRQ numbers using
gpiod_to_irq. - Adjust Device Tree: Ensure GPIO properties in the DT are compatible with the
gpiodAPI. - Include Proper Headers: Ensure you include
<linux/gpio/consumer.h>and<linux/of_gpio.h>for GPIO functions.
By following these steps, you align your driver with mainline Linux best practices, leveraging the robust and modern GPIO management provided by the
gpiodAPI.
Example Refactored Code Snippet:
#include <linux/gpio/consumer.h> #include <linux/of_gpio.h> #include <linux/interrupt.h> // Update your platform data structure struct fts_ts_platform_data { struct gpio_desc *reset_gpio_desc; unsigned int reset_gpio_flags; struct gpio_desc *irq_gpio_desc; unsigned int irq_gpio_flags; // ... other members ... }; static int fts_parse_dt(struct device *dev, struct device_node *np, struct fts_ts_platform_data *pdata) { pdata->reset_gpio_desc = devm_gpiod_get_flags_from_of_node(dev, np, "focaltech,reset-gpio", 0, &pdata->reset_gpio_flags); if (IS_ERR(pdata->reset_gpio_desc)) { dev_err(dev, "Unable to get reset_gpio\n"); return PTR_ERR(pdata->reset_gpio_desc); } pdata->irq_gpio_desc = devm_gpiod_get_flags_from_of_node(dev, np, "focaltech,irq-gpio", 0, &pdata->irq_gpio_flags); if (IS_ERR(pdata->irq_gpio_desc)) { dev_err(dev, "Unable to get irq_gpio\n"); return PTR_ERR(pdata->irq_gpio_desc); } return 0; } static int fts_irq_registration(struct fts_ts_data *ts_data) { int ret; struct fts_ts_platform_data *pdata = ts_data->pdata; ts_data->irq = gpiod_to_irq(pdata->irq_gpio_desc); if (ts_data->irq < 0) { FTS_ERROR("Unable to convert irq GPIO to IRQ number"); return ts_data->irq; } pdata->irq_gpio_flags |= IRQF_TRIGGER_FALLING | IRQF_ONESHOT; FTS_INFO("irq:%d, flag:%x", ts_data->irq, pdata->irq_gpio_flags); ret = request_threaded_irq(ts_data->irq, NULL, fts_irq_handler, pdata->irq_gpio_flags, FTS_DRIVER_NAME, ts_data); if (ret) { FTS_ERROR("Failed to request threaded IRQ"); } return ret; }By implementing these changes, you ensure that your driver adheres to mainline Linux standards, enhancing maintainability and compatibility with future kernel versions.
References:
Replace deprecated
of_get_named_gpio_flagscalls with the modern GPIO descriptor (gpiod) API. Modify your code to use functions likedevm_gpiod_get_flags_from_of_nodeto obtain GPIO descriptors and flags. Here's how you can update the affected code:#include <linux/gpio/consumer.h> // In your platform data structure struct fts_ts_platform_data { struct gpio_desc *reset_gpio_desc; unsigned int reset_gpio_flags; struct gpio_desc *irq_gpio_desc; unsigned int irq_gpio_flags; // ... other members ... }; // Update GPIO acquisition pdata->reset_gpio_desc = devm_gpiod_get_flags_from_of_node(dev, np, "focaltech,reset-gpio", 0, &pdata->reset_gpio_flags); if (IS_ERR(pdata->reset_gpio_desc)) { FTS_ERROR("Unable to get reset_gpio"); return PTR_ERR(pdata->reset_gpio_desc); } pdata->irq_gpio_desc = devm_gpiod_get_flags_from_of_node(dev, np, "focaltech,irq-gpio", 0, &pdata->irq_gpio_flags); if (IS_ERR(pdata->irq_gpio_desc)) { FTS_ERROR("Unable to get irq_gpio"); return PTR_ERR(pdata->irq_gpio_desc); } // Update IRQ registration ts_data->irq = gpiod_to_irq(pdata->irq_gpio_desc); if (ts_data->irq < 0) { FTS_ERROR("Unable to convert irq GPIO to IRQ number"); return ts_data->irq; } // Continue with request_threaded_irq as beforeThis approach uses the current GPIO API, ensuring compatibility with mainline Linux and adhering to best practices.
- Use