2019-05-19 16:51:31 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2008-04-25 07:11:44 +04:00
/*
* DS1287 clockevent driver
*
2013-01-22 15:59:30 +04:00
* Copyright ( C ) 2008 Yoichi Yuasa < yuasa @ linux - mips . org >
2008-04-25 07:11:44 +04:00
*/
# include <linux/clockchips.h>
# include <linux/init.h>
# include <linux/interrupt.h>
# include <linux/mc146818rtc.h>
2010-10-07 17:08:54 +04:00
# include <linux/irq.h>
2008-04-25 07:11:44 +04:00
# include <asm/time.h>
int ds1287_timer_state ( void )
{
return ( CMOS_READ ( RTC_REG_C ) & RTC_PF ) ! = 0 ;
}
int ds1287_set_base_clock ( unsigned int hz )
{
u8 rate ;
switch ( hz ) {
case 128 :
rate = 0x9 ;
break ;
case 256 :
rate = 0x8 ;
break ;
case 1024 :
rate = 0x6 ;
break ;
default :
return - EINVAL ;
}
CMOS_WRITE ( RTC_REF_CLCK_32KHZ | rate , RTC_REG_A ) ;
return 0 ;
}
static int ds1287_set_next_event ( unsigned long delta ,
struct clock_event_device * evt )
{
return - EINVAL ;
}
2015-07-06 14:11:56 +03:00
static int ds1287_shutdown ( struct clock_event_device * evt )
2008-04-25 07:11:44 +04:00
{
u8 val ;
spin_lock ( & rtc_lock ) ;
val = CMOS_READ ( RTC_REG_B ) ;
2015-07-06 14:11:56 +03:00
val & = ~ RTC_PIE ;
CMOS_WRITE ( val , RTC_REG_B ) ;
2008-04-25 07:11:44 +04:00
2015-07-06 14:11:56 +03:00
spin_unlock ( & rtc_lock ) ;
return 0 ;
}
2008-04-25 07:11:44 +04:00
2015-07-06 14:11:56 +03:00
static int ds1287_set_periodic ( struct clock_event_device * evt )
{
u8 val ;
spin_lock ( & rtc_lock ) ;
val = CMOS_READ ( RTC_REG_B ) ;
val | = RTC_PIE ;
2008-04-25 07:11:44 +04:00
CMOS_WRITE ( val , RTC_REG_B ) ;
spin_unlock ( & rtc_lock ) ;
2015-07-06 14:11:56 +03:00
return 0 ;
2008-04-25 07:11:44 +04:00
}
static void ds1287_event_handler ( struct clock_event_device * dev )
{
}
static struct clock_event_device ds1287_clockevent = {
2015-07-06 14:11:56 +03:00
. name = " ds1287 " ,
. features = CLOCK_EVT_FEAT_PERIODIC ,
. set_next_event = ds1287_set_next_event ,
. set_state_shutdown = ds1287_shutdown ,
. set_state_periodic = ds1287_set_periodic ,
. tick_resume = ds1287_shutdown ,
. event_handler = ds1287_event_handler ,
2008-04-25 07:11:44 +04:00
} ;
static irqreturn_t ds1287_interrupt ( int irq , void * dev_id )
{
struct clock_event_device * cd = & ds1287_clockevent ;
/* Ack the RTC interrupt. */
CMOS_READ ( RTC_REG_C ) ;
cd - > event_handler ( cd ) ;
return IRQ_HANDLED ;
}
int __init ds1287_clockevent_init ( int irq )
{
2020-03-05 14:57:53 +03:00
unsigned long flags = IRQF_PERCPU | IRQF_TIMER ;
2008-04-25 07:11:44 +04:00
struct clock_event_device * cd ;
cd = & ds1287_clockevent ;
cd - > rating = 100 ;
cd - > irq = irq ;
clockevent_set_clock ( cd , 32768 ) ;
cd - > max_delta_ns = clockevent_delta2ns ( 0x7fffffff , cd ) ;
2017-03-30 22:47:32 +03:00
cd - > max_delta_ticks = 0x7fffffff ;
2008-04-25 07:11:44 +04:00
cd - > min_delta_ns = clockevent_delta2ns ( 0x300 , cd ) ;
2017-03-30 22:47:32 +03:00
cd - > min_delta_ticks = 0x300 ;
2008-12-13 13:50:26 +03:00
cd - > cpumask = cpumask_of ( 0 ) ;
2008-04-25 07:11:44 +04:00
clockevents_register_device ( & ds1287_clockevent ) ;
2020-03-05 14:57:53 +03:00
return request_irq ( irq , ds1287_interrupt , flags , " ds1287 " , NULL ) ;
2008-04-25 07:11:44 +04:00
}