2013-07-04 23:13:24 +04:00
/*
* gpio_backlight . c - Simple GPIO - controlled backlight
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
*/
# include <linux/backlight.h>
# include <linux/err.h>
# include <linux/fb.h>
2017-05-30 14:48:21 +03:00
# include <linux/gpio.h> /* Only for legacy support */
# include <linux/gpio/consumer.h>
2013-07-04 23:13:24 +04:00
# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/module.h>
2014-02-27 18:28:33 +04:00
# include <linux/of.h>
# include <linux/of_gpio.h>
2013-07-04 23:13:24 +04:00
# include <linux/platform_data/gpio_backlight.h>
# include <linux/platform_device.h>
2019-06-19 18:21:27 +03:00
# include <linux/property.h>
2013-07-04 23:13:24 +04:00
# include <linux/slab.h>
struct gpio_backlight {
struct device * dev ;
struct device * fbdev ;
2017-05-30 14:48:21 +03:00
struct gpio_desc * gpiod ;
2014-02-27 18:28:33 +04:00
int def_value ;
2013-07-04 23:13:24 +04:00
} ;
static int gpio_backlight_update_status ( struct backlight_device * bl )
{
struct gpio_backlight * gbl = bl_get_data ( bl ) ;
int brightness = bl - > props . brightness ;
if ( bl - > props . power ! = FB_BLANK_UNBLANK | |
bl - > props . fb_blank ! = FB_BLANK_UNBLANK | |
bl - > props . state & ( BL_CORE_SUSPENDED | BL_CORE_FBBLANK ) )
brightness = 0 ;
2017-05-30 14:48:22 +03:00
gpiod_set_value_cansleep ( gbl - > gpiod , brightness ) ;
2013-07-04 23:13:24 +04:00
return 0 ;
}
static int gpio_backlight_check_fb ( struct backlight_device * bl ,
struct fb_info * info )
{
struct gpio_backlight * gbl = bl_get_data ( bl ) ;
return gbl - > fbdev = = NULL | | gbl - > fbdev = = info - > dev ;
}
static const struct backlight_ops gpio_backlight_ops = {
. options = BL_CORE_SUSPENDRESUME ,
. update_status = gpio_backlight_update_status ,
. check_fb = gpio_backlight_check_fb ,
} ;
2014-02-27 18:28:33 +04:00
static int gpio_backlight_probe_dt ( struct platform_device * pdev ,
struct gpio_backlight * gbl )
{
2017-05-30 14:48:21 +03:00
struct device * dev = & pdev - > dev ;
enum gpiod_flags flags ;
int ret ;
2019-06-19 18:21:27 +03:00
gbl - > def_value = device_property_read_bool ( dev , " default-on " ) ;
2017-05-30 14:48:21 +03:00
flags = gbl - > def_value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW ;
2014-02-27 18:28:33 +04:00
2017-05-30 14:48:21 +03:00
gbl - > gpiod = devm_gpiod_get ( dev , NULL , flags ) ;
if ( IS_ERR ( gbl - > gpiod ) ) {
ret = PTR_ERR ( gbl - > gpiod ) ;
2014-02-27 18:28:33 +04:00
2017-05-30 14:48:21 +03:00
if ( ret ! = - EPROBE_DEFER ) {
dev_err ( dev ,
2014-02-27 18:28:33 +04:00
" Error: The gpios parameter is missing or invalid. \n " ) ;
}
2017-05-30 14:48:21 +03:00
return ret ;
2014-02-27 18:28:33 +04:00
}
return 0 ;
}
2013-07-04 23:13:24 +04:00
static int gpio_backlight_probe ( struct platform_device * pdev )
{
2013-11-13 03:09:04 +04:00
struct gpio_backlight_platform_data * pdata =
dev_get_platdata ( & pdev - > dev ) ;
2013-07-04 23:13:24 +04:00
struct backlight_properties props ;
struct backlight_device * bl ;
struct gpio_backlight * gbl ;
int ret ;
gbl = devm_kzalloc ( & pdev - > dev , sizeof ( * gbl ) , GFP_KERNEL ) ;
if ( gbl = = NULL )
return - ENOMEM ;
gbl - > dev = & pdev - > dev ;
2014-02-27 18:28:33 +04:00
2019-06-19 18:21:27 +03:00
if ( pdev - > dev . fwnode ) {
2014-02-27 18:28:33 +04:00
ret = gpio_backlight_probe_dt ( pdev , gbl ) ;
if ( ret )
return ret ;
2019-06-19 18:21:27 +03:00
} else if ( pdata ) {
2017-05-30 14:48:21 +03:00
/*
* Legacy platform data GPIO retrieveal . Do not expand
* the use of this code path , currently only used by one
* SH board .
*/
unsigned long flags = GPIOF_DIR_OUT ;
2014-02-27 18:28:33 +04:00
gbl - > fbdev = pdata - > fbdev ;
gbl - > def_value = pdata - > def_value ;
2017-05-30 14:48:22 +03:00
flags | = gbl - > def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW ;
2017-05-30 14:48:21 +03:00
ret = devm_gpio_request_one ( gbl - > dev , pdata - > gpio , flags ,
pdata ? pdata - > name : " backlight " ) ;
if ( ret < 0 ) {
dev_err ( & pdev - > dev , " unable to request GPIO \n " ) ;
return ret ;
}
gbl - > gpiod = gpio_to_desc ( pdata - > gpio ) ;
if ( ! gbl - > gpiod )
return - EINVAL ;
2019-06-19 18:21:27 +03:00
} else {
dev_err ( & pdev - > dev ,
" failed to find platform data or device tree node. \n " ) ;
return - ENODEV ;
2013-07-04 23:13:24 +04:00
}
memset ( & props , 0 , sizeof ( props ) ) ;
props . type = BACKLIGHT_RAW ;
props . max_brightness = 1 ;
2013-11-13 03:09:16 +04:00
bl = devm_backlight_device_register ( & pdev - > dev , dev_name ( & pdev - > dev ) ,
& pdev - > dev , gbl , & gpio_backlight_ops ,
& props ) ;
2013-07-04 23:13:24 +04:00
if ( IS_ERR ( bl ) ) {
dev_err ( & pdev - > dev , " failed to register backlight \n " ) ;
return PTR_ERR ( bl ) ;
}
2014-02-27 18:28:33 +04:00
bl - > props . brightness = gbl - > def_value ;
2013-07-04 23:13:24 +04:00
backlight_update_status ( bl ) ;
platform_set_drvdata ( pdev , bl ) ;
return 0 ;
}
2014-02-27 18:28:33 +04:00
static struct of_device_id gpio_backlight_of_match [ ] = {
{ . compatible = " gpio-backlight " } ,
{ /* sentinel */ }
} ;
2015-04-16 03:21:24 +03:00
MODULE_DEVICE_TABLE ( of , gpio_backlight_of_match ) ;
2014-02-27 18:28:33 +04:00
2013-07-04 23:13:24 +04:00
static struct platform_driver gpio_backlight_driver = {
. driver = {
. name = " gpio-backlight " ,
2019-06-19 18:21:27 +03:00
. of_match_table = gpio_backlight_of_match ,
2013-07-04 23:13:24 +04:00
} ,
. probe = gpio_backlight_probe ,
} ;
module_platform_driver ( gpio_backlight_driver ) ;
MODULE_AUTHOR ( " Laurent Pinchart <laurent.pinchart@ideasonboard.com> " ) ;
MODULE_DESCRIPTION ( " GPIO-based Backlight Driver " ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_ALIAS ( " platform:gpio-backlight " ) ;