Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * w1-gpio - GPIO w1 bus master driver
0004  *
0005  * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <linux/slab.h>
0012 #include <linux/w1-gpio.h>
0013 #include <linux/gpio/consumer.h>
0014 #include <linux/of_platform.h>
0015 #include <linux/err.h>
0016 #include <linux/of.h>
0017 #include <linux/delay.h>
0018 
0019 #include <linux/w1.h>
0020 
0021 static u8 w1_gpio_set_pullup(void *data, int delay)
0022 {
0023     struct w1_gpio_platform_data *pdata = data;
0024 
0025     if (delay) {
0026         pdata->pullup_duration = delay;
0027     } else {
0028         if (pdata->pullup_duration) {
0029             /*
0030              * This will OVERRIDE open drain emulation and force-pull
0031              * the line high for some time.
0032              */
0033             gpiod_set_raw_value(pdata->gpiod, 1);
0034             msleep(pdata->pullup_duration);
0035             /*
0036              * This will simply set the line as input since we are doing
0037              * open drain emulation in the GPIO library.
0038              */
0039             gpiod_set_value(pdata->gpiod, 1);
0040         }
0041         pdata->pullup_duration = 0;
0042     }
0043 
0044     return 0;
0045 }
0046 
0047 static void w1_gpio_write_bit(void *data, u8 bit)
0048 {
0049     struct w1_gpio_platform_data *pdata = data;
0050 
0051     gpiod_set_value(pdata->gpiod, bit);
0052 }
0053 
0054 static u8 w1_gpio_read_bit(void *data)
0055 {
0056     struct w1_gpio_platform_data *pdata = data;
0057 
0058     return gpiod_get_value(pdata->gpiod) ? 1 : 0;
0059 }
0060 
0061 #if defined(CONFIG_OF)
0062 static const struct of_device_id w1_gpio_dt_ids[] = {
0063     { .compatible = "w1-gpio" },
0064     {}
0065 };
0066 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
0067 #endif
0068 
0069 static int w1_gpio_probe(struct platform_device *pdev)
0070 {
0071     struct w1_bus_master *master;
0072     struct w1_gpio_platform_data *pdata;
0073     struct device *dev = &pdev->dev;
0074     struct device_node *np = dev->of_node;
0075     /* Enforce open drain mode by default */
0076     enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
0077     int err;
0078 
0079     if (of_have_populated_dt()) {
0080         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0081         if (!pdata)
0082             return -ENOMEM;
0083 
0084         /*
0085          * This parameter means that something else than the gpiolib has
0086          * already set the line into open drain mode, so we should just
0087          * driver it high/low like we are in full control of the line and
0088          * open drain will happen transparently.
0089          */
0090         if (of_get_property(np, "linux,open-drain", NULL))
0091             gflags = GPIOD_OUT_LOW;
0092 
0093         pdev->dev.platform_data = pdata;
0094     }
0095     pdata = dev_get_platdata(dev);
0096 
0097     if (!pdata) {
0098         dev_err(dev, "No configuration data\n");
0099         return -ENXIO;
0100     }
0101 
0102     master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
0103             GFP_KERNEL);
0104     if (!master) {
0105         dev_err(dev, "Out of memory\n");
0106         return -ENOMEM;
0107     }
0108 
0109     pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
0110     if (IS_ERR(pdata->gpiod)) {
0111         dev_err(dev, "gpio_request (pin) failed\n");
0112         return PTR_ERR(pdata->gpiod);
0113     }
0114 
0115     pdata->pullup_gpiod =
0116         devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
0117     if (IS_ERR(pdata->pullup_gpiod)) {
0118         dev_err(dev, "gpio_request_one "
0119             "(ext_pullup_enable_pin) failed\n");
0120         return PTR_ERR(pdata->pullup_gpiod);
0121     }
0122 
0123     master->data = pdata;
0124     master->read_bit = w1_gpio_read_bit;
0125     gpiod_direction_output(pdata->gpiod, 1);
0126     master->write_bit = w1_gpio_write_bit;
0127 
0128     /*
0129      * If we are using open drain emulation from the GPIO library,
0130      * we need to use this pullup function that hammers the line
0131      * high using a raw accessor to provide pull-up for the w1
0132      * line.
0133      */
0134     if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
0135         master->set_pullup = w1_gpio_set_pullup;
0136 
0137     err = w1_add_master_device(master);
0138     if (err) {
0139         dev_err(dev, "w1_add_master device failed\n");
0140         return err;
0141     }
0142 
0143     if (pdata->enable_external_pullup)
0144         pdata->enable_external_pullup(1);
0145 
0146     if (pdata->pullup_gpiod)
0147         gpiod_set_value(pdata->pullup_gpiod, 1);
0148 
0149     platform_set_drvdata(pdev, master);
0150 
0151     return 0;
0152 }
0153 
0154 static int w1_gpio_remove(struct platform_device *pdev)
0155 {
0156     struct w1_bus_master *master = platform_get_drvdata(pdev);
0157     struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
0158 
0159     if (pdata->enable_external_pullup)
0160         pdata->enable_external_pullup(0);
0161 
0162     if (pdata->pullup_gpiod)
0163         gpiod_set_value(pdata->pullup_gpiod, 0);
0164 
0165     w1_remove_master_device(master);
0166 
0167     return 0;
0168 }
0169 
0170 static int __maybe_unused w1_gpio_suspend(struct device *dev)
0171 {
0172     struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
0173 
0174     if (pdata->enable_external_pullup)
0175         pdata->enable_external_pullup(0);
0176 
0177     return 0;
0178 }
0179 
0180 static int __maybe_unused w1_gpio_resume(struct device *dev)
0181 {
0182     struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
0183 
0184     if (pdata->enable_external_pullup)
0185         pdata->enable_external_pullup(1);
0186 
0187     return 0;
0188 }
0189 
0190 static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
0191 
0192 static struct platform_driver w1_gpio_driver = {
0193     .driver = {
0194         .name   = "w1-gpio",
0195         .pm = &w1_gpio_pm_ops,
0196         .of_match_table = of_match_ptr(w1_gpio_dt_ids),
0197     },
0198     .probe = w1_gpio_probe,
0199     .remove = w1_gpio_remove,
0200 };
0201 
0202 module_platform_driver(w1_gpio_driver);
0203 
0204 MODULE_DESCRIPTION("GPIO w1 bus master driver");
0205 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
0206 MODULE_LICENSE("GPL");