2018-12-11 20:57:48 +03:00
// SPDX-License-Identifier: GPL-2.0
2014-09-05 16:21:34 +04:00
/*
* Copyright ( C ) 2013 - 2014 Texas Instruments Incorporated - http : //www.ti.com
2015-06-28 17:24:55 +03:00
*
* Authors :
* Jyri Sarha < jsarha @ ti . com >
* Sergej Sawazki < ce3a @ gmx . de >
2014-09-05 16:21:34 +04:00
*
2015-06-28 17:24:55 +03:00
* Gpio controlled clock implementation
2014-09-05 16:21:34 +04:00
*/
# include <linux/clk-provider.h>
2015-06-28 17:24:53 +03:00
# include <linux/export.h>
2014-09-05 16:21:34 +04:00
# include <linux/slab.h>
2014-09-30 21:16:22 +04:00
# include <linux/gpio/consumer.h>
2014-09-05 16:21:34 +04:00
# include <linux/err.h>
# include <linux/device.h>
2016-02-03 04:09:26 +03:00
# include <linux/platform_device.h>
# include <linux/of_device.h>
2014-09-05 16:21:34 +04:00
/**
* DOC : basic gpio gated clock which can be enabled and disabled
* with gpio output
* Traits of this clock :
* prepare - clk_ ( un ) prepare only ensures parent is ( un ) prepared
* enable - clk_enable and clk_disable are functional & control gpio
* rate - inherits rate from parent . No clk_set_rate support
* parent - fixed parent . No clk_set_parent support
*/
static int clk_gpio_gate_enable ( struct clk_hw * hw )
{
struct clk_gpio * clk = to_clk_gpio ( hw ) ;
gpiod_set_value ( clk - > gpiod , 1 ) ;
return 0 ;
}
static void clk_gpio_gate_disable ( struct clk_hw * hw )
{
struct clk_gpio * clk = to_clk_gpio ( hw ) ;
gpiod_set_value ( clk - > gpiod , 0 ) ;
}
static int clk_gpio_gate_is_enabled ( struct clk_hw * hw )
{
struct clk_gpio * clk = to_clk_gpio ( hw ) ;
return gpiod_get_value ( clk - > gpiod ) ;
}
const struct clk_ops clk_gpio_gate_ops = {
. enable = clk_gpio_gate_enable ,
. disable = clk_gpio_gate_disable ,
. is_enabled = clk_gpio_gate_is_enabled ,
} ;
EXPORT_SYMBOL_GPL ( clk_gpio_gate_ops ) ;
/**
2015-06-28 17:24:55 +03:00
* DOC : basic clock multiplexer which can be controlled with a gpio output
* Traits of this clock :
* prepare - clk_prepare only ensures that parents are prepared
* rate - rate is only affected by parent switching . No clk_set_rate support
* parent - parent is adjustable through clk_set_parent
2014-09-05 16:21:34 +04:00
*/
2015-06-28 17:24:55 +03:00
static u8 clk_gpio_mux_get_parent ( struct clk_hw * hw )
2014-09-05 16:21:34 +04:00
{
2015-06-28 17:24:55 +03:00
struct clk_gpio * clk = to_clk_gpio ( hw ) ;
2018-03-13 11:54:03 +03:00
return gpiod_get_value_cansleep ( clk - > gpiod ) ;
2015-06-28 17:24:55 +03:00
}
static int clk_gpio_mux_set_parent ( struct clk_hw * hw , u8 index )
{
struct clk_gpio * clk = to_clk_gpio ( hw ) ;
2018-03-13 11:54:03 +03:00
gpiod_set_value_cansleep ( clk - > gpiod , index ) ;
2015-06-28 17:24:55 +03:00
return 0 ;
}
const struct clk_ops clk_gpio_mux_ops = {
. get_parent = clk_gpio_mux_get_parent ,
. set_parent = clk_gpio_mux_set_parent ,
. determine_rate = __clk_mux_determine_rate ,
} ;
EXPORT_SYMBOL_GPL ( clk_gpio_mux_ops ) ;
2016-02-07 11:27:55 +03:00
static struct clk_hw * clk_register_gpio ( struct device * dev , const char * name ,
2017-09-24 19:19:18 +03:00
const char * const * parent_names , u8 num_parents , struct gpio_desc * gpiod ,
unsigned long flags , const struct clk_ops * clk_gpio_ops )
2015-06-28 17:24:55 +03:00
{
struct clk_gpio * clk_gpio ;
2016-02-07 11:27:55 +03:00
struct clk_hw * hw ;
2015-06-28 17:24:55 +03:00
struct clk_init_data init = { } ;
2014-09-05 16:21:34 +04:00
int err ;
2015-06-28 17:24:55 +03:00
if ( dev )
clk_gpio = devm_kzalloc ( dev , sizeof ( * clk_gpio ) , GFP_KERNEL ) ;
else
clk_gpio = kzalloc ( sizeof ( * clk_gpio ) , GFP_KERNEL ) ;
if ( ! clk_gpio )
return ERR_PTR ( - ENOMEM ) ;
2014-09-05 16:21:34 +04:00
init . name = name ;
2015-06-28 17:24:55 +03:00
init . ops = clk_gpio_ops ;
2014-09-05 16:21:34 +04:00
init . flags = flags | CLK_IS_BASIC ;
2015-06-28 17:24:55 +03:00
init . parent_names = parent_names ;
init . num_parents = num_parents ;
2014-09-05 16:21:34 +04:00
2017-09-24 19:19:18 +03:00
clk_gpio - > gpiod = gpiod ;
2014-09-05 16:21:34 +04:00
clk_gpio - > hw . init = & init ;
2016-02-07 11:27:55 +03:00
hw = & clk_gpio - > hw ;
2015-06-28 17:24:55 +03:00
if ( dev )
2016-02-07 11:27:55 +03:00
err = devm_clk_hw_register ( dev , hw ) ;
2015-06-28 17:24:55 +03:00
else
2016-02-07 11:27:55 +03:00
err = clk_hw_register ( NULL , hw ) ;
2014-09-05 16:21:34 +04:00
2016-02-07 11:27:55 +03:00
if ( ! err )
return hw ;
2014-09-05 16:21:34 +04:00
2015-06-28 17:24:55 +03:00
if ( ! dev ) {
2014-09-05 16:21:34 +04:00
kfree ( clk_gpio ) ;
2015-06-28 17:24:55 +03:00
}
2014-09-05 16:21:34 +04:00
2016-02-07 11:27:55 +03:00
return ERR_PTR ( err ) ;
2014-09-05 16:21:34 +04:00
}
2015-06-28 17:24:55 +03:00
/**
2016-02-07 11:27:55 +03:00
* clk_hw_register_gpio_gate - register a gpio clock gate with the clock
* framework
2015-06-28 17:24:55 +03:00
* @ dev : device that is registering this clock
* @ name : name of this clock
* @ parent_name : name of this clock ' s parent
2017-09-24 19:19:18 +03:00
* @ gpiod : gpio descriptor to gate this clock
2015-06-28 17:24:55 +03:00
* @ flags : clock flags
*/
2016-02-07 11:27:55 +03:00
struct clk_hw * clk_hw_register_gpio_gate ( struct device * dev , const char * name ,
2017-09-24 19:19:18 +03:00
const char * parent_name , struct gpio_desc * gpiod ,
2015-06-28 17:24:55 +03:00
unsigned long flags )
{
return clk_register_gpio ( dev , name ,
( parent_name ? & parent_name : NULL ) ,
2017-09-24 19:19:18 +03:00
( parent_name ? 1 : 0 ) , gpiod , flags ,
2015-06-28 17:24:55 +03:00
& clk_gpio_gate_ops ) ;
}
2016-02-07 11:27:55 +03:00
EXPORT_SYMBOL_GPL ( clk_hw_register_gpio_gate ) ;
struct clk * clk_register_gpio_gate ( struct device * dev , const char * name ,
2017-09-24 19:19:18 +03:00
const char * parent_name , struct gpio_desc * gpiod ,
2016-02-07 11:27:55 +03:00
unsigned long flags )
{
struct clk_hw * hw ;
2017-09-24 19:19:18 +03:00
hw = clk_hw_register_gpio_gate ( dev , name , parent_name , gpiod , flags ) ;
2016-02-07 11:27:55 +03:00
if ( IS_ERR ( hw ) )
return ERR_CAST ( hw ) ;
return hw - > clk ;
}
2014-09-05 16:21:34 +04:00
EXPORT_SYMBOL_GPL ( clk_register_gpio_gate ) ;
2015-06-28 17:24:55 +03:00
/**
2016-02-07 11:27:55 +03:00
* clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
2015-06-28 17:24:55 +03:00
* @ dev : device that is registering this clock
* @ name : name of this clock
* @ parent_names : names of this clock ' s parents
* @ num_parents : number of parents listed in @ parent_names
2017-09-24 19:19:18 +03:00
* @ gpiod : gpio descriptor to gate this clock
2015-06-28 17:24:55 +03:00
* @ flags : clock flags
*/
2016-02-07 11:27:55 +03:00
struct clk_hw * clk_hw_register_gpio_mux ( struct device * dev , const char * name ,
2017-09-24 19:19:18 +03:00
const char * const * parent_names , u8 num_parents , struct gpio_desc * gpiod ,
unsigned long flags )
2015-06-28 17:24:55 +03:00
{
if ( num_parents ! = 2 ) {
pr_err ( " mux-clock %s must have 2 parents \n " , name ) ;
return ERR_PTR ( - EINVAL ) ;
}
return clk_register_gpio ( dev , name , parent_names , num_parents ,
2017-09-24 19:19:18 +03:00
gpiod , flags , & clk_gpio_mux_ops ) ;
2015-06-28 17:24:55 +03:00
}
2016-02-07 11:27:55 +03:00
EXPORT_SYMBOL_GPL ( clk_hw_register_gpio_mux ) ;
struct clk * clk_register_gpio_mux ( struct device * dev , const char * name ,
2017-09-24 19:19:18 +03:00
const char * const * parent_names , u8 num_parents , struct gpio_desc * gpiod ,
unsigned long flags )
2016-02-07 11:27:55 +03:00
{
struct clk_hw * hw ;
hw = clk_hw_register_gpio_mux ( dev , name , parent_names , num_parents ,
2017-09-24 19:19:18 +03:00
gpiod , flags ) ;
2016-02-07 11:27:55 +03:00
if ( IS_ERR ( hw ) )
return ERR_CAST ( hw ) ;
return hw - > clk ;
}
2015-06-28 17:24:55 +03:00
EXPORT_SYMBOL_GPL ( clk_register_gpio_mux ) ;
2016-02-03 04:09:26 +03:00
static int gpio_clk_driver_probe ( struct platform_device * pdev )
2014-09-05 16:21:34 +04:00
{
2016-02-03 04:09:26 +03:00
struct device_node * node = pdev - > dev . of_node ;
const char * * parent_names , * gpio_name ;
2016-02-20 04:31:52 +03:00
unsigned int num_parents ;
2017-09-24 19:19:18 +03:00
struct gpio_desc * gpiod ;
2016-02-03 04:09:26 +03:00
struct clk * clk ;
2017-09-24 19:19:18 +03:00
bool is_mux ;
int ret ;
2016-02-03 04:09:26 +03:00
num_parents = of_clk_get_parent_count ( node ) ;
if ( num_parents ) {
parent_names = devm_kcalloc ( & pdev - > dev , num_parents ,
sizeof ( char * ) , GFP_KERNEL ) ;
if ( ! parent_names )
return - ENOMEM ;
2014-09-05 16:21:34 +04:00
2016-02-03 04:09:26 +03:00
of_clk_parent_fill ( node , parent_names , num_parents ) ;
} else {
parent_names = NULL ;
2014-09-05 16:21:34 +04:00
}
2016-02-03 04:09:26 +03:00
is_mux = of_device_is_compatible ( node , " gpio-mux-clock " ) ;
2017-09-24 19:19:18 +03:00
gpio_name = is_mux ? " select " : " enable " ;
2017-09-24 19:19:19 +03:00
gpiod = devm_gpiod_get ( & pdev - > dev , gpio_name , GPIOD_OUT_LOW ) ;
2017-09-24 19:19:18 +03:00
if ( IS_ERR ( gpiod ) ) {
ret = PTR_ERR ( gpiod ) ;
if ( ret = = - EPROBE_DEFER )
2018-08-28 18:44:29 +03:00
pr_debug ( " %pOFn: %s: GPIOs not yet available, retry later \n " ,
node , __func__ ) ;
2015-06-28 17:24:55 +03:00
else
2018-08-28 18:44:29 +03:00
pr_err ( " %pOFn: %s: Can't get '%s' named GPIO property \n " ,
node , __func__ ,
2016-02-03 04:09:26 +03:00
gpio_name ) ;
2017-09-24 19:19:18 +03:00
return ret ;
2014-09-05 16:21:34 +04:00
}
2016-02-03 04:09:26 +03:00
if ( is_mux )
clk = clk_register_gpio_mux ( & pdev - > dev , node - > name ,
2017-09-24 19:19:18 +03:00
parent_names , num_parents , gpiod , 0 ) ;
2016-02-03 04:09:26 +03:00
else
clk = clk_register_gpio_gate ( & pdev - > dev , node - > name ,
2017-09-24 19:19:18 +03:00
parent_names ? parent_names [ 0 ] : NULL , gpiod ,
0 ) ;
2016-02-03 04:09:26 +03:00
if ( IS_ERR ( clk ) )
return PTR_ERR ( clk ) ;
2014-09-05 16:21:34 +04:00
2016-02-03 04:09:26 +03:00
return of_clk_add_provider ( node , of_clk_src_simple_get , clk ) ;
2015-06-28 17:24:55 +03:00
}
2016-02-03 04:09:26 +03:00
static const struct of_device_id gpio_clk_match_table [ ] = {
{ . compatible = " gpio-mux-clock " } ,
{ . compatible = " gpio-gate-clock " } ,
{ }
} ;
2015-06-28 17:24:55 +03:00
2016-02-03 04:09:26 +03:00
static struct platform_driver gpio_clk_driver = {
. probe = gpio_clk_driver_probe ,
. driver = {
. name = " gpio-clk " ,
. of_match_table = gpio_clk_match_table ,
} ,
} ;
builtin_platform_driver ( gpio_clk_driver ) ;