2019-05-27 09:55:21 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2021-11-15 06:05:36 +03:00
/*
2015-02-02 13:21:59 +03:00
* drivers / extcon / extcon - usb - gpio . c - USB GPIO extcon driver
*
2020-07-13 16:19:53 +03:00
* Copyright ( C ) 2015 Texas Instruments Incorporated - https : //www.ti.com
2015-02-02 13:21:59 +03:00
* Author : Roger Quadros < rogerq @ ti . com >
*/
2017-09-21 06:11:24 +03:00
# include <linux/extcon-provider.h>
2015-05-05 19:32:20 +03:00
# include <linux/gpio/consumer.h>
2015-02-02 13:21:59 +03:00
# include <linux/init.h>
# include <linux/interrupt.h>
# include <linux/irq.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/platform_device.h>
# include <linux/slab.h>
# include <linux/workqueue.h>
2017-01-04 10:19:51 +03:00
# include <linux/pinctrl/consumer.h>
2021-07-15 22:06:06 +03:00
# include <linux/mod_devicetable.h>
2015-02-02 13:21:59 +03:00
# define USB_GPIO_DEBOUNCE_MS 20 /* ms */
struct usb_extcon_info {
struct device * dev ;
struct extcon_dev * edev ;
struct gpio_desc * id_gpiod ;
2016-09-20 17:53:55 +03:00
struct gpio_desc * vbus_gpiod ;
2015-02-02 13:21:59 +03:00
int id_irq ;
2016-09-20 17:53:55 +03:00
int vbus_irq ;
2015-02-02 13:21:59 +03:00
unsigned long debounce_jiffies ;
struct delayed_work wq_detcable ;
} ;
2015-06-12 05:10:06 +03:00
static const unsigned int usb_extcon_cable [ ] = {
extcon: Use the unique id for external connector instead of string
This patch uses the unique id to identify the type of external connector instead
of string name. The string name have the many potential issues. So, this patch
defines the 'extcon' enumeration which includes all supported external connector
on EXTCON subsystem. If new external connector is necessary, the unique id of
new connector have to be added in 'extcon' enumeration. There are current
supported external connector in 'enum extcon' as following:
enum extcon {
EXTCON_NONE = 0x0,
/* USB external connector */
EXTCON_USB = 0x1,
EXTCON_USB_HOST = 0x2,
/* Charger external connector */
EXTCON_TA = 0x10,
EXTCON_FAST_CHARGER = 0x11,
EXTCON_SLOW_CHARGER = 0x12,
EXTCON_CHARGE_DOWNSTREAM = 0x13,
/* Audio and video external connector */
EXTCON_LINE_IN = 0x20,
EXTCON_LINE_OUT = 0x21,
EXTCON_MICROPHONE = 0x22,
EXTCON_HEADPHONE = 0x23,
EXTCON_HDMI = 0x30,
EXTCON_MHL = 0x31,
EXTCON_DVI = 0x32,
EXTCON_VGA = 0x33,
EXTCON_SPDIF_IN = 0x34,
EXTCON_SPDIF_OUT = 0x35,
EXTCON_VIDEO_IN = 0x36,
EXTCON_VIDEO_OUT = 0x37,
/* Miscellaneous external connector */
EXTCON_DOCK = 0x50,
EXTCON_JIG = 0x51,
EXTCON_MECHANICAL = 0x52,
EXTCON_END,
};
For example in extcon-arizona.c:
To use unique id removes the potential issue about handling
the inconsistent name of external connector with string.
- Previously, use the string to register the type of arizona jack connector
static const char *arizona_cable[] = {
"Mechanical",
"Microphone",
"Headphone",
"Line-out",
};
- Newly, use the unique id to register the type of arizona jack connector
static const enum extcon arizona_cable[] = {
EXTCON_MECHANICAL,
EXTCON_MICROPHONE,
EXTCON_HEADPHONE,
EXTCON_LINE_OUT,
EXTCON_NONE,
};
And this patch modify the prototype of extcon_{get|set}_cable_state_() which
uses the 'enum extcon id' instead of 'cable_index'. Because although one more
extcon drivers support USB cable, each extcon driver might has the differnt
'cable_index' for USB cable. All extcon drivers can use the unique id number
for same external connector with modified extcon_{get|set}_cable_state_().
- Previously, use 'cable_index' on these functions:
extcon_get_cable_state_(struct extcon_dev*, int cable_index)
extcon_set_cable_state_(struct extcon_dev*, int cable_index, bool state)
-Newly, use 'enum extcon id' on these functions:
extcon_get_cable_state_(struct extcon_dev*, enum extcon id)
extcon_set_cable_state_(struct extcon_dev*, enum extcon id, bool state)
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Felipe Balbi <balbi@ti.com>
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
Acked-by: Roger Quadros <rogerq@ti.com>
Acked-by: Charles Keepax <ckeepax@opensource.wolfsonmicro.com>
Acked-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
Reviewed-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
[arnd: Report the build break about drivers/usb/phy/phy-tahvo.c after using the
unique id for external connector insteadf of string]
Reported-by: Arnd Bergmann <arnd@arndb.de>
[dan.carpenter: Report the build warning of extcon_{set|get}_cable_state_()]
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
2015-04-24 13:16:05 +03:00
EXTCON_USB ,
EXTCON_USB_HOST ,
EXTCON_NONE ,
2015-02-02 13:21:59 +03:00
} ;
2016-09-20 17:53:55 +03:00
/*
* " USB " = VBUS and " USB-HOST " = ! ID , so we have :
* Both " USB " and " USB-HOST " can ' t be set as active at the
* same time so if " USB-HOST " is active ( i . e . ID is 0 ) we keep " USB " inactive
* even if VBUS is on .
*
* State | ID | VBUS
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* [ 1 ] USB | H | H
* [ 2 ] none | H | L
* [ 3 ] USB - HOST | L | H
* [ 4 ] USB - HOST | L | L
*
* In case we have only one of these signals :
* - VBUS only - we want to distinguish between [ 1 ] and [ 2 ] , so ID is always 1.
* - ID only - we want to distinguish between [ 1 ] and [ 4 ] , so VBUS = ID .
*/
2015-02-02 13:21:59 +03:00
static void usb_extcon_detect_cable ( struct work_struct * work )
{
2016-09-20 17:53:55 +03:00
int id , vbus ;
2015-02-02 13:21:59 +03:00
struct usb_extcon_info * info = container_of ( to_delayed_work ( work ) ,
struct usb_extcon_info ,
wq_detcable ) ;
2016-09-20 17:53:55 +03:00
/* check ID and VBUS and update cable state */
id = info - > id_gpiod ?
gpiod_get_value_cansleep ( info - > id_gpiod ) : 1 ;
vbus = info - > vbus_gpiod ?
gpiod_get_value_cansleep ( info - > vbus_gpiod ) : id ;
/* at first we clean states which are no longer active */
if ( id )
2016-08-16 09:55:34 +03:00
extcon_set_state_sync ( info - > edev , EXTCON_USB_HOST , false ) ;
2016-09-20 17:53:55 +03:00
if ( ! vbus )
2016-08-16 09:55:34 +03:00
extcon_set_state_sync ( info - > edev , EXTCON_USB , false ) ;
2016-09-20 17:53:55 +03:00
if ( ! id ) {
2016-08-16 09:55:34 +03:00
extcon_set_state_sync ( info - > edev , EXTCON_USB_HOST , true ) ;
2016-09-20 17:53:55 +03:00
} else {
if ( vbus )
extcon_set_state_sync ( info - > edev , EXTCON_USB , true ) ;
2015-02-02 13:21:59 +03:00
}
}
static irqreturn_t usb_irq_handler ( int irq , void * dev_id )
{
struct usb_extcon_info * info = dev_id ;
queue_delayed_work ( system_power_efficient_wq , & info - > wq_detcable ,
info - > debounce_jiffies ) ;
return IRQ_HANDLED ;
}
static int usb_extcon_probe ( struct platform_device * pdev )
{
struct device * dev = & pdev - > dev ;
struct device_node * np = dev - > of_node ;
struct usb_extcon_info * info ;
int ret ;
2017-03-21 21:37:47 +03:00
if ( ! np )
2015-02-02 13:21:59 +03:00
return - EINVAL ;
info = devm_kzalloc ( & pdev - > dev , sizeof ( * info ) , GFP_KERNEL ) ;
if ( ! info )
return - ENOMEM ;
info - > dev = dev ;
2016-09-20 17:53:55 +03:00
info - > id_gpiod = devm_gpiod_get_optional ( & pdev - > dev , " id " , GPIOD_IN ) ;
info - > vbus_gpiod = devm_gpiod_get_optional ( & pdev - > dev , " vbus " ,
GPIOD_IN ) ;
if ( ! info - > id_gpiod & & ! info - > vbus_gpiod ) {
dev_err ( dev , " failed to get gpios \n " ) ;
return - ENODEV ;
2015-02-02 13:21:59 +03:00
}
2016-09-20 17:53:55 +03:00
if ( IS_ERR ( info - > id_gpiod ) )
return PTR_ERR ( info - > id_gpiod ) ;
if ( IS_ERR ( info - > vbus_gpiod ) )
return PTR_ERR ( info - > vbus_gpiod ) ;
2015-04-02 16:13:02 +03:00
info - > edev = devm_extcon_dev_allocate ( dev , usb_extcon_cable ) ;
if ( IS_ERR ( info - > edev ) ) {
dev_err ( dev , " failed to allocate extcon device \n " ) ;
return - ENOMEM ;
}
ret = devm_extcon_dev_register ( dev , info - > edev ) ;
if ( ret < 0 ) {
dev_err ( dev , " failed to register extcon device \n " ) ;
return ret ;
}
2016-09-20 17:53:55 +03:00
if ( info - > id_gpiod )
ret = gpiod_set_debounce ( info - > id_gpiod ,
USB_GPIO_DEBOUNCE_MS * 1000 ) ;
if ( ! ret & & info - > vbus_gpiod )
ret = gpiod_set_debounce ( info - > vbus_gpiod ,
USB_GPIO_DEBOUNCE_MS * 1000 ) ;
2015-02-02 13:21:59 +03:00
if ( ret < 0 )
info - > debounce_jiffies = msecs_to_jiffies ( USB_GPIO_DEBOUNCE_MS ) ;
INIT_DELAYED_WORK ( & info - > wq_detcable , usb_extcon_detect_cable ) ;
2016-09-20 17:53:55 +03:00
if ( info - > id_gpiod ) {
info - > id_irq = gpiod_to_irq ( info - > id_gpiod ) ;
if ( info - > id_irq < 0 ) {
dev_err ( dev , " failed to get ID IRQ \n " ) ;
return info - > id_irq ;
}
ret = devm_request_threaded_irq ( dev , info - > id_irq , NULL ,
usb_irq_handler ,
IRQF_TRIGGER_RISING |
IRQF_TRIGGER_FALLING | IRQF_ONESHOT ,
pdev - > name , info ) ;
if ( ret < 0 ) {
dev_err ( dev , " failed to request handler for ID IRQ \n " ) ;
return ret ;
}
2015-02-02 13:21:59 +03:00
}
2016-09-20 17:53:55 +03:00
if ( info - > vbus_gpiod ) {
info - > vbus_irq = gpiod_to_irq ( info - > vbus_gpiod ) ;
if ( info - > vbus_irq < 0 ) {
dev_err ( dev , " failed to get VBUS IRQ \n " ) ;
return info - > vbus_irq ;
}
ret = devm_request_threaded_irq ( dev , info - > vbus_irq , NULL ,
usb_irq_handler ,
IRQF_TRIGGER_RISING |
IRQF_TRIGGER_FALLING | IRQF_ONESHOT ,
pdev - > name , info ) ;
if ( ret < 0 ) {
dev_err ( dev , " failed to request handler for VBUS IRQ \n " ) ;
return ret ;
}
2015-02-02 13:21:59 +03:00
}
platform_set_drvdata ( pdev , info ) ;
2017-02-13 12:21:52 +03:00
device_set_wakeup_capable ( & pdev - > dev , true ) ;
2015-02-02 13:21:59 +03:00
/* Perform initial detection */
usb_extcon_detect_cable ( & info - > wq_detcable . work ) ;
return 0 ;
}
2024-02-25 18:54:55 +03:00
static void usb_extcon_remove ( struct platform_device * pdev )
2015-02-02 13:21:59 +03:00
{
struct usb_extcon_info * info = platform_get_drvdata ( pdev ) ;
cancel_delayed_work_sync ( & info - > wq_detcable ) ;
2016-04-06 20:32:37 +03:00
device_init_wakeup ( & pdev - > dev , false ) ;
2015-02-02 13:21:59 +03:00
}
# ifdef CONFIG_PM_SLEEP
static int usb_extcon_suspend ( struct device * dev )
{
struct usb_extcon_info * info = dev_get_drvdata ( dev ) ;
int ret = 0 ;
2016-09-20 17:53:55 +03:00
if ( device_may_wakeup ( dev ) ) {
if ( info - > id_gpiod ) {
ret = enable_irq_wake ( info - > id_irq ) ;
if ( ret )
return ret ;
}
if ( info - > vbus_gpiod ) {
ret = enable_irq_wake ( info - > vbus_irq ) ;
if ( ret ) {
if ( info - > id_gpiod )
disable_irq_wake ( info - > id_irq ) ;
return ret ;
}
}
}
2017-01-04 10:19:51 +03:00
if ( ! device_may_wakeup ( dev ) )
pinctrl_pm_select_sleep_state ( dev ) ;
2015-02-02 13:21:59 +03:00
return ret ;
}
static int usb_extcon_resume ( struct device * dev )
{
struct usb_extcon_info * info = dev_get_drvdata ( dev ) ;
int ret = 0 ;
2017-01-04 10:19:51 +03:00
if ( ! device_may_wakeup ( dev ) )
pinctrl_pm_select_default_state ( dev ) ;
2016-09-20 17:53:55 +03:00
if ( device_may_wakeup ( dev ) ) {
if ( info - > id_gpiod ) {
ret = disable_irq_wake ( info - > id_irq ) ;
if ( ret )
return ret ;
}
if ( info - > vbus_gpiod ) {
ret = disable_irq_wake ( info - > vbus_irq ) ;
if ( ret ) {
if ( info - > id_gpiod )
enable_irq_wake ( info - > id_irq ) ;
return ret ;
}
}
}
2017-02-15 15:31:28 +03:00
queue_delayed_work ( system_power_efficient_wq ,
& info - > wq_detcable , 0 ) ;
2015-02-02 13:21:59 +03:00
return ret ;
}
# endif
static SIMPLE_DEV_PM_OPS ( usb_extcon_pm_ops ,
usb_extcon_suspend , usb_extcon_resume ) ;
2015-03-06 19:41:36 +03:00
static const struct of_device_id usb_extcon_dt_match [ ] = {
2015-02-02 13:21:59 +03:00
{ . compatible = " linux,extcon-usb-gpio " , } ,
{ /* sentinel */ }
} ;
MODULE_DEVICE_TABLE ( of , usb_extcon_dt_match ) ;
2016-04-25 11:04:47 +03:00
static const struct platform_device_id usb_extcon_platform_ids [ ] = {
{ . name = " extcon-usb-gpio " , } ,
{ /* sentinel */ }
} ;
MODULE_DEVICE_TABLE ( platform , usb_extcon_platform_ids ) ;
2015-02-02 13:21:59 +03:00
static struct platform_driver usb_extcon_driver = {
. probe = usb_extcon_probe ,
2024-02-25 18:54:55 +03:00
. remove_new = usb_extcon_remove ,
2015-02-02 13:21:59 +03:00
. driver = {
. name = " extcon-usb-gpio " ,
. pm = & usb_extcon_pm_ops ,
. of_match_table = usb_extcon_dt_match ,
} ,
2016-04-25 11:04:47 +03:00
. id_table = usb_extcon_platform_ids ,
2015-02-02 13:21:59 +03:00
} ;
module_platform_driver ( usb_extcon_driver ) ;
MODULE_AUTHOR ( " Roger Quadros <rogerq@ti.com> " ) ;
MODULE_DESCRIPTION ( " USB GPIO extcon driver " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;