1e6e7376cSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0 2e6e7376cSAlexandre Belloni /* 3e6e7376cSAlexandre Belloni * RTC driver for the Micro Crystal RV3028 4e6e7376cSAlexandre Belloni * 5e6e7376cSAlexandre Belloni * Copyright (C) 2019 Micro Crystal SA 6e6e7376cSAlexandre Belloni * 7e6e7376cSAlexandre Belloni * Alexandre Belloni <alexandre.belloni@bootlin.com> 8e6e7376cSAlexandre Belloni * 9e6e7376cSAlexandre Belloni */ 10e6e7376cSAlexandre Belloni 11f583c341SParthiban Nallathambi #include <linux/clk-provider.h> 12e6e7376cSAlexandre Belloni #include <linux/bcd.h> 13e6e7376cSAlexandre Belloni #include <linux/bitops.h> 14e6e7376cSAlexandre Belloni #include <linux/i2c.h> 15e6e7376cSAlexandre Belloni #include <linux/interrupt.h> 16e6e7376cSAlexandre Belloni #include <linux/kernel.h> 17e6e7376cSAlexandre Belloni #include <linux/log2.h> 18e6e7376cSAlexandre Belloni #include <linux/module.h> 19e6e7376cSAlexandre Belloni #include <linux/of_device.h> 20e6e7376cSAlexandre Belloni #include <linux/regmap.h> 21e6e7376cSAlexandre Belloni #include <linux/rtc.h> 22e6e7376cSAlexandre Belloni 23e6e7376cSAlexandre Belloni #define RV3028_SEC 0x00 24e6e7376cSAlexandre Belloni #define RV3028_MIN 0x01 25e6e7376cSAlexandre Belloni #define RV3028_HOUR 0x02 26e6e7376cSAlexandre Belloni #define RV3028_WDAY 0x03 27e6e7376cSAlexandre Belloni #define RV3028_DAY 0x04 28e6e7376cSAlexandre Belloni #define RV3028_MONTH 0x05 29e6e7376cSAlexandre Belloni #define RV3028_YEAR 0x06 30e6e7376cSAlexandre Belloni #define RV3028_ALARM_MIN 0x07 31e6e7376cSAlexandre Belloni #define RV3028_ALARM_HOUR 0x08 32e6e7376cSAlexandre Belloni #define RV3028_ALARM_DAY 0x09 33e6e7376cSAlexandre Belloni #define RV3028_STATUS 0x0E 34e6e7376cSAlexandre Belloni #define RV3028_CTRL1 0x0F 35e6e7376cSAlexandre Belloni #define RV3028_CTRL2 0x10 36e6e7376cSAlexandre Belloni #define RV3028_EVT_CTRL 0x13 37e6e7376cSAlexandre Belloni #define RV3028_TS_COUNT 0x14 38e6e7376cSAlexandre Belloni #define RV3028_TS_SEC 0x15 39e6e7376cSAlexandre Belloni #define RV3028_RAM1 0x1F 40e6e7376cSAlexandre Belloni #define RV3028_EEPROM_ADDR 0x25 41e6e7376cSAlexandre Belloni #define RV3028_EEPROM_DATA 0x26 42e6e7376cSAlexandre Belloni #define RV3028_EEPROM_CMD 0x27 43e6e7376cSAlexandre Belloni #define RV3028_CLKOUT 0x35 44e6e7376cSAlexandre Belloni #define RV3028_OFFSET 0x36 45e6e7376cSAlexandre Belloni #define RV3028_BACKUP 0x37 46e6e7376cSAlexandre Belloni 47e6e7376cSAlexandre Belloni #define RV3028_STATUS_PORF BIT(0) 48e6e7376cSAlexandre Belloni #define RV3028_STATUS_EVF BIT(1) 49e6e7376cSAlexandre Belloni #define RV3028_STATUS_AF BIT(2) 50e6e7376cSAlexandre Belloni #define RV3028_STATUS_TF BIT(3) 51e6e7376cSAlexandre Belloni #define RV3028_STATUS_UF BIT(4) 52e6e7376cSAlexandre Belloni #define RV3028_STATUS_BSF BIT(5) 53e6e7376cSAlexandre Belloni #define RV3028_STATUS_CLKF BIT(6) 54e6e7376cSAlexandre Belloni #define RV3028_STATUS_EEBUSY BIT(7) 55e6e7376cSAlexandre Belloni 56f583c341SParthiban Nallathambi #define RV3028_CLKOUT_FD_MASK GENMASK(2, 0) 57f583c341SParthiban Nallathambi #define RV3028_CLKOUT_PORIE BIT(3) 58f583c341SParthiban Nallathambi #define RV3028_CLKOUT_CLKSY BIT(6) 59f583c341SParthiban Nallathambi #define RV3028_CLKOUT_CLKOE BIT(7) 60f583c341SParthiban Nallathambi 61e6e7376cSAlexandre Belloni #define RV3028_CTRL1_EERD BIT(3) 62e6e7376cSAlexandre Belloni #define RV3028_CTRL1_WADA BIT(5) 63e6e7376cSAlexandre Belloni 64e6e7376cSAlexandre Belloni #define RV3028_CTRL2_RESET BIT(0) 65e6e7376cSAlexandre Belloni #define RV3028_CTRL2_12_24 BIT(1) 66e6e7376cSAlexandre Belloni #define RV3028_CTRL2_EIE BIT(2) 67e6e7376cSAlexandre Belloni #define RV3028_CTRL2_AIE BIT(3) 68e6e7376cSAlexandre Belloni #define RV3028_CTRL2_TIE BIT(4) 69e6e7376cSAlexandre Belloni #define RV3028_CTRL2_UIE BIT(5) 70e6e7376cSAlexandre Belloni #define RV3028_CTRL2_TSE BIT(7) 71e6e7376cSAlexandre Belloni 72e6e7376cSAlexandre Belloni #define RV3028_EVT_CTRL_TSR BIT(2) 73e6e7376cSAlexandre Belloni 74e6e7376cSAlexandre Belloni #define RV3028_EEPROM_CMD_WRITE 0x21 75e6e7376cSAlexandre Belloni #define RV3028_EEPROM_CMD_READ 0x22 76e6e7376cSAlexandre Belloni 77e6e7376cSAlexandre Belloni #define RV3028_EEBUSY_POLL 10000 78e6e7376cSAlexandre Belloni #define RV3028_EEBUSY_TIMEOUT 100000 79e6e7376cSAlexandre Belloni 80e6e7376cSAlexandre Belloni #define RV3028_BACKUP_TCE BIT(5) 81e6e7376cSAlexandre Belloni #define RV3028_BACKUP_TCR_MASK GENMASK(1,0) 82e6e7376cSAlexandre Belloni 83e6e7376cSAlexandre Belloni #define OFFSET_STEP_PPT 953674 84e6e7376cSAlexandre Belloni 85e6e7376cSAlexandre Belloni enum rv3028_type { 86e6e7376cSAlexandre Belloni rv_3028, 87e6e7376cSAlexandre Belloni }; 88e6e7376cSAlexandre Belloni 89e6e7376cSAlexandre Belloni struct rv3028_data { 90e6e7376cSAlexandre Belloni struct regmap *regmap; 91e6e7376cSAlexandre Belloni struct rtc_device *rtc; 92e6e7376cSAlexandre Belloni enum rv3028_type type; 93f583c341SParthiban Nallathambi #ifdef CONFIG_COMMON_CLK 94f583c341SParthiban Nallathambi struct clk_hw clkout_hw; 95f583c341SParthiban Nallathambi #endif 96e6e7376cSAlexandre Belloni }; 97e6e7376cSAlexandre Belloni 98*c1efae14SAlexandre Belloni static u16 rv3028_trickle_resistors[] = {3000, 5000, 9000, 15000}; 99e6e7376cSAlexandre Belloni 100e6e7376cSAlexandre Belloni static ssize_t timestamp0_store(struct device *dev, 101e6e7376cSAlexandre Belloni struct device_attribute *attr, 102e6e7376cSAlexandre Belloni const char *buf, size_t count) 103e6e7376cSAlexandre Belloni { 104e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent); 105e6e7376cSAlexandre Belloni 106e6e7376cSAlexandre Belloni regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR, 107e6e7376cSAlexandre Belloni RV3028_EVT_CTRL_TSR); 108e6e7376cSAlexandre Belloni 109e6e7376cSAlexandre Belloni return count; 110e6e7376cSAlexandre Belloni }; 111e6e7376cSAlexandre Belloni 112e6e7376cSAlexandre Belloni static ssize_t timestamp0_show(struct device *dev, 113e6e7376cSAlexandre Belloni struct device_attribute *attr, char *buf) 114e6e7376cSAlexandre Belloni { 115e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent); 116e6e7376cSAlexandre Belloni struct rtc_time tm; 117e6e7376cSAlexandre Belloni int ret, count; 118e6e7376cSAlexandre Belloni u8 date[6]; 119e6e7376cSAlexandre Belloni 120e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count); 121e6e7376cSAlexandre Belloni if (ret) 122e6e7376cSAlexandre Belloni return ret; 123e6e7376cSAlexandre Belloni 124e6e7376cSAlexandre Belloni if (!count) 125e6e7376cSAlexandre Belloni return 0; 126e6e7376cSAlexandre Belloni 127e6e7376cSAlexandre Belloni ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date, 128e6e7376cSAlexandre Belloni sizeof(date)); 129e6e7376cSAlexandre Belloni if (ret) 130e6e7376cSAlexandre Belloni return ret; 131e6e7376cSAlexandre Belloni 132e6e7376cSAlexandre Belloni tm.tm_sec = bcd2bin(date[0]); 133e6e7376cSAlexandre Belloni tm.tm_min = bcd2bin(date[1]); 134e6e7376cSAlexandre Belloni tm.tm_hour = bcd2bin(date[2]); 135e6e7376cSAlexandre Belloni tm.tm_mday = bcd2bin(date[3]); 136e6e7376cSAlexandre Belloni tm.tm_mon = bcd2bin(date[4]) - 1; 137e6e7376cSAlexandre Belloni tm.tm_year = bcd2bin(date[5]) + 100; 138e6e7376cSAlexandre Belloni 139e6e7376cSAlexandre Belloni ret = rtc_valid_tm(&tm); 140e6e7376cSAlexandre Belloni if (ret) 141e6e7376cSAlexandre Belloni return ret; 142e6e7376cSAlexandre Belloni 143e6e7376cSAlexandre Belloni return sprintf(buf, "%llu\n", 144e6e7376cSAlexandre Belloni (unsigned long long)rtc_tm_to_time64(&tm)); 145e6e7376cSAlexandre Belloni }; 146e6e7376cSAlexandre Belloni 147e6e7376cSAlexandre Belloni static DEVICE_ATTR_RW(timestamp0); 148e6e7376cSAlexandre Belloni 149e6e7376cSAlexandre Belloni static ssize_t timestamp0_count_show(struct device *dev, 150e6e7376cSAlexandre Belloni struct device_attribute *attr, char *buf) 151e6e7376cSAlexandre Belloni { 152e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent); 153e6e7376cSAlexandre Belloni int ret, count; 154e6e7376cSAlexandre Belloni 155e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count); 156e6e7376cSAlexandre Belloni if (ret) 157e6e7376cSAlexandre Belloni return ret; 158e6e7376cSAlexandre Belloni 159e6e7376cSAlexandre Belloni return sprintf(buf, "%u\n", count); 160e6e7376cSAlexandre Belloni }; 161e6e7376cSAlexandre Belloni 162e6e7376cSAlexandre Belloni static DEVICE_ATTR_RO(timestamp0_count); 163e6e7376cSAlexandre Belloni 164e6e7376cSAlexandre Belloni static struct attribute *rv3028_attrs[] = { 165e6e7376cSAlexandre Belloni &dev_attr_timestamp0.attr, 166e6e7376cSAlexandre Belloni &dev_attr_timestamp0_count.attr, 167e6e7376cSAlexandre Belloni NULL 168e6e7376cSAlexandre Belloni }; 169e6e7376cSAlexandre Belloni 170e6e7376cSAlexandre Belloni static const struct attribute_group rv3028_attr_group = { 171e6e7376cSAlexandre Belloni .attrs = rv3028_attrs, 172e6e7376cSAlexandre Belloni }; 173e6e7376cSAlexandre Belloni 174e6e7376cSAlexandre Belloni static irqreturn_t rv3028_handle_irq(int irq, void *dev_id) 175e6e7376cSAlexandre Belloni { 176e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_id; 177e6e7376cSAlexandre Belloni unsigned long events = 0; 178e6e7376cSAlexandre Belloni u32 status = 0, ctrl = 0; 179e6e7376cSAlexandre Belloni 180e6e7376cSAlexandre Belloni if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 || 181e6e7376cSAlexandre Belloni status == 0) { 182e6e7376cSAlexandre Belloni return IRQ_NONE; 183e6e7376cSAlexandre Belloni } 184e6e7376cSAlexandre Belloni 185e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_PORF) 186e6e7376cSAlexandre Belloni dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n"); 187e6e7376cSAlexandre Belloni 188e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_TF) { 189e6e7376cSAlexandre Belloni status |= RV3028_STATUS_TF; 190e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_TIE; 191e6e7376cSAlexandre Belloni events |= RTC_PF; 192e6e7376cSAlexandre Belloni } 193e6e7376cSAlexandre Belloni 194e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_AF) { 195e6e7376cSAlexandre Belloni status |= RV3028_STATUS_AF; 196e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_AIE; 197e6e7376cSAlexandre Belloni events |= RTC_AF; 198e6e7376cSAlexandre Belloni } 199e6e7376cSAlexandre Belloni 200e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_UF) { 201e6e7376cSAlexandre Belloni status |= RV3028_STATUS_UF; 202e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_UIE; 203e6e7376cSAlexandre Belloni events |= RTC_UF; 204e6e7376cSAlexandre Belloni } 205e6e7376cSAlexandre Belloni 206e6e7376cSAlexandre Belloni if (events) { 207e6e7376cSAlexandre Belloni rtc_update_irq(rv3028->rtc, 1, events); 208e6e7376cSAlexandre Belloni regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0); 209e6e7376cSAlexandre Belloni regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0); 210e6e7376cSAlexandre Belloni } 211e6e7376cSAlexandre Belloni 212e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_EVF) { 213e6e7376cSAlexandre Belloni sysfs_notify(&rv3028->rtc->dev.kobj, NULL, 214e6e7376cSAlexandre Belloni dev_attr_timestamp0.attr.name); 215e6e7376cSAlexandre Belloni dev_warn(&rv3028->rtc->dev, "event detected"); 216e6e7376cSAlexandre Belloni } 217e6e7376cSAlexandre Belloni 218e6e7376cSAlexandre Belloni return IRQ_HANDLED; 219e6e7376cSAlexandre Belloni } 220e6e7376cSAlexandre Belloni 221e6e7376cSAlexandre Belloni static int rv3028_get_time(struct device *dev, struct rtc_time *tm) 222e6e7376cSAlexandre Belloni { 223e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 224e6e7376cSAlexandre Belloni u8 date[7]; 225e6e7376cSAlexandre Belloni int ret, status; 226e6e7376cSAlexandre Belloni 227e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 228e6e7376cSAlexandre Belloni if (ret < 0) 229e6e7376cSAlexandre Belloni return ret; 230e6e7376cSAlexandre Belloni 231e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_PORF) { 232e6e7376cSAlexandre Belloni dev_warn(dev, "Voltage low, data is invalid.\n"); 233e6e7376cSAlexandre Belloni return -EINVAL; 234e6e7376cSAlexandre Belloni } 235e6e7376cSAlexandre Belloni 236e6e7376cSAlexandre Belloni ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date)); 237e6e7376cSAlexandre Belloni if (ret) 238e6e7376cSAlexandre Belloni return ret; 239e6e7376cSAlexandre Belloni 240e6e7376cSAlexandre Belloni tm->tm_sec = bcd2bin(date[RV3028_SEC] & 0x7f); 241e6e7376cSAlexandre Belloni tm->tm_min = bcd2bin(date[RV3028_MIN] & 0x7f); 242e6e7376cSAlexandre Belloni tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f); 243e6e7376cSAlexandre Belloni tm->tm_wday = ilog2(date[RV3028_WDAY] & 0x7f); 244e6e7376cSAlexandre Belloni tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f); 245e6e7376cSAlexandre Belloni tm->tm_mon = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1; 246e6e7376cSAlexandre Belloni tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100; 247e6e7376cSAlexandre Belloni 248e6e7376cSAlexandre Belloni return 0; 249e6e7376cSAlexandre Belloni } 250e6e7376cSAlexandre Belloni 251e6e7376cSAlexandre Belloni static int rv3028_set_time(struct device *dev, struct rtc_time *tm) 252e6e7376cSAlexandre Belloni { 253e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 254e6e7376cSAlexandre Belloni u8 date[7]; 255e6e7376cSAlexandre Belloni int ret; 256e6e7376cSAlexandre Belloni 257e6e7376cSAlexandre Belloni date[RV3028_SEC] = bin2bcd(tm->tm_sec); 258e6e7376cSAlexandre Belloni date[RV3028_MIN] = bin2bcd(tm->tm_min); 259e6e7376cSAlexandre Belloni date[RV3028_HOUR] = bin2bcd(tm->tm_hour); 260e6e7376cSAlexandre Belloni date[RV3028_WDAY] = 1 << (tm->tm_wday); 261e6e7376cSAlexandre Belloni date[RV3028_DAY] = bin2bcd(tm->tm_mday); 262e6e7376cSAlexandre Belloni date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1); 263e6e7376cSAlexandre Belloni date[RV3028_YEAR] = bin2bcd(tm->tm_year - 100); 264e6e7376cSAlexandre Belloni 265e6e7376cSAlexandre Belloni /* 266e6e7376cSAlexandre Belloni * Writing to the Seconds register has the same effect as setting RESET 267e6e7376cSAlexandre Belloni * bit to 1 268e6e7376cSAlexandre Belloni */ 269e6e7376cSAlexandre Belloni ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date, 270e6e7376cSAlexandre Belloni sizeof(date)); 271e6e7376cSAlexandre Belloni if (ret) 272e6e7376cSAlexandre Belloni return ret; 273e6e7376cSAlexandre Belloni 274e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 275e6e7376cSAlexandre Belloni RV3028_STATUS_PORF, 0); 276e6e7376cSAlexandre Belloni 277e6e7376cSAlexandre Belloni return ret; 278e6e7376cSAlexandre Belloni } 279e6e7376cSAlexandre Belloni 280e6e7376cSAlexandre Belloni static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 281e6e7376cSAlexandre Belloni { 282e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 283e6e7376cSAlexandre Belloni u8 alarmvals[3]; 284e6e7376cSAlexandre Belloni int status, ctrl, ret; 285e6e7376cSAlexandre Belloni 286e6e7376cSAlexandre Belloni ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals, 287e6e7376cSAlexandre Belloni sizeof(alarmvals)); 288e6e7376cSAlexandre Belloni if (ret) 289e6e7376cSAlexandre Belloni return ret; 290e6e7376cSAlexandre Belloni 291e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 292e6e7376cSAlexandre Belloni if (ret < 0) 293e6e7376cSAlexandre Belloni return ret; 294e6e7376cSAlexandre Belloni 295e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl); 296e6e7376cSAlexandre Belloni if (ret < 0) 297e6e7376cSAlexandre Belloni return ret; 298e6e7376cSAlexandre Belloni 299e6e7376cSAlexandre Belloni alrm->time.tm_sec = 0; 300e6e7376cSAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 301e6e7376cSAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 302e6e7376cSAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 303e6e7376cSAlexandre Belloni 304e6e7376cSAlexandre Belloni alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE); 305e6e7376cSAlexandre Belloni alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled; 306e6e7376cSAlexandre Belloni 307e6e7376cSAlexandre Belloni return 0; 308e6e7376cSAlexandre Belloni } 309e6e7376cSAlexandre Belloni 310e6e7376cSAlexandre Belloni static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 311e6e7376cSAlexandre Belloni { 312e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 313e6e7376cSAlexandre Belloni u8 alarmvals[3]; 314e6e7376cSAlexandre Belloni u8 ctrl = 0; 315e6e7376cSAlexandre Belloni int ret; 316e6e7376cSAlexandre Belloni 317e6e7376cSAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 318e6e7376cSAlexandre Belloni if (alrm->time.tm_sec) { 319e6e7376cSAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 320e6e7376cSAlexandre Belloni 321e6e7376cSAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 322e6e7376cSAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 323e6e7376cSAlexandre Belloni } 324e6e7376cSAlexandre Belloni 325e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 326e6e7376cSAlexandre Belloni RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0); 327e6e7376cSAlexandre Belloni if (ret) 328e6e7376cSAlexandre Belloni return ret; 329e6e7376cSAlexandre Belloni 330e6e7376cSAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 331e6e7376cSAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 332e6e7376cSAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 333e6e7376cSAlexandre Belloni 334e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 335e6e7376cSAlexandre Belloni RV3028_STATUS_AF, 0); 336e6e7376cSAlexandre Belloni if (ret) 337e6e7376cSAlexandre Belloni return ret; 338e6e7376cSAlexandre Belloni 339e6e7376cSAlexandre Belloni ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals, 340e6e7376cSAlexandre Belloni sizeof(alarmvals)); 341e6e7376cSAlexandre Belloni if (ret) 342e6e7376cSAlexandre Belloni return ret; 343e6e7376cSAlexandre Belloni 344e6e7376cSAlexandre Belloni if (alrm->enabled) { 345e6e7376cSAlexandre Belloni if (rv3028->rtc->uie_rtctimer.enabled) 346e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_UIE; 347e6e7376cSAlexandre Belloni if (rv3028->rtc->aie_timer.enabled) 348e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_AIE; 349e6e7376cSAlexandre Belloni } 350e6e7376cSAlexandre Belloni 351e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 352e6e7376cSAlexandre Belloni RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl); 353e6e7376cSAlexandre Belloni 354e6e7376cSAlexandre Belloni return ret; 355e6e7376cSAlexandre Belloni } 356e6e7376cSAlexandre Belloni 357e6e7376cSAlexandre Belloni static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled) 358e6e7376cSAlexandre Belloni { 359e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 360e6e7376cSAlexandre Belloni int ctrl = 0, ret; 361e6e7376cSAlexandre Belloni 362e6e7376cSAlexandre Belloni if (enabled) { 363e6e7376cSAlexandre Belloni if (rv3028->rtc->uie_rtctimer.enabled) 364e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_UIE; 365e6e7376cSAlexandre Belloni if (rv3028->rtc->aie_timer.enabled) 366e6e7376cSAlexandre Belloni ctrl |= RV3028_CTRL2_AIE; 367e6e7376cSAlexandre Belloni } 368e6e7376cSAlexandre Belloni 369e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 370e6e7376cSAlexandre Belloni RV3028_STATUS_AF | RV3028_STATUS_UF, 0); 371e6e7376cSAlexandre Belloni if (ret) 372e6e7376cSAlexandre Belloni return ret; 373e6e7376cSAlexandre Belloni 374e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 375e6e7376cSAlexandre Belloni RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl); 376e6e7376cSAlexandre Belloni if (ret) 377e6e7376cSAlexandre Belloni return ret; 378e6e7376cSAlexandre Belloni 379e6e7376cSAlexandre Belloni return 0; 380e6e7376cSAlexandre Belloni } 381e6e7376cSAlexandre Belloni 382e6e7376cSAlexandre Belloni static int rv3028_read_offset(struct device *dev, long *offset) 383e6e7376cSAlexandre Belloni { 384e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 385e6e7376cSAlexandre Belloni int ret, value, steps; 386e6e7376cSAlexandre Belloni 387e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value); 388e6e7376cSAlexandre Belloni if (ret < 0) 389e6e7376cSAlexandre Belloni return ret; 390e6e7376cSAlexandre Belloni 391e6e7376cSAlexandre Belloni steps = sign_extend32(value << 1, 8); 392e6e7376cSAlexandre Belloni 393e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value); 394e6e7376cSAlexandre Belloni if (ret < 0) 395e6e7376cSAlexandre Belloni return ret; 396e6e7376cSAlexandre Belloni 397e6e7376cSAlexandre Belloni steps += value >> 7; 398e6e7376cSAlexandre Belloni 399e6e7376cSAlexandre Belloni *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000); 400e6e7376cSAlexandre Belloni 401e6e7376cSAlexandre Belloni return 0; 402e6e7376cSAlexandre Belloni } 403e6e7376cSAlexandre Belloni 404e6e7376cSAlexandre Belloni static int rv3028_set_offset(struct device *dev, long offset) 405e6e7376cSAlexandre Belloni { 406e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 407e6e7376cSAlexandre Belloni int ret; 408e6e7376cSAlexandre Belloni 409e6e7376cSAlexandre Belloni offset = clamp(offset, -244141L, 243187L) * 1000; 410e6e7376cSAlexandre Belloni offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT); 411e6e7376cSAlexandre Belloni 412e6e7376cSAlexandre Belloni ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1); 413e6e7376cSAlexandre Belloni if (ret < 0) 414e6e7376cSAlexandre Belloni return ret; 415e6e7376cSAlexandre Belloni 416e6e7376cSAlexandre Belloni return regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7), 417e6e7376cSAlexandre Belloni offset << 7); 418e6e7376cSAlexandre Belloni } 419e6e7376cSAlexandre Belloni 420e6e7376cSAlexandre Belloni static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 421e6e7376cSAlexandre Belloni { 422e6e7376cSAlexandre Belloni struct rv3028_data *rv3028 = dev_get_drvdata(dev); 423e6e7376cSAlexandre Belloni int status, ret = 0; 424e6e7376cSAlexandre Belloni 425e6e7376cSAlexandre Belloni switch (cmd) { 426e6e7376cSAlexandre Belloni case RTC_VL_READ: 427e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 428e6e7376cSAlexandre Belloni if (ret < 0) 429e6e7376cSAlexandre Belloni return ret; 430e6e7376cSAlexandre Belloni 43186e655f9SAlexandre Belloni status = status & RV3028_STATUS_PORF ? RTC_VL_DATA_INVALID : 0; 43286e655f9SAlexandre Belloni return put_user(status, (unsigned int __user *)arg); 433e6e7376cSAlexandre Belloni 434e6e7376cSAlexandre Belloni default: 435e6e7376cSAlexandre Belloni return -ENOIOCTLCMD; 436e6e7376cSAlexandre Belloni } 437e6e7376cSAlexandre Belloni } 438e6e7376cSAlexandre Belloni 439e6e7376cSAlexandre Belloni static int rv3028_nvram_write(void *priv, unsigned int offset, void *val, 440e6e7376cSAlexandre Belloni size_t bytes) 441e6e7376cSAlexandre Belloni { 442e6e7376cSAlexandre Belloni return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes); 443e6e7376cSAlexandre Belloni } 444e6e7376cSAlexandre Belloni 445e6e7376cSAlexandre Belloni static int rv3028_nvram_read(void *priv, unsigned int offset, void *val, 446e6e7376cSAlexandre Belloni size_t bytes) 447e6e7376cSAlexandre Belloni { 448e6e7376cSAlexandre Belloni return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes); 449e6e7376cSAlexandre Belloni } 450e6e7376cSAlexandre Belloni 451e6e7376cSAlexandre Belloni static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val, 452e6e7376cSAlexandre Belloni size_t bytes) 453e6e7376cSAlexandre Belloni { 454e6e7376cSAlexandre Belloni u32 status, ctrl1; 455e6e7376cSAlexandre Belloni int i, ret, err; 456e6e7376cSAlexandre Belloni u8 *buf = val; 457e6e7376cSAlexandre Belloni 458e6e7376cSAlexandre Belloni ret = regmap_read(priv, RV3028_CTRL1, &ctrl1); 459e6e7376cSAlexandre Belloni if (ret) 460e6e7376cSAlexandre Belloni return ret; 461e6e7376cSAlexandre Belloni 462e6e7376cSAlexandre Belloni if (!(ctrl1 & RV3028_CTRL1_EERD)) { 463e6e7376cSAlexandre Belloni ret = regmap_update_bits(priv, RV3028_CTRL1, 464e6e7376cSAlexandre Belloni RV3028_CTRL1_EERD, RV3028_CTRL1_EERD); 465e6e7376cSAlexandre Belloni if (ret) 466e6e7376cSAlexandre Belloni return ret; 467e6e7376cSAlexandre Belloni 468e6e7376cSAlexandre Belloni ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status, 469e6e7376cSAlexandre Belloni !(status & RV3028_STATUS_EEBUSY), 470e6e7376cSAlexandre Belloni RV3028_EEBUSY_POLL, 471e6e7376cSAlexandre Belloni RV3028_EEBUSY_TIMEOUT); 472e6e7376cSAlexandre Belloni if (ret) 473e6e7376cSAlexandre Belloni goto restore_eerd; 474e6e7376cSAlexandre Belloni } 475e6e7376cSAlexandre Belloni 476e6e7376cSAlexandre Belloni for (i = 0; i < bytes; i++) { 477e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i); 478e6e7376cSAlexandre Belloni if (ret) 479e6e7376cSAlexandre Belloni goto restore_eerd; 480e6e7376cSAlexandre Belloni 481e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_DATA, buf[i]); 482e6e7376cSAlexandre Belloni if (ret) 483e6e7376cSAlexandre Belloni goto restore_eerd; 484e6e7376cSAlexandre Belloni 485e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0); 486e6e7376cSAlexandre Belloni if (ret) 487e6e7376cSAlexandre Belloni goto restore_eerd; 488e6e7376cSAlexandre Belloni 489e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_CMD, 490e6e7376cSAlexandre Belloni RV3028_EEPROM_CMD_WRITE); 491e6e7376cSAlexandre Belloni if (ret) 492e6e7376cSAlexandre Belloni goto restore_eerd; 493e6e7376cSAlexandre Belloni 494e6e7376cSAlexandre Belloni usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT); 495e6e7376cSAlexandre Belloni 496e6e7376cSAlexandre Belloni ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status, 497e6e7376cSAlexandre Belloni !(status & RV3028_STATUS_EEBUSY), 498e6e7376cSAlexandre Belloni RV3028_EEBUSY_POLL, 499e6e7376cSAlexandre Belloni RV3028_EEBUSY_TIMEOUT); 500e6e7376cSAlexandre Belloni if (ret) 501e6e7376cSAlexandre Belloni goto restore_eerd; 502e6e7376cSAlexandre Belloni } 503e6e7376cSAlexandre Belloni 504e6e7376cSAlexandre Belloni restore_eerd: 505e6e7376cSAlexandre Belloni if (!(ctrl1 & RV3028_CTRL1_EERD)) 506e6e7376cSAlexandre Belloni { 507e6e7376cSAlexandre Belloni err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD, 508e6e7376cSAlexandre Belloni 0); 509e6e7376cSAlexandre Belloni if (err && !ret) 510e6e7376cSAlexandre Belloni ret = err; 511e6e7376cSAlexandre Belloni } 512e6e7376cSAlexandre Belloni 513e6e7376cSAlexandre Belloni return ret; 514e6e7376cSAlexandre Belloni } 515e6e7376cSAlexandre Belloni 516e6e7376cSAlexandre Belloni static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val, 517e6e7376cSAlexandre Belloni size_t bytes) 518e6e7376cSAlexandre Belloni { 519e6e7376cSAlexandre Belloni u32 status, ctrl1, data; 520e6e7376cSAlexandre Belloni int i, ret, err; 521e6e7376cSAlexandre Belloni u8 *buf = val; 522e6e7376cSAlexandre Belloni 523e6e7376cSAlexandre Belloni ret = regmap_read(priv, RV3028_CTRL1, &ctrl1); 524e6e7376cSAlexandre Belloni if (ret) 525e6e7376cSAlexandre Belloni return ret; 526e6e7376cSAlexandre Belloni 527e6e7376cSAlexandre Belloni if (!(ctrl1 & RV3028_CTRL1_EERD)) { 528e6e7376cSAlexandre Belloni ret = regmap_update_bits(priv, RV3028_CTRL1, 529e6e7376cSAlexandre Belloni RV3028_CTRL1_EERD, RV3028_CTRL1_EERD); 530e6e7376cSAlexandre Belloni if (ret) 531e6e7376cSAlexandre Belloni return ret; 532e6e7376cSAlexandre Belloni 533e6e7376cSAlexandre Belloni ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status, 534e6e7376cSAlexandre Belloni !(status & RV3028_STATUS_EEBUSY), 535e6e7376cSAlexandre Belloni RV3028_EEBUSY_POLL, 536e6e7376cSAlexandre Belloni RV3028_EEBUSY_TIMEOUT); 537e6e7376cSAlexandre Belloni if (ret) 538e6e7376cSAlexandre Belloni goto restore_eerd; 539e6e7376cSAlexandre Belloni } 540e6e7376cSAlexandre Belloni 541e6e7376cSAlexandre Belloni for (i = 0; i < bytes; i++) { 542e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i); 543e6e7376cSAlexandre Belloni if (ret) 544e6e7376cSAlexandre Belloni goto restore_eerd; 545e6e7376cSAlexandre Belloni 546e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0); 547e6e7376cSAlexandre Belloni if (ret) 548e6e7376cSAlexandre Belloni goto restore_eerd; 549e6e7376cSAlexandre Belloni 550e6e7376cSAlexandre Belloni ret = regmap_write(priv, RV3028_EEPROM_CMD, 551e6e7376cSAlexandre Belloni RV3028_EEPROM_CMD_READ); 552e6e7376cSAlexandre Belloni if (ret) 553e6e7376cSAlexandre Belloni goto restore_eerd; 554e6e7376cSAlexandre Belloni 555e6e7376cSAlexandre Belloni ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status, 556e6e7376cSAlexandre Belloni !(status & RV3028_STATUS_EEBUSY), 557e6e7376cSAlexandre Belloni RV3028_EEBUSY_POLL, 558e6e7376cSAlexandre Belloni RV3028_EEBUSY_TIMEOUT); 559e6e7376cSAlexandre Belloni if (ret) 560e6e7376cSAlexandre Belloni goto restore_eerd; 561e6e7376cSAlexandre Belloni 562e6e7376cSAlexandre Belloni ret = regmap_read(priv, RV3028_EEPROM_DATA, &data); 563e6e7376cSAlexandre Belloni if (ret) 564e6e7376cSAlexandre Belloni goto restore_eerd; 565e6e7376cSAlexandre Belloni buf[i] = data; 566e6e7376cSAlexandre Belloni } 567e6e7376cSAlexandre Belloni 568e6e7376cSAlexandre Belloni restore_eerd: 569e6e7376cSAlexandre Belloni if (!(ctrl1 & RV3028_CTRL1_EERD)) 570e6e7376cSAlexandre Belloni { 571e6e7376cSAlexandre Belloni err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD, 572e6e7376cSAlexandre Belloni 0); 573e6e7376cSAlexandre Belloni if (err && !ret) 574e6e7376cSAlexandre Belloni ret = err; 575e6e7376cSAlexandre Belloni } 576e6e7376cSAlexandre Belloni 577e6e7376cSAlexandre Belloni return ret; 578e6e7376cSAlexandre Belloni } 579e6e7376cSAlexandre Belloni 580f583c341SParthiban Nallathambi #ifdef CONFIG_COMMON_CLK 581f583c341SParthiban Nallathambi #define clkout_hw_to_rv3028(hw) container_of(hw, struct rv3028_data, clkout_hw) 582f583c341SParthiban Nallathambi 583f583c341SParthiban Nallathambi static int clkout_rates[] = { 584f583c341SParthiban Nallathambi 32768, 585f583c341SParthiban Nallathambi 8192, 586f583c341SParthiban Nallathambi 1024, 587f583c341SParthiban Nallathambi 64, 588f583c341SParthiban Nallathambi 32, 589f583c341SParthiban Nallathambi 1, 590f583c341SParthiban Nallathambi }; 591f583c341SParthiban Nallathambi 592f583c341SParthiban Nallathambi static unsigned long rv3028_clkout_recalc_rate(struct clk_hw *hw, 593f583c341SParthiban Nallathambi unsigned long parent_rate) 594f583c341SParthiban Nallathambi { 595f583c341SParthiban Nallathambi int clkout, ret; 596f583c341SParthiban Nallathambi struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 597f583c341SParthiban Nallathambi 598f583c341SParthiban Nallathambi ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout); 599f583c341SParthiban Nallathambi if (ret < 0) 600f583c341SParthiban Nallathambi return 0; 601f583c341SParthiban Nallathambi 602f583c341SParthiban Nallathambi clkout &= RV3028_CLKOUT_FD_MASK; 603f583c341SParthiban Nallathambi return clkout_rates[clkout]; 604f583c341SParthiban Nallathambi } 605f583c341SParthiban Nallathambi 606f583c341SParthiban Nallathambi static long rv3028_clkout_round_rate(struct clk_hw *hw, unsigned long rate, 607f583c341SParthiban Nallathambi unsigned long *prate) 608f583c341SParthiban Nallathambi { 609f583c341SParthiban Nallathambi int i; 610f583c341SParthiban Nallathambi 611f583c341SParthiban Nallathambi for (i = 0; i < ARRAY_SIZE(clkout_rates); i++) 612f583c341SParthiban Nallathambi if (clkout_rates[i] <= rate) 613f583c341SParthiban Nallathambi return clkout_rates[i]; 614f583c341SParthiban Nallathambi 615f583c341SParthiban Nallathambi return 0; 616f583c341SParthiban Nallathambi } 617f583c341SParthiban Nallathambi 618f583c341SParthiban Nallathambi static int rv3028_clkout_set_rate(struct clk_hw *hw, unsigned long rate, 619f583c341SParthiban Nallathambi unsigned long parent_rate) 620f583c341SParthiban Nallathambi { 621f583c341SParthiban Nallathambi int i, ret; 62200e8e87fSAlexandre Belloni u32 enabled; 623f583c341SParthiban Nallathambi struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 624f583c341SParthiban Nallathambi 62500e8e87fSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &enabled); 62600e8e87fSAlexandre Belloni if (ret < 0) 62700e8e87fSAlexandre Belloni return ret; 62800e8e87fSAlexandre Belloni 629f583c341SParthiban Nallathambi ret = regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0); 630f583c341SParthiban Nallathambi if (ret < 0) 631f583c341SParthiban Nallathambi return ret; 632f583c341SParthiban Nallathambi 63300e8e87fSAlexandre Belloni enabled &= RV3028_CLKOUT_CLKOE; 634f583c341SParthiban Nallathambi 63500e8e87fSAlexandre Belloni for (i = 0; i < ARRAY_SIZE(clkout_rates); i++) 63600e8e87fSAlexandre Belloni if (clkout_rates[i] == rate) 637f583c341SParthiban Nallathambi return regmap_write(rv3028->regmap, RV3028_CLKOUT, 63800e8e87fSAlexandre Belloni RV3028_CLKOUT_CLKSY | enabled | i); 639f583c341SParthiban Nallathambi 640f583c341SParthiban Nallathambi return -EINVAL; 641f583c341SParthiban Nallathambi } 642f583c341SParthiban Nallathambi 643f583c341SParthiban Nallathambi static int rv3028_clkout_prepare(struct clk_hw *hw) 644f583c341SParthiban Nallathambi { 645f583c341SParthiban Nallathambi struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 646f583c341SParthiban Nallathambi 647f583c341SParthiban Nallathambi return regmap_write(rv3028->regmap, RV3028_CLKOUT, 648f583c341SParthiban Nallathambi RV3028_CLKOUT_CLKSY | RV3028_CLKOUT_CLKOE); 649f583c341SParthiban Nallathambi } 650f583c341SParthiban Nallathambi 651f583c341SParthiban Nallathambi static void rv3028_clkout_unprepare(struct clk_hw *hw) 652f583c341SParthiban Nallathambi { 653f583c341SParthiban Nallathambi struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 654f583c341SParthiban Nallathambi 655f583c341SParthiban Nallathambi regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0); 656f583c341SParthiban Nallathambi regmap_update_bits(rv3028->regmap, RV3028_STATUS, 657f583c341SParthiban Nallathambi RV3028_STATUS_CLKF, 0); 658f583c341SParthiban Nallathambi } 659f583c341SParthiban Nallathambi 660f583c341SParthiban Nallathambi static int rv3028_clkout_is_prepared(struct clk_hw *hw) 661f583c341SParthiban Nallathambi { 662f583c341SParthiban Nallathambi int clkout, ret; 663f583c341SParthiban Nallathambi struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw); 664f583c341SParthiban Nallathambi 665f583c341SParthiban Nallathambi ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout); 666f583c341SParthiban Nallathambi if (ret < 0) 667f583c341SParthiban Nallathambi return ret; 668f583c341SParthiban Nallathambi 669f583c341SParthiban Nallathambi return !!(clkout & RV3028_CLKOUT_CLKOE); 670f583c341SParthiban Nallathambi } 671f583c341SParthiban Nallathambi 672f583c341SParthiban Nallathambi static const struct clk_ops rv3028_clkout_ops = { 673f583c341SParthiban Nallathambi .prepare = rv3028_clkout_prepare, 674f583c341SParthiban Nallathambi .unprepare = rv3028_clkout_unprepare, 675f583c341SParthiban Nallathambi .is_prepared = rv3028_clkout_is_prepared, 676f583c341SParthiban Nallathambi .recalc_rate = rv3028_clkout_recalc_rate, 677f583c341SParthiban Nallathambi .round_rate = rv3028_clkout_round_rate, 678f583c341SParthiban Nallathambi .set_rate = rv3028_clkout_set_rate, 679f583c341SParthiban Nallathambi }; 680f583c341SParthiban Nallathambi 681f583c341SParthiban Nallathambi static int rv3028_clkout_register_clk(struct rv3028_data *rv3028, 682f583c341SParthiban Nallathambi struct i2c_client *client) 683f583c341SParthiban Nallathambi { 684f583c341SParthiban Nallathambi int ret; 685f583c341SParthiban Nallathambi struct clk *clk; 686f583c341SParthiban Nallathambi struct clk_init_data init; 687f583c341SParthiban Nallathambi struct device_node *node = client->dev.of_node; 688f583c341SParthiban Nallathambi 689f583c341SParthiban Nallathambi ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS, 690f583c341SParthiban Nallathambi RV3028_STATUS_CLKF, 0); 691f583c341SParthiban Nallathambi if (ret < 0) 692f583c341SParthiban Nallathambi return ret; 693f583c341SParthiban Nallathambi 694f583c341SParthiban Nallathambi init.name = "rv3028-clkout"; 695f583c341SParthiban Nallathambi init.ops = &rv3028_clkout_ops; 696f583c341SParthiban Nallathambi init.flags = 0; 697f583c341SParthiban Nallathambi init.parent_names = NULL; 698f583c341SParthiban Nallathambi init.num_parents = 0; 699f583c341SParthiban Nallathambi rv3028->clkout_hw.init = &init; 700f583c341SParthiban Nallathambi 701f583c341SParthiban Nallathambi /* optional override of the clockname */ 702f583c341SParthiban Nallathambi of_property_read_string(node, "clock-output-names", &init.name); 703f583c341SParthiban Nallathambi 704f583c341SParthiban Nallathambi /* register the clock */ 705f583c341SParthiban Nallathambi clk = devm_clk_register(&client->dev, &rv3028->clkout_hw); 706f583c341SParthiban Nallathambi if (!IS_ERR(clk)) 707f583c341SParthiban Nallathambi of_clk_add_provider(node, of_clk_src_simple_get, clk); 708f583c341SParthiban Nallathambi 709f583c341SParthiban Nallathambi return 0; 710f583c341SParthiban Nallathambi } 711f583c341SParthiban Nallathambi #endif 712f583c341SParthiban Nallathambi 713e6e7376cSAlexandre Belloni static struct rtc_class_ops rv3028_rtc_ops = { 714e6e7376cSAlexandre Belloni .read_time = rv3028_get_time, 715e6e7376cSAlexandre Belloni .set_time = rv3028_set_time, 716e6e7376cSAlexandre Belloni .read_offset = rv3028_read_offset, 717e6e7376cSAlexandre Belloni .set_offset = rv3028_set_offset, 718e6e7376cSAlexandre Belloni .ioctl = rv3028_ioctl, 719e6e7376cSAlexandre Belloni }; 720e6e7376cSAlexandre Belloni 721e6e7376cSAlexandre Belloni static const struct regmap_config regmap_config = { 722e6e7376cSAlexandre Belloni .reg_bits = 8, 723e6e7376cSAlexandre Belloni .val_bits = 8, 724e6e7376cSAlexandre Belloni .max_register = 0x37, 725e6e7376cSAlexandre Belloni }; 726e6e7376cSAlexandre Belloni 727e6e7376cSAlexandre Belloni static int rv3028_probe(struct i2c_client *client) 728e6e7376cSAlexandre Belloni { 729e6e7376cSAlexandre Belloni struct rv3028_data *rv3028; 730e6e7376cSAlexandre Belloni int ret, status; 731e6e7376cSAlexandre Belloni u32 ohms; 732e6e7376cSAlexandre Belloni struct nvmem_config nvmem_cfg = { 733e6e7376cSAlexandre Belloni .name = "rv3028_nvram", 734e6e7376cSAlexandre Belloni .word_size = 1, 735e6e7376cSAlexandre Belloni .stride = 1, 736e6e7376cSAlexandre Belloni .size = 2, 737e6e7376cSAlexandre Belloni .type = NVMEM_TYPE_BATTERY_BACKED, 738e6e7376cSAlexandre Belloni .reg_read = rv3028_nvram_read, 739e6e7376cSAlexandre Belloni .reg_write = rv3028_nvram_write, 740e6e7376cSAlexandre Belloni }; 741e6e7376cSAlexandre Belloni struct nvmem_config eeprom_cfg = { 742e6e7376cSAlexandre Belloni .name = "rv3028_eeprom", 743e6e7376cSAlexandre Belloni .word_size = 1, 744e6e7376cSAlexandre Belloni .stride = 1, 745e6e7376cSAlexandre Belloni .size = 43, 746e6e7376cSAlexandre Belloni .type = NVMEM_TYPE_EEPROM, 747e6e7376cSAlexandre Belloni .reg_read = rv3028_eeprom_read, 748e6e7376cSAlexandre Belloni .reg_write = rv3028_eeprom_write, 749e6e7376cSAlexandre Belloni }; 750e6e7376cSAlexandre Belloni 751e6e7376cSAlexandre Belloni rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data), 752e6e7376cSAlexandre Belloni GFP_KERNEL); 753e6e7376cSAlexandre Belloni if (!rv3028) 754e6e7376cSAlexandre Belloni return -ENOMEM; 755e6e7376cSAlexandre Belloni 756e6e7376cSAlexandre Belloni rv3028->regmap = devm_regmap_init_i2c(client, ®map_config); 757c3b29bf6SChuhong Yuan if (IS_ERR(rv3028->regmap)) 758c3b29bf6SChuhong Yuan return PTR_ERR(rv3028->regmap); 759e6e7376cSAlexandre Belloni 760e6e7376cSAlexandre Belloni i2c_set_clientdata(client, rv3028); 761e6e7376cSAlexandre Belloni 762e6e7376cSAlexandre Belloni ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status); 763e6e7376cSAlexandre Belloni if (ret < 0) 764e6e7376cSAlexandre Belloni return ret; 765e6e7376cSAlexandre Belloni 766e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_PORF) 767e6e7376cSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 768e6e7376cSAlexandre Belloni 769e6e7376cSAlexandre Belloni if (status & RV3028_STATUS_AF) 770e6e7376cSAlexandre Belloni dev_warn(&client->dev, "An alarm may have been missed.\n"); 771e6e7376cSAlexandre Belloni 772e6e7376cSAlexandre Belloni rv3028->rtc = devm_rtc_allocate_device(&client->dev); 77344c638ceSAlexandre Belloni if (IS_ERR(rv3028->rtc)) 774e6e7376cSAlexandre Belloni return PTR_ERR(rv3028->rtc); 775e6e7376cSAlexandre Belloni 776e6e7376cSAlexandre Belloni if (client->irq > 0) { 777e6e7376cSAlexandre Belloni ret = devm_request_threaded_irq(&client->dev, client->irq, 778e6e7376cSAlexandre Belloni NULL, rv3028_handle_irq, 779e6e7376cSAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 780e6e7376cSAlexandre Belloni "rv3028", rv3028); 781e6e7376cSAlexandre Belloni if (ret) { 782e6e7376cSAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 783e6e7376cSAlexandre Belloni client->irq = 0; 784e6e7376cSAlexandre Belloni } else { 785e6e7376cSAlexandre Belloni rv3028_rtc_ops.read_alarm = rv3028_get_alarm; 786e6e7376cSAlexandre Belloni rv3028_rtc_ops.set_alarm = rv3028_set_alarm; 787e6e7376cSAlexandre Belloni rv3028_rtc_ops.alarm_irq_enable = rv3028_alarm_irq_enable; 788e6e7376cSAlexandre Belloni } 789e6e7376cSAlexandre Belloni } 790e6e7376cSAlexandre Belloni 791e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1, 792e6e7376cSAlexandre Belloni RV3028_CTRL1_WADA, RV3028_CTRL1_WADA); 793e6e7376cSAlexandre Belloni if (ret) 794e6e7376cSAlexandre Belloni return ret; 795e6e7376cSAlexandre Belloni 796e6e7376cSAlexandre Belloni /* setup timestamping */ 797e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2, 798e6e7376cSAlexandre Belloni RV3028_CTRL2_EIE | RV3028_CTRL2_TSE, 799e6e7376cSAlexandre Belloni RV3028_CTRL2_EIE | RV3028_CTRL2_TSE); 800e6e7376cSAlexandre Belloni if (ret) 801e6e7376cSAlexandre Belloni return ret; 802e6e7376cSAlexandre Belloni 803e6e7376cSAlexandre Belloni /* setup trickle charger */ 804e6e7376cSAlexandre Belloni if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms", 805e6e7376cSAlexandre Belloni &ohms)) { 806e6e7376cSAlexandre Belloni int i; 807e6e7376cSAlexandre Belloni 808e6e7376cSAlexandre Belloni for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++) 809e6e7376cSAlexandre Belloni if (ohms == rv3028_trickle_resistors[i]) 810e6e7376cSAlexandre Belloni break; 811e6e7376cSAlexandre Belloni 812e6e7376cSAlexandre Belloni if (i < ARRAY_SIZE(rv3028_trickle_resistors)) { 813e6e7376cSAlexandre Belloni ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP, 814e6e7376cSAlexandre Belloni RV3028_BACKUP_TCE | 815e6e7376cSAlexandre Belloni RV3028_BACKUP_TCR_MASK, 816e6e7376cSAlexandre Belloni RV3028_BACKUP_TCE | i); 817e6e7376cSAlexandre Belloni if (ret) 818e6e7376cSAlexandre Belloni return ret; 819e6e7376cSAlexandre Belloni } else { 820e6e7376cSAlexandre Belloni dev_warn(&client->dev, "invalid trickle resistor value\n"); 821e6e7376cSAlexandre Belloni } 822e6e7376cSAlexandre Belloni } 823e6e7376cSAlexandre Belloni 824e6e7376cSAlexandre Belloni ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group); 825e6e7376cSAlexandre Belloni if (ret) 826e6e7376cSAlexandre Belloni return ret; 827e6e7376cSAlexandre Belloni 828e6e7376cSAlexandre Belloni rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 829e6e7376cSAlexandre Belloni rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099; 830e6e7376cSAlexandre Belloni rv3028->rtc->ops = &rv3028_rtc_ops; 831e6e7376cSAlexandre Belloni ret = rtc_register_device(rv3028->rtc); 832e6e7376cSAlexandre Belloni if (ret) 833e6e7376cSAlexandre Belloni return ret; 834e6e7376cSAlexandre Belloni 835e6e7376cSAlexandre Belloni nvmem_cfg.priv = rv3028->regmap; 836e6e7376cSAlexandre Belloni rtc_nvmem_register(rv3028->rtc, &nvmem_cfg); 837e6e7376cSAlexandre Belloni eeprom_cfg.priv = rv3028->regmap; 838e6e7376cSAlexandre Belloni rtc_nvmem_register(rv3028->rtc, &eeprom_cfg); 839e6e7376cSAlexandre Belloni 840e6e7376cSAlexandre Belloni rv3028->rtc->max_user_freq = 1; 841e6e7376cSAlexandre Belloni 842f583c341SParthiban Nallathambi #ifdef CONFIG_COMMON_CLK 843f583c341SParthiban Nallathambi rv3028_clkout_register_clk(rv3028, client); 844f583c341SParthiban Nallathambi #endif 845e6e7376cSAlexandre Belloni return 0; 846e6e7376cSAlexandre Belloni } 847e6e7376cSAlexandre Belloni 848e6e7376cSAlexandre Belloni static const struct of_device_id rv3028_of_match[] = { 849e6e7376cSAlexandre Belloni { .compatible = "microcrystal,rv3028", }, 850e6e7376cSAlexandre Belloni { } 851e6e7376cSAlexandre Belloni }; 852e6e7376cSAlexandre Belloni MODULE_DEVICE_TABLE(of, rv3028_of_match); 853e6e7376cSAlexandre Belloni 854e6e7376cSAlexandre Belloni static struct i2c_driver rv3028_driver = { 855e6e7376cSAlexandre Belloni .driver = { 856e6e7376cSAlexandre Belloni .name = "rtc-rv3028", 857e6e7376cSAlexandre Belloni .of_match_table = of_match_ptr(rv3028_of_match), 858e6e7376cSAlexandre Belloni }, 859e6e7376cSAlexandre Belloni .probe_new = rv3028_probe, 860e6e7376cSAlexandre Belloni }; 861e6e7376cSAlexandre Belloni module_i2c_driver(rv3028_driver); 862e6e7376cSAlexandre Belloni 863e6e7376cSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 864e6e7376cSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver"); 865e6e7376cSAlexandre Belloni MODULE_LICENSE("GPL v2"); 866