linux/drivers/rtc/rtc-max8997.c
<<
>>
Prefs
   1/*
   2 * RTC driver for Maxim MAX8997
   3 *
   4 * Copyright (C) 2013 Samsung Electronics Co.Ltd
   5 *
   6 *  based on rtc-max8998.c
   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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16
  17#include <linux/slab.h>
  18#include <linux/rtc.h>
  19#include <linux/delay.h>
  20#include <linux/mutex.h>
  21#include <linux/module.h>
  22#include <linux/platform_device.h>
  23#include <linux/mfd/max8997-private.h>
  24#include <linux/irqdomain.h>
  25
  26/* Module parameter for WTSR function control */
  27static int wtsr_en = 1;
  28module_param(wtsr_en, int, 0444);
  29MODULE_PARM_DESC(wtsr_en, "Watchdog Timeout & Software Reset (default=on)");
  30/* Module parameter for SMPL function control */
  31static int smpl_en = 1;
  32module_param(smpl_en, int, 0444);
  33MODULE_PARM_DESC(smpl_en, "Sudden Momentary Power Loss (default=on)");
  34
  35/* RTC Control Register */
  36#define BCD_EN_SHIFT                    0
  37#define BCD_EN_MASK                     (1 << BCD_EN_SHIFT)
  38#define MODEL24_SHIFT                   1
  39#define MODEL24_MASK                    (1 << MODEL24_SHIFT)
  40/* RTC Update Register1 */
  41#define RTC_UDR_SHIFT                   0
  42#define RTC_UDR_MASK                    (1 << RTC_UDR_SHIFT)
  43/* WTSR and SMPL Register */
  44#define WTSRT_SHIFT                     0
  45#define SMPLT_SHIFT                     2
  46#define WTSR_EN_SHIFT                   6
  47#define SMPL_EN_SHIFT                   7
  48#define WTSRT_MASK                      (3 << WTSRT_SHIFT)
  49#define SMPLT_MASK                      (3 << SMPLT_SHIFT)
  50#define WTSR_EN_MASK                    (1 << WTSR_EN_SHIFT)
  51#define SMPL_EN_MASK                    (1 << SMPL_EN_SHIFT)
  52/* RTC Hour register */
  53#define HOUR_PM_SHIFT                   6
  54#define HOUR_PM_MASK                    (1 << HOUR_PM_SHIFT)
  55/* RTC Alarm Enable */
  56#define ALARM_ENABLE_SHIFT              7
  57#define ALARM_ENABLE_MASK               (1 << ALARM_ENABLE_SHIFT)
  58
  59enum {
  60        RTC_SEC = 0,
  61        RTC_MIN,
  62        RTC_HOUR,
  63        RTC_WEEKDAY,
  64        RTC_MONTH,
  65        RTC_YEAR,
  66        RTC_DATE,
  67        RTC_NR_TIME
  68};
  69
  70struct max8997_rtc_info {
  71        struct device           *dev;
  72        struct max8997_dev      *max8997;
  73        struct i2c_client       *rtc;
  74        struct rtc_device       *rtc_dev;
  75        struct mutex            lock;
  76        int virq;
  77        int rtc_24hr_mode;
  78};
  79
  80static void max8997_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
  81                                   int rtc_24hr_mode)
  82{
  83        tm->tm_sec = data[RTC_SEC] & 0x7f;
  84        tm->tm_min = data[RTC_MIN] & 0x7f;
  85        if (rtc_24hr_mode)
  86                tm->tm_hour = data[RTC_HOUR] & 0x1f;
  87        else {
  88                tm->tm_hour = data[RTC_HOUR] & 0x0f;
  89                if (data[RTC_HOUR] & HOUR_PM_MASK)
  90                        tm->tm_hour += 12;
  91        }
  92
  93        tm->tm_wday = fls(data[RTC_WEEKDAY] & 0x7f) - 1;
  94        tm->tm_mday = data[RTC_DATE] & 0x1f;
  95        tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
  96        tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
  97        tm->tm_yday = 0;
  98        tm->tm_isdst = 0;
  99}
 100
 101static int max8997_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
 102{
 103        data[RTC_SEC] = tm->tm_sec;
 104        data[RTC_MIN] = tm->tm_min;
 105        data[RTC_HOUR] = tm->tm_hour;
 106        data[RTC_WEEKDAY] = 1 << tm->tm_wday;
 107        data[RTC_DATE] = tm->tm_mday;
 108        data[RTC_MONTH] = tm->tm_mon + 1;
 109        data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
 110
 111        if (tm->tm_year < 100) {
 112                pr_warn("RTC cannot handle the year %d.  Assume it's 2000.\n",
 113                        1900 + tm->tm_year);
 114                return -EINVAL;
 115        }
 116        return 0;
 117}
 118
 119static inline int max8997_rtc_set_update_reg(struct max8997_rtc_info *info)
 120{
 121        int ret;
 122
 123        ret = max8997_write_reg(info->rtc, MAX8997_RTC_UPDATE1,
 124                                                RTC_UDR_MASK);
 125        if (ret < 0)
 126                dev_err(info->dev, "%s: fail to write update reg(%d)\n",
 127                                __func__, ret);
 128        else {
 129                /* Minimum 16ms delay required before RTC update.
 130                 * Otherwise, we may read and update based on out-of-date
 131                 * value */
 132                msleep(20);
 133        }
 134
 135        return ret;
 136}
 137
 138static int max8997_rtc_read_time(struct device *dev, struct rtc_time *tm)
 139{
 140        struct max8997_rtc_info *info = dev_get_drvdata(dev);
 141        u8 data[RTC_NR_TIME];
 142        int ret;
 143
 144        mutex_lock(&info->lock);
 145        ret = max8997_bulk_read(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
 146        mutex_unlock(&info->lock);
 147
 148        if (ret < 0) {
 149                dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__,
 150                                ret);
 151                return ret;
 152        }
 153
 154        max8997_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
 155
 156        return rtc_valid_tm(tm);
 157}
 158
 159static int max8997_rtc_set_time(struct device *dev, struct rtc_time *tm)
 160{
 161        struct max8997_rtc_info *info = dev_get_drvdata(dev);
 162        u8 data[RTC_NR_TIME];
 163        int ret;
 164
 165        ret = max8997_rtc_tm_to_data(tm, data);
 166        if (ret < 0)
 167                return ret;
 168
 169        mutex_lock(&info->lock);
 170
 171        ret = max8997_bulk_write(info->rtc, MAX8997_RTC_SEC, RTC_NR_TIME, data);
 172        if (ret < 0) {
 173                dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
 174                                ret);
 175                goto out;
 176        }
 177
 178        ret = max8997_rtc_set_update_reg(info);
 179out:
 180        mutex_unlock(&info->lock);
 181        return ret;
 182}
 183
 184static int max8997_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 185{
 186        struct max8997_rtc_info *info = dev_get_drvdata(dev);
 187        u8 data[RTC_NR_TIME];
 188        u8 val;
 189        int i, ret;
 190
 191        mutex_lock(&info->lock);
 192
 193        ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
 194                        data);
 195        if (ret < 0) {
 196                dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
 197                                __func__, __LINE__, ret);
 198                goto out;
 199        }
 200
 201        max8997_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
 202
 203        alrm->enabled = 0;
 204        for (i = 0; i < RTC_NR_TIME; i++) {
 205                if (data[i] & ALARM_ENABLE_MASK) {
 206                        alrm->enabled = 1;
 207                        break;
 208                }
 209        }
 210
 211        alrm->pending = 0;
 212        ret = max8997_read_reg(info->max8997->i2c, MAX8997_REG_STATUS1, &val);
 213        if (ret < 0) {
 214                dev_err(info->dev, "%s:%d fail to read status1 reg(%d)\n",
 215                                __func__, __LINE__, ret);
 216                goto out;
 217        }
 218
 219        if (val & (1 << 4)) /* RTCA1 */
 220                alrm->pending = 1;
 221
 222out:
 223        mutex_unlock(&info->lock);
 224        return 0;
 225}
 226
 227static int max8997_rtc_stop_alarm(struct max8997_rtc_info *info)
 228{
 229        u8 data[RTC_NR_TIME];
 230        int ret, i;
 231
 232        if (!mutex_is_locked(&info->lock))
 233                dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
 234
 235        ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
 236                                data);
 237        if (ret < 0) {
 238                dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
 239                                __func__, ret);
 240                goto out;
 241        }
 242
 243        for (i = 0; i < RTC_NR_TIME; i++)
 244                data[i] &= ~ALARM_ENABLE_MASK;
 245
 246        ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
 247                                 data);
 248        if (ret < 0) {
 249                dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
 250                                __func__, ret);
 251                goto out;
 252        }
 253
 254        ret = max8997_rtc_set_update_reg(info);
 255out:
 256        return ret;
 257}
 258
 259static int max8997_rtc_start_alarm(struct max8997_rtc_info *info)
 260{
 261        u8 data[RTC_NR_TIME];
 262        int ret;
 263
 264        if (!mutex_is_locked(&info->lock))
 265                dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
 266
 267        ret = max8997_bulk_read(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
 268                                data);
 269        if (ret < 0) {
 270                dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
 271                                __func__, ret);
 272                goto out;
 273        }
 274
 275        data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
 276        data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
 277        data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
 278        data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
 279        if (data[RTC_MONTH] & 0xf)
 280                data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
 281        if (data[RTC_YEAR] & 0x7f)
 282                data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
 283        if (data[RTC_DATE] & 0x1f)
 284                data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
 285
 286        ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
 287                                 data);
 288        if (ret < 0) {
 289                dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
 290                                __func__, ret);
 291                goto out;
 292        }
 293
 294        ret = max8997_rtc_set_update_reg(info);
 295out:
 296        return ret;
 297}
 298static int max8997_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 299{
 300        struct max8997_rtc_info *info = dev_get_drvdata(dev);
 301        u8 data[RTC_NR_TIME];
 302        int ret;
 303
 304        ret = max8997_rtc_tm_to_data(&alrm->time, data);
 305        if (ret < 0)
 306                return ret;
 307
 308        dev_info(info->dev, "%s: %d-%02d-%02d %02d:%02d:%02d\n", __func__,
 309                        data[RTC_YEAR] + 2000, data[RTC_MONTH], data[RTC_DATE],
 310                        data[RTC_HOUR], data[RTC_MIN], data[RTC_SEC]);
 311
 312        mutex_lock(&info->lock);
 313
 314        ret = max8997_rtc_stop_alarm(info);
 315        if (ret < 0)
 316                goto out;
 317
 318        ret = max8997_bulk_write(info->rtc, MAX8997_RTC_ALARM1_SEC, RTC_NR_TIME,
 319                                data);
 320        if (ret < 0) {
 321                dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
 322                                __func__, ret);
 323                goto out;
 324        }
 325
 326        ret = max8997_rtc_set_update_reg(info);
 327        if (ret < 0)
 328                goto out;
 329
 330        if (alrm->enabled)
 331                ret = max8997_rtc_start_alarm(info);
 332out:
 333        mutex_unlock(&info->lock);
 334        return ret;
 335}
 336
 337static int max8997_rtc_alarm_irq_enable(struct device *dev,
 338                                        unsigned int enabled)
 339{
 340        struct max8997_rtc_info *info = dev_get_drvdata(dev);
 341        int ret;
 342
 343        mutex_lock(&info->lock);
 344        if (enabled)
 345                ret = max8997_rtc_start_alarm(info);
 346        else
 347                ret = max8997_rtc_stop_alarm(info);
 348        mutex_unlock(&info->lock);
 349
 350        return ret;
 351}
 352
 353static irqreturn_t max8997_rtc_alarm_irq(int irq, void *data)
 354{
 355        struct max8997_rtc_info *info = data;
 356
 357        dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
 358
 359        rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
 360
 361        return IRQ_HANDLED;
 362}
 363
 364static const struct rtc_class_ops max8997_rtc_ops = {
 365        .read_time = max8997_rtc_read_time,
 366        .set_time = max8997_rtc_set_time,
 367        .read_alarm = max8997_rtc_read_alarm,
 368        .set_alarm = max8997_rtc_set_alarm,
 369        .alarm_irq_enable = max8997_rtc_alarm_irq_enable,
 370};
 371
 372static void max8997_rtc_enable_wtsr(struct max8997_rtc_info *info, bool enable)
 373{
 374        int ret;
 375        u8 val, mask;
 376
 377        if (!wtsr_en)
 378                return;
 379
 380        if (enable)
 381                val = (1 << WTSR_EN_SHIFT) | (3 << WTSRT_SHIFT);
 382        else
 383                val = 0;
 384
 385        mask = WTSR_EN_MASK | WTSRT_MASK;
 386
 387        dev_info(info->dev, "%s: %s WTSR\n", __func__,
 388                        enable ? "enable" : "disable");
 389
 390        ret = max8997_update_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, val, mask);
 391        if (ret < 0) {
 392                dev_err(info->dev, "%s: fail to update WTSR reg(%d)\n",
 393                                __func__, ret);
 394                return;
 395        }
 396
 397        max8997_rtc_set_update_reg(info);
 398}
 399
 400static void max8997_rtc_enable_smpl(struct max8997_rtc_info *info, bool enable)
 401{
 402        int ret;
 403        u8 val, mask;
 404
 405        if (!smpl_en)
 406                return;
 407
 408        if (enable)
 409                val = (1 << SMPL_EN_SHIFT) | (0 << SMPLT_SHIFT);
 410        else
 411                val = 0;
 412
 413        mask = SMPL_EN_MASK | SMPLT_MASK;
 414
 415        dev_info(info->dev, "%s: %s SMPL\n", __func__,
 416                        enable ? "enable" : "disable");
 417
 418        ret = max8997_update_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, val, mask);
 419        if (ret < 0) {
 420                dev_err(info->dev, "%s: fail to update SMPL reg(%d)\n",
 421                                __func__, ret);
 422                return;
 423        }
 424
 425        max8997_rtc_set_update_reg(info);
 426
 427        val = 0;
 428        max8997_read_reg(info->rtc, MAX8997_RTC_WTSR_SMPL, &val);
 429        pr_info("WTSR_SMPL(0x%02x)\n", val);
 430}
 431
 432static int max8997_rtc_init_reg(struct max8997_rtc_info *info)
 433{
 434        u8 data[2];
 435        int ret;
 436
 437        /* Set RTC control register : Binary mode, 24hour mdoe */
 438        data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
 439        data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
 440
 441        info->rtc_24hr_mode = 1;
 442
 443        ret = max8997_bulk_write(info->rtc, MAX8997_RTC_CTRLMASK, 2, data);
 444        if (ret < 0) {
 445                dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
 446                                __func__, ret);
 447                return ret;
 448        }
 449
 450        ret = max8997_rtc_set_update_reg(info);
 451        return ret;
 452}
 453
 454static int max8997_rtc_probe(struct platform_device *pdev)
 455{
 456        struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
 457        struct max8997_rtc_info *info;
 458        int ret, virq;
 459
 460        info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_rtc_info),
 461                        GFP_KERNEL);
 462        if (!info)
 463                return -ENOMEM;
 464
 465        mutex_init(&info->lock);
 466        info->dev = &pdev->dev;
 467        info->max8997 = max8997;
 468        info->rtc = max8997->rtc;
 469
 470        platform_set_drvdata(pdev, info);
 471
 472        ret = max8997_rtc_init_reg(info);
 473
 474        if (ret < 0) {
 475                dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
 476                return ret;
 477        }
 478
 479        max8997_rtc_enable_wtsr(info, true);
 480        max8997_rtc_enable_smpl(info, true);
 481
 482        device_init_wakeup(&pdev->dev, 1);
 483
 484        info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max8997-rtc",
 485                                        &max8997_rtc_ops, THIS_MODULE);
 486
 487        if (IS_ERR(info->rtc_dev)) {
 488                ret = PTR_ERR(info->rtc_dev);
 489                dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
 490                return ret;
 491        }
 492
 493        virq = irq_create_mapping(max8997->irq_domain, MAX8997_PMICIRQ_RTCA1);
 494        if (!virq) {
 495                dev_err(&pdev->dev, "Failed to create mapping alarm IRQ\n");
 496                ret = -ENXIO;
 497                goto err_out;
 498        }
 499        info->virq = virq;
 500
 501        ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
 502                                max8997_rtc_alarm_irq, 0,
 503                                "rtc-alarm0", info);
 504        if (ret < 0)
 505                dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
 506                        info->virq, ret);
 507
 508err_out:
 509        return ret;
 510}
 511
 512static void max8997_rtc_shutdown(struct platform_device *pdev)
 513{
 514        struct max8997_rtc_info *info = platform_get_drvdata(pdev);
 515
 516        max8997_rtc_enable_wtsr(info, false);
 517        max8997_rtc_enable_smpl(info, false);
 518}
 519
 520static const struct platform_device_id rtc_id[] = {
 521        { "max8997-rtc", 0 },
 522        {},
 523};
 524MODULE_DEVICE_TABLE(platform, rtc_id);
 525
 526static struct platform_driver max8997_rtc_driver = {
 527        .driver         = {
 528                .name   = "max8997-rtc",
 529        },
 530        .probe          = max8997_rtc_probe,
 531        .shutdown       = max8997_rtc_shutdown,
 532        .id_table       = rtc_id,
 533};
 534
 535module_platform_driver(max8997_rtc_driver);
 536
 537MODULE_DESCRIPTION("Maxim MAX8997 RTC driver");
 538MODULE_AUTHOR("<ms925.kim@samsung.com>");
 539MODULE_LICENSE("GPL");
 540