Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

extcon: palmas: Support GPIO based USB ID detection

Some palmas based chip variants do not have OTG based ID logic.
For these variants we rely on GPIO based USB ID detection.

These chips do have VBUS comparator for VBUS detection so we
continue to use the old way of detecting VBUS.

Signed-off-by: Roger Quadros <rogerq@ti.com>
Acked-by: Chanwoo Choi <cw00.choi@samsung.com>
Acked-by: Lee Jones <lee.jones@linaro.org>
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>

authored by

Roger Quadros and committed by
Chanwoo Choi
92b7cb5d a598af7f

+126 -16
+4 -1
Documentation/devicetree/bindings/extcon/extcon-palmas.txt
··· 10 10 11 11 Optional Properties: 12 12 - ti,wakeup : To enable the wakeup comparator in probe 13 - - ti,enable-id-detection: Perform ID detection. 13 + - ti,enable-id-detection: Perform ID detection. If id-gpio is specified 14 + it performs id-detection using GPIO else using OTG core. 14 15 - ti,enable-vbus-detection: Perform VBUS detection. 16 + - id-gpio: gpio for GPIO ID detection. See gpio binding. 17 + - debounce-delay-ms: debounce delay for GPIO ID pin in milliseconds. 15 18 16 19 palmas-usb { 17 20 compatible = "ti,twl6035-usb", "ti,palmas-usb";
+114 -15
drivers/extcon/extcon-palmas.c
··· 28 28 #include <linux/mfd/palmas.h> 29 29 #include <linux/of.h> 30 30 #include <linux/of_platform.h> 31 + #include <linux/of_gpio.h> 32 + #include <linux/workqueue.h> 33 + 34 + #define USB_GPIO_DEBOUNCE_MS 20 /* ms */ 31 35 32 36 static const unsigned int palmas_extcon_cable[] = { 33 37 EXTCON_USB, ··· 122 118 return IRQ_HANDLED; 123 119 } 124 120 121 + static void palmas_gpio_id_detect(struct work_struct *work) 122 + { 123 + int id; 124 + struct palmas_usb *palmas_usb = container_of(to_delayed_work(work), 125 + struct palmas_usb, 126 + wq_detectid); 127 + struct extcon_dev *edev = palmas_usb->edev; 128 + 129 + if (!palmas_usb->id_gpiod) 130 + return; 131 + 132 + id = gpiod_get_value_cansleep(palmas_usb->id_gpiod); 133 + 134 + if (id) { 135 + extcon_set_cable_state_(edev, EXTCON_USB_HOST, false); 136 + dev_info(palmas_usb->dev, "USB-HOST cable is detached\n"); 137 + } else { 138 + extcon_set_cable_state_(edev, EXTCON_USB_HOST, true); 139 + dev_info(palmas_usb->dev, "USB-HOST cable is attached\n"); 140 + } 141 + } 142 + 143 + static irqreturn_t palmas_gpio_id_irq_handler(int irq, void *_palmas_usb) 144 + { 145 + struct palmas_usb *palmas_usb = _palmas_usb; 146 + 147 + queue_delayed_work(system_power_efficient_wq, &palmas_usb->wq_detectid, 148 + palmas_usb->sw_debounce_jiffies); 149 + 150 + return IRQ_HANDLED; 151 + } 152 + 125 153 static void palmas_enable_irq(struct palmas_usb *palmas_usb) 126 154 { 127 155 palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, 128 156 PALMAS_USB_VBUS_CTRL_SET, 129 157 PALMAS_USB_VBUS_CTRL_SET_VBUS_ACT_COMP); 130 158 131 - palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, 132 - PALMAS_USB_ID_CTRL_SET, PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP); 159 + if (palmas_usb->enable_id_detection) { 160 + palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, 161 + PALMAS_USB_ID_CTRL_SET, 162 + PALMAS_USB_ID_CTRL_SET_ID_ACT_COMP); 133 163 134 - palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, 135 - PALMAS_USB_ID_INT_EN_HI_SET, 136 - PALMAS_USB_ID_INT_EN_HI_SET_ID_GND | 137 - PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT); 164 + palmas_write(palmas_usb->palmas, PALMAS_USB_OTG_BASE, 165 + PALMAS_USB_ID_INT_EN_HI_SET, 166 + PALMAS_USB_ID_INT_EN_HI_SET_ID_GND | 167 + PALMAS_USB_ID_INT_EN_HI_SET_ID_FLOAT); 168 + } 138 169 139 170 if (palmas_usb->enable_vbus_detection) 140 171 palmas_vbus_irq_handler(palmas_usb->vbus_irq, palmas_usb); ··· 208 169 palmas_usb->wakeup = pdata->wakeup; 209 170 } 210 171 172 + palmas_usb->id_gpiod = devm_gpiod_get_optional(&pdev->dev, "id"); 173 + if (IS_ERR(palmas_usb->id_gpiod)) { 174 + dev_err(&pdev->dev, "failed to get id gpio\n"); 175 + return PTR_ERR(palmas_usb->id_gpiod); 176 + } 177 + 178 + if (palmas_usb->enable_id_detection && palmas_usb->id_gpiod) { 179 + palmas_usb->enable_id_detection = false; 180 + palmas_usb->enable_gpio_id_detection = true; 181 + } 182 + 183 + if (palmas_usb->enable_gpio_id_detection) { 184 + u32 debounce; 185 + 186 + if (of_property_read_u32(node, "debounce-delay-ms", &debounce)) 187 + debounce = USB_GPIO_DEBOUNCE_MS; 188 + 189 + status = gpiod_set_debounce(palmas_usb->id_gpiod, 190 + debounce * 1000); 191 + if (status < 0) 192 + palmas_usb->sw_debounce_jiffies = msecs_to_jiffies(debounce); 193 + } 194 + 195 + INIT_DELAYED_WORK(&palmas_usb->wq_detectid, palmas_gpio_id_detect); 196 + 211 197 palmas->usb = palmas_usb; 212 198 palmas_usb->palmas = palmas; 213 199 214 200 palmas_usb->dev = &pdev->dev; 215 - 216 - palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data, 217 - PALMAS_ID_OTG_IRQ); 218 - palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data, 219 - PALMAS_ID_IRQ); 220 - palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data, 221 - PALMAS_VBUS_OTG_IRQ); 222 - palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data, 223 - PALMAS_VBUS_IRQ); 224 201 225 202 palmas_usb_wakeup(palmas, palmas_usb->wakeup); 226 203 ··· 256 201 } 257 202 258 203 if (palmas_usb->enable_id_detection) { 204 + palmas_usb->id_otg_irq = regmap_irq_get_virq(palmas->irq_data, 205 + PALMAS_ID_OTG_IRQ); 206 + palmas_usb->id_irq = regmap_irq_get_virq(palmas->irq_data, 207 + PALMAS_ID_IRQ); 259 208 status = devm_request_threaded_irq(palmas_usb->dev, 260 209 palmas_usb->id_irq, 261 210 NULL, palmas_id_irq_handler, ··· 271 212 palmas_usb->id_irq, status); 272 213 return status; 273 214 } 215 + } else if (palmas_usb->enable_gpio_id_detection) { 216 + palmas_usb->gpio_id_irq = gpiod_to_irq(palmas_usb->id_gpiod); 217 + if (palmas_usb->gpio_id_irq < 0) { 218 + dev_err(&pdev->dev, "failed to get id irq\n"); 219 + return palmas_usb->gpio_id_irq; 220 + } 221 + status = devm_request_threaded_irq(&pdev->dev, 222 + palmas_usb->gpio_id_irq, 223 + NULL, 224 + palmas_gpio_id_irq_handler, 225 + IRQF_TRIGGER_RISING | 226 + IRQF_TRIGGER_FALLING | 227 + IRQF_ONESHOT, 228 + "palmas_usb_id", 229 + palmas_usb); 230 + if (status < 0) { 231 + dev_err(&pdev->dev, 232 + "failed to request handler for id irq\n"); 233 + return status; 234 + } 274 235 } 275 236 276 237 if (palmas_usb->enable_vbus_detection) { 238 + palmas_usb->vbus_otg_irq = regmap_irq_get_virq(palmas->irq_data, 239 + PALMAS_VBUS_OTG_IRQ); 240 + palmas_usb->vbus_irq = regmap_irq_get_virq(palmas->irq_data, 241 + PALMAS_VBUS_IRQ); 277 242 status = devm_request_threaded_irq(palmas_usb->dev, 278 243 palmas_usb->vbus_irq, NULL, 279 244 palmas_vbus_irq_handler, ··· 312 229 } 313 230 314 231 palmas_enable_irq(palmas_usb); 232 + /* perform initial detection */ 233 + palmas_gpio_id_detect(&palmas_usb->wq_detectid.work); 315 234 device_set_wakeup_capable(&pdev->dev, true); 235 + return 0; 236 + } 237 + 238 + static int palmas_usb_remove(struct platform_device *pdev) 239 + { 240 + struct palmas_usb *palmas_usb = platform_get_drvdata(pdev); 241 + 242 + cancel_delayed_work_sync(&palmas_usb->wq_detectid); 243 + 316 244 return 0; 317 245 } 318 246 ··· 337 243 enable_irq_wake(palmas_usb->vbus_irq); 338 244 if (palmas_usb->enable_id_detection) 339 245 enable_irq_wake(palmas_usb->id_irq); 246 + if (palmas_usb->enable_gpio_id_detection) 247 + enable_irq_wake(palmas_usb->gpio_id_irq); 340 248 } 341 249 return 0; 342 250 } ··· 352 256 disable_irq_wake(palmas_usb->vbus_irq); 353 257 if (palmas_usb->enable_id_detection) 354 258 disable_irq_wake(palmas_usb->id_irq); 259 + if (palmas_usb->enable_gpio_id_detection) 260 + disable_irq_wake(palmas_usb->gpio_id_irq); 355 261 } 356 262 return 0; 357 263 }; ··· 371 273 372 274 static struct platform_driver palmas_usb_driver = { 373 275 .probe = palmas_usb_probe, 276 + .remove = palmas_usb_remove, 374 277 .driver = { 375 278 .name = "palmas-usb", 376 279 .of_match_table = of_palmas_match_tbl,
+1
drivers/extcon/extcon-usb-gpio.c
··· 15 15 */ 16 16 17 17 #include <linux/extcon.h> 18 + #include <linux/gpio.h> 18 19 #include <linux/gpio/consumer.h> 19 20 #include <linux/init.h> 20 21 #include <linux/interrupt.h>
+7
include/linux/mfd/palmas.h
··· 21 21 #include <linux/regmap.h> 22 22 #include <linux/regulator/driver.h> 23 23 #include <linux/extcon.h> 24 + #include <linux/of_gpio.h> 24 25 #include <linux/usb/phy_companion.h> 25 26 26 27 #define PALMAS_NUM_CLIENTS 3 ··· 552 551 int vbus_otg_irq; 553 552 int vbus_irq; 554 553 554 + int gpio_id_irq; 555 + struct gpio_desc *id_gpiod; 556 + unsigned long sw_debounce_jiffies; 557 + struct delayed_work wq_detectid; 558 + 555 559 enum palmas_usb_state linkstat; 556 560 int wakeup; 557 561 bool enable_vbus_detection; 558 562 bool enable_id_detection; 563 + bool enable_gpio_id_detection; 559 564 }; 560 565 561 566 #define comparator_to_palmas(x) container_of((x), struct palmas_usb, comparator)