1*2eeaa532SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0 2*2eeaa532SAlexandre Belloni /* 3*2eeaa532SAlexandre Belloni * RTC driver for the Micro Crystal RV3032 4*2eeaa532SAlexandre Belloni * 5*2eeaa532SAlexandre Belloni * Copyright (C) 2020 Micro Crystal SA 6*2eeaa532SAlexandre Belloni * 7*2eeaa532SAlexandre Belloni * Alexandre Belloni <alexandre.belloni@bootlin.com> 8*2eeaa532SAlexandre Belloni * 9*2eeaa532SAlexandre Belloni */ 10*2eeaa532SAlexandre Belloni 11*2eeaa532SAlexandre Belloni #include <linux/clk.h> 12*2eeaa532SAlexandre Belloni #include <linux/clk-provider.h> 13*2eeaa532SAlexandre Belloni #include <linux/bcd.h> 14*2eeaa532SAlexandre Belloni #include <linux/bitfield.h> 15*2eeaa532SAlexandre Belloni #include <linux/bitops.h> 16*2eeaa532SAlexandre Belloni #include <linux/hwmon.h> 17*2eeaa532SAlexandre Belloni #include <linux/i2c.h> 18*2eeaa532SAlexandre Belloni #include <linux/interrupt.h> 19*2eeaa532SAlexandre Belloni #include <linux/kernel.h> 20*2eeaa532SAlexandre Belloni #include <linux/log2.h> 21*2eeaa532SAlexandre Belloni #include <linux/module.h> 22*2eeaa532SAlexandre Belloni #include <linux/of_device.h> 23*2eeaa532SAlexandre Belloni #include <linux/regmap.h> 24*2eeaa532SAlexandre Belloni #include <linux/rtc.h> 25*2eeaa532SAlexandre Belloni 26*2eeaa532SAlexandre Belloni #define RV3032_SEC 0x01 27*2eeaa532SAlexandre Belloni #define RV3032_MIN 0x02 28*2eeaa532SAlexandre Belloni #define RV3032_HOUR 0x03 29*2eeaa532SAlexandre Belloni #define RV3032_WDAY 0x04 30*2eeaa532SAlexandre Belloni #define RV3032_DAY 0x05 31*2eeaa532SAlexandre Belloni #define RV3032_MONTH 0x06 32*2eeaa532SAlexandre Belloni #define RV3032_YEAR 0x07 33*2eeaa532SAlexandre Belloni #define RV3032_ALARM_MIN 0x08 34*2eeaa532SAlexandre Belloni #define RV3032_ALARM_HOUR 0x09 35*2eeaa532SAlexandre Belloni #define RV3032_ALARM_DAY 0x0A 36*2eeaa532SAlexandre Belloni #define RV3032_STATUS 0x0D 37*2eeaa532SAlexandre Belloni #define RV3032_TLSB 0x0E 38*2eeaa532SAlexandre Belloni #define RV3032_TMSB 0x0F 39*2eeaa532SAlexandre Belloni #define RV3032_CTRL1 0x10 40*2eeaa532SAlexandre Belloni #define RV3032_CTRL2 0x11 41*2eeaa532SAlexandre Belloni #define RV3032_CTRL3 0x12 42*2eeaa532SAlexandre Belloni #define RV3032_TS_CTRL 0x13 43*2eeaa532SAlexandre Belloni #define RV3032_CLK_IRQ 0x14 44*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_ADDR 0x3D 45*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_DATA 0x3E 46*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD 0x3F 47*2eeaa532SAlexandre Belloni #define RV3032_RAM1 0x40 48*2eeaa532SAlexandre Belloni #define RV3032_PMU 0xC0 49*2eeaa532SAlexandre Belloni #define RV3032_OFFSET 0xC1 50*2eeaa532SAlexandre Belloni #define RV3032_CLKOUT1 0xC2 51*2eeaa532SAlexandre Belloni #define RV3032_CLKOUT2 0xC3 52*2eeaa532SAlexandre Belloni #define RV3032_TREF0 0xC4 53*2eeaa532SAlexandre Belloni #define RV3032_TREF1 0xC5 54*2eeaa532SAlexandre Belloni 55*2eeaa532SAlexandre Belloni #define RV3032_STATUS_VLF BIT(0) 56*2eeaa532SAlexandre Belloni #define RV3032_STATUS_PORF BIT(1) 57*2eeaa532SAlexandre Belloni #define RV3032_STATUS_EVF BIT(2) 58*2eeaa532SAlexandre Belloni #define RV3032_STATUS_AF BIT(3) 59*2eeaa532SAlexandre Belloni #define RV3032_STATUS_TF BIT(4) 60*2eeaa532SAlexandre Belloni #define RV3032_STATUS_UF BIT(5) 61*2eeaa532SAlexandre Belloni #define RV3032_STATUS_TLF BIT(6) 62*2eeaa532SAlexandre Belloni #define RV3032_STATUS_THF BIT(7) 63*2eeaa532SAlexandre Belloni 64*2eeaa532SAlexandre Belloni #define RV3032_TLSB_CLKF BIT(1) 65*2eeaa532SAlexandre Belloni #define RV3032_TLSB_EEBUSY BIT(2) 66*2eeaa532SAlexandre Belloni #define RV3032_TLSB_TEMP GENMASK(7, 4) 67*2eeaa532SAlexandre Belloni 68*2eeaa532SAlexandre Belloni #define RV3032_CLKOUT2_HFD_MSK GENMASK(4, 0) 69*2eeaa532SAlexandre Belloni #define RV3032_CLKOUT2_FD_MSK GENMASK(6, 5) 70*2eeaa532SAlexandre Belloni #define RV3032_CLKOUT2_OS BIT(7) 71*2eeaa532SAlexandre Belloni 72*2eeaa532SAlexandre Belloni #define RV3032_CTRL1_EERD BIT(3) 73*2eeaa532SAlexandre Belloni #define RV3032_CTRL1_WADA BIT(5) 74*2eeaa532SAlexandre Belloni 75*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_STOP BIT(0) 76*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_EIE BIT(2) 77*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_AIE BIT(3) 78*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_TIE BIT(4) 79*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_UIE BIT(5) 80*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_CLKIE BIT(6) 81*2eeaa532SAlexandre Belloni #define RV3032_CTRL2_TSE BIT(7) 82*2eeaa532SAlexandre Belloni 83*2eeaa532SAlexandre Belloni #define RV3032_PMU_TCM GENMASK(1, 0) 84*2eeaa532SAlexandre Belloni #define RV3032_PMU_TCR GENMASK(3, 2) 85*2eeaa532SAlexandre Belloni #define RV3032_PMU_BSM GENMASK(5, 4) 86*2eeaa532SAlexandre Belloni #define RV3032_PMU_NCLKE BIT(6) 87*2eeaa532SAlexandre Belloni 88*2eeaa532SAlexandre Belloni #define RV3032_PMU_BSM_DSM 1 89*2eeaa532SAlexandre Belloni #define RV3032_PMU_BSM_LSM 2 90*2eeaa532SAlexandre Belloni 91*2eeaa532SAlexandre Belloni #define RV3032_OFFSET_MSK GENMASK(5, 0) 92*2eeaa532SAlexandre Belloni 93*2eeaa532SAlexandre Belloni #define RV3032_EVT_CTRL_TSR BIT(2) 94*2eeaa532SAlexandre Belloni 95*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD_UPDATE 0x11 96*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD_WRITE 0x21 97*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD_READ 0x22 98*2eeaa532SAlexandre Belloni 99*2eeaa532SAlexandre Belloni #define RV3032_EEPROM_USER 0xCB 100*2eeaa532SAlexandre Belloni 101*2eeaa532SAlexandre Belloni #define RV3032_EEBUSY_POLL 10000 102*2eeaa532SAlexandre Belloni #define RV3032_EEBUSY_TIMEOUT 100000 103*2eeaa532SAlexandre Belloni 104*2eeaa532SAlexandre Belloni #define OFFSET_STEP_PPT 238419 105*2eeaa532SAlexandre Belloni 106*2eeaa532SAlexandre Belloni struct rv3032_data { 107*2eeaa532SAlexandre Belloni struct regmap *regmap; 108*2eeaa532SAlexandre Belloni struct rtc_device *rtc; 109*2eeaa532SAlexandre Belloni #ifdef CONFIG_COMMON_CLK 110*2eeaa532SAlexandre Belloni struct clk_hw clkout_hw; 111*2eeaa532SAlexandre Belloni #endif 112*2eeaa532SAlexandre Belloni }; 113*2eeaa532SAlexandre Belloni 114*2eeaa532SAlexandre Belloni static u16 rv3032_trickle_resistors[] = {1000, 2000, 7000, 11000}; 115*2eeaa532SAlexandre Belloni static u16 rv3032_trickle_voltages[] = {0, 1750, 3000, 4400}; 116*2eeaa532SAlexandre Belloni 117*2eeaa532SAlexandre Belloni static int rv3032_exit_eerd(struct rv3032_data *rv3032, u32 eerd) 118*2eeaa532SAlexandre Belloni { 119*2eeaa532SAlexandre Belloni if (eerd) 120*2eeaa532SAlexandre Belloni return 0; 121*2eeaa532SAlexandre Belloni 122*2eeaa532SAlexandre Belloni return regmap_update_bits(rv3032->regmap, RV3032_CTRL1, RV3032_CTRL1_EERD, 0); 123*2eeaa532SAlexandre Belloni } 124*2eeaa532SAlexandre Belloni 125*2eeaa532SAlexandre Belloni static int rv3032_enter_eerd(struct rv3032_data *rv3032, u32 *eerd) 126*2eeaa532SAlexandre Belloni { 127*2eeaa532SAlexandre Belloni u32 ctrl1, status; 128*2eeaa532SAlexandre Belloni int ret; 129*2eeaa532SAlexandre Belloni 130*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CTRL1, &ctrl1); 131*2eeaa532SAlexandre Belloni if (ret) 132*2eeaa532SAlexandre Belloni return ret; 133*2eeaa532SAlexandre Belloni 134*2eeaa532SAlexandre Belloni *eerd = ctrl1 & RV3032_CTRL1_EERD; 135*2eeaa532SAlexandre Belloni if (*eerd) 136*2eeaa532SAlexandre Belloni return 0; 137*2eeaa532SAlexandre Belloni 138*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL1, 139*2eeaa532SAlexandre Belloni RV3032_CTRL1_EERD, RV3032_CTRL1_EERD); 140*2eeaa532SAlexandre Belloni if (ret) 141*2eeaa532SAlexandre Belloni return ret; 142*2eeaa532SAlexandre Belloni 143*2eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 144*2eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 145*2eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 146*2eeaa532SAlexandre Belloni if (ret) { 147*2eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, *eerd); 148*2eeaa532SAlexandre Belloni 149*2eeaa532SAlexandre Belloni return ret; 150*2eeaa532SAlexandre Belloni } 151*2eeaa532SAlexandre Belloni 152*2eeaa532SAlexandre Belloni return 0; 153*2eeaa532SAlexandre Belloni } 154*2eeaa532SAlexandre Belloni 155*2eeaa532SAlexandre Belloni static int rv3032_update_cfg(struct rv3032_data *rv3032, unsigned int reg, 156*2eeaa532SAlexandre Belloni unsigned int mask, unsigned int val) 157*2eeaa532SAlexandre Belloni { 158*2eeaa532SAlexandre Belloni u32 status, eerd; 159*2eeaa532SAlexandre Belloni int ret; 160*2eeaa532SAlexandre Belloni 161*2eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 162*2eeaa532SAlexandre Belloni if (ret) 163*2eeaa532SAlexandre Belloni return ret; 164*2eeaa532SAlexandre Belloni 165*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, reg, mask, val); 166*2eeaa532SAlexandre Belloni if (ret) 167*2eeaa532SAlexandre Belloni goto exit_eerd; 168*2eeaa532SAlexandre Belloni 169*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, RV3032_EEPROM_CMD_UPDATE); 170*2eeaa532SAlexandre Belloni if (ret) 171*2eeaa532SAlexandre Belloni goto exit_eerd; 172*2eeaa532SAlexandre Belloni 173*2eeaa532SAlexandre Belloni usleep_range(46000, RV3032_EEBUSY_TIMEOUT); 174*2eeaa532SAlexandre Belloni 175*2eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 176*2eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 177*2eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 178*2eeaa532SAlexandre Belloni 179*2eeaa532SAlexandre Belloni exit_eerd: 180*2eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 181*2eeaa532SAlexandre Belloni 182*2eeaa532SAlexandre Belloni return ret; 183*2eeaa532SAlexandre Belloni } 184*2eeaa532SAlexandre Belloni 185*2eeaa532SAlexandre Belloni static irqreturn_t rv3032_handle_irq(int irq, void *dev_id) 186*2eeaa532SAlexandre Belloni { 187*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_id; 188*2eeaa532SAlexandre Belloni unsigned long events = 0; 189*2eeaa532SAlexandre Belloni u32 status = 0, ctrl = 0; 190*2eeaa532SAlexandre Belloni 191*2eeaa532SAlexandre Belloni if (regmap_read(rv3032->regmap, RV3032_STATUS, &status) < 0 || 192*2eeaa532SAlexandre Belloni status == 0) { 193*2eeaa532SAlexandre Belloni return IRQ_NONE; 194*2eeaa532SAlexandre Belloni } 195*2eeaa532SAlexandre Belloni 196*2eeaa532SAlexandre Belloni if (status & RV3032_STATUS_TF) { 197*2eeaa532SAlexandre Belloni status |= RV3032_STATUS_TF; 198*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_TIE; 199*2eeaa532SAlexandre Belloni events |= RTC_PF; 200*2eeaa532SAlexandre Belloni } 201*2eeaa532SAlexandre Belloni 202*2eeaa532SAlexandre Belloni if (status & RV3032_STATUS_AF) { 203*2eeaa532SAlexandre Belloni status |= RV3032_STATUS_AF; 204*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_AIE; 205*2eeaa532SAlexandre Belloni events |= RTC_AF; 206*2eeaa532SAlexandre Belloni } 207*2eeaa532SAlexandre Belloni 208*2eeaa532SAlexandre Belloni if (status & RV3032_STATUS_UF) { 209*2eeaa532SAlexandre Belloni status |= RV3032_STATUS_UF; 210*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_UIE; 211*2eeaa532SAlexandre Belloni events |= RTC_UF; 212*2eeaa532SAlexandre Belloni } 213*2eeaa532SAlexandre Belloni 214*2eeaa532SAlexandre Belloni if (events) { 215*2eeaa532SAlexandre Belloni rtc_update_irq(rv3032->rtc, 1, events); 216*2eeaa532SAlexandre Belloni regmap_update_bits(rv3032->regmap, RV3032_STATUS, status, 0); 217*2eeaa532SAlexandre Belloni regmap_update_bits(rv3032->regmap, RV3032_CTRL2, ctrl, 0); 218*2eeaa532SAlexandre Belloni } 219*2eeaa532SAlexandre Belloni 220*2eeaa532SAlexandre Belloni return IRQ_HANDLED; 221*2eeaa532SAlexandre Belloni } 222*2eeaa532SAlexandre Belloni 223*2eeaa532SAlexandre Belloni static int rv3032_get_time(struct device *dev, struct rtc_time *tm) 224*2eeaa532SAlexandre Belloni { 225*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 226*2eeaa532SAlexandre Belloni u8 date[7]; 227*2eeaa532SAlexandre Belloni int ret, status; 228*2eeaa532SAlexandre Belloni 229*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 230*2eeaa532SAlexandre Belloni if (ret < 0) 231*2eeaa532SAlexandre Belloni return ret; 232*2eeaa532SAlexandre Belloni 233*2eeaa532SAlexandre Belloni if (status & (RV3032_STATUS_PORF | RV3032_STATUS_VLF)) 234*2eeaa532SAlexandre Belloni return -EINVAL; 235*2eeaa532SAlexandre Belloni 236*2eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_SEC, date, sizeof(date)); 237*2eeaa532SAlexandre Belloni if (ret) 238*2eeaa532SAlexandre Belloni return ret; 239*2eeaa532SAlexandre Belloni 240*2eeaa532SAlexandre Belloni tm->tm_sec = bcd2bin(date[0] & 0x7f); 241*2eeaa532SAlexandre Belloni tm->tm_min = bcd2bin(date[1] & 0x7f); 242*2eeaa532SAlexandre Belloni tm->tm_hour = bcd2bin(date[2] & 0x3f); 243*2eeaa532SAlexandre Belloni tm->tm_wday = date[3] & 0x7; 244*2eeaa532SAlexandre Belloni tm->tm_mday = bcd2bin(date[4] & 0x3f); 245*2eeaa532SAlexandre Belloni tm->tm_mon = bcd2bin(date[5] & 0x1f) - 1; 246*2eeaa532SAlexandre Belloni tm->tm_year = bcd2bin(date[6]) + 100; 247*2eeaa532SAlexandre Belloni 248*2eeaa532SAlexandre Belloni return 0; 249*2eeaa532SAlexandre Belloni } 250*2eeaa532SAlexandre Belloni 251*2eeaa532SAlexandre Belloni static int rv3032_set_time(struct device *dev, struct rtc_time *tm) 252*2eeaa532SAlexandre Belloni { 253*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 254*2eeaa532SAlexandre Belloni u8 date[7]; 255*2eeaa532SAlexandre Belloni int ret; 256*2eeaa532SAlexandre Belloni 257*2eeaa532SAlexandre Belloni date[0] = bin2bcd(tm->tm_sec); 258*2eeaa532SAlexandre Belloni date[1] = bin2bcd(tm->tm_min); 259*2eeaa532SAlexandre Belloni date[2] = bin2bcd(tm->tm_hour); 260*2eeaa532SAlexandre Belloni date[3] = tm->tm_wday; 261*2eeaa532SAlexandre Belloni date[4] = bin2bcd(tm->tm_mday); 262*2eeaa532SAlexandre Belloni date[5] = bin2bcd(tm->tm_mon + 1); 263*2eeaa532SAlexandre Belloni date[6] = bin2bcd(tm->tm_year - 100); 264*2eeaa532SAlexandre Belloni 265*2eeaa532SAlexandre Belloni ret = regmap_bulk_write(rv3032->regmap, RV3032_SEC, date, 266*2eeaa532SAlexandre Belloni sizeof(date)); 267*2eeaa532SAlexandre Belloni if (ret) 268*2eeaa532SAlexandre Belloni return ret; 269*2eeaa532SAlexandre Belloni 270*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS, 271*2eeaa532SAlexandre Belloni RV3032_STATUS_PORF | RV3032_STATUS_VLF, 0); 272*2eeaa532SAlexandre Belloni 273*2eeaa532SAlexandre Belloni return ret; 274*2eeaa532SAlexandre Belloni } 275*2eeaa532SAlexandre Belloni 276*2eeaa532SAlexandre Belloni static int rv3032_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 277*2eeaa532SAlexandre Belloni { 278*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 279*2eeaa532SAlexandre Belloni u8 alarmvals[3]; 280*2eeaa532SAlexandre Belloni int status, ctrl, ret; 281*2eeaa532SAlexandre Belloni 282*2eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_ALARM_MIN, alarmvals, 283*2eeaa532SAlexandre Belloni sizeof(alarmvals)); 284*2eeaa532SAlexandre Belloni if (ret) 285*2eeaa532SAlexandre Belloni return ret; 286*2eeaa532SAlexandre Belloni 287*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 288*2eeaa532SAlexandre Belloni if (ret < 0) 289*2eeaa532SAlexandre Belloni return ret; 290*2eeaa532SAlexandre Belloni 291*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CTRL2, &ctrl); 292*2eeaa532SAlexandre Belloni if (ret < 0) 293*2eeaa532SAlexandre Belloni return ret; 294*2eeaa532SAlexandre Belloni 295*2eeaa532SAlexandre Belloni alrm->time.tm_sec = 0; 296*2eeaa532SAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 297*2eeaa532SAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 298*2eeaa532SAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 299*2eeaa532SAlexandre Belloni 300*2eeaa532SAlexandre Belloni alrm->enabled = !!(ctrl & RV3032_CTRL2_AIE); 301*2eeaa532SAlexandre Belloni alrm->pending = (status & RV3032_STATUS_AF) && alrm->enabled; 302*2eeaa532SAlexandre Belloni 303*2eeaa532SAlexandre Belloni return 0; 304*2eeaa532SAlexandre Belloni } 305*2eeaa532SAlexandre Belloni 306*2eeaa532SAlexandre Belloni static int rv3032_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 307*2eeaa532SAlexandre Belloni { 308*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 309*2eeaa532SAlexandre Belloni u8 alarmvals[3]; 310*2eeaa532SAlexandre Belloni u8 ctrl = 0; 311*2eeaa532SAlexandre Belloni int ret; 312*2eeaa532SAlexandre Belloni 313*2eeaa532SAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 314*2eeaa532SAlexandre Belloni if (alrm->time.tm_sec) { 315*2eeaa532SAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 316*2eeaa532SAlexandre Belloni 317*2eeaa532SAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 318*2eeaa532SAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 319*2eeaa532SAlexandre Belloni } 320*2eeaa532SAlexandre Belloni 321*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, 322*2eeaa532SAlexandre Belloni RV3032_CTRL2_AIE | RV3032_CTRL2_UIE, 0); 323*2eeaa532SAlexandre Belloni if (ret) 324*2eeaa532SAlexandre Belloni return ret; 325*2eeaa532SAlexandre Belloni 326*2eeaa532SAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 327*2eeaa532SAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 328*2eeaa532SAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 329*2eeaa532SAlexandre Belloni 330*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS, 331*2eeaa532SAlexandre Belloni RV3032_STATUS_AF, 0); 332*2eeaa532SAlexandre Belloni if (ret) 333*2eeaa532SAlexandre Belloni return ret; 334*2eeaa532SAlexandre Belloni 335*2eeaa532SAlexandre Belloni ret = regmap_bulk_write(rv3032->regmap, RV3032_ALARM_MIN, alarmvals, 336*2eeaa532SAlexandre Belloni sizeof(alarmvals)); 337*2eeaa532SAlexandre Belloni if (ret) 338*2eeaa532SAlexandre Belloni return ret; 339*2eeaa532SAlexandre Belloni 340*2eeaa532SAlexandre Belloni if (alrm->enabled) { 341*2eeaa532SAlexandre Belloni if (rv3032->rtc->uie_rtctimer.enabled) 342*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_UIE; 343*2eeaa532SAlexandre Belloni if (rv3032->rtc->aie_timer.enabled) 344*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_AIE; 345*2eeaa532SAlexandre Belloni } 346*2eeaa532SAlexandre Belloni 347*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, 348*2eeaa532SAlexandre Belloni RV3032_CTRL2_UIE | RV3032_CTRL2_AIE, ctrl); 349*2eeaa532SAlexandre Belloni 350*2eeaa532SAlexandre Belloni return ret; 351*2eeaa532SAlexandre Belloni } 352*2eeaa532SAlexandre Belloni 353*2eeaa532SAlexandre Belloni static int rv3032_alarm_irq_enable(struct device *dev, unsigned int enabled) 354*2eeaa532SAlexandre Belloni { 355*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 356*2eeaa532SAlexandre Belloni int ctrl = 0, ret; 357*2eeaa532SAlexandre Belloni 358*2eeaa532SAlexandre Belloni if (enabled) { 359*2eeaa532SAlexandre Belloni if (rv3032->rtc->uie_rtctimer.enabled) 360*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_UIE; 361*2eeaa532SAlexandre Belloni if (rv3032->rtc->aie_timer.enabled) 362*2eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_AIE; 363*2eeaa532SAlexandre Belloni } 364*2eeaa532SAlexandre Belloni 365*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS, 366*2eeaa532SAlexandre Belloni RV3032_STATUS_AF | RV3032_STATUS_UF, 0); 367*2eeaa532SAlexandre Belloni if (ret) 368*2eeaa532SAlexandre Belloni return ret; 369*2eeaa532SAlexandre Belloni 370*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, 371*2eeaa532SAlexandre Belloni RV3032_CTRL2_UIE | RV3032_CTRL2_AIE, ctrl); 372*2eeaa532SAlexandre Belloni if (ret) 373*2eeaa532SAlexandre Belloni return ret; 374*2eeaa532SAlexandre Belloni 375*2eeaa532SAlexandre Belloni return 0; 376*2eeaa532SAlexandre Belloni } 377*2eeaa532SAlexandre Belloni 378*2eeaa532SAlexandre Belloni static int rv3032_read_offset(struct device *dev, long *offset) 379*2eeaa532SAlexandre Belloni { 380*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 381*2eeaa532SAlexandre Belloni int ret, value, steps; 382*2eeaa532SAlexandre Belloni 383*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_OFFSET, &value); 384*2eeaa532SAlexandre Belloni if (ret < 0) 385*2eeaa532SAlexandre Belloni return ret; 386*2eeaa532SAlexandre Belloni 387*2eeaa532SAlexandre Belloni steps = sign_extend32(FIELD_GET(RV3032_OFFSET_MSK, value), 5); 388*2eeaa532SAlexandre Belloni 389*2eeaa532SAlexandre Belloni *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000); 390*2eeaa532SAlexandre Belloni 391*2eeaa532SAlexandre Belloni return 0; 392*2eeaa532SAlexandre Belloni } 393*2eeaa532SAlexandre Belloni 394*2eeaa532SAlexandre Belloni static int rv3032_set_offset(struct device *dev, long offset) 395*2eeaa532SAlexandre Belloni { 396*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 397*2eeaa532SAlexandre Belloni 398*2eeaa532SAlexandre Belloni offset = clamp(offset, -7629L, 7391L) * 1000; 399*2eeaa532SAlexandre Belloni offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT); 400*2eeaa532SAlexandre Belloni 401*2eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_OFFSET, RV3032_OFFSET_MSK, 402*2eeaa532SAlexandre Belloni FIELD_PREP(RV3032_OFFSET_MSK, offset)); 403*2eeaa532SAlexandre Belloni } 404*2eeaa532SAlexandre Belloni 405*2eeaa532SAlexandre Belloni static int rv3032_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 406*2eeaa532SAlexandre Belloni { 407*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 408*2eeaa532SAlexandre Belloni int status, val = 0, ret = 0; 409*2eeaa532SAlexandre Belloni 410*2eeaa532SAlexandre Belloni switch (cmd) { 411*2eeaa532SAlexandre Belloni case RTC_VL_READ: 412*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 413*2eeaa532SAlexandre Belloni if (ret < 0) 414*2eeaa532SAlexandre Belloni return ret; 415*2eeaa532SAlexandre Belloni 416*2eeaa532SAlexandre Belloni if (status & (RV3032_STATUS_PORF | RV3032_STATUS_VLF)) 417*2eeaa532SAlexandre Belloni val = RTC_VL_DATA_INVALID; 418*2eeaa532SAlexandre Belloni return put_user(val, (unsigned int __user *)arg); 419*2eeaa532SAlexandre Belloni 420*2eeaa532SAlexandre Belloni default: 421*2eeaa532SAlexandre Belloni return -ENOIOCTLCMD; 422*2eeaa532SAlexandre Belloni } 423*2eeaa532SAlexandre Belloni } 424*2eeaa532SAlexandre Belloni 425*2eeaa532SAlexandre Belloni static int rv3032_nvram_write(void *priv, unsigned int offset, void *val, size_t bytes) 426*2eeaa532SAlexandre Belloni { 427*2eeaa532SAlexandre Belloni return regmap_bulk_write(priv, RV3032_RAM1 + offset, val, bytes); 428*2eeaa532SAlexandre Belloni } 429*2eeaa532SAlexandre Belloni 430*2eeaa532SAlexandre Belloni static int rv3032_nvram_read(void *priv, unsigned int offset, void *val, size_t bytes) 431*2eeaa532SAlexandre Belloni { 432*2eeaa532SAlexandre Belloni return regmap_bulk_read(priv, RV3032_RAM1 + offset, val, bytes); 433*2eeaa532SAlexandre Belloni } 434*2eeaa532SAlexandre Belloni 435*2eeaa532SAlexandre Belloni static int rv3032_eeprom_write(void *priv, unsigned int offset, void *val, size_t bytes) 436*2eeaa532SAlexandre Belloni { 437*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = priv; 438*2eeaa532SAlexandre Belloni u32 status, eerd; 439*2eeaa532SAlexandre Belloni int i, ret; 440*2eeaa532SAlexandre Belloni u8 *buf = val; 441*2eeaa532SAlexandre Belloni 442*2eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 443*2eeaa532SAlexandre Belloni if (ret) 444*2eeaa532SAlexandre Belloni return ret; 445*2eeaa532SAlexandre Belloni 446*2eeaa532SAlexandre Belloni for (i = 0; i < bytes; i++) { 447*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_ADDR, 448*2eeaa532SAlexandre Belloni RV3032_EEPROM_USER + offset + i); 449*2eeaa532SAlexandre Belloni if (ret) 450*2eeaa532SAlexandre Belloni goto exit_eerd; 451*2eeaa532SAlexandre Belloni 452*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_DATA, buf[i]); 453*2eeaa532SAlexandre Belloni if (ret) 454*2eeaa532SAlexandre Belloni goto exit_eerd; 455*2eeaa532SAlexandre Belloni 456*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, 457*2eeaa532SAlexandre Belloni RV3032_EEPROM_CMD_WRITE); 458*2eeaa532SAlexandre Belloni if (ret) 459*2eeaa532SAlexandre Belloni goto exit_eerd; 460*2eeaa532SAlexandre Belloni 461*2eeaa532SAlexandre Belloni usleep_range(RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 462*2eeaa532SAlexandre Belloni 463*2eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 464*2eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 465*2eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 466*2eeaa532SAlexandre Belloni if (ret) 467*2eeaa532SAlexandre Belloni goto exit_eerd; 468*2eeaa532SAlexandre Belloni } 469*2eeaa532SAlexandre Belloni 470*2eeaa532SAlexandre Belloni exit_eerd: 471*2eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 472*2eeaa532SAlexandre Belloni 473*2eeaa532SAlexandre Belloni return ret; 474*2eeaa532SAlexandre Belloni } 475*2eeaa532SAlexandre Belloni 476*2eeaa532SAlexandre Belloni static int rv3032_eeprom_read(void *priv, unsigned int offset, void *val, size_t bytes) 477*2eeaa532SAlexandre Belloni { 478*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = priv; 479*2eeaa532SAlexandre Belloni u32 status, eerd, data; 480*2eeaa532SAlexandre Belloni int i, ret; 481*2eeaa532SAlexandre Belloni u8 *buf = val; 482*2eeaa532SAlexandre Belloni 483*2eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 484*2eeaa532SAlexandre Belloni if (ret) 485*2eeaa532SAlexandre Belloni return ret; 486*2eeaa532SAlexandre Belloni 487*2eeaa532SAlexandre Belloni for (i = 0; i < bytes; i++) { 488*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_ADDR, 489*2eeaa532SAlexandre Belloni RV3032_EEPROM_USER + offset + i); 490*2eeaa532SAlexandre Belloni if (ret) 491*2eeaa532SAlexandre Belloni goto exit_eerd; 492*2eeaa532SAlexandre Belloni 493*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, 494*2eeaa532SAlexandre Belloni RV3032_EEPROM_CMD_READ); 495*2eeaa532SAlexandre Belloni if (ret) 496*2eeaa532SAlexandre Belloni goto exit_eerd; 497*2eeaa532SAlexandre Belloni 498*2eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 499*2eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 500*2eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 501*2eeaa532SAlexandre Belloni if (ret) 502*2eeaa532SAlexandre Belloni goto exit_eerd; 503*2eeaa532SAlexandre Belloni 504*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_EEPROM_DATA, &data); 505*2eeaa532SAlexandre Belloni if (ret) 506*2eeaa532SAlexandre Belloni goto exit_eerd; 507*2eeaa532SAlexandre Belloni buf[i] = data; 508*2eeaa532SAlexandre Belloni } 509*2eeaa532SAlexandre Belloni 510*2eeaa532SAlexandre Belloni exit_eerd: 511*2eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 512*2eeaa532SAlexandre Belloni 513*2eeaa532SAlexandre Belloni return ret; 514*2eeaa532SAlexandre Belloni } 515*2eeaa532SAlexandre Belloni 516*2eeaa532SAlexandre Belloni static int rv3032_trickle_charger_setup(struct device *dev, struct rv3032_data *rv3032) 517*2eeaa532SAlexandre Belloni { 518*2eeaa532SAlexandre Belloni u32 val, ohms, voltage; 519*2eeaa532SAlexandre Belloni int i; 520*2eeaa532SAlexandre Belloni 521*2eeaa532SAlexandre Belloni val = FIELD_PREP(RV3032_PMU_TCM, 1) | FIELD_PREP(RV3032_PMU_BSM, RV3032_PMU_BSM_DSM); 522*2eeaa532SAlexandre Belloni if (!device_property_read_u32(dev, "trickle-voltage-millivolt", &voltage)) { 523*2eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(rv3032_trickle_voltages); i++) 524*2eeaa532SAlexandre Belloni if (voltage == rv3032_trickle_voltages[i]) 525*2eeaa532SAlexandre Belloni break; 526*2eeaa532SAlexandre Belloni if (i < ARRAY_SIZE(rv3032_trickle_voltages)) 527*2eeaa532SAlexandre Belloni val = FIELD_PREP(RV3032_PMU_TCM, i) | 528*2eeaa532SAlexandre Belloni FIELD_PREP(RV3032_PMU_BSM, RV3032_PMU_BSM_LSM); 529*2eeaa532SAlexandre Belloni } 530*2eeaa532SAlexandre Belloni 531*2eeaa532SAlexandre Belloni if (device_property_read_u32(dev, "trickle-resistor-ohms", &ohms)) 532*2eeaa532SAlexandre Belloni return 0; 533*2eeaa532SAlexandre Belloni 534*2eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(rv3032_trickle_resistors); i++) 535*2eeaa532SAlexandre Belloni if (ohms == rv3032_trickle_resistors[i]) 536*2eeaa532SAlexandre Belloni break; 537*2eeaa532SAlexandre Belloni 538*2eeaa532SAlexandre Belloni if (i >= ARRAY_SIZE(rv3032_trickle_resistors)) { 539*2eeaa532SAlexandre Belloni dev_warn(dev, "invalid trickle resistor value\n"); 540*2eeaa532SAlexandre Belloni 541*2eeaa532SAlexandre Belloni return 0; 542*2eeaa532SAlexandre Belloni } 543*2eeaa532SAlexandre Belloni 544*2eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_PMU, 545*2eeaa532SAlexandre Belloni RV3032_PMU_TCR | RV3032_PMU_TCM | RV3032_PMU_BSM, 546*2eeaa532SAlexandre Belloni val | FIELD_PREP(RV3032_PMU_TCR, i)); 547*2eeaa532SAlexandre Belloni } 548*2eeaa532SAlexandre Belloni 549*2eeaa532SAlexandre Belloni #ifdef CONFIG_COMMON_CLK 550*2eeaa532SAlexandre Belloni #define clkout_hw_to_rv3032(hw) container_of(hw, struct rv3032_data, clkout_hw) 551*2eeaa532SAlexandre Belloni 552*2eeaa532SAlexandre Belloni static int clkout_xtal_rates[] = { 553*2eeaa532SAlexandre Belloni 32768, 554*2eeaa532SAlexandre Belloni 1024, 555*2eeaa532SAlexandre Belloni 64, 556*2eeaa532SAlexandre Belloni 1, 557*2eeaa532SAlexandre Belloni }; 558*2eeaa532SAlexandre Belloni 559*2eeaa532SAlexandre Belloni #define RV3032_HFD_STEP 8192 560*2eeaa532SAlexandre Belloni 561*2eeaa532SAlexandre Belloni static unsigned long rv3032_clkout_recalc_rate(struct clk_hw *hw, 562*2eeaa532SAlexandre Belloni unsigned long parent_rate) 563*2eeaa532SAlexandre Belloni { 564*2eeaa532SAlexandre Belloni int clkout, ret; 565*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 566*2eeaa532SAlexandre Belloni 567*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CLKOUT2, &clkout); 568*2eeaa532SAlexandre Belloni if (ret < 0) 569*2eeaa532SAlexandre Belloni return 0; 570*2eeaa532SAlexandre Belloni 571*2eeaa532SAlexandre Belloni if (clkout & RV3032_CLKOUT2_OS) { 572*2eeaa532SAlexandre Belloni unsigned long rate = FIELD_GET(RV3032_CLKOUT2_HFD_MSK, clkout) << 8; 573*2eeaa532SAlexandre Belloni 574*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CLKOUT1, &clkout); 575*2eeaa532SAlexandre Belloni if (ret < 0) 576*2eeaa532SAlexandre Belloni return 0; 577*2eeaa532SAlexandre Belloni 578*2eeaa532SAlexandre Belloni rate += clkout + 1; 579*2eeaa532SAlexandre Belloni 580*2eeaa532SAlexandre Belloni return rate * RV3032_HFD_STEP; 581*2eeaa532SAlexandre Belloni } 582*2eeaa532SAlexandre Belloni 583*2eeaa532SAlexandre Belloni return clkout_xtal_rates[FIELD_GET(RV3032_CLKOUT2_FD_MSK, clkout)]; 584*2eeaa532SAlexandre Belloni } 585*2eeaa532SAlexandre Belloni 586*2eeaa532SAlexandre Belloni static long rv3032_clkout_round_rate(struct clk_hw *hw, unsigned long rate, 587*2eeaa532SAlexandre Belloni unsigned long *prate) 588*2eeaa532SAlexandre Belloni { 589*2eeaa532SAlexandre Belloni int i, hfd; 590*2eeaa532SAlexandre Belloni 591*2eeaa532SAlexandre Belloni if (rate < RV3032_HFD_STEP) 592*2eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(clkout_xtal_rates); i++) 593*2eeaa532SAlexandre Belloni if (clkout_xtal_rates[i] <= rate) 594*2eeaa532SAlexandre Belloni return clkout_xtal_rates[i]; 595*2eeaa532SAlexandre Belloni 596*2eeaa532SAlexandre Belloni hfd = DIV_ROUND_CLOSEST(rate, RV3032_HFD_STEP); 597*2eeaa532SAlexandre Belloni 598*2eeaa532SAlexandre Belloni return RV3032_HFD_STEP * clamp(hfd, 0, 8192); 599*2eeaa532SAlexandre Belloni } 600*2eeaa532SAlexandre Belloni 601*2eeaa532SAlexandre Belloni static int rv3032_clkout_set_rate(struct clk_hw *hw, unsigned long rate, 602*2eeaa532SAlexandre Belloni unsigned long parent_rate) 603*2eeaa532SAlexandre Belloni { 604*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 605*2eeaa532SAlexandre Belloni u32 status, eerd; 606*2eeaa532SAlexandre Belloni int i, hfd, ret; 607*2eeaa532SAlexandre Belloni 608*2eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(clkout_xtal_rates); i++) { 609*2eeaa532SAlexandre Belloni if (clkout_xtal_rates[i] == rate) { 610*2eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_CLKOUT2, 0xff, 611*2eeaa532SAlexandre Belloni FIELD_PREP(RV3032_CLKOUT2_FD_MSK, i)); 612*2eeaa532SAlexandre Belloni } 613*2eeaa532SAlexandre Belloni } 614*2eeaa532SAlexandre Belloni 615*2eeaa532SAlexandre Belloni hfd = DIV_ROUND_CLOSEST(rate, RV3032_HFD_STEP); 616*2eeaa532SAlexandre Belloni hfd = clamp(hfd, 1, 8192) - 1; 617*2eeaa532SAlexandre Belloni 618*2eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 619*2eeaa532SAlexandre Belloni if (ret) 620*2eeaa532SAlexandre Belloni goto exit_eerd; 621*2eeaa532SAlexandre Belloni 622*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_CLKOUT1, hfd & 0xff); 623*2eeaa532SAlexandre Belloni if (ret) 624*2eeaa532SAlexandre Belloni return ret; 625*2eeaa532SAlexandre Belloni 626*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_CLKOUT2, RV3032_CLKOUT2_OS | 627*2eeaa532SAlexandre Belloni FIELD_PREP(RV3032_CLKOUT2_HFD_MSK, hfd >> 8)); 628*2eeaa532SAlexandre Belloni if (ret) 629*2eeaa532SAlexandre Belloni goto exit_eerd; 630*2eeaa532SAlexandre Belloni 631*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, RV3032_EEPROM_CMD_UPDATE); 632*2eeaa532SAlexandre Belloni if (ret) 633*2eeaa532SAlexandre Belloni goto exit_eerd; 634*2eeaa532SAlexandre Belloni 635*2eeaa532SAlexandre Belloni usleep_range(46000, RV3032_EEBUSY_TIMEOUT); 636*2eeaa532SAlexandre Belloni 637*2eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 638*2eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 639*2eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 640*2eeaa532SAlexandre Belloni 641*2eeaa532SAlexandre Belloni exit_eerd: 642*2eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 643*2eeaa532SAlexandre Belloni 644*2eeaa532SAlexandre Belloni return ret; 645*2eeaa532SAlexandre Belloni } 646*2eeaa532SAlexandre Belloni 647*2eeaa532SAlexandre Belloni static int rv3032_clkout_prepare(struct clk_hw *hw) 648*2eeaa532SAlexandre Belloni { 649*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 650*2eeaa532SAlexandre Belloni 651*2eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_PMU, RV3032_PMU_NCLKE, 0); 652*2eeaa532SAlexandre Belloni } 653*2eeaa532SAlexandre Belloni 654*2eeaa532SAlexandre Belloni static void rv3032_clkout_unprepare(struct clk_hw *hw) 655*2eeaa532SAlexandre Belloni { 656*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 657*2eeaa532SAlexandre Belloni 658*2eeaa532SAlexandre Belloni rv3032_update_cfg(rv3032, RV3032_PMU, RV3032_PMU_NCLKE, RV3032_PMU_NCLKE); 659*2eeaa532SAlexandre Belloni } 660*2eeaa532SAlexandre Belloni 661*2eeaa532SAlexandre Belloni static int rv3032_clkout_is_prepared(struct clk_hw *hw) 662*2eeaa532SAlexandre Belloni { 663*2eeaa532SAlexandre Belloni int val, ret; 664*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 665*2eeaa532SAlexandre Belloni 666*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_PMU, &val); 667*2eeaa532SAlexandre Belloni if (ret < 0) 668*2eeaa532SAlexandre Belloni return ret; 669*2eeaa532SAlexandre Belloni 670*2eeaa532SAlexandre Belloni return !(val & RV3032_PMU_NCLKE); 671*2eeaa532SAlexandre Belloni } 672*2eeaa532SAlexandre Belloni 673*2eeaa532SAlexandre Belloni static const struct clk_ops rv3032_clkout_ops = { 674*2eeaa532SAlexandre Belloni .prepare = rv3032_clkout_prepare, 675*2eeaa532SAlexandre Belloni .unprepare = rv3032_clkout_unprepare, 676*2eeaa532SAlexandre Belloni .is_prepared = rv3032_clkout_is_prepared, 677*2eeaa532SAlexandre Belloni .recalc_rate = rv3032_clkout_recalc_rate, 678*2eeaa532SAlexandre Belloni .round_rate = rv3032_clkout_round_rate, 679*2eeaa532SAlexandre Belloni .set_rate = rv3032_clkout_set_rate, 680*2eeaa532SAlexandre Belloni }; 681*2eeaa532SAlexandre Belloni 682*2eeaa532SAlexandre Belloni static int rv3032_clkout_register_clk(struct rv3032_data *rv3032, 683*2eeaa532SAlexandre Belloni struct i2c_client *client) 684*2eeaa532SAlexandre Belloni { 685*2eeaa532SAlexandre Belloni int ret; 686*2eeaa532SAlexandre Belloni struct clk *clk; 687*2eeaa532SAlexandre Belloni struct clk_init_data init; 688*2eeaa532SAlexandre Belloni struct device_node *node = client->dev.of_node; 689*2eeaa532SAlexandre Belloni 690*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_TLSB, RV3032_TLSB_CLKF, 0); 691*2eeaa532SAlexandre Belloni if (ret < 0) 692*2eeaa532SAlexandre Belloni return ret; 693*2eeaa532SAlexandre Belloni 694*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, RV3032_CTRL2_CLKIE, 0); 695*2eeaa532SAlexandre Belloni if (ret < 0) 696*2eeaa532SAlexandre Belloni return ret; 697*2eeaa532SAlexandre Belloni 698*2eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_CLK_IRQ, 0); 699*2eeaa532SAlexandre Belloni if (ret < 0) 700*2eeaa532SAlexandre Belloni return ret; 701*2eeaa532SAlexandre Belloni 702*2eeaa532SAlexandre Belloni init.name = "rv3032-clkout"; 703*2eeaa532SAlexandre Belloni init.ops = &rv3032_clkout_ops; 704*2eeaa532SAlexandre Belloni init.flags = 0; 705*2eeaa532SAlexandre Belloni init.parent_names = NULL; 706*2eeaa532SAlexandre Belloni init.num_parents = 0; 707*2eeaa532SAlexandre Belloni rv3032->clkout_hw.init = &init; 708*2eeaa532SAlexandre Belloni 709*2eeaa532SAlexandre Belloni of_property_read_string(node, "clock-output-names", &init.name); 710*2eeaa532SAlexandre Belloni 711*2eeaa532SAlexandre Belloni clk = devm_clk_register(&client->dev, &rv3032->clkout_hw); 712*2eeaa532SAlexandre Belloni if (!IS_ERR(clk)) 713*2eeaa532SAlexandre Belloni of_clk_add_provider(node, of_clk_src_simple_get, clk); 714*2eeaa532SAlexandre Belloni 715*2eeaa532SAlexandre Belloni return 0; 716*2eeaa532SAlexandre Belloni } 717*2eeaa532SAlexandre Belloni #endif 718*2eeaa532SAlexandre Belloni 719*2eeaa532SAlexandre Belloni static int rv3032_hwmon_read_temp(struct device *dev, long *mC) 720*2eeaa532SAlexandre Belloni { 721*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 722*2eeaa532SAlexandre Belloni u8 buf[2]; 723*2eeaa532SAlexandre Belloni int temp, prev = 0; 724*2eeaa532SAlexandre Belloni int ret; 725*2eeaa532SAlexandre Belloni 726*2eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_TLSB, buf, sizeof(buf)); 727*2eeaa532SAlexandre Belloni if (ret) 728*2eeaa532SAlexandre Belloni return ret; 729*2eeaa532SAlexandre Belloni 730*2eeaa532SAlexandre Belloni temp = sign_extend32(buf[1], 7) << 4; 731*2eeaa532SAlexandre Belloni temp |= FIELD_GET(RV3032_TLSB_TEMP, buf[0]); 732*2eeaa532SAlexandre Belloni 733*2eeaa532SAlexandre Belloni /* No blocking or shadowing on RV3032_TLSB and RV3032_TMSB */ 734*2eeaa532SAlexandre Belloni do { 735*2eeaa532SAlexandre Belloni prev = temp; 736*2eeaa532SAlexandre Belloni 737*2eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_TLSB, buf, sizeof(buf)); 738*2eeaa532SAlexandre Belloni if (ret) 739*2eeaa532SAlexandre Belloni return ret; 740*2eeaa532SAlexandre Belloni 741*2eeaa532SAlexandre Belloni temp = sign_extend32(buf[1], 7) << 4; 742*2eeaa532SAlexandre Belloni temp |= FIELD_GET(RV3032_TLSB_TEMP, buf[0]); 743*2eeaa532SAlexandre Belloni } while (temp != prev); 744*2eeaa532SAlexandre Belloni 745*2eeaa532SAlexandre Belloni *mC = (temp * 1000) / 16; 746*2eeaa532SAlexandre Belloni 747*2eeaa532SAlexandre Belloni return 0; 748*2eeaa532SAlexandre Belloni } 749*2eeaa532SAlexandre Belloni 750*2eeaa532SAlexandre Belloni static umode_t rv3032_hwmon_is_visible(const void *data, enum hwmon_sensor_types type, 751*2eeaa532SAlexandre Belloni u32 attr, int channel) 752*2eeaa532SAlexandre Belloni { 753*2eeaa532SAlexandre Belloni if (type != hwmon_temp) 754*2eeaa532SAlexandre Belloni return 0; 755*2eeaa532SAlexandre Belloni 756*2eeaa532SAlexandre Belloni switch (attr) { 757*2eeaa532SAlexandre Belloni case hwmon_temp_input: 758*2eeaa532SAlexandre Belloni return 0444; 759*2eeaa532SAlexandre Belloni default: 760*2eeaa532SAlexandre Belloni return 0; 761*2eeaa532SAlexandre Belloni } 762*2eeaa532SAlexandre Belloni } 763*2eeaa532SAlexandre Belloni 764*2eeaa532SAlexandre Belloni static int rv3032_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 765*2eeaa532SAlexandre Belloni u32 attr, int channel, long *temp) 766*2eeaa532SAlexandre Belloni { 767*2eeaa532SAlexandre Belloni int err; 768*2eeaa532SAlexandre Belloni 769*2eeaa532SAlexandre Belloni switch (attr) { 770*2eeaa532SAlexandre Belloni case hwmon_temp_input: 771*2eeaa532SAlexandre Belloni err = rv3032_hwmon_read_temp(dev, temp); 772*2eeaa532SAlexandre Belloni break; 773*2eeaa532SAlexandre Belloni default: 774*2eeaa532SAlexandre Belloni err = -EOPNOTSUPP; 775*2eeaa532SAlexandre Belloni break; 776*2eeaa532SAlexandre Belloni } 777*2eeaa532SAlexandre Belloni 778*2eeaa532SAlexandre Belloni return err; 779*2eeaa532SAlexandre Belloni } 780*2eeaa532SAlexandre Belloni 781*2eeaa532SAlexandre Belloni static const struct hwmon_channel_info *rv3032_hwmon_info[] = { 782*2eeaa532SAlexandre Belloni HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), 783*2eeaa532SAlexandre Belloni HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST), 784*2eeaa532SAlexandre Belloni NULL 785*2eeaa532SAlexandre Belloni }; 786*2eeaa532SAlexandre Belloni 787*2eeaa532SAlexandre Belloni static const struct hwmon_ops rv3032_hwmon_hwmon_ops = { 788*2eeaa532SAlexandre Belloni .is_visible = rv3032_hwmon_is_visible, 789*2eeaa532SAlexandre Belloni .read = rv3032_hwmon_read, 790*2eeaa532SAlexandre Belloni }; 791*2eeaa532SAlexandre Belloni 792*2eeaa532SAlexandre Belloni static const struct hwmon_chip_info rv3032_hwmon_chip_info = { 793*2eeaa532SAlexandre Belloni .ops = &rv3032_hwmon_hwmon_ops, 794*2eeaa532SAlexandre Belloni .info = rv3032_hwmon_info, 795*2eeaa532SAlexandre Belloni }; 796*2eeaa532SAlexandre Belloni 797*2eeaa532SAlexandre Belloni static void rv3032_hwmon_register(struct device *dev) 798*2eeaa532SAlexandre Belloni { 799*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 800*2eeaa532SAlexandre Belloni 801*2eeaa532SAlexandre Belloni if (!IS_REACHABLE(CONFIG_HWMON)) 802*2eeaa532SAlexandre Belloni return; 803*2eeaa532SAlexandre Belloni 804*2eeaa532SAlexandre Belloni devm_hwmon_device_register_with_info(dev, "rv3032", rv3032, &rv3032_hwmon_chip_info, NULL); 805*2eeaa532SAlexandre Belloni } 806*2eeaa532SAlexandre Belloni 807*2eeaa532SAlexandre Belloni static struct rtc_class_ops rv3032_rtc_ops = { 808*2eeaa532SAlexandre Belloni .read_time = rv3032_get_time, 809*2eeaa532SAlexandre Belloni .set_time = rv3032_set_time, 810*2eeaa532SAlexandre Belloni .read_offset = rv3032_read_offset, 811*2eeaa532SAlexandre Belloni .set_offset = rv3032_set_offset, 812*2eeaa532SAlexandre Belloni .ioctl = rv3032_ioctl, 813*2eeaa532SAlexandre Belloni }; 814*2eeaa532SAlexandre Belloni 815*2eeaa532SAlexandre Belloni static const struct regmap_config regmap_config = { 816*2eeaa532SAlexandre Belloni .reg_bits = 8, 817*2eeaa532SAlexandre Belloni .val_bits = 8, 818*2eeaa532SAlexandre Belloni .max_register = 0xCA, 819*2eeaa532SAlexandre Belloni }; 820*2eeaa532SAlexandre Belloni 821*2eeaa532SAlexandre Belloni static int rv3032_probe(struct i2c_client *client) 822*2eeaa532SAlexandre Belloni { 823*2eeaa532SAlexandre Belloni struct rv3032_data *rv3032; 824*2eeaa532SAlexandre Belloni int ret, status; 825*2eeaa532SAlexandre Belloni struct nvmem_config nvmem_cfg = { 826*2eeaa532SAlexandre Belloni .name = "rv3032_nvram", 827*2eeaa532SAlexandre Belloni .word_size = 1, 828*2eeaa532SAlexandre Belloni .stride = 1, 829*2eeaa532SAlexandre Belloni .size = 16, 830*2eeaa532SAlexandre Belloni .type = NVMEM_TYPE_BATTERY_BACKED, 831*2eeaa532SAlexandre Belloni .reg_read = rv3032_nvram_read, 832*2eeaa532SAlexandre Belloni .reg_write = rv3032_nvram_write, 833*2eeaa532SAlexandre Belloni }; 834*2eeaa532SAlexandre Belloni struct nvmem_config eeprom_cfg = { 835*2eeaa532SAlexandre Belloni .name = "rv3032_eeprom", 836*2eeaa532SAlexandre Belloni .word_size = 1, 837*2eeaa532SAlexandre Belloni .stride = 1, 838*2eeaa532SAlexandre Belloni .size = 32, 839*2eeaa532SAlexandre Belloni .type = NVMEM_TYPE_EEPROM, 840*2eeaa532SAlexandre Belloni .reg_read = rv3032_eeprom_read, 841*2eeaa532SAlexandre Belloni .reg_write = rv3032_eeprom_write, 842*2eeaa532SAlexandre Belloni }; 843*2eeaa532SAlexandre Belloni 844*2eeaa532SAlexandre Belloni rv3032 = devm_kzalloc(&client->dev, sizeof(struct rv3032_data), 845*2eeaa532SAlexandre Belloni GFP_KERNEL); 846*2eeaa532SAlexandre Belloni if (!rv3032) 847*2eeaa532SAlexandre Belloni return -ENOMEM; 848*2eeaa532SAlexandre Belloni 849*2eeaa532SAlexandre Belloni rv3032->regmap = devm_regmap_init_i2c(client, ®map_config); 850*2eeaa532SAlexandre Belloni if (IS_ERR(rv3032->regmap)) 851*2eeaa532SAlexandre Belloni return PTR_ERR(rv3032->regmap); 852*2eeaa532SAlexandre Belloni 853*2eeaa532SAlexandre Belloni i2c_set_clientdata(client, rv3032); 854*2eeaa532SAlexandre Belloni 855*2eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 856*2eeaa532SAlexandre Belloni if (ret < 0) 857*2eeaa532SAlexandre Belloni return ret; 858*2eeaa532SAlexandre Belloni 859*2eeaa532SAlexandre Belloni rv3032->rtc = devm_rtc_allocate_device(&client->dev); 860*2eeaa532SAlexandre Belloni if (IS_ERR(rv3032->rtc)) 861*2eeaa532SAlexandre Belloni return PTR_ERR(rv3032->rtc); 862*2eeaa532SAlexandre Belloni 863*2eeaa532SAlexandre Belloni if (client->irq > 0) { 864*2eeaa532SAlexandre Belloni ret = devm_request_threaded_irq(&client->dev, client->irq, 865*2eeaa532SAlexandre Belloni NULL, rv3032_handle_irq, 866*2eeaa532SAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 867*2eeaa532SAlexandre Belloni "rv3032", rv3032); 868*2eeaa532SAlexandre Belloni if (ret) { 869*2eeaa532SAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 870*2eeaa532SAlexandre Belloni client->irq = 0; 871*2eeaa532SAlexandre Belloni } else { 872*2eeaa532SAlexandre Belloni rv3032_rtc_ops.read_alarm = rv3032_get_alarm; 873*2eeaa532SAlexandre Belloni rv3032_rtc_ops.set_alarm = rv3032_set_alarm; 874*2eeaa532SAlexandre Belloni rv3032_rtc_ops.alarm_irq_enable = rv3032_alarm_irq_enable; 875*2eeaa532SAlexandre Belloni } 876*2eeaa532SAlexandre Belloni } 877*2eeaa532SAlexandre Belloni 878*2eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL1, 879*2eeaa532SAlexandre Belloni RV3032_CTRL1_WADA, RV3032_CTRL1_WADA); 880*2eeaa532SAlexandre Belloni if (ret) 881*2eeaa532SAlexandre Belloni return ret; 882*2eeaa532SAlexandre Belloni 883*2eeaa532SAlexandre Belloni rv3032_trickle_charger_setup(&client->dev, rv3032); 884*2eeaa532SAlexandre Belloni 885*2eeaa532SAlexandre Belloni rv3032->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 886*2eeaa532SAlexandre Belloni rv3032->rtc->range_max = RTC_TIMESTAMP_END_2099; 887*2eeaa532SAlexandre Belloni rv3032->rtc->ops = &rv3032_rtc_ops; 888*2eeaa532SAlexandre Belloni ret = rtc_register_device(rv3032->rtc); 889*2eeaa532SAlexandre Belloni if (ret) 890*2eeaa532SAlexandre Belloni return ret; 891*2eeaa532SAlexandre Belloni 892*2eeaa532SAlexandre Belloni nvmem_cfg.priv = rv3032; 893*2eeaa532SAlexandre Belloni rtc_nvmem_register(rv3032->rtc, &nvmem_cfg); 894*2eeaa532SAlexandre Belloni eeprom_cfg.priv = rv3032; 895*2eeaa532SAlexandre Belloni rtc_nvmem_register(rv3032->rtc, &eeprom_cfg); 896*2eeaa532SAlexandre Belloni 897*2eeaa532SAlexandre Belloni rv3032->rtc->max_user_freq = 1; 898*2eeaa532SAlexandre Belloni 899*2eeaa532SAlexandre Belloni #ifdef CONFIG_COMMON_CLK 900*2eeaa532SAlexandre Belloni rv3032_clkout_register_clk(rv3032, client); 901*2eeaa532SAlexandre Belloni #endif 902*2eeaa532SAlexandre Belloni 903*2eeaa532SAlexandre Belloni rv3032_hwmon_register(&client->dev); 904*2eeaa532SAlexandre Belloni 905*2eeaa532SAlexandre Belloni return 0; 906*2eeaa532SAlexandre Belloni } 907*2eeaa532SAlexandre Belloni 908*2eeaa532SAlexandre Belloni static const struct of_device_id rv3032_of_match[] = { 909*2eeaa532SAlexandre Belloni { .compatible = "microcrystal,rv3032", }, 910*2eeaa532SAlexandre Belloni { } 911*2eeaa532SAlexandre Belloni }; 912*2eeaa532SAlexandre Belloni MODULE_DEVICE_TABLE(of, rv3032_of_match); 913*2eeaa532SAlexandre Belloni 914*2eeaa532SAlexandre Belloni static struct i2c_driver rv3032_driver = { 915*2eeaa532SAlexandre Belloni .driver = { 916*2eeaa532SAlexandre Belloni .name = "rtc-rv3032", 917*2eeaa532SAlexandre Belloni .of_match_table = of_match_ptr(rv3032_of_match), 918*2eeaa532SAlexandre Belloni }, 919*2eeaa532SAlexandre Belloni .probe_new = rv3032_probe, 920*2eeaa532SAlexandre Belloni }; 921*2eeaa532SAlexandre Belloni module_i2c_driver(rv3032_driver); 922*2eeaa532SAlexandre Belloni 923*2eeaa532SAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 924*2eeaa532SAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV3032 RTC driver"); 925*2eeaa532SAlexandre Belloni MODULE_LICENSE("GPL v2"); 926