root/drivers/rtc/rtc-bd70528.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bd70528_set_wake
  2. bd70528_set_elapsed_tmr
  3. bd70528_set_rtc_based_timers
  4. bd70528_re_enable_rtc_based_timers
  5. bd70528_disable_rtc_based_timers
  6. tmday2rtc
  7. tm2rtc
  8. rtc2tm
  9. bd70528_set_alarm
  10. bd70528_read_alarm
  11. bd70528_set_time_locked
  12. bd70528_set_time
  13. bd70528_get_time
  14. bd70528_alm_enable
  15. alm_hndlr
  16. bd70528_probe

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 //
   3 // Copyright (C) 2018 ROHM Semiconductors
   4 //
   5 // RTC driver for ROHM BD70528 PMIC
   6 
   7 #include <linux/bcd.h>
   8 #include <linux/mfd/rohm-bd70528.h>
   9 #include <linux/module.h>
  10 #include <linux/of.h>
  11 #include <linux/platform_device.h>
  12 #include <linux/regmap.h>
  13 #include <linux/rtc.h>
  14 
  15 /*
  16  * We read regs RTC_SEC => RTC_YEAR
  17  * this struct is ordered according to chip registers.
  18  * Keep it u8 only to avoid padding issues.
  19  */
  20 struct bd70528_rtc_day {
  21         u8 sec;
  22         u8 min;
  23         u8 hour;
  24 } __packed;
  25 
  26 struct bd70528_rtc_data {
  27         struct bd70528_rtc_day time;
  28         u8 week;
  29         u8 day;
  30         u8 month;
  31         u8 year;
  32 } __packed;
  33 
  34 struct bd70528_rtc_wake {
  35         struct bd70528_rtc_day time;
  36         u8 ctrl;
  37 } __packed;
  38 
  39 struct bd70528_rtc_alm {
  40         struct bd70528_rtc_data data;
  41         u8 alm_mask;
  42         u8 alm_repeat;
  43 } __packed;
  44 
  45 struct bd70528_rtc {
  46         struct rohm_regmap_dev *mfd;
  47         struct device *dev;
  48 };
  49 
  50 static int bd70528_set_wake(struct rohm_regmap_dev *bd70528,
  51                             int enable, int *old_state)
  52 {
  53         int ret;
  54         unsigned int ctrl_reg;
  55 
  56         ret = regmap_read(bd70528->regmap, BD70528_REG_WAKE_EN, &ctrl_reg);
  57         if (ret)
  58                 return ret;
  59 
  60         if (old_state) {
  61                 if (ctrl_reg & BD70528_MASK_WAKE_EN)
  62                         *old_state |= BD70528_WAKE_STATE_BIT;
  63                 else
  64                         *old_state &= ~BD70528_WAKE_STATE_BIT;
  65 
  66                 if (!enable == !(*old_state & BD70528_WAKE_STATE_BIT))
  67                         return 0;
  68         }
  69 
  70         if (enable)
  71                 ctrl_reg |= BD70528_MASK_WAKE_EN;
  72         else
  73                 ctrl_reg &= ~BD70528_MASK_WAKE_EN;
  74 
  75         return regmap_write(bd70528->regmap, BD70528_REG_WAKE_EN,
  76                             ctrl_reg);
  77 }
  78 
  79 static int bd70528_set_elapsed_tmr(struct rohm_regmap_dev *bd70528,
  80                                    int enable, int *old_state)
  81 {
  82         int ret;
  83         unsigned int ctrl_reg;
  84 
  85         /*
  86          * TBD
  87          * What is the purpose of elapsed timer ?
  88          * Is the timeout registers counting down, or is the disable - re-enable
  89          * going to restart the elapsed-time counting? If counting is restarted
  90          * the timeout should be decreased by the amount of time that has
  91          * elapsed since starting the timer. Maybe we should store the monotonic
  92          * clock value when timer is started so that if RTC is set while timer
  93          * is armed we could do the compensation. This is a hack if RTC/system
  94          * clk are drifting. OTOH, RTC controlled via I2C is in any case
  95          * inaccurate...
  96          */
  97         ret = regmap_read(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
  98                           &ctrl_reg);
  99         if (ret)
 100                 return ret;
 101 
 102         if (old_state) {
 103                 if (ctrl_reg & BD70528_MASK_ELAPSED_TIMER_EN)
 104                         *old_state |= BD70528_ELAPSED_STATE_BIT;
 105                 else
 106                         *old_state &= ~BD70528_ELAPSED_STATE_BIT;
 107 
 108                 if ((!enable) == (!(*old_state & BD70528_ELAPSED_STATE_BIT)))
 109                         return 0;
 110         }
 111 
 112         if (enable)
 113                 ctrl_reg |= BD70528_MASK_ELAPSED_TIMER_EN;
 114         else
 115                 ctrl_reg &= ~BD70528_MASK_ELAPSED_TIMER_EN;
 116 
 117         return regmap_write(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
 118                             ctrl_reg);
 119 }
 120 
 121 static int bd70528_set_rtc_based_timers(struct bd70528_rtc *r, int new_state,
 122                                         int *old_state)
 123 {
 124         int ret;
 125 
 126         ret = bd70528_wdt_set(r->mfd, new_state & BD70528_WDT_STATE_BIT,
 127                               old_state);
 128         if (ret) {
 129                 dev_err(r->dev,
 130                         "Failed to disable WDG for RTC setting (%d)\n", ret);
 131                 return ret;
 132         }
 133         ret = bd70528_set_elapsed_tmr(r->mfd,
 134                                       new_state & BD70528_ELAPSED_STATE_BIT,
 135                                       old_state);
 136         if (ret) {
 137                 dev_err(r->dev,
 138                         "Failed to disable 'elapsed timer' for RTC setting\n");
 139                 return ret;
 140         }
 141         ret = bd70528_set_wake(r->mfd, new_state & BD70528_WAKE_STATE_BIT,
 142                                old_state);
 143         if (ret) {
 144                 dev_err(r->dev,
 145                         "Failed to disable 'wake timer' for RTC setting\n");
 146                 return ret;
 147         }
 148 
 149         return ret;
 150 }
 151 
 152 static int bd70528_re_enable_rtc_based_timers(struct bd70528_rtc *r,
 153                                               int old_state)
 154 {
 155         return bd70528_set_rtc_based_timers(r, old_state, NULL);
 156 }
 157 
 158 static int bd70528_disable_rtc_based_timers(struct bd70528_rtc *r,
 159                                             int *old_state)
 160 {
 161         return bd70528_set_rtc_based_timers(r, 0, old_state);
 162 }
 163 
 164 static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
 165 {
 166         d->sec &= ~BD70528_MASK_RTC_SEC;
 167         d->min &= ~BD70528_MASK_RTC_MINUTE;
 168         d->hour &= ~BD70528_MASK_RTC_HOUR;
 169         d->sec |= bin2bcd(t->tm_sec);
 170         d->min |= bin2bcd(t->tm_min);
 171         d->hour |= bin2bcd(t->tm_hour);
 172 }
 173 
 174 static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
 175 {
 176         r->day &= ~BD70528_MASK_RTC_DAY;
 177         r->week &= ~BD70528_MASK_RTC_WEEK;
 178         r->month &= ~BD70528_MASK_RTC_MONTH;
 179         /*
 180          * PM and 24H bits are not used by Wake - thus we clear them
 181          * here and not in tmday2rtc() which is also used by wake.
 182          */
 183         r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
 184 
 185         tmday2rtc(t, &r->time);
 186         /*
 187          * We do always set time in 24H mode.
 188          */
 189         r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
 190         r->day |= bin2bcd(t->tm_mday);
 191         r->week |= bin2bcd(t->tm_wday);
 192         r->month |= bin2bcd(t->tm_mon + 1);
 193         r->year = bin2bcd(t->tm_year - 100);
 194 }
 195 
 196 static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
 197 {
 198         t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
 199         t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
 200         t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
 201         /*
 202          * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
 203          * is not BCD value but tells whether it is AM or PM
 204          */
 205         if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
 206                 t->tm_hour %= 12;
 207                 if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
 208                         t->tm_hour += 12;
 209         }
 210         t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
 211         t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
 212         t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
 213         t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
 214 }
 215 
 216 static int bd70528_set_alarm(struct device *dev, struct rtc_wkalrm *a)
 217 {
 218         struct bd70528_rtc_wake wake;
 219         struct bd70528_rtc_alm alm;
 220         int ret;
 221         struct bd70528_rtc *r = dev_get_drvdata(dev);
 222         struct rohm_regmap_dev *bd70528 = r->mfd;
 223 
 224         ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_WAKE_START,
 225                                &wake, sizeof(wake));
 226         if (ret) {
 227                 dev_err(dev, "Failed to read wake regs\n");
 228                 return ret;
 229         }
 230 
 231         ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_ALM_START,
 232                                &alm, sizeof(alm));
 233         if (ret) {
 234                 dev_err(dev, "Failed to read alarm regs\n");
 235                 return ret;
 236         }
 237 
 238         tm2rtc(&a->time, &alm.data);
 239         tmday2rtc(&a->time, &wake.time);
 240 
 241         if (a->enabled) {
 242                 alm.alm_mask &= ~BD70528_MASK_ALM_EN;
 243                 wake.ctrl |= BD70528_MASK_WAKE_EN;
 244         } else {
 245                 alm.alm_mask |= BD70528_MASK_ALM_EN;
 246                 wake.ctrl &= ~BD70528_MASK_WAKE_EN;
 247         }
 248 
 249         ret = regmap_bulk_write(bd70528->regmap,
 250                                 BD70528_REG_RTC_WAKE_START, &wake,
 251                                 sizeof(wake));
 252         if (ret) {
 253                 dev_err(dev, "Failed to set wake time\n");
 254                 return ret;
 255         }
 256         ret = regmap_bulk_write(bd70528->regmap, BD70528_REG_RTC_ALM_START,
 257                                 &alm, sizeof(alm));
 258         if (ret)
 259                 dev_err(dev, "Failed to set alarm time\n");
 260 
 261         return ret;
 262 }
 263 
 264 static int bd70528_read_alarm(struct device *dev, struct rtc_wkalrm *a)
 265 {
 266         struct bd70528_rtc_alm alm;
 267         int ret;
 268         struct bd70528_rtc *r = dev_get_drvdata(dev);
 269         struct rohm_regmap_dev *bd70528 = r->mfd;
 270 
 271         ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_ALM_START,
 272                                &alm, sizeof(alm));
 273         if (ret) {
 274                 dev_err(dev, "Failed to read alarm regs\n");
 275                 return ret;
 276         }
 277 
 278         rtc2tm(&alm.data, &a->time);
 279         a->time.tm_mday = -1;
 280         a->time.tm_mon = -1;
 281         a->time.tm_year = -1;
 282         a->enabled = !(alm.alm_mask & BD70528_MASK_ALM_EN);
 283         a->pending = 0;
 284 
 285         return 0;
 286 }
 287 
 288 static int bd70528_set_time_locked(struct device *dev, struct rtc_time *t)
 289 {
 290         int ret, tmpret, old_states;
 291         struct bd70528_rtc_data rtc_data;
 292         struct bd70528_rtc *r = dev_get_drvdata(dev);
 293         struct rohm_regmap_dev *bd70528 = r->mfd;
 294 
 295         ret = bd70528_disable_rtc_based_timers(r, &old_states);
 296         if (ret)
 297                 return ret;
 298 
 299         tmpret = regmap_bulk_read(bd70528->regmap,
 300                                   BD70528_REG_RTC_START, &rtc_data,
 301                                   sizeof(rtc_data));
 302         if (tmpret) {
 303                 dev_err(dev, "Failed to read RTC time registers\n");
 304                 goto renable_out;
 305         }
 306         tm2rtc(t, &rtc_data);
 307 
 308         tmpret = regmap_bulk_write(bd70528->regmap,
 309                                    BD70528_REG_RTC_START, &rtc_data,
 310                                    sizeof(rtc_data));
 311         if (tmpret) {
 312                 dev_err(dev, "Failed to set RTC time\n");
 313                 goto renable_out;
 314         }
 315 
 316 renable_out:
 317         ret = bd70528_re_enable_rtc_based_timers(r, old_states);
 318         if (tmpret)
 319                 ret = tmpret;
 320 
 321         return ret;
 322 }
 323 
 324 static int bd70528_set_time(struct device *dev, struct rtc_time *t)
 325 {
 326         int ret;
 327         struct bd70528_rtc *r = dev_get_drvdata(dev);
 328 
 329         bd70528_wdt_lock(r->mfd);
 330         ret = bd70528_set_time_locked(dev, t);
 331         bd70528_wdt_unlock(r->mfd);
 332         return ret;
 333 }
 334 
 335 static int bd70528_get_time(struct device *dev, struct rtc_time *t)
 336 {
 337         struct bd70528_rtc *r = dev_get_drvdata(dev);
 338         struct rohm_regmap_dev *bd70528 = r->mfd;
 339         struct bd70528_rtc_data rtc_data;
 340         int ret;
 341 
 342         /* read the RTC date and time registers all at once */
 343         ret = regmap_bulk_read(bd70528->regmap,
 344                                BD70528_REG_RTC_START, &rtc_data,
 345                                sizeof(rtc_data));
 346         if (ret) {
 347                 dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
 348                 return ret;
 349         }
 350 
 351         rtc2tm(&rtc_data, t);
 352 
 353         return 0;
 354 }
 355 
 356 static int bd70528_alm_enable(struct device *dev, unsigned int enabled)
 357 {
 358         int ret;
 359         unsigned int enableval = BD70528_MASK_ALM_EN;
 360         struct bd70528_rtc *r = dev_get_drvdata(dev);
 361 
 362         if (enabled)
 363                 enableval = 0;
 364 
 365         bd70528_wdt_lock(r->mfd);
 366         ret = bd70528_set_wake(r->mfd, enabled, NULL);
 367         if (ret) {
 368                 dev_err(dev, "Failed to change wake state\n");
 369                 goto out_unlock;
 370         }
 371         ret = regmap_update_bits(r->mfd->regmap, BD70528_REG_RTC_ALM_MASK,
 372                                  BD70528_MASK_ALM_EN, enableval);
 373         if (ret)
 374                 dev_err(dev, "Failed to change alarm state\n");
 375 
 376 out_unlock:
 377         bd70528_wdt_unlock(r->mfd);
 378         return ret;
 379 }
 380 
 381 static const struct rtc_class_ops bd70528_rtc_ops = {
 382         .read_time              = bd70528_get_time,
 383         .set_time               = bd70528_set_time,
 384         .read_alarm             = bd70528_read_alarm,
 385         .set_alarm              = bd70528_set_alarm,
 386         .alarm_irq_enable       = bd70528_alm_enable,
 387 };
 388 
 389 static irqreturn_t alm_hndlr(int irq, void *data)
 390 {
 391         struct rtc_device *rtc = data;
 392 
 393         rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
 394         return IRQ_HANDLED;
 395 }
 396 
 397 static int bd70528_probe(struct platform_device *pdev)
 398 {
 399         struct bd70528_rtc *bd_rtc;
 400         struct rohm_regmap_dev *mfd;
 401         int ret;
 402         struct rtc_device *rtc;
 403         int irq;
 404         unsigned int hr;
 405 
 406         mfd = dev_get_drvdata(pdev->dev.parent);
 407         if (!mfd) {
 408                 dev_err(&pdev->dev, "No MFD driver data\n");
 409                 return -EINVAL;
 410         }
 411         bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
 412         if (!bd_rtc)
 413                 return -ENOMEM;
 414 
 415         bd_rtc->mfd = mfd;
 416         bd_rtc->dev = &pdev->dev;
 417 
 418         irq = platform_get_irq_byname(pdev, "bd70528-rtc-alm");
 419         if (irq < 0)
 420                 return irq;
 421 
 422         platform_set_drvdata(pdev, bd_rtc);
 423 
 424         ret = regmap_read(mfd->regmap, BD70528_REG_RTC_HOUR, &hr);
 425 
 426         if (ret) {
 427                 dev_err(&pdev->dev, "Failed to reag RTC clock\n");
 428                 return ret;
 429         }
 430 
 431         if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
 432                 struct rtc_time t;
 433 
 434                 ret = bd70528_get_time(&pdev->dev, &t);
 435 
 436                 if (!ret)
 437                         ret = bd70528_set_time(&pdev->dev, &t);
 438 
 439                 if (ret) {
 440                         dev_err(&pdev->dev,
 441                                 "Setting 24H clock for RTC failed\n");
 442                         return ret;
 443                 }
 444         }
 445 
 446         device_set_wakeup_capable(&pdev->dev, true);
 447         device_wakeup_enable(&pdev->dev);
 448 
 449         rtc = devm_rtc_allocate_device(&pdev->dev);
 450         if (IS_ERR(rtc)) {
 451                 dev_err(&pdev->dev, "RTC device creation failed\n");
 452                 return PTR_ERR(rtc);
 453         }
 454 
 455         rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
 456         rtc->range_max = RTC_TIMESTAMP_END_2099;
 457         rtc->ops = &bd70528_rtc_ops;
 458 
 459         /* Request alarm IRQ prior to registerig the RTC */
 460         ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
 461                                         IRQF_ONESHOT, "bd70528-rtc", rtc);
 462         if (ret)
 463                 return ret;
 464 
 465         /*
 466          *  BD70528 irq controller is not touching the main mask register.
 467          *  So enable the RTC block interrupts at main level. We can just
 468          *  leave them enabled as irq-controller should disable irqs
 469          *  from sub-registers when IRQ is disabled or freed.
 470          */
 471         ret = regmap_update_bits(mfd->regmap,
 472                                  BD70528_REG_INT_MAIN_MASK,
 473                                  BD70528_INT_RTC_MASK, 0);
 474         if (ret) {
 475                 dev_err(&pdev->dev, "Failed to enable RTC interrupts\n");
 476                 return ret;
 477         }
 478 
 479         return rtc_register_device(rtc);
 480 }
 481 
 482 static struct platform_driver bd70528_rtc = {
 483         .driver = {
 484                 .name = "bd70528-rtc"
 485         },
 486         .probe = bd70528_probe,
 487 };
 488 
 489 module_platform_driver(bd70528_rtc);
 490 
 491 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
 492 MODULE_DESCRIPTION("BD70528 RTC driver");
 493 MODULE_LICENSE("GPL");
 494 MODULE_ALIAS("platform:bd70528-rtc");

/* [<][>][^][v][top][bottom][index][help] */