Lines Matching +full:data +full:- +full:out
1 // SPDX-License-Identifier: GPL-2.0+
7 // based on rtc-max8997.c
16 #include <linux/mfd/max77686-private.h>
23 #define MAX77686_INVALID_I2C_ADDR (-1)
26 #define MAX77686_INVALID_REG (-1)
63 * struct max77686_rtc_driver_data - model-specific configuration
179 .name = "max77686-rtc",
205 .name = "max77714-rtc",
210 .num_irqs = ARRAY_SIZE(max77686_rtc_irqs) - 1, /* no WTSR on 77714 */
274 .name = "max77802-rtc",
293 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm, in max77686_rtc_data_to_tm() argument
296 u8 mask = info->drv_data->mask; in max77686_rtc_data_to_tm()
298 tm->tm_sec = data[RTC_SEC] & mask; in max77686_rtc_data_to_tm()
299 tm->tm_min = data[RTC_MIN] & mask; in max77686_rtc_data_to_tm()
300 tm->tm_hour = data[RTC_HOUR] & 0x1f; in max77686_rtc_data_to_tm()
302 /* Only a single bit is set in data[], so fls() would be equivalent */ in max77686_rtc_data_to_tm()
303 tm->tm_wday = ffs(data[RTC_WEEKDAY] & mask) - 1; in max77686_rtc_data_to_tm()
304 tm->tm_mday = data[RTC_MONTHDAY] & 0x1f; in max77686_rtc_data_to_tm()
305 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1; in max77686_rtc_data_to_tm()
306 tm->tm_year = data[RTC_YEAR] & mask; in max77686_rtc_data_to_tm()
307 tm->tm_yday = 0; in max77686_rtc_data_to_tm()
308 tm->tm_isdst = 0; in max77686_rtc_data_to_tm()
314 if (!info->drv_data->alarm_enable_reg) in max77686_rtc_data_to_tm()
315 tm->tm_year += 100; in max77686_rtc_data_to_tm()
318 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data, in max77686_rtc_tm_to_data() argument
321 data[RTC_SEC] = tm->tm_sec; in max77686_rtc_tm_to_data()
322 data[RTC_MIN] = tm->tm_min; in max77686_rtc_tm_to_data()
323 data[RTC_HOUR] = tm->tm_hour; in max77686_rtc_tm_to_data()
324 data[RTC_WEEKDAY] = 1 << tm->tm_wday; in max77686_rtc_tm_to_data()
325 data[RTC_MONTHDAY] = tm->tm_mday; in max77686_rtc_tm_to_data()
326 data[RTC_MONTH] = tm->tm_mon + 1; in max77686_rtc_tm_to_data()
328 if (info->drv_data->alarm_enable_reg) { in max77686_rtc_tm_to_data()
329 data[RTC_YEAR] = tm->tm_year; in max77686_rtc_tm_to_data()
333 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0; in max77686_rtc_tm_to_data()
335 if (tm->tm_year < 100) { in max77686_rtc_tm_to_data()
336 dev_err(info->dev, "RTC cannot handle the year %d.\n", in max77686_rtc_tm_to_data()
337 1900 + tm->tm_year); in max77686_rtc_tm_to_data()
338 return -EINVAL; in max77686_rtc_tm_to_data()
348 unsigned int data; in max77686_rtc_update() local
349 unsigned long delay = info->drv_data->delay; in max77686_rtc_update()
352 data = 1 << RTC_UDR_SHIFT; in max77686_rtc_update()
354 data = 1 << RTC_RBUDR_SHIFT; in max77686_rtc_update()
356 ret = regmap_update_bits(info->rtc_regmap, in max77686_rtc_update()
357 info->drv_data->map[REG_RTC_UPDATE0], in max77686_rtc_update()
358 data, data); in max77686_rtc_update()
360 dev_err(info->dev, "Fail to write update reg(ret=%d, data=0x%x)\n", in max77686_rtc_update()
361 ret, data); in max77686_rtc_update()
373 u8 data[RTC_NR_TIME]; in max77686_rtc_read_time() local
376 mutex_lock(&info->lock); in max77686_rtc_read_time()
380 goto out; in max77686_rtc_read_time()
382 ret = regmap_bulk_read(info->rtc_regmap, in max77686_rtc_read_time()
383 info->drv_data->map[REG_RTC_SEC], in max77686_rtc_read_time()
384 data, ARRAY_SIZE(data)); in max77686_rtc_read_time()
386 dev_err(info->dev, "Fail to read time reg(%d)\n", ret); in max77686_rtc_read_time()
387 goto out; in max77686_rtc_read_time()
390 max77686_rtc_data_to_tm(data, tm, info); in max77686_rtc_read_time()
392 out: in max77686_rtc_read_time()
393 mutex_unlock(&info->lock); in max77686_rtc_read_time()
400 u8 data[RTC_NR_TIME]; in max77686_rtc_set_time() local
403 ret = max77686_rtc_tm_to_data(tm, data, info); in max77686_rtc_set_time()
407 mutex_lock(&info->lock); in max77686_rtc_set_time()
409 ret = regmap_bulk_write(info->rtc_regmap, in max77686_rtc_set_time()
410 info->drv_data->map[REG_RTC_SEC], in max77686_rtc_set_time()
411 data, ARRAY_SIZE(data)); in max77686_rtc_set_time()
413 dev_err(info->dev, "Fail to write time reg(%d)\n", ret); in max77686_rtc_set_time()
414 goto out; in max77686_rtc_set_time()
419 out: in max77686_rtc_set_time()
420 mutex_unlock(&info->lock); in max77686_rtc_set_time()
427 u8 data[RTC_NR_TIME]; in max77686_rtc_read_alarm() local
429 const unsigned int *map = info->drv_data->map; in max77686_rtc_read_alarm()
432 mutex_lock(&info->lock); in max77686_rtc_read_alarm()
436 goto out; in max77686_rtc_read_alarm()
438 ret = regmap_bulk_read(info->rtc_regmap, map[REG_ALARM1_SEC], in max77686_rtc_read_alarm()
439 data, ARRAY_SIZE(data)); in max77686_rtc_read_alarm()
441 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret); in max77686_rtc_read_alarm()
442 goto out; in max77686_rtc_read_alarm()
445 max77686_rtc_data_to_tm(data, &alrm->time, info); in max77686_rtc_read_alarm()
447 alrm->enabled = 0; in max77686_rtc_read_alarm()
449 if (info->drv_data->alarm_enable_reg) { in max77686_rtc_read_alarm()
451 ret = -EINVAL; in max77686_rtc_read_alarm()
452 dev_err(info->dev, in max77686_rtc_read_alarm()
454 goto out; in max77686_rtc_read_alarm()
457 ret = regmap_read(info->rtc_regmap, map[REG_RTC_AE1], &val); in max77686_rtc_read_alarm()
459 dev_err(info->dev, in max77686_rtc_read_alarm()
461 goto out; in max77686_rtc_read_alarm()
465 alrm->enabled = 1; in max77686_rtc_read_alarm()
467 for (i = 0; i < ARRAY_SIZE(data); i++) { in max77686_rtc_read_alarm()
468 if (data[i] & ALARM_ENABLE_MASK) { in max77686_rtc_read_alarm()
469 alrm->enabled = 1; in max77686_rtc_read_alarm()
475 alrm->pending = 0; in max77686_rtc_read_alarm()
477 if (info->drv_data->alarm_pending_status_reg == MAX77686_INVALID_REG) in max77686_rtc_read_alarm()
478 goto out; in max77686_rtc_read_alarm()
480 ret = regmap_read(info->regmap, in max77686_rtc_read_alarm()
481 info->drv_data->alarm_pending_status_reg, &val); in max77686_rtc_read_alarm()
483 dev_err(info->dev, in max77686_rtc_read_alarm()
485 goto out; in max77686_rtc_read_alarm()
489 alrm->pending = 1; in max77686_rtc_read_alarm()
491 out: in max77686_rtc_read_alarm()
492 mutex_unlock(&info->lock); in max77686_rtc_read_alarm()
498 u8 data[RTC_NR_TIME]; in max77686_rtc_stop_alarm() local
501 const unsigned int *map = info->drv_data->map; in max77686_rtc_stop_alarm()
503 if (!mutex_is_locked(&info->lock)) in max77686_rtc_stop_alarm()
504 dev_warn(info->dev, "%s: should have mutex locked\n", __func__); in max77686_rtc_stop_alarm()
508 goto out; in max77686_rtc_stop_alarm()
510 if (info->drv_data->alarm_enable_reg) { in max77686_rtc_stop_alarm()
512 ret = -EINVAL; in max77686_rtc_stop_alarm()
513 dev_err(info->dev, in max77686_rtc_stop_alarm()
515 goto out; in max77686_rtc_stop_alarm()
518 ret = regmap_write(info->rtc_regmap, map[REG_RTC_AE1], 0); in max77686_rtc_stop_alarm()
520 ret = regmap_bulk_read(info->rtc_regmap, map[REG_ALARM1_SEC], in max77686_rtc_stop_alarm()
521 data, ARRAY_SIZE(data)); in max77686_rtc_stop_alarm()
523 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret); in max77686_rtc_stop_alarm()
524 goto out; in max77686_rtc_stop_alarm()
527 max77686_rtc_data_to_tm(data, &tm, info); in max77686_rtc_stop_alarm()
529 for (i = 0; i < ARRAY_SIZE(data); i++) in max77686_rtc_stop_alarm()
530 data[i] &= ~ALARM_ENABLE_MASK; in max77686_rtc_stop_alarm()
532 ret = regmap_bulk_write(info->rtc_regmap, map[REG_ALARM1_SEC], in max77686_rtc_stop_alarm()
533 data, ARRAY_SIZE(data)); in max77686_rtc_stop_alarm()
537 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret); in max77686_rtc_stop_alarm()
538 goto out; in max77686_rtc_stop_alarm()
542 out: in max77686_rtc_stop_alarm()
548 u8 data[RTC_NR_TIME]; in max77686_rtc_start_alarm() local
551 const unsigned int *map = info->drv_data->map; in max77686_rtc_start_alarm()
553 if (!mutex_is_locked(&info->lock)) in max77686_rtc_start_alarm()
554 dev_warn(info->dev, "%s: should have mutex locked\n", __func__); in max77686_rtc_start_alarm()
558 goto out; in max77686_rtc_start_alarm()
560 if (info->drv_data->alarm_enable_reg) { in max77686_rtc_start_alarm()
561 ret = regmap_write(info->rtc_regmap, map[REG_RTC_AE1], in max77686_rtc_start_alarm()
564 ret = regmap_bulk_read(info->rtc_regmap, map[REG_ALARM1_SEC], in max77686_rtc_start_alarm()
565 data, ARRAY_SIZE(data)); in max77686_rtc_start_alarm()
567 dev_err(info->dev, "Fail to read alarm reg(%d)\n", ret); in max77686_rtc_start_alarm()
568 goto out; in max77686_rtc_start_alarm()
571 max77686_rtc_data_to_tm(data, &tm, info); in max77686_rtc_start_alarm()
573 data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT); in max77686_rtc_start_alarm()
574 data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT); in max77686_rtc_start_alarm()
575 data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT); in max77686_rtc_start_alarm()
576 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK; in max77686_rtc_start_alarm()
577 if (data[RTC_MONTH] & 0xf) in max77686_rtc_start_alarm()
578 data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT); in max77686_rtc_start_alarm()
579 if (data[RTC_YEAR] & info->drv_data->mask) in max77686_rtc_start_alarm()
580 data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT); in max77686_rtc_start_alarm()
581 if (data[RTC_MONTHDAY] & 0x1f) in max77686_rtc_start_alarm()
582 data[RTC_MONTHDAY] |= (1 << ALARM_ENABLE_SHIFT); in max77686_rtc_start_alarm()
584 ret = regmap_bulk_write(info->rtc_regmap, map[REG_ALARM1_SEC], in max77686_rtc_start_alarm()
585 data, ARRAY_SIZE(data)); in max77686_rtc_start_alarm()
589 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret); in max77686_rtc_start_alarm()
590 goto out; in max77686_rtc_start_alarm()
594 out: in max77686_rtc_start_alarm()
601 u8 data[RTC_NR_TIME]; in max77686_rtc_set_alarm() local
604 ret = max77686_rtc_tm_to_data(&alrm->time, data, info); in max77686_rtc_set_alarm()
608 mutex_lock(&info->lock); in max77686_rtc_set_alarm()
612 goto out; in max77686_rtc_set_alarm()
614 ret = regmap_bulk_write(info->rtc_regmap, in max77686_rtc_set_alarm()
615 info->drv_data->map[REG_ALARM1_SEC], in max77686_rtc_set_alarm()
616 data, ARRAY_SIZE(data)); in max77686_rtc_set_alarm()
619 dev_err(info->dev, "Fail to write alarm reg(%d)\n", ret); in max77686_rtc_set_alarm()
620 goto out; in max77686_rtc_set_alarm()
625 goto out; in max77686_rtc_set_alarm()
627 if (alrm->enabled) in max77686_rtc_set_alarm()
629 out: in max77686_rtc_set_alarm()
630 mutex_unlock(&info->lock); in max77686_rtc_set_alarm()
640 mutex_lock(&info->lock); in max77686_rtc_alarm_irq_enable()
645 mutex_unlock(&info->lock); in max77686_rtc_alarm_irq_enable()
650 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data) in max77686_rtc_alarm_irq() argument
652 struct max77686_rtc_info *info = data; in max77686_rtc_alarm_irq()
654 dev_dbg(info->dev, "RTC alarm IRQ: %d\n", irq); in max77686_rtc_alarm_irq()
656 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); in max77686_rtc_alarm_irq()
671 u8 data[2]; in max77686_rtc_init_reg() local
675 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); in max77686_rtc_init_reg()
676 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT); in max77686_rtc_init_reg()
678 ret = regmap_bulk_write(info->rtc_regmap, in max77686_rtc_init_reg()
679 info->drv_data->map[REG_RTC_CONTROLM], in max77686_rtc_init_reg()
680 data, ARRAY_SIZE(data)); in max77686_rtc_init_reg()
682 dev_err(info->dev, "Fail to write controlm reg(%d)\n", ret); in max77686_rtc_init_reg()
692 struct device *parent = info->dev->parent; in max77686_init_rtc_regmap()
696 if (info->drv_data->rtc_irq_from_platform) { in max77686_init_rtc_regmap()
697 struct platform_device *pdev = to_platform_device(info->dev); in max77686_init_rtc_regmap()
699 info->rtc_irq = platform_get_irq(pdev, 0); in max77686_init_rtc_regmap()
700 if (info->rtc_irq < 0) in max77686_init_rtc_regmap()
701 return info->rtc_irq; in max77686_init_rtc_regmap()
703 info->rtc_irq = parent_i2c->irq; in max77686_init_rtc_regmap()
706 info->regmap = dev_get_regmap(parent, NULL); in max77686_init_rtc_regmap()
707 if (!info->regmap) { in max77686_init_rtc_regmap()
708 dev_err(info->dev, "Failed to get rtc regmap\n"); in max77686_init_rtc_regmap()
709 return -ENODEV; in max77686_init_rtc_regmap()
712 if (info->drv_data->rtc_i2c_addr == MAX77686_INVALID_I2C_ADDR) { in max77686_init_rtc_regmap()
713 info->rtc_regmap = info->regmap; in max77686_init_rtc_regmap()
717 info->rtc = devm_i2c_new_dummy_device(info->dev, parent_i2c->adapter, in max77686_init_rtc_regmap()
718 info->drv_data->rtc_i2c_addr); in max77686_init_rtc_regmap()
719 if (IS_ERR(info->rtc)) { in max77686_init_rtc_regmap()
720 dev_err(info->dev, "Failed to allocate I2C device for RTC\n"); in max77686_init_rtc_regmap()
721 return PTR_ERR(info->rtc); in max77686_init_rtc_regmap()
724 info->rtc_regmap = devm_regmap_init_i2c(info->rtc, in max77686_init_rtc_regmap()
725 info->drv_data->regmap_config); in max77686_init_rtc_regmap()
726 if (IS_ERR(info->rtc_regmap)) { in max77686_init_rtc_regmap()
727 ret = PTR_ERR(info->rtc_regmap); in max77686_init_rtc_regmap()
728 dev_err(info->dev, "Failed to allocate RTC regmap: %d\n", ret); in max77686_init_rtc_regmap()
733 ret = regmap_add_irq_chip(info->rtc_regmap, info->rtc_irq, in max77686_init_rtc_regmap()
735 0, info->drv_data->rtc_irq_chip, in max77686_init_rtc_regmap()
736 &info->rtc_irq_data); in max77686_init_rtc_regmap()
738 dev_err(info->dev, "Failed to add RTC irq chip: %d\n", ret); in max77686_init_rtc_regmap()
751 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info), in max77686_rtc_probe()
754 return -ENOMEM; in max77686_rtc_probe()
756 mutex_init(&info->lock); in max77686_rtc_probe()
757 info->dev = &pdev->dev; in max77686_rtc_probe()
758 info->drv_data = (const struct max77686_rtc_driver_data *) in max77686_rtc_probe()
759 id->driver_data; in max77686_rtc_probe()
769 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret); in max77686_rtc_probe()
773 device_init_wakeup(&pdev->dev, 1); in max77686_rtc_probe()
775 info->rtc_dev = devm_rtc_device_register(&pdev->dev, id->name, in max77686_rtc_probe()
778 if (IS_ERR(info->rtc_dev)) { in max77686_rtc_probe()
779 ret = PTR_ERR(info->rtc_dev); in max77686_rtc_probe()
780 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); in max77686_rtc_probe()
782 ret = -EINVAL; in max77686_rtc_probe()
786 info->virq = regmap_irq_get_virq(info->rtc_irq_data, in max77686_rtc_probe()
788 if (info->virq <= 0) { in max77686_rtc_probe()
789 ret = -ENXIO; in max77686_rtc_probe()
793 ret = request_threaded_irq(info->virq, NULL, max77686_rtc_alarm_irq, 0, in max77686_rtc_probe()
794 "rtc-alarm1", info); in max77686_rtc_probe()
796 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", in max77686_rtc_probe()
797 info->virq, ret); in max77686_rtc_probe()
804 regmap_del_irq_chip(info->rtc_irq, info->rtc_irq_data); in max77686_rtc_probe()
813 free_irq(info->virq, info); in max77686_rtc_remove()
814 regmap_del_irq_chip(info->rtc_irq, info->rtc_irq_data); in max77686_rtc_remove()
828 ret = enable_irq_wake(info->virq); in max77686_rtc_suspend()
839 if (!info->drv_data->rtc_irq_from_platform) in max77686_rtc_suspend()
840 disable_irq(info->rtc_irq); in max77686_rtc_suspend()
849 if (!info->drv_data->rtc_irq_from_platform) in max77686_rtc_resume()
850 enable_irq(info->rtc_irq); in max77686_rtc_resume()
855 return disable_irq_wake(info->virq); in max77686_rtc_resume()
866 { "max77686-rtc", .driver_data = (kernel_ulong_t)&max77686_drv_data, },
867 { "max77802-rtc", .driver_data = (kernel_ulong_t)&max77802_drv_data, },
868 { "max77620-rtc", .driver_data = (kernel_ulong_t)&max77620_drv_data, },
869 { "max77714-rtc", .driver_data = (kernel_ulong_t)&max77714_drv_data, },
876 .name = "max77686-rtc",