2019-05-27 08:55:01 +02:00
// SPDX-License-Identifier: GPL-2.0-or-later
2013-10-11 11:41:41 +02:00
/*
* Copyright ( C ) 2013 Boris BREZILLON < b . brezillon @ overkiz . com >
*/
# include <linux/clk-provider.h>
# include <linux/clkdev.h>
# include <linux/clk/at91_pmc.h>
# include <linux/of.h>
2014-09-07 08:14:29 +02:00
# include <linux/mfd/syscon.h>
# include <linux/regmap.h>
2013-10-11 11:41:41 +02:00
# include "pmc.h"
# define SYSTEM_MAX_ID 31
# define SYSTEM_MAX_NAME_SZ 32
# define to_clk_system(hw) container_of(hw, struct clk_system, hw)
struct clk_system {
struct clk_hw hw ;
2014-09-07 08:14:29 +02:00
struct regmap * regmap ;
2013-10-11 11:41:41 +02:00
u8 id ;
} ;
2014-03-11 10:00:34 +01:00
static inline int is_pck ( int id )
{
return ( id > = 8 ) & & ( id < = 15 ) ;
}
2014-09-07 08:14:29 +02:00
static inline bool clk_system_ready ( struct regmap * regmap , int id )
{
unsigned int status ;
regmap_read ( regmap , AT91_PMC_SR , & status ) ;
2020-07-22 10:38:17 +03:00
return ! ! ( status & ( 1 < < id ) ) ;
2014-09-07 08:14:29 +02:00
}
2014-03-11 10:00:34 +01:00
static int clk_system_prepare ( struct clk_hw * hw )
2013-10-11 11:41:41 +02:00
{
struct clk_system * sys = to_clk_system ( hw ) ;
2014-09-07 08:14:29 +02:00
regmap_write ( sys - > regmap , AT91_PMC_SCER , 1 < < sys - > id ) ;
2014-03-11 10:00:34 +01:00
if ( ! is_pck ( sys - > id ) )
return 0 ;
2015-09-16 23:47:39 +02:00
while ( ! clk_system_ready ( sys - > regmap , sys - > id ) )
cpu_relax ( ) ;
2013-10-11 11:41:41 +02:00
return 0 ;
}
2014-03-11 10:00:34 +01:00
static void clk_system_unprepare ( struct clk_hw * hw )
2013-10-11 11:41:41 +02:00
{
struct clk_system * sys = to_clk_system ( hw ) ;
2014-09-07 08:14:29 +02:00
regmap_write ( sys - > regmap , AT91_PMC_SCDR , 1 < < sys - > id ) ;
2013-10-11 11:41:41 +02:00
}
2014-03-11 10:00:34 +01:00
static int clk_system_is_prepared ( struct clk_hw * hw )
2013-10-11 11:41:41 +02:00
{
struct clk_system * sys = to_clk_system ( hw ) ;
2014-09-07 08:14:29 +02:00
unsigned int status ;
regmap_read ( sys - > regmap , AT91_PMC_SCSR , & status ) ;
2013-10-11 11:41:41 +02:00
2014-09-07 08:14:29 +02:00
if ( ! ( status & ( 1 < < sys - > id ) ) )
2014-03-11 10:00:34 +01:00
return 0 ;
if ( ! is_pck ( sys - > id ) )
return 1 ;
2014-09-07 08:14:29 +02:00
regmap_read ( sys - > regmap , AT91_PMC_SR , & status ) ;
2020-07-22 10:38:17 +03:00
return ! ! ( status & ( 1 < < sys - > id ) ) ;
2013-10-11 11:41:41 +02:00
}
static const struct clk_ops system_ops = {
2014-03-11 10:00:34 +01:00
. prepare = clk_system_prepare ,
. unprepare = clk_system_unprepare ,
. is_prepared = clk_system_is_prepared ,
2013-10-11 11:41:41 +02:00
} ;
2018-10-16 16:21:44 +02:00
struct clk_hw * __init
2014-09-07 08:14:29 +02:00
at91_clk_register_system ( struct regmap * regmap , const char * name ,
2015-09-16 23:47:39 +02:00
const char * parent_name , u8 id )
2013-10-11 11:41:41 +02:00
{
struct clk_system * sys ;
2016-06-01 14:31:22 -07:00
struct clk_hw * hw ;
2013-10-11 11:41:41 +02:00
struct clk_init_data init ;
2016-06-01 14:31:22 -07:00
int ret ;
2013-10-11 11:41:41 +02:00
if ( ! parent_name | | id > SYSTEM_MAX_ID )
return ERR_PTR ( - EINVAL ) ;
sys = kzalloc ( sizeof ( * sys ) , GFP_KERNEL ) ;
if ( ! sys )
return ERR_PTR ( - ENOMEM ) ;
init . name = name ;
init . ops = & system_ops ;
init . parent_names = & parent_name ;
init . num_parents = 1 ;
2014-07-08 18:21:16 +02:00
init . flags = CLK_SET_RATE_PARENT ;
2013-10-11 11:41:41 +02:00
sys - > id = id ;
sys - > hw . init = & init ;
2014-09-07 08:14:29 +02:00
sys - > regmap = regmap ;
2013-10-11 11:41:41 +02:00
2016-06-01 14:31:22 -07:00
hw = & sys - > hw ;
ret = clk_hw_register ( NULL , & sys - > hw ) ;
if ( ret ) {
2013-10-11 11:41:41 +02:00
kfree ( sys ) ;
2016-06-01 14:31:22 -07:00
hw = ERR_PTR ( ret ) ;
}
2013-10-11 11:41:41 +02:00
2016-06-01 14:31:22 -07:00
return hw ;
2013-10-11 11:41:41 +02:00
}