linux/drivers/rtc/rtc-palmas.c
<<
>>
Prefs
   1/*
   2 * rtc-palmas.c -- Palmas Real Time Clock driver.
   3
   4 * RTC driver for TI Palma series devices like TPS65913,
   5 * TPS65914 power management IC.
   6 *
   7 * Copyright (c) 2012, NVIDIA Corporation.
   8 *
   9 * Author: Laxman Dewangan <ldewangan@nvidia.com>
  10 *
  11 * This program is free software; you can redistribute it and/or
  12 * modify it under the terms of the GNU General Public License as
  13 * published by the Free Software Foundation version 2.
  14 *
  15 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  16 * whether express or implied; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18 * General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23 * 02111-1307, USA
  24 */
  25
  26#include <linux/bcd.h>
  27#include <linux/errno.h>
  28#include <linux/init.h>
  29#include <linux/interrupt.h>
  30#include <linux/kernel.h>
  31#include <linux/mfd/palmas.h>
  32#include <linux/module.h>
  33#include <linux/rtc.h>
  34#include <linux/types.h>
  35#include <linux/platform_device.h>
  36#include <linux/pm.h>
  37
  38struct palmas_rtc {
  39        struct rtc_device       *rtc;
  40        struct device           *dev;
  41        unsigned int            irq;
  42};
  43
  44/* Total number of RTC registers needed to set time*/
  45#define PALMAS_NUM_TIME_REGS    (PALMAS_YEARS_REG - PALMAS_SECONDS_REG + 1)
  46
  47static int palmas_rtc_read_time(struct device *dev, struct rtc_time *tm)
  48{
  49        unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
  50        struct palmas *palmas = dev_get_drvdata(dev->parent);
  51        int ret;
  52
  53        /* Copy RTC counting registers to static registers or latches */
  54        ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
  55                PALMAS_RTC_CTRL_REG_GET_TIME, PALMAS_RTC_CTRL_REG_GET_TIME);
  56        if (ret < 0) {
  57                dev_err(dev, "RTC CTRL reg update failed, err: %d\n", ret);
  58                return ret;
  59        }
  60
  61        ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
  62                        rtc_data, PALMAS_NUM_TIME_REGS);
  63        if (ret < 0) {
  64                dev_err(dev, "RTC_SECONDS reg read failed, err = %d\n", ret);
  65                return ret;
  66        }
  67
  68        tm->tm_sec = bcd2bin(rtc_data[0]);
  69        tm->tm_min = bcd2bin(rtc_data[1]);
  70        tm->tm_hour = bcd2bin(rtc_data[2]);
  71        tm->tm_mday = bcd2bin(rtc_data[3]);
  72        tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
  73        tm->tm_year = bcd2bin(rtc_data[5]) + 100;
  74
  75        return ret;
  76}
  77
  78static int palmas_rtc_set_time(struct device *dev, struct rtc_time *tm)
  79{
  80        unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
  81        struct palmas *palmas = dev_get_drvdata(dev->parent);
  82        int ret;
  83
  84        rtc_data[0] = bin2bcd(tm->tm_sec);
  85        rtc_data[1] = bin2bcd(tm->tm_min);
  86        rtc_data[2] = bin2bcd(tm->tm_hour);
  87        rtc_data[3] = bin2bcd(tm->tm_mday);
  88        rtc_data[4] = bin2bcd(tm->tm_mon + 1);
  89        rtc_data[5] = bin2bcd(tm->tm_year - 100);
  90
  91        /* Stop RTC while updating the RTC time registers */
  92        ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
  93                PALMAS_RTC_CTRL_REG_STOP_RTC, 0);
  94        if (ret < 0) {
  95                dev_err(dev, "RTC stop failed, err = %d\n", ret);
  96                return ret;
  97        }
  98
  99        ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
 100                rtc_data, PALMAS_NUM_TIME_REGS);
 101        if (ret < 0) {
 102                dev_err(dev, "RTC_SECONDS reg write failed, err = %d\n", ret);
 103                return ret;
 104        }
 105
 106        /* Start back RTC */
 107        ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
 108                PALMAS_RTC_CTRL_REG_STOP_RTC, PALMAS_RTC_CTRL_REG_STOP_RTC);
 109        if (ret < 0)
 110                dev_err(dev, "RTC start failed, err = %d\n", ret);
 111        return ret;
 112}
 113
 114static int palmas_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
 115{
 116        struct palmas *palmas = dev_get_drvdata(dev->parent);
 117        u8 val;
 118
 119        val = enabled ? PALMAS_RTC_INTERRUPTS_REG_IT_ALARM : 0;
 120        return palmas_write(palmas, PALMAS_RTC_BASE,
 121                PALMAS_RTC_INTERRUPTS_REG, val);
 122}
 123
 124static int palmas_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
 125{
 126        unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
 127        u32 int_val;
 128        struct palmas *palmas = dev_get_drvdata(dev->parent);
 129        int ret;
 130
 131        ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE,
 132                        PALMAS_ALARM_SECONDS_REG,
 133                        alarm_data, PALMAS_NUM_TIME_REGS);
 134        if (ret < 0) {
 135                dev_err(dev, "RTC_ALARM_SECONDS read failed, err = %d\n", ret);
 136                return ret;
 137        }
 138
 139        alm->time.tm_sec = bcd2bin(alarm_data[0]);
 140        alm->time.tm_min = bcd2bin(alarm_data[1]);
 141        alm->time.tm_hour = bcd2bin(alarm_data[2]);
 142        alm->time.tm_mday = bcd2bin(alarm_data[3]);
 143        alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
 144        alm->time.tm_year = bcd2bin(alarm_data[5]) + 100;
 145
 146        ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_INTERRUPTS_REG,
 147                        &int_val);
 148        if (ret < 0) {
 149                dev_err(dev, "RTC_INTERRUPTS reg read failed, err = %d\n", ret);
 150                return ret;
 151        }
 152
 153        if (int_val & PALMAS_RTC_INTERRUPTS_REG_IT_ALARM)
 154                alm->enabled = 1;
 155        return ret;
 156}
 157
 158static int palmas_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
 159{
 160        unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
 161        struct palmas *palmas = dev_get_drvdata(dev->parent);
 162        int ret;
 163
 164        ret = palmas_rtc_alarm_irq_enable(dev, 0);
 165        if (ret < 0) {
 166                dev_err(dev, "Disable RTC alarm failed\n");
 167                return ret;
 168        }
 169
 170        alarm_data[0] = bin2bcd(alm->time.tm_sec);
 171        alarm_data[1] = bin2bcd(alm->time.tm_min);
 172        alarm_data[2] = bin2bcd(alm->time.tm_hour);
 173        alarm_data[3] = bin2bcd(alm->time.tm_mday);
 174        alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
 175        alarm_data[5] = bin2bcd(alm->time.tm_year - 100);
 176
 177        ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE,
 178                PALMAS_ALARM_SECONDS_REG, alarm_data, PALMAS_NUM_TIME_REGS);
 179        if (ret < 0) {
 180                dev_err(dev, "ALARM_SECONDS_REG write failed, err = %d\n", ret);
 181                return ret;
 182        }
 183
 184        if (alm->enabled)
 185                ret = palmas_rtc_alarm_irq_enable(dev, 1);
 186        return ret;
 187}
 188
 189static int palmas_clear_interrupts(struct device *dev)
 190{
 191        struct palmas *palmas = dev_get_drvdata(dev->parent);
 192        unsigned int rtc_reg;
 193        int ret;
 194
 195        ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
 196                                &rtc_reg);
 197        if (ret < 0) {
 198                dev_err(dev, "RTC_STATUS read failed, err = %d\n", ret);
 199                return ret;
 200        }
 201
 202        ret = palmas_write(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
 203                        rtc_reg);
 204        if (ret < 0) {
 205                dev_err(dev, "RTC_STATUS write failed, err = %d\n", ret);
 206                return ret;
20as.c#L138" id="L138" class="line" name="L138"> 1382/a>      2          1452/a>      2 if ( 1382/a>      2          1312/a>      2 return palmas_rtc_al class="sa>( =  1902/a>}
 205
 *( = ((coclaxsa>( 1452/a>static2int  *dev)
palmas_rtc_aa>();
 1452/a>       ef="drivers/rtc/rtc-palm2s.c#L21sref">ret 
 1942/a>      2 struct  1772/a>      2 u8 palmas_write((struct d href="drivers/rtc/rtc-palmas.c#L190" id=="L194" class="line" name="L194"> 1942/a>      2f="drivers/rtc/rtc-palma2.c#L121sref">ret < 0) {
 1492/a>      2 dev_err(dev, "RTC_INTERRUPTS reg read failed, err = %d\n&  class="s evicef="+code=ret" class="sref">ret);
 206      2 return ret;
 1942/a>      2          1522/a>}
 1532/a>
alarm_data[3] = cla="sref"irq/rtc-palmas.c#L cla="sref"irqmas, );
( 206static2int ;
 1942/a>       ef="drivers/rtc/rtc-palm2s.c#L226" id="L152" class="line" name="L152"> 1522/a>      2 unsigned char  1772/a>      2 u32rupts" f">rtc_wkalrm *palmas_rtc_alopstruct pa="L149" class="line" name="L149"> 1492/a>      2 struct pr - 100);
palmas_rtc_al clasode=tm" class="sreflmas_rtc_al clasode=" id="L203" class="line" name="L203"> 203      2 int retr - 100);
(struct  "sref"" class="sref">palmas_rtc_ala>(struct ="L203" class="line" name="L203"> 203
pr - 100);
sref"" class="sref">palmas_rtc_al clas>(struct ="L203" class="line" name="L203"> 203      2 ret =  - 100);
(s>(struct "sref"" class="sref">palmas_rtc_ala>(s(struct ="L203" class="line" name="L203"> 203}
 - 100);
(dev, 1e" class="sref">palmas_rtc_alarm_irq_enable(dev, 1="L203" class="line" name="L203"> 203
 1942/a>      2 if ( 1952/a>      2         palmas_rtc_set_alarmprob/a>(device * = "RTC_INTEp190" id="L190" class="line" name="L190"> 1902/a>      2         return  1492/a>      2 }
 *palmas = dev_get_drvdata(dev->"RTC_INTEp190" ident);
 - 100);
 1922/a>
palmas *( = ((NULLa>( 1942/a>      2 ret;
 1302/a>      2  1312/a>      2 alm->((190m_kzallola>("RTC_INTEp190" ident);
 *( 203      2 PALMAGFP_KERNELa>( 1922/a>      2 alm-> <!a> *( 1922/a>      2 alm->eg" clas/a>;
 * 1302/a>      2f="drivers/rtc/rtc-palma2.c#L126" id="L146" class="line" name="L146"> 1462/a>      2 ret = 
 1072/a>      2                 &u8 palmas_write((struct dc_reg);
"RTC_INTEp190" ident);
 1922/a>
ret < 0) {
 1492/a>      2         dev_err(dev, );
"RTC_INTEp190" ident);
ret);
 206      2         return ret;
 1512/a>      2 }
 1522/a>
 1532/a>      2 if (alarm_data[3] =lmas_rtc_aa>();
);
"RTC_INTEp190" ident);
 1512/a>      2         alma> *"RTC_INTEp190" id
( 1512/a>      2 return  1462/a>}
 1072/a>
ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
 1082/a>static2int dev_err =  1082/a>      2ef="drivers/rtc/rtc-palm2s.c#L25 class="sref">dev_err =  1512/a>      2 unsigned char  1492/a>      2 struct ret<(dev, );
"RTC_INTEp190" ident);
ret);
 206      2 int ;
 1512/a>
 1522/a>      2  1952/a>      2 if (ret = );
palmas_updatlatfoREGref"irq/rtc-palmas.c#LtlatfoREGref"irq>->"RTC_INTEp190" id
 1762/a>      2          1772/a>      2         return ret = );
palmas_upda claSde=de#L20islastruct "RTC_INTEp190" ident);
);
"RTC_INTEp190" ident);
 1772/a>static2 }
dev_err = <<<<<<<<);
 206      2f="drivers/rtc/rtc-palma2.c#L126sref">ret < 0) {
, );
 1492/a>      2 dev_err(palmas_updaPTR_ERR"+code=PALMAS_RPTR_ERRmas, );
 206      2 ret<(dev, );
"RTC_INTEp190" ident);
ret);
 206      2 ;
 1512/a>      2  1522/a>      2  1952/a>      2 alarm_data[5] =almas_update_bits" class="sref">palmas_updarequest_th claed"irq/rtc-palmas.c#L equest_th claed"irqmas, );
 1772/a>
( 1772/a>      2 in; 0) {
 1772/a>static2         in; 0) {
, <,"L177" class="line" name="L177"> 1772/a>      2 if (dev_err = dc_reg);
"RTC_INTEp190" ident);
( 1512/a>      2          1492/a>      2         return ret<(dev, );
"RTC_INTEp190" ident);
ret);
 206      2 }
palmas_upda claSde=de#unL20islastruct ();
 206
;
 1512/a>      2 if (alm="L152" class="line" name="L152"> 1522/a>      2          1462/a>      2 return ret);
, );
"RTC_INTEp190" ident);
 1862/a>}
;
 1452/a>
 1522/a>      2int  1702/a>{
palmas_rtc_set_alarmremov=tm" class="sreflmas_rtc_al cmov=>,  * = "RTC_INTEp190" id="L190" class="line" name="L190"> 1902/a>      2 struct  1492/a>      2 unsigned int ( = ((tlatfoREGref" href="+code=dev" clasplatfoREGref">dev->"RTC_INTEp190" idd="L186" class="line" name="L186"> 1862/a>      2 int  1842/a>
alma> *dev, 1sc_reg);
"RTC_INTEp190" ident);
 1762/a>      2 ret = , );
( 1512/a>      2                        2&29s="sref">ret);
, );
 206      2 if (;
 1452/a>      2          1522/a>      2         return  1703/a>      3 }
( 1703/1>      3 struct palmas_rtc_set_alarmsuspenda>(,  *dev)
 1903/a>      3  2053/a>      3                  *( = ((a(dev->par)="L206" class="line" name="L206"> 2063/a>      3 if ( 1953/a>      3         (par)="L190" class="line" name="L190"> 1903/6>      3                        3+code3ret" class="sref">ret<;wakerent" class="srdevwakemas, );
 2063/7>      3 if (30code=int_val"/a>;
 1453/a>      3          1523/a>      3 if ( 1703/a>      3         palmas_rtc_set_alarmresue=tm" class="sreflmas_rtc_al csue=>,  *dev)
 1903/a>      3 return  1493/a>}
( = ((a(dev->par)="L206" class="line" name="L206"> 2063/a>
 1843/a>static3int (par)="L190" class="line" name="L190"> 1903/a>      3ef="drivers/rtc/rtc-palm3s.c#L31sref">retef">ret);
wakerent" class="srrhref="ref">wakemas, );
 2063/a>      3 struct ret;
 1453/a>      3  1883/a>      3f="drivers/rtc/rtc-palma3.c#L131sref"#end=f"L188" class="line" name="L188"> 1883/a>      3  1703/a>      3 return  *palmas_rtc_set_alarmpmlopstruct  1493/a>      3         alm->, , (pode=d" clmresue=tm" class="sreflmas_rtc_al csue=>, <="L190" class="line" name="L190"> 1903/a>}
 1943/a>
 1843/a>static3int rtc_wkalrm *palmas_rtc_set_alarmSs="liv" class="sref"p_set_alarmSs="lilmas 1493/a>      3ef="drivers/rtc/rtc-palm3s.c#L32sref">retr - 100);
(a>ef">ret<" class="sref">palmas_rtc_alprob/a>(,"L177" class="line" name="L177"> 1773/a>      3 unsigned char retr - 100);
ret<" class="sref">palmas_rtc_alremov=tm" class="sreflmas_rtc_al cmov=>, <,"L177" class="line" name="L177"> 1773/a>      3 retr - 100);
ret<" ="L149" class="line" name="L149"> 1493/a>      3 struct  - 100);
paTHIS_MODUL="+code=PALMAS_RTHIS_MODUL=6" i,"L177" class="line" name="L177"> 1773/a>      3 int dev_errr - 100);
"RTC_INTEa hr" idet<" c read failed, err = %dL177">-me=ef">ret);
"L177" class="line" name="L177"> 1773/a>
dev_errr - 100);
sref"" c_reg);
 1773/a>      3 ret = <}
"L177" class="line" name="L177"> 1773/a>}
 1943/a>
 1843/a>      3 if ();
,  2063/a>      3          1463/a>      3         return ,  1673/a>      3 }
 1673/a>      3f="drivers/rtc/rtc-palma3.c#L133lass=a>,  1673/a>      3 ,  1673/a>      3 


LXR ="drunity>, lxs@codux.no>, <.
Redpill Lodpro Aamas<, provider of Loduxaconsult="drand operptsons serc=des sincee=re5.