linux/drivers/rtc/rtc-max8998.c
<<
>>
Prefs
   1/*
   2 * RTC driver for Maxim MAX8998
   3 *
   4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
   5 * Author: Minkyu Kang <mk7.kang@samsung.com>
   6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
   7 *
   8 *  This program is free software; you can redistribute  it and/or modify it
   9 *  under  the terms of  the GNU General  Public License as published by the
  10 *  Free Software Foundation;  either version 2 of the  License, or (at your
  11 *  option) any later version.
  12 *
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/i2c.h>
  17#include <linux/slab.h>
  18#include <linux/bcd.h>
  19#include <linux/irqdomain.h>
  20#include <linux/rtc.h>
  21#include <linux/platform_device.h>
  22#include <linux/mfd/max8998.h>
  23#include <linux/mfd/max8998-private.h>
  24#include <linux/delay.h>
  25
  26#define MAX8998_RTC_SEC                 0x00
  27#define MAX8998_RTC_MIN                 0x01
  28#define MAX8998_RTC_HOUR                0x02
  29#define MAX8998_RTC_WEEKDAY             0x03
  30#define MAX8998_RTC_DATE                0x04
  31#define MAX8998_RTC_MONTH               0x05
  32#define MAX8998_RTC_YEAR1               0x06
  33#define MAX8998_RTC_YEAR2               0x07
  34#define MAX8998_ALARM0_SEC              0x08
  35#define MAX8998_ALARM0_MIN              0x09
  36#define MAX8998_ALARM0_HOUR             0x0a
  37#define MAX8998_ALARM0_WEEKDAY          0x0b
  38#define MAX8998_ALARM0_DATE             0x0c
  39#define MAX8998_ALARM0_MONTH            0x0d
  40#define MAX8998_ALARM0_YEAR1            0x0e
  41#define MAX8998_ALARM0_YEAR2            0x0f
  42#define MAX8998_ALARM1_SEC              0x10
  43#define MAX8998_ALARM1_MIN              0x11
  44#define MAX8998_ALARM1_HOUR             0x12
  45#define MAX8998_ALARM1_WEEKDAY          0x13
  46#define MAX8998_ALARM1_DATE             0x14
  47#define MAX8998_ALARM1_MONTH            0x15
  48#define MAX8998_ALARM1_YEAR1            0x16
  49#define MAX8998_ALARM1_YEAR2            0x17
  50#define MAX8998_ALARM0_CONF             0x18
  51#define MAX8998_ALARM1_CONF             0x19
  52#define MAX8998_RTC_STATUS              0x1a
  53#define MAX8998_WTSR_SMPL_CNTL          0x1b
  54#define MAX8998_TEST                    0x1f
  55
  56#define HOUR_12                         (1 << 7)
  57#define HOUR_PM                         (1 << 5)
  58#define ALARM0_STATUS                   (1 << 1)
  59#define ALARM1_STATUS                   (1 << 2)
  60
  61enum {
  62        RTC_SEC = 0,
  63        RTC_MIN,
  64        RTC_HOUR,
  65        RTC_WEEKDAY,
  66        RTC_DATE,
  67        RTC_MONTH,
  68        RTC_YEAR1,
  69        RTC_YEAR2,
  70};
  71
  72struct max8998_rtc_info {
  73        struct device           *dev;
  74        struct max8998_dev      *max8998;
  75        struct i2c_client       *rtc;
  76        struct rtc_device       *rtc_dev;
  77        int irq;
  78        bool lp3974_bug_workaround;
  79};
  80
  81static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
  82{
  83        tm->tm_sec = bcd2bin(data[RTC_SEC]);
  84        tm->tm_min = bcd2bin(data[RTC_MIN]);
  85        if (data[RTC_HOUR] & HOUR_12) {
  86                tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
  87                if (data[RTC_HOUR] & HOUR_PM)
  88                        tm->tm_hour += 12;
  89        } else
  90                tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
  91
  92        tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
  93        tm->tm_mday = bcd2bin(data[RTC_DATE]);
  94        tm->tm_mon = bcd2bin(data[RTC_MONTH]);
  95        tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
  96        tm->tm_year -= 1900;
  97}
  98
  99static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
 100{
 101        data[RTC_SEC] = bin2bcd(tm->tm_sec);
 102        data[RTC_MIN] = bin2bcd(tm->tm_min);
 103        data[RTC_HOUR] = bin2bcd(tm->tm_hour);
 104        data[RTC_WEEKDAY] = tm->tm_wday;
 105        data[RTC_DATE] = bin2bcd(tm->tm_mday);
 106        data[RTC_MONTH] = bin2bcd(tm->tm_mon);
 107        data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
 108        data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
 109}
 110
 111static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
 112{
 113        struct max8998_rtc_info *info = dev_get_drvdata(dev);
 114        u8 data[8];
 115        int ret;
 116
 117        ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
 118        if (ret < 0)
 119                return ret;
 120
 121        max8998_data_to_tm(data, tm);
 122
 123        return rtc_valid_tm(tm);
 124}
 125
 126static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
 127{
 128        struct max8998_rtc_info *info = dev_get_drvdata(dev);
 129        u8 data[8];
 130        int ret;
 131
 132        max8998_tm_to_data(tm, data);
 133
 134        ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
 135
 136        if (info->lp3974_bug_workaround)
 137                msleep(2000);
 138
 139        return ret;
 140}
 141
 142static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 143{
 144        struct max8998_rtc_info *info = dev_get_drvdata(dev);
 145        u8 data[8];
 146        u8 val;
 147        int ret;
 148
 149        ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
 150        if (ret < 0)
 151                return ret;
 152
 153        max8998_data_to_tm(data, &alrm->time);
 154
 155        ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
 156        if (ret < 0)
 157                return ret;
 158
 159        alrm->enabled = !!val;
 160
 161        ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
 162        if (ret < 0)
 163                return ret;
 164
 165        if (val & ALARM0_STATUS)
 166                alrm->pending = 1;
 167        else
 168                alrm->pending = 0;
 169
 170        return 0;
 171}
 172
 173static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
 174{
 175        int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
 176
 177        if (info->lp3974_bug_workaround)
 178                msleep(2000);
 179
 180        return ret;
 181}
 182
 183static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
 184{
 185        int ret;
 186        u8 alarm0_conf = 0x77;
 187
 188        /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
 189        if (info->lp3974_bug_workaround)
 190                alarm0_conf = 0x57;
 191
 192        ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
 193
 194        if (info->lp3974_bug_workaround)
 195                msleep(2000);
 196
 197        return ret;
 198}
 199
 200static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 201{
 202        struct max8998_rtc_info *info = dev_get_drvdata(dev);
 203        u8 data[8];
 204        int ret;
 205
 206        max8998_tm_to_data(&alrm->time, data);
 207
 208        ret = max8998_rtc_stop_alarm(info);
 209        if (ret < 0)
 210                return ret;
 211
 212        ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
 213        if (ret < 0)
 214                return ret;
 215
 216        if (info->lp3974_bug_workaround)
 217                msleep(2000);
 218
 219        if (alrm->enabled)
 220                ret = max8998_rtc_start_alarm(info);
 221
 222        return ret;
 223}
 224
 225static int max8998_rtc_alarm_irq_enable(struct device *dev,
 226                                        unsigned int enabled)
 227{
 228        struct max8998_rtc_info *info = dev_get_drvdata(dev);
 229
 230        if (enabled)
 231                return max8998_rtc_start_alarm(info);
 232        else
 233                return max8998_rtc_stop_alarm(info);
 234}
 235
 236static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
 237{
 238        struct max8998_rtc_info *info = data;
 239
 240        rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
 241
 242        return IRQ_HANDLED;
 243}
 244
 245static const struct rtc_class_ops max8998_rtc_ops = {
 246        .read_time = max8998_rtc_read_time,
 247        .set_time = max8998_rtc_set_time,
 248        .read_alarm = max8998_rtc_read_alarm,
 249        .set_alarm = max8998_rtc_set_alarm,
 250        .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
 251};
 252
 253static int max8998_rtc_probe(struct platform_device *pdev)
 254{
 255        struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
 256        struct max8998_platform_data *pdata = max8998->pdata;
 257        struct max8998_rtc_info *info;
 258        int ret;
 259
 260        info = devm_kzalloc(&pdev->dev, sizeof(struct max8998_rtc_info),
 261                        GFP_KERNEL);
 262        if (!info)
 263                return -ENOMEM;
 264
 265        info->dev = &pdev->dev;
 266        info->max8998 = max8998;
 267        info->rtc = max8998->rtc;
 268
 269        platform_set_drvdata(pdev, info);
 270
 271        info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8998-rtc",
 272                        &max8998_rtc_ops, THIS_MODULE);
 273
 274        if (IS_ERR(info->rtc_dev)) {
 275                ret = PTR_ERR(info->rtc_dev);
 276                dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
 277                return ret;
 278        }
 279
 280        if (!max8998->irq_domain)
 281                goto no_irq;
 282
 283        info->irq = irq_create_mapping(max8998->irq_domain, MAX8998_IRQ_ALARM0);
 284        if (!info->irq) {
 285                dev_warn(&pdev->dev, "Failed to map alarm IRQ\n");
 286                goto no_irq;
 287        }
 288
 289        ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
 290                                max8998_rtc_alarm_irq, 0, "rtc-alarm0", info);
 291
 292        if (ret < 0)
 293                dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
 294                        info->irq, ret);
 295
 296no_irq:
 297        dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
 298        if (pdata && pdata->rtc_delay) {
 299                info->lp3974_bug_workaround = true;
 300                dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
 301                                " RTC updates will be extremely slow.\n");
 302        }
 303
 304        return 0;
 305}
 306
 307static const struct platform_device_id max8998_rtc_id[] = {
 308        { "max8998-rtc", TYPE_MAX8998 },
 309        { "lp3974-rtc", TYPE_LP3974 },
 310        { }
 311};
 312
 313static struct platform_driver max8998_rtc_driver = {
 314        .driver         = {
 315                .name   = "max8998-rtc",
 316                .owner  = THIS_MODULE,
 317        },
 318        .probe          = max8998_rtc_probe,
 319        .id_table       = max8998_rtc_id,
 320};
 321
 322module_platform_driver(max8998_rtc_driver);
 323
 324MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
 325MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
 326MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
 327MODULE_LICENSE("GPL");
 328
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.