1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * w1-gpio - GPIO w1 bus master driver 4 * 5 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi> 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/mod_devicetable.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <linux/property.h> 16 #include <linux/types.h> 17 18 #include <linux/w1.h> 19 20 struct w1_gpio_ddata { 21 struct gpio_desc *gpiod; 22 struct gpio_desc *pullup_gpiod; 23 unsigned int pullup_duration; 24 }; 25 26 static u8 w1_gpio_set_pullup(void *data, int delay) 27 { 28 struct w1_gpio_ddata *ddata = data; 29 30 if (delay) { 31 ddata->pullup_duration = delay; 32 } else { 33 if (ddata->pullup_duration) { 34 /* 35 * This will OVERRIDE open drain emulation and force-pull 36 * the line high for some time. 37 */ 38 gpiod_set_raw_value(ddata->gpiod, 1); 39 msleep(ddata->pullup_duration); 40 /* 41 * This will simply set the line as input since we are doing 42 * open drain emulation in the GPIO library. 43 */ 44 gpiod_set_value(ddata->gpiod, 1); 45 } 46 ddata->pullup_duration = 0; 47 } 48 49 return 0; 50 } 51 52 static void w1_gpio_write_bit(void *data, u8 bit) 53 { 54 struct w1_gpio_ddata *ddata = data; 55 56 gpiod_set_value(ddata->gpiod, bit); 57 } 58 59 static u8 w1_gpio_read_bit(void *data) 60 { 61 struct w1_gpio_ddata *ddata = data; 62 63 return gpiod_get_value(ddata->gpiod) ? 1 : 0; 64 } 65 66 static int w1_gpio_probe(struct platform_device *pdev) 67 { 68 struct w1_bus_master *master; 69 struct w1_gpio_ddata *ddata; 70 struct device *dev = &pdev->dev; 71 /* Enforce open drain mode by default */ 72 enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN; 73 int err; 74 75 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 76 if (!ddata) 77 return -ENOMEM; 78 79 /* 80 * This parameter means that something else than the gpiolib has 81 * already set the line into open drain mode, so we should just 82 * driver it high/low like we are in full control of the line and 83 * open drain will happen transparently. 84 */ 85 if (device_property_present(dev, "linux,open-drain")) 86 gflags = GPIOD_OUT_LOW; 87 88 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL); 89 if (!master) 90 return -ENOMEM; 91 92 ddata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags); 93 if (IS_ERR(ddata->gpiod)) 94 return dev_err_probe(dev, PTR_ERR(ddata->gpiod), "gpio_request (pin) failed\n"); 95 96 ddata->pullup_gpiod = 97 devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW); 98 if (IS_ERR(ddata->pullup_gpiod)) 99 return dev_err_probe(dev, PTR_ERR(ddata->pullup_gpiod), 100 "gpio_request (ext_pullup_enable_pin) failed\n"); 101 102 master->data = ddata; 103 master->read_bit = w1_gpio_read_bit; 104 gpiod_direction_output(ddata->gpiod, 1); 105 master->write_bit = w1_gpio_write_bit; 106 107 /* 108 * If we are using open drain emulation from the GPIO library, 109 * we need to use this pullup function that hammers the line 110 * high using a raw accessor to provide pull-up for the w1 111 * line. 112 */ 113 if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN) 114 master->set_pullup = w1_gpio_set_pullup; 115 116 err = w1_add_master_device(master); 117 if (err) 118 return dev_err_probe(dev, err, "w1_add_master device failed\n"); 119 120 gpiod_set_value(ddata->pullup_gpiod, 1); 121 122 platform_set_drvdata(pdev, master); 123 124 return 0; 125 } 126 127 static void w1_gpio_remove(struct platform_device *pdev) 128 { 129 struct w1_bus_master *master = platform_get_drvdata(pdev); 130 struct w1_gpio_ddata *ddata = master->data; 131 132 gpiod_set_value(ddata->pullup_gpiod, 0); 133 134 w1_remove_master_device(master); 135 } 136 137 static const struct of_device_id w1_gpio_dt_ids[] = { 138 { .compatible = "w1-gpio" }, 139 {} 140 }; 141 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids); 142 143 static struct platform_driver w1_gpio_driver = { 144 .driver = { 145 .name = "w1-gpio", 146 .of_match_table = w1_gpio_dt_ids, 147 }, 148 .probe = w1_gpio_probe, 149 .remove_new = w1_gpio_remove, 150 }; 151 152 module_platform_driver(w1_gpio_driver); 153 154 MODULE_DESCRIPTION("GPIO w1 bus master driver"); 155 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>"); 156 MODULE_LICENSE("GPL"); 157