linux/drivers/rtc/rtc-bq32k.c
<<
>>
Prefs
   1/*
   2 * Driver for TI BQ32000 RTC.
   3 *
   4 * Copyright (C) 2009 Semihalf.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 */
  10
  11#include <linux/module.h>
  12#include <linux/i2c.h>
  13#include <linux/rtc.h>
  14#include <linux/init.h>
  15#include <linux/errno.h>
  16#include <linux/bcd.h>
  17
  18#define BQ32K_SECONDS           0x00    /* Seconds register address */
  19#define BQ32K_SECONDS_MASK      0x7F    /* Mask over seconds value */
  20#define BQ32K_STOP              0x80    /* Oscillator Stop flat */
  21
  22#define BQ32K_MINUTES           0x01    /* Minutes register address */
  23#define BQ32K_MINUTES_MASK      0x7F    /* Mask over minutes value */
  24#define BQ32K_OF                0x80    /* Oscillator Failure flag */
  25
  26#define BQ32K_HOURS_MASK        0x3F    /* Mask over hours value */
  27#define BQ32K_CENT              0x40    /* Century flag */
  28#define BQ32K_CENT_EN           0x80    /* Century flag enable bit */
  29
  30struct bq32k_regs {
  31        uint8_t         seconds;
  32        uint8_t         minutes;
  33        uint8_t         cent_hours;
  34        uint8_t         day;
  35        uint8_t         date;
  36        uint8_t         month;
  37        uint8_t         years;
  38};
  39
  40static struct i2c_driver bq32k_driver;
  41
  42static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
  43{
  44        struct i2c_client *client = to_i2c_client(dev);
  45        struct i2c_msg msgs[] = {
  46                {
  47                        .addr = client->addr,
  48                        .flags = 0,
  49                        .len = 1,
  50                        .buf = &off,
  51                }, {
  52                        .addr = client->addr,
  53                        .flags = I2C_M_RD,
  54                        .len = len,
  55                        .buf = data,
  56                }
  57        };
  58
  59        if (i2c_transfer(client->adapter, msgs, 2) == 2)
  60                return 0;
  61
  62        return -EIO;
  63}
  64
  65static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
  66{
  67        struct i2c_client *client = to_i2c_client(dev);
  68        uint8_t buffer[len + 1];
  69
  70        buffer[0] = off;
  71        memcpy(&buffer[1], data, len);
  72
  73        if (i2c_master_send(client, buffer, len + 1) == len + 1)
  74                return 0;
  75
  76        return -EIO;
  77}
  78
  79static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
  80{
  81        struct bq32k_regs regs;
  82        int error;
  83
  84        error = bq32k_read(dev, &regs, 0, sizeof(regs));
  85        if (error)
  86                return error;
  87
  88        tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
  89        tm->tm_min = bcd2bin(regs.minutes & BQ32K_SECONDS_MASK);
  90        tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
  91        tm->tm_mday = bcd2bin(regs.date);
  92        tm->tm_wday = bcd2bin(regs.day) - 1;
  93        tm->tm_mon = bcd2bin(regs.month) - 1;
  94        tm->tm_year = bcd2bin(regs.years) +
  95                                ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
  96
  97        return rtc_valid_tm(tm);
  98}
  99
 100static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
 101{
 102        struct bq32k_regs regs;
 103
 104        regs.seconds = bin2bcd(tm->tm_sec);
 105        regs.minutes = bin2bcd(tm->tm_min);
 106        regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
 107        regs.day = bin2bcd(tm->tm_wday + 1);
 108        regs.date = bin2bcd(tm->tm_mday);
 109        regs.month = bin2bcd(tm->tm_mon + 1);
 110
 111        if (tm->tm_year >= 100) {
 112                regs.cent_hours |= BQ32K_CENT;
 113                regs.years = bin2bcd(tm->tm_year - 100);
 114        } else
 115                regs.years = bin2bcd(tm->tm_year);
 116
 117        return bq32k_write(dev, &regs, 0, sizeof(regs));
 118}
 119
 120static constsdrivers/rtc/rtc-bq32k.c#L117" id="Lne" nL5m"sref">regs))="Lne" nL5m"ref="+code=regs" cla_set_time<5m"sref">regs)_set_time<5m" href="="drivers/rtc/rtc-bq32k.c#L112" id="L21" class1="line" name="L21">  21<1/a>
<12    if (bq32k_rtc(struct <(bq32k_rtc_read_time(struct <="drivers/rtc/rtc-bq32k.c#L56" id="L122" class1="line" name="L22">  22<1/a>#d122   if (bq32k_rtc/a>(struct < (bq32k_rtc_set_time(struct <="drivers/rtc/rtc-bq32k.c#L56" id="L1213" class="line" name="L23">  23<1/a>#d1fine   24<1/a>#d12href="drivers/rtc/rtc-bq32k.c#L65" id="L125" class1="line" name="L25">  25<1/a>
<12ic int bq32k_rtc_set_tprob"ct i2c_client *client =   26<1/a>#d12            return  *i2c_master_se> *i2c_masterdhref="drivers/rtc/rtc-bq32k.c#L101" id="L27" class1="line" name="L27">  27<1/a>#d1fine <="drivers/rtc/rtc-bq32k.c#L112" id="L218" class="line" name="L28">  28<1/a>#d12    device *dev, struct a href="+code=off" claslass="sref">client->dev, struct ="drivers/rtc/rtc-bq32k.c#L58" id="L1219" class="line" name="L29">  29<1/a>
<129   devicedevice  30<1/a>st13    uint8_t regs));
uct ="drivers/rtc/rtc-bq32k.c#L58" id="L131" class1="line" name="L31">  31<1/a>  1     error;
  32<1/a>  13href="drivers/rtc/rtc-bq32k.c#L73" id="L133" class1="line" name="L33">  33<1/a>  1     tm_mdayclient->adapter, ,FUNC_/a>sref">adapter,FUNC_/a>a href"drivers/rtc/rtc-bq32k.c#L73" id="L134" class1="line" name="L34">  34<1/a>  13            return 0;
adapter  35<1/a>  13href="drivers/rtc/rtc-bq32k.c#L76" id="L136" class1="line" name="L36">  36<1/a>  1     /* Century flheckor Stop flat */
  37<1/a>  1     error = bq32k_read(dev, &regs));
uct ="+code=msgs" claONDS" class="sref">BQ32K_SECONDS         ,ref="drivers/rtc/rtc-bq32k.c#L110" id="L38" class1="line" name="L38">  38<1/a>};138   error = regs));
uct  href="+code=BQ32K_SECONDS_MAs="sref">BQ32K_STOP         )ef="drivers/rtc/rtc-bq32k.c#L112" id="L39" class1="line" name="L39">  39<1/a>
<13                    .+code=dev" class="_war"sref">tm_mondev, &it ang">"r Stop flatwas halted. Ret   40<1/a>st14            return 0;+code=regs" class=sref">regs));
uct  href== ~+code=BQ32K_SECONDS_MAs="sref">BQ32K_STOP         ="drivers/rtc/rtc-bq32k.c#L110" id="L41" class1="line" name="L41">  41<1/a>
<14            }, {
error = bq32k_write(dev, &regs));
uct ="+code=msgs" claONDS" class="sref">BQ32K_SECONDS         ,ref="drivers/rtc/rtc-bq32k.c#L110" id="L42" class1="line" name="L42">  42<1/a>st142   if (  43<1/a>{
14    if (error)
  44<1/a>  14            return 0;
error;
  45<1/a>  14href="drivers/rtc/rtc-bq32k.c#L76" id="L146" class1="line" name="L46">  46<1/a>  14    /* Century flheckor Stop flatlag */
  47<1/a>  14    error = bq32k_read(dev, &regs));
uct ="+code=msgs" claONDS" class="sref">BQ32K_MINUTES         ,ref="drivers/rtc/rtc-bq32k.c#L110" id="L48" class1="line" name="L48">  48<1/a>  148   error = regs));
uct  href="+code=BQ32K_SECONDS_M="sref">BQ32K_OF         )ef="drivers/rtc/rtc-bq32k.c#L112" id="L49" class1="line" name="L49">  49<1/a>  1                     .+code=dev" class="_war"sref">tm_mondev, &it ang">"r Stop flatlag */<.flheckoRTC bat  50<1/a>  1                     .+code=regs" class=sref">regs));
uct  href== ~+code=BQ32K_SECONDS_M="sref">BQ32K_OF         ="drivers/rtc/rtc-bq32k.c#L110" id="L51" class1="line" name="L51">  51<1/a>  1             }, {
error = bq32k_write(dev, &regs));
uct ="+code=msgs" claONDS" class="sref">BQ32K_MINUTES         ,ref="drivers/rtc/rtc-bq32k.c#L110" id="L52" class1="line" name="L52">  52<1/a>  152   if (  53<1/a>  15    if (error)
  54<1/a>  1                     .
error;
  55<1/a>  15href="drivers/rtc/rtc-bq32k.c#L76" id="L156" class1="line" name="L56">  56<1/a>  15    devicedevicelass="sref">bq32k_driver;
bq32k_dri>;
  sref">bq32k_dri">  p;client->dev, struct ="drivers/rtc/rtc-bq32k.c#L56" id="L157" class1="line" name="L57">  57<1/a>  15                    .regs)_set_time<5m" hre="+code=msgs" claTHIS_MODULEsref">regs)THIS_MODULE href="drivers/rtc/rtc-bq32k.c#L116" id="L58" class1="line" name="L58">  58<1/a>
<158   adapterdevice  59<1/a>  15                    .
adapterdevice  60<1/a>  16href="drivers/rtc/rtc-bq32k.c#L111" id="L61" class1="line" name="L61">  61<1/a>
<16    (>->ss="sref">data,t" c/a>(>->ss="ref="+code=dev" claslass="sref">client->device  62<1/a>  16href="drivers/rtc/rtc-bq32k.c#L73" id="L163" class1="line" name="L63">  63<1/a>}
16    if (  64<1/a>
<1 href=="drivers/rtc/rtc-bq32k.c#L119" id="L65" class1="line" name="L65">  65<1/a>st16href="drivers/rtc/rtc-bq32k.c#L76" id="L166" class1="line" name="L66">  66<1/a>{
1a hreft clientbq32k_rtc_set_tiemov"ct i2c_client *client =   67<1/a>  16ine <="drivers/rtc/rtc-bq32k.c#L112" id="L68" class1="line" name="L68">  68<1/a>  16    devicedevicedata,t" cga>(>->ss="ref="+code=dev" claslass="sref">client->  69<1/a>
<1 href="drivers/rtc/rtc-bq32k.c#L70" id="L170" class1="line" name="L70">  70<1/a>  1     devicedevice  71<1/a>  1       72<1/a>
<1 href=="drivers/rtc/rtc-bq32k.c#L119" id="L73" class1="line" name="L73">  73<1/a>  17href="drivers/rtc/rtc-bq32k.c#L104" id="L74" class1="line" name="L74">  74<1/a>  1      nstsdrivers/rtc/rtc-bq32k.c#L117" ir *i2c_master_se> *i2c_maste_set_ti"href href="drivers/rtc/rtc-bq32k.c#L46" id="L175" class1="line" name="L75">  75<1/a>
<17            <{h+s="comment">it ang">"46" 000"a href=, 0 }="drivers/rtc/rtc-bq32k.c#L56" id="L176" class1="line" name="L76">  76<1/a>  1     return -<{r="drivers/rtc/rtc-bq32k.c#L119" id="L77" class1="line" name="L77">  77<1/a>}
1a href=="drivers/rtc/rtc-bq32k.c#L75" id="L178" class1="line" name="L78">  78<1/a>
<1 href=+code=regs" claMODULE_DEVICE_TABLEsref">regs)MODULE_DEVICE_TABLEref="+code=dev" clasr_ssref">i2c_master_si2c_maste_set_ti"hrefe="drivers/rtc/rtc-bq32k.c#L16" id="L179" class1="line" name="L79">  79<1/a>st17href="drivers/rtc/rtc-bq32k.c#L70" id="L180" class1="line" name="L80">  80<1/a>{
18ic struct i2c_driver bq32k_driver;
  81<1/a>  18    if (bq32k_dri>;
  82<1/a>  18              sref">bq32k_dri">  p;it ang">"46" k"a href=,"drivers/rtc/rtc-bq32k.c#L46" id="L183" class1="line" name="L83">  83<1/a>
<18            bq32k_driown="      ="+code=bq32k_reaTHIS_MODULEsref">regs)THIS_MODULE hre,"drivers/rtc/rtc-bq32k.c#L46" id="L184" class1="line" name="L84">  84<1/a>  1       85<1/a>  1     if (bq32k_rtcprob"ct < bq32k_rtc_set_tprob"ct <="drivers/rtc/rtc-bq32k.c#L56" id="L186" class1="line" name="L86">  86<1/a>  1             r"+code=years" cl" mov"sref">bq32k_rtciemov"ct <clientiemov"sref">bq32k_rtc_set_tiemov"ct <)="drivers/rtc/rtc-bq32k.c#L56" id="L187" class1="line" name="L87">  87<1/a>
<187           r"+code=years" cl(i2c_driveri2c_maste_set_ti"href="drivers/rtc/rtc-bq32k.c#L56" id="L188" class=""line" name="L18">  18
<18a href="drivers/rtc/rtc-bq32k.c#L39" id="L189" class1="line" name="L89">  89<1/a>  18href="drivers/rtc/rtc-bq32k.c#L70" id="L190" class1="line" name="L90">  90<1/a>  1     <+code=bq32k_reamodule_r" class="sref">i2c_drivemodule_r" class="ref="+code=dev" clastelass="sref">bq32k_driver;
  91<1/a>  19href="drivers/rtc/rtc-bq32k.c#L62" id="L192" class1="line" name="L92">  92<1/a>  1     <+code=regs" claMODULE_AUTHORsref">adapterit ang">"Semihalf, Piotr Ziecik <kosmo@semihalf.comef=""a href=f="drivers/rtc/rtc-bq32k.c#L110" id="L93" class1="line" name="L93">  93<1/a>  1     <+code=regs" claMODULE_DESCR:e=regs" claMODULE_DESCR:e=regs" claMODULE_DESCR:e=regs" cldC.c#L107" id="L10ESCR:e=regs" cldC.lt;kosmo@semihalf.comef=""aTI "L52L17 I2C110"  93<1/lass1="line" name="L93">  93<1/a>  1     <+code=regs"drivers/ass="sref">tm->i2c_master_s  93<1/a>  1     <+code=regscode=date   ((lxs@f="ux.noode=r
lxs.f="ux.no kindly hosted by 0x8http://www.redpill-f="pro.no">Redpill L="pro A="line"provider of L="uxer_seuld="L and opera