12eeaa532SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0 22eeaa532SAlexandre Belloni /* 32eeaa532SAlexandre Belloni * RTC driver for the Micro Crystal RV3032 42eeaa532SAlexandre Belloni * 52eeaa532SAlexandre Belloni * Copyright (C) 2020 Micro Crystal SA 62eeaa532SAlexandre Belloni * 72eeaa532SAlexandre Belloni * Alexandre Belloni <alexandre.belloni@bootlin.com> 82eeaa532SAlexandre Belloni * 92eeaa532SAlexandre Belloni */ 102eeaa532SAlexandre Belloni 112eeaa532SAlexandre Belloni #include <linux/clk.h> 122eeaa532SAlexandre Belloni #include <linux/clk-provider.h> 132eeaa532SAlexandre Belloni #include <linux/bcd.h> 142eeaa532SAlexandre Belloni #include <linux/bitfield.h> 152eeaa532SAlexandre Belloni #include <linux/bitops.h> 162eeaa532SAlexandre Belloni #include <linux/hwmon.h> 172eeaa532SAlexandre Belloni #include <linux/i2c.h> 182eeaa532SAlexandre Belloni #include <linux/interrupt.h> 192eeaa532SAlexandre Belloni #include <linux/kernel.h> 202eeaa532SAlexandre Belloni #include <linux/log2.h> 212eeaa532SAlexandre Belloni #include <linux/module.h> 222eeaa532SAlexandre Belloni #include <linux/of_device.h> 232eeaa532SAlexandre Belloni #include <linux/regmap.h> 242eeaa532SAlexandre Belloni #include <linux/rtc.h> 252eeaa532SAlexandre Belloni 262eeaa532SAlexandre Belloni #define RV3032_SEC 0x01 272eeaa532SAlexandre Belloni #define RV3032_MIN 0x02 282eeaa532SAlexandre Belloni #define RV3032_HOUR 0x03 292eeaa532SAlexandre Belloni #define RV3032_WDAY 0x04 302eeaa532SAlexandre Belloni #define RV3032_DAY 0x05 312eeaa532SAlexandre Belloni #define RV3032_MONTH 0x06 322eeaa532SAlexandre Belloni #define RV3032_YEAR 0x07 332eeaa532SAlexandre Belloni #define RV3032_ALARM_MIN 0x08 342eeaa532SAlexandre Belloni #define RV3032_ALARM_HOUR 0x09 352eeaa532SAlexandre Belloni #define RV3032_ALARM_DAY 0x0A 362eeaa532SAlexandre Belloni #define RV3032_STATUS 0x0D 372eeaa532SAlexandre Belloni #define RV3032_TLSB 0x0E 382eeaa532SAlexandre Belloni #define RV3032_TMSB 0x0F 392eeaa532SAlexandre Belloni #define RV3032_CTRL1 0x10 402eeaa532SAlexandre Belloni #define RV3032_CTRL2 0x11 412eeaa532SAlexandre Belloni #define RV3032_CTRL3 0x12 422eeaa532SAlexandre Belloni #define RV3032_TS_CTRL 0x13 432eeaa532SAlexandre Belloni #define RV3032_CLK_IRQ 0x14 442eeaa532SAlexandre Belloni #define RV3032_EEPROM_ADDR 0x3D 452eeaa532SAlexandre Belloni #define RV3032_EEPROM_DATA 0x3E 462eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD 0x3F 472eeaa532SAlexandre Belloni #define RV3032_RAM1 0x40 482eeaa532SAlexandre Belloni #define RV3032_PMU 0xC0 492eeaa532SAlexandre Belloni #define RV3032_OFFSET 0xC1 502eeaa532SAlexandre Belloni #define RV3032_CLKOUT1 0xC2 512eeaa532SAlexandre Belloni #define RV3032_CLKOUT2 0xC3 522eeaa532SAlexandre Belloni #define RV3032_TREF0 0xC4 532eeaa532SAlexandre Belloni #define RV3032_TREF1 0xC5 542eeaa532SAlexandre Belloni 552eeaa532SAlexandre Belloni #define RV3032_STATUS_VLF BIT(0) 562eeaa532SAlexandre Belloni #define RV3032_STATUS_PORF BIT(1) 572eeaa532SAlexandre Belloni #define RV3032_STATUS_EVF BIT(2) 582eeaa532SAlexandre Belloni #define RV3032_STATUS_AF BIT(3) 592eeaa532SAlexandre Belloni #define RV3032_STATUS_TF BIT(4) 602eeaa532SAlexandre Belloni #define RV3032_STATUS_UF BIT(5) 612eeaa532SAlexandre Belloni #define RV3032_STATUS_TLF BIT(6) 622eeaa532SAlexandre Belloni #define RV3032_STATUS_THF BIT(7) 632eeaa532SAlexandre Belloni 642eeaa532SAlexandre Belloni #define RV3032_TLSB_CLKF BIT(1) 652eeaa532SAlexandre Belloni #define RV3032_TLSB_EEBUSY BIT(2) 662eeaa532SAlexandre Belloni #define RV3032_TLSB_TEMP GENMASK(7, 4) 672eeaa532SAlexandre Belloni 682eeaa532SAlexandre Belloni #define RV3032_CLKOUT2_HFD_MSK GENMASK(4, 0) 692eeaa532SAlexandre Belloni #define RV3032_CLKOUT2_FD_MSK GENMASK(6, 5) 702eeaa532SAlexandre Belloni #define RV3032_CLKOUT2_OS BIT(7) 712eeaa532SAlexandre Belloni 722eeaa532SAlexandre Belloni #define RV3032_CTRL1_EERD BIT(3) 732eeaa532SAlexandre Belloni #define RV3032_CTRL1_WADA BIT(5) 742eeaa532SAlexandre Belloni 752eeaa532SAlexandre Belloni #define RV3032_CTRL2_STOP BIT(0) 762eeaa532SAlexandre Belloni #define RV3032_CTRL2_EIE BIT(2) 772eeaa532SAlexandre Belloni #define RV3032_CTRL2_AIE BIT(3) 782eeaa532SAlexandre Belloni #define RV3032_CTRL2_TIE BIT(4) 792eeaa532SAlexandre Belloni #define RV3032_CTRL2_UIE BIT(5) 802eeaa532SAlexandre Belloni #define RV3032_CTRL2_CLKIE BIT(6) 812eeaa532SAlexandre Belloni #define RV3032_CTRL2_TSE BIT(7) 822eeaa532SAlexandre Belloni 832eeaa532SAlexandre Belloni #define RV3032_PMU_TCM GENMASK(1, 0) 842eeaa532SAlexandre Belloni #define RV3032_PMU_TCR GENMASK(3, 2) 852eeaa532SAlexandre Belloni #define RV3032_PMU_BSM GENMASK(5, 4) 862eeaa532SAlexandre Belloni #define RV3032_PMU_NCLKE BIT(6) 872eeaa532SAlexandre Belloni 882eeaa532SAlexandre Belloni #define RV3032_PMU_BSM_DSM 1 892eeaa532SAlexandre Belloni #define RV3032_PMU_BSM_LSM 2 902eeaa532SAlexandre Belloni 912eeaa532SAlexandre Belloni #define RV3032_OFFSET_MSK GENMASK(5, 0) 922eeaa532SAlexandre Belloni 932eeaa532SAlexandre Belloni #define RV3032_EVT_CTRL_TSR BIT(2) 942eeaa532SAlexandre Belloni 952eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD_UPDATE 0x11 962eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD_WRITE 0x21 972eeaa532SAlexandre Belloni #define RV3032_EEPROM_CMD_READ 0x22 982eeaa532SAlexandre Belloni 992eeaa532SAlexandre Belloni #define RV3032_EEPROM_USER 0xCB 1002eeaa532SAlexandre Belloni 1012eeaa532SAlexandre Belloni #define RV3032_EEBUSY_POLL 10000 1022eeaa532SAlexandre Belloni #define RV3032_EEBUSY_TIMEOUT 100000 1032eeaa532SAlexandre Belloni 1042eeaa532SAlexandre Belloni #define OFFSET_STEP_PPT 238419 1052eeaa532SAlexandre Belloni 1062eeaa532SAlexandre Belloni struct rv3032_data { 1072eeaa532SAlexandre Belloni struct regmap *regmap; 1082eeaa532SAlexandre Belloni struct rtc_device *rtc; 1092eeaa532SAlexandre Belloni #ifdef CONFIG_COMMON_CLK 1102eeaa532SAlexandre Belloni struct clk_hw clkout_hw; 1112eeaa532SAlexandre Belloni #endif 1122eeaa532SAlexandre Belloni }; 1132eeaa532SAlexandre Belloni 1142eeaa532SAlexandre Belloni static u16 rv3032_trickle_resistors[] = {1000, 2000, 7000, 11000}; 1152eeaa532SAlexandre Belloni static u16 rv3032_trickle_voltages[] = {0, 1750, 3000, 4400}; 1162eeaa532SAlexandre Belloni 1172eeaa532SAlexandre Belloni static int rv3032_exit_eerd(struct rv3032_data *rv3032, u32 eerd) 1182eeaa532SAlexandre Belloni { 1192eeaa532SAlexandre Belloni if (eerd) 1202eeaa532SAlexandre Belloni return 0; 1212eeaa532SAlexandre Belloni 1222eeaa532SAlexandre Belloni return regmap_update_bits(rv3032->regmap, RV3032_CTRL1, RV3032_CTRL1_EERD, 0); 1232eeaa532SAlexandre Belloni } 1242eeaa532SAlexandre Belloni 1252eeaa532SAlexandre Belloni static int rv3032_enter_eerd(struct rv3032_data *rv3032, u32 *eerd) 1262eeaa532SAlexandre Belloni { 1272eeaa532SAlexandre Belloni u32 ctrl1, status; 1282eeaa532SAlexandre Belloni int ret; 1292eeaa532SAlexandre Belloni 1302eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CTRL1, &ctrl1); 1312eeaa532SAlexandre Belloni if (ret) 1322eeaa532SAlexandre Belloni return ret; 1332eeaa532SAlexandre Belloni 1342eeaa532SAlexandre Belloni *eerd = ctrl1 & RV3032_CTRL1_EERD; 1352eeaa532SAlexandre Belloni if (*eerd) 1362eeaa532SAlexandre Belloni return 0; 1372eeaa532SAlexandre Belloni 1382eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL1, 1392eeaa532SAlexandre Belloni RV3032_CTRL1_EERD, RV3032_CTRL1_EERD); 1402eeaa532SAlexandre Belloni if (ret) 1412eeaa532SAlexandre Belloni return ret; 1422eeaa532SAlexandre Belloni 1432eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 1442eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 1452eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 1462eeaa532SAlexandre Belloni if (ret) { 1472eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, *eerd); 1482eeaa532SAlexandre Belloni 1492eeaa532SAlexandre Belloni return ret; 1502eeaa532SAlexandre Belloni } 1512eeaa532SAlexandre Belloni 1522eeaa532SAlexandre Belloni return 0; 1532eeaa532SAlexandre Belloni } 1542eeaa532SAlexandre Belloni 1552eeaa532SAlexandre Belloni static int rv3032_update_cfg(struct rv3032_data *rv3032, unsigned int reg, 1562eeaa532SAlexandre Belloni unsigned int mask, unsigned int val) 1572eeaa532SAlexandre Belloni { 1582eeaa532SAlexandre Belloni u32 status, eerd; 1592eeaa532SAlexandre Belloni int ret; 1602eeaa532SAlexandre Belloni 1612eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 1622eeaa532SAlexandre Belloni if (ret) 1632eeaa532SAlexandre Belloni return ret; 1642eeaa532SAlexandre Belloni 1652eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, reg, mask, val); 1662eeaa532SAlexandre Belloni if (ret) 1672eeaa532SAlexandre Belloni goto exit_eerd; 1682eeaa532SAlexandre Belloni 1692eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, RV3032_EEPROM_CMD_UPDATE); 1702eeaa532SAlexandre Belloni if (ret) 1712eeaa532SAlexandre Belloni goto exit_eerd; 1722eeaa532SAlexandre Belloni 1732eeaa532SAlexandre Belloni usleep_range(46000, RV3032_EEBUSY_TIMEOUT); 1742eeaa532SAlexandre Belloni 1752eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 1762eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 1772eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 1782eeaa532SAlexandre Belloni 1792eeaa532SAlexandre Belloni exit_eerd: 1802eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 1812eeaa532SAlexandre Belloni 1822eeaa532SAlexandre Belloni return ret; 1832eeaa532SAlexandre Belloni } 1842eeaa532SAlexandre Belloni 1852eeaa532SAlexandre Belloni static irqreturn_t rv3032_handle_irq(int irq, void *dev_id) 1862eeaa532SAlexandre Belloni { 1872eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_id; 1882eeaa532SAlexandre Belloni unsigned long events = 0; 1892eeaa532SAlexandre Belloni u32 status = 0, ctrl = 0; 1902eeaa532SAlexandre Belloni 1912eeaa532SAlexandre Belloni if (regmap_read(rv3032->regmap, RV3032_STATUS, &status) < 0 || 1922eeaa532SAlexandre Belloni status == 0) { 1932eeaa532SAlexandre Belloni return IRQ_NONE; 1942eeaa532SAlexandre Belloni } 1952eeaa532SAlexandre Belloni 1962eeaa532SAlexandre Belloni if (status & RV3032_STATUS_TF) { 1972eeaa532SAlexandre Belloni status |= RV3032_STATUS_TF; 1982eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_TIE; 1992eeaa532SAlexandre Belloni events |= RTC_PF; 2002eeaa532SAlexandre Belloni } 2012eeaa532SAlexandre Belloni 2022eeaa532SAlexandre Belloni if (status & RV3032_STATUS_AF) { 2032eeaa532SAlexandre Belloni status |= RV3032_STATUS_AF; 2042eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_AIE; 2052eeaa532SAlexandre Belloni events |= RTC_AF; 2062eeaa532SAlexandre Belloni } 2072eeaa532SAlexandre Belloni 2082eeaa532SAlexandre Belloni if (status & RV3032_STATUS_UF) { 2092eeaa532SAlexandre Belloni status |= RV3032_STATUS_UF; 2102eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_UIE; 2112eeaa532SAlexandre Belloni events |= RTC_UF; 2122eeaa532SAlexandre Belloni } 2132eeaa532SAlexandre Belloni 2142eeaa532SAlexandre Belloni if (events) { 2152eeaa532SAlexandre Belloni rtc_update_irq(rv3032->rtc, 1, events); 2162eeaa532SAlexandre Belloni regmap_update_bits(rv3032->regmap, RV3032_STATUS, status, 0); 2172eeaa532SAlexandre Belloni regmap_update_bits(rv3032->regmap, RV3032_CTRL2, ctrl, 0); 2182eeaa532SAlexandre Belloni } 2192eeaa532SAlexandre Belloni 2202eeaa532SAlexandre Belloni return IRQ_HANDLED; 2212eeaa532SAlexandre Belloni } 2222eeaa532SAlexandre Belloni 2232eeaa532SAlexandre Belloni static int rv3032_get_time(struct device *dev, struct rtc_time *tm) 2242eeaa532SAlexandre Belloni { 2252eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 2262eeaa532SAlexandre Belloni u8 date[7]; 2272eeaa532SAlexandre Belloni int ret, status; 2282eeaa532SAlexandre Belloni 2292eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 2302eeaa532SAlexandre Belloni if (ret < 0) 2312eeaa532SAlexandre Belloni return ret; 2322eeaa532SAlexandre Belloni 2332eeaa532SAlexandre Belloni if (status & (RV3032_STATUS_PORF | RV3032_STATUS_VLF)) 2342eeaa532SAlexandre Belloni return -EINVAL; 2352eeaa532SAlexandre Belloni 2362eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_SEC, date, sizeof(date)); 2372eeaa532SAlexandre Belloni if (ret) 2382eeaa532SAlexandre Belloni return ret; 2392eeaa532SAlexandre Belloni 2402eeaa532SAlexandre Belloni tm->tm_sec = bcd2bin(date[0] & 0x7f); 2412eeaa532SAlexandre Belloni tm->tm_min = bcd2bin(date[1] & 0x7f); 2422eeaa532SAlexandre Belloni tm->tm_hour = bcd2bin(date[2] & 0x3f); 2432eeaa532SAlexandre Belloni tm->tm_wday = date[3] & 0x7; 2442eeaa532SAlexandre Belloni tm->tm_mday = bcd2bin(date[4] & 0x3f); 2452eeaa532SAlexandre Belloni tm->tm_mon = bcd2bin(date[5] & 0x1f) - 1; 2462eeaa532SAlexandre Belloni tm->tm_year = bcd2bin(date[6]) + 100; 2472eeaa532SAlexandre Belloni 2482eeaa532SAlexandre Belloni return 0; 2492eeaa532SAlexandre Belloni } 2502eeaa532SAlexandre Belloni 2512eeaa532SAlexandre Belloni static int rv3032_set_time(struct device *dev, struct rtc_time *tm) 2522eeaa532SAlexandre Belloni { 2532eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 2542eeaa532SAlexandre Belloni u8 date[7]; 2552eeaa532SAlexandre Belloni int ret; 2562eeaa532SAlexandre Belloni 2572eeaa532SAlexandre Belloni date[0] = bin2bcd(tm->tm_sec); 2582eeaa532SAlexandre Belloni date[1] = bin2bcd(tm->tm_min); 2592eeaa532SAlexandre Belloni date[2] = bin2bcd(tm->tm_hour); 2602eeaa532SAlexandre Belloni date[3] = tm->tm_wday; 2612eeaa532SAlexandre Belloni date[4] = bin2bcd(tm->tm_mday); 2622eeaa532SAlexandre Belloni date[5] = bin2bcd(tm->tm_mon + 1); 2632eeaa532SAlexandre Belloni date[6] = bin2bcd(tm->tm_year - 100); 2642eeaa532SAlexandre Belloni 2652eeaa532SAlexandre Belloni ret = regmap_bulk_write(rv3032->regmap, RV3032_SEC, date, 2662eeaa532SAlexandre Belloni sizeof(date)); 2672eeaa532SAlexandre Belloni if (ret) 2682eeaa532SAlexandre Belloni return ret; 2692eeaa532SAlexandre Belloni 2702eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS, 2712eeaa532SAlexandre Belloni RV3032_STATUS_PORF | RV3032_STATUS_VLF, 0); 2722eeaa532SAlexandre Belloni 2732eeaa532SAlexandre Belloni return ret; 2742eeaa532SAlexandre Belloni } 2752eeaa532SAlexandre Belloni 2762eeaa532SAlexandre Belloni static int rv3032_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 2772eeaa532SAlexandre Belloni { 2782eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 2792eeaa532SAlexandre Belloni u8 alarmvals[3]; 2802eeaa532SAlexandre Belloni int status, ctrl, ret; 2812eeaa532SAlexandre Belloni 2822eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_ALARM_MIN, alarmvals, 2832eeaa532SAlexandre Belloni sizeof(alarmvals)); 2842eeaa532SAlexandre Belloni if (ret) 2852eeaa532SAlexandre Belloni return ret; 2862eeaa532SAlexandre Belloni 2872eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 2882eeaa532SAlexandre Belloni if (ret < 0) 2892eeaa532SAlexandre Belloni return ret; 2902eeaa532SAlexandre Belloni 2912eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CTRL2, &ctrl); 2922eeaa532SAlexandre Belloni if (ret < 0) 2932eeaa532SAlexandre Belloni return ret; 2942eeaa532SAlexandre Belloni 2952eeaa532SAlexandre Belloni alrm->time.tm_sec = 0; 2962eeaa532SAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 2972eeaa532SAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 2982eeaa532SAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 2992eeaa532SAlexandre Belloni 3002eeaa532SAlexandre Belloni alrm->enabled = !!(ctrl & RV3032_CTRL2_AIE); 3012eeaa532SAlexandre Belloni alrm->pending = (status & RV3032_STATUS_AF) && alrm->enabled; 3022eeaa532SAlexandre Belloni 3032eeaa532SAlexandre Belloni return 0; 3042eeaa532SAlexandre Belloni } 3052eeaa532SAlexandre Belloni 3062eeaa532SAlexandre Belloni static int rv3032_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3072eeaa532SAlexandre Belloni { 3082eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 3092eeaa532SAlexandre Belloni u8 alarmvals[3]; 3102eeaa532SAlexandre Belloni u8 ctrl = 0; 3112eeaa532SAlexandre Belloni int ret; 3122eeaa532SAlexandre Belloni 3132eeaa532SAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 3142eeaa532SAlexandre Belloni if (alrm->time.tm_sec) { 3152eeaa532SAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 3162eeaa532SAlexandre Belloni 3172eeaa532SAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 3182eeaa532SAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 3192eeaa532SAlexandre Belloni } 3202eeaa532SAlexandre Belloni 3212eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, 3222eeaa532SAlexandre Belloni RV3032_CTRL2_AIE | RV3032_CTRL2_UIE, 0); 3232eeaa532SAlexandre Belloni if (ret) 3242eeaa532SAlexandre Belloni return ret; 3252eeaa532SAlexandre Belloni 3262eeaa532SAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 3272eeaa532SAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 3282eeaa532SAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 3292eeaa532SAlexandre Belloni 3302eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS, 3312eeaa532SAlexandre Belloni RV3032_STATUS_AF, 0); 3322eeaa532SAlexandre Belloni if (ret) 3332eeaa532SAlexandre Belloni return ret; 3342eeaa532SAlexandre Belloni 3352eeaa532SAlexandre Belloni ret = regmap_bulk_write(rv3032->regmap, RV3032_ALARM_MIN, alarmvals, 3362eeaa532SAlexandre Belloni sizeof(alarmvals)); 3372eeaa532SAlexandre Belloni if (ret) 3382eeaa532SAlexandre Belloni return ret; 3392eeaa532SAlexandre Belloni 3402eeaa532SAlexandre Belloni if (alrm->enabled) { 3412eeaa532SAlexandre Belloni if (rv3032->rtc->uie_rtctimer.enabled) 3422eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_UIE; 3432eeaa532SAlexandre Belloni if (rv3032->rtc->aie_timer.enabled) 3442eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_AIE; 3452eeaa532SAlexandre Belloni } 3462eeaa532SAlexandre Belloni 3472eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, 3482eeaa532SAlexandre Belloni RV3032_CTRL2_UIE | RV3032_CTRL2_AIE, ctrl); 3492eeaa532SAlexandre Belloni 3502eeaa532SAlexandre Belloni return ret; 3512eeaa532SAlexandre Belloni } 3522eeaa532SAlexandre Belloni 3532eeaa532SAlexandre Belloni static int rv3032_alarm_irq_enable(struct device *dev, unsigned int enabled) 3542eeaa532SAlexandre Belloni { 3552eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 3562eeaa532SAlexandre Belloni int ctrl = 0, ret; 3572eeaa532SAlexandre Belloni 3582eeaa532SAlexandre Belloni if (enabled) { 3592eeaa532SAlexandre Belloni if (rv3032->rtc->uie_rtctimer.enabled) 3602eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_UIE; 3612eeaa532SAlexandre Belloni if (rv3032->rtc->aie_timer.enabled) 3622eeaa532SAlexandre Belloni ctrl |= RV3032_CTRL2_AIE; 3632eeaa532SAlexandre Belloni } 3642eeaa532SAlexandre Belloni 3652eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_STATUS, 3662eeaa532SAlexandre Belloni RV3032_STATUS_AF | RV3032_STATUS_UF, 0); 3672eeaa532SAlexandre Belloni if (ret) 3682eeaa532SAlexandre Belloni return ret; 3692eeaa532SAlexandre Belloni 3702eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, 3712eeaa532SAlexandre Belloni RV3032_CTRL2_UIE | RV3032_CTRL2_AIE, ctrl); 3722eeaa532SAlexandre Belloni if (ret) 3732eeaa532SAlexandre Belloni return ret; 3742eeaa532SAlexandre Belloni 3752eeaa532SAlexandre Belloni return 0; 3762eeaa532SAlexandre Belloni } 3772eeaa532SAlexandre Belloni 3782eeaa532SAlexandre Belloni static int rv3032_read_offset(struct device *dev, long *offset) 3792eeaa532SAlexandre Belloni { 3802eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 3812eeaa532SAlexandre Belloni int ret, value, steps; 3822eeaa532SAlexandre Belloni 3832eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_OFFSET, &value); 3842eeaa532SAlexandre Belloni if (ret < 0) 3852eeaa532SAlexandre Belloni return ret; 3862eeaa532SAlexandre Belloni 3872eeaa532SAlexandre Belloni steps = sign_extend32(FIELD_GET(RV3032_OFFSET_MSK, value), 5); 3882eeaa532SAlexandre Belloni 3892eeaa532SAlexandre Belloni *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000); 3902eeaa532SAlexandre Belloni 3912eeaa532SAlexandre Belloni return 0; 3922eeaa532SAlexandre Belloni } 3932eeaa532SAlexandre Belloni 3942eeaa532SAlexandre Belloni static int rv3032_set_offset(struct device *dev, long offset) 3952eeaa532SAlexandre Belloni { 3962eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 3972eeaa532SAlexandre Belloni 3982eeaa532SAlexandre Belloni offset = clamp(offset, -7629L, 7391L) * 1000; 3992eeaa532SAlexandre Belloni offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT); 4002eeaa532SAlexandre Belloni 4012eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_OFFSET, RV3032_OFFSET_MSK, 4022eeaa532SAlexandre Belloni FIELD_PREP(RV3032_OFFSET_MSK, offset)); 4032eeaa532SAlexandre Belloni } 4042eeaa532SAlexandre Belloni 4052eeaa532SAlexandre Belloni static int rv3032_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 4062eeaa532SAlexandre Belloni { 4072eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 4082eeaa532SAlexandre Belloni int status, val = 0, ret = 0; 4092eeaa532SAlexandre Belloni 4102eeaa532SAlexandre Belloni switch (cmd) { 4112eeaa532SAlexandre Belloni case RTC_VL_READ: 4122eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 4132eeaa532SAlexandre Belloni if (ret < 0) 4142eeaa532SAlexandre Belloni return ret; 4152eeaa532SAlexandre Belloni 4162eeaa532SAlexandre Belloni if (status & (RV3032_STATUS_PORF | RV3032_STATUS_VLF)) 4172eeaa532SAlexandre Belloni val = RTC_VL_DATA_INVALID; 4182eeaa532SAlexandre Belloni return put_user(val, (unsigned int __user *)arg); 4192eeaa532SAlexandre Belloni 4202eeaa532SAlexandre Belloni default: 4212eeaa532SAlexandre Belloni return -ENOIOCTLCMD; 4222eeaa532SAlexandre Belloni } 4232eeaa532SAlexandre Belloni } 4242eeaa532SAlexandre Belloni 4252eeaa532SAlexandre Belloni static int rv3032_nvram_write(void *priv, unsigned int offset, void *val, size_t bytes) 4262eeaa532SAlexandre Belloni { 4272eeaa532SAlexandre Belloni return regmap_bulk_write(priv, RV3032_RAM1 + offset, val, bytes); 4282eeaa532SAlexandre Belloni } 4292eeaa532SAlexandre Belloni 4302eeaa532SAlexandre Belloni static int rv3032_nvram_read(void *priv, unsigned int offset, void *val, size_t bytes) 4312eeaa532SAlexandre Belloni { 4322eeaa532SAlexandre Belloni return regmap_bulk_read(priv, RV3032_RAM1 + offset, val, bytes); 4332eeaa532SAlexandre Belloni } 4342eeaa532SAlexandre Belloni 4352eeaa532SAlexandre Belloni static int rv3032_eeprom_write(void *priv, unsigned int offset, void *val, size_t bytes) 4362eeaa532SAlexandre Belloni { 4372eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = priv; 4382eeaa532SAlexandre Belloni u32 status, eerd; 4392eeaa532SAlexandre Belloni int i, ret; 4402eeaa532SAlexandre Belloni u8 *buf = val; 4412eeaa532SAlexandre Belloni 4422eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 4432eeaa532SAlexandre Belloni if (ret) 4442eeaa532SAlexandre Belloni return ret; 4452eeaa532SAlexandre Belloni 4462eeaa532SAlexandre Belloni for (i = 0; i < bytes; i++) { 4472eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_ADDR, 4482eeaa532SAlexandre Belloni RV3032_EEPROM_USER + offset + i); 4492eeaa532SAlexandre Belloni if (ret) 4502eeaa532SAlexandre Belloni goto exit_eerd; 4512eeaa532SAlexandre Belloni 4522eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_DATA, buf[i]); 4532eeaa532SAlexandre Belloni if (ret) 4542eeaa532SAlexandre Belloni goto exit_eerd; 4552eeaa532SAlexandre Belloni 4562eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, 4572eeaa532SAlexandre Belloni RV3032_EEPROM_CMD_WRITE); 4582eeaa532SAlexandre Belloni if (ret) 4592eeaa532SAlexandre Belloni goto exit_eerd; 4602eeaa532SAlexandre Belloni 4612eeaa532SAlexandre Belloni usleep_range(RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 4622eeaa532SAlexandre Belloni 4632eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 4642eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 4652eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 4662eeaa532SAlexandre Belloni if (ret) 4672eeaa532SAlexandre Belloni goto exit_eerd; 4682eeaa532SAlexandre Belloni } 4692eeaa532SAlexandre Belloni 4702eeaa532SAlexandre Belloni exit_eerd: 4712eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 4722eeaa532SAlexandre Belloni 4732eeaa532SAlexandre Belloni return ret; 4742eeaa532SAlexandre Belloni } 4752eeaa532SAlexandre Belloni 4762eeaa532SAlexandre Belloni static int rv3032_eeprom_read(void *priv, unsigned int offset, void *val, size_t bytes) 4772eeaa532SAlexandre Belloni { 4782eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = priv; 4792eeaa532SAlexandre Belloni u32 status, eerd, data; 4802eeaa532SAlexandre Belloni int i, ret; 4812eeaa532SAlexandre Belloni u8 *buf = val; 4822eeaa532SAlexandre Belloni 4832eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 4842eeaa532SAlexandre Belloni if (ret) 4852eeaa532SAlexandre Belloni return ret; 4862eeaa532SAlexandre Belloni 4872eeaa532SAlexandre Belloni for (i = 0; i < bytes; i++) { 4882eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_ADDR, 4892eeaa532SAlexandre Belloni RV3032_EEPROM_USER + offset + i); 4902eeaa532SAlexandre Belloni if (ret) 4912eeaa532SAlexandre Belloni goto exit_eerd; 4922eeaa532SAlexandre Belloni 4932eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, 4942eeaa532SAlexandre Belloni RV3032_EEPROM_CMD_READ); 4952eeaa532SAlexandre Belloni if (ret) 4962eeaa532SAlexandre Belloni goto exit_eerd; 4972eeaa532SAlexandre Belloni 4982eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 4992eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 5002eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 5012eeaa532SAlexandre Belloni if (ret) 5022eeaa532SAlexandre Belloni goto exit_eerd; 5032eeaa532SAlexandre Belloni 5042eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_EEPROM_DATA, &data); 5052eeaa532SAlexandre Belloni if (ret) 5062eeaa532SAlexandre Belloni goto exit_eerd; 5072eeaa532SAlexandre Belloni buf[i] = data; 5082eeaa532SAlexandre Belloni } 5092eeaa532SAlexandre Belloni 5102eeaa532SAlexandre Belloni exit_eerd: 5112eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 5122eeaa532SAlexandre Belloni 5132eeaa532SAlexandre Belloni return ret; 5142eeaa532SAlexandre Belloni } 5152eeaa532SAlexandre Belloni 5162eeaa532SAlexandre Belloni static int rv3032_trickle_charger_setup(struct device *dev, struct rv3032_data *rv3032) 5172eeaa532SAlexandre Belloni { 5182eeaa532SAlexandre Belloni u32 val, ohms, voltage; 5192eeaa532SAlexandre Belloni int i; 5202eeaa532SAlexandre Belloni 5212eeaa532SAlexandre Belloni val = FIELD_PREP(RV3032_PMU_TCM, 1) | FIELD_PREP(RV3032_PMU_BSM, RV3032_PMU_BSM_DSM); 5222eeaa532SAlexandre Belloni if (!device_property_read_u32(dev, "trickle-voltage-millivolt", &voltage)) { 5232eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(rv3032_trickle_voltages); i++) 5242eeaa532SAlexandre Belloni if (voltage == rv3032_trickle_voltages[i]) 5252eeaa532SAlexandre Belloni break; 5262eeaa532SAlexandre Belloni if (i < ARRAY_SIZE(rv3032_trickle_voltages)) 5272eeaa532SAlexandre Belloni val = FIELD_PREP(RV3032_PMU_TCM, i) | 5282eeaa532SAlexandre Belloni FIELD_PREP(RV3032_PMU_BSM, RV3032_PMU_BSM_LSM); 5292eeaa532SAlexandre Belloni } 5302eeaa532SAlexandre Belloni 5312eeaa532SAlexandre Belloni if (device_property_read_u32(dev, "trickle-resistor-ohms", &ohms)) 5322eeaa532SAlexandre Belloni return 0; 5332eeaa532SAlexandre Belloni 5342eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(rv3032_trickle_resistors); i++) 5352eeaa532SAlexandre Belloni if (ohms == rv3032_trickle_resistors[i]) 5362eeaa532SAlexandre Belloni break; 5372eeaa532SAlexandre Belloni 5382eeaa532SAlexandre Belloni if (i >= ARRAY_SIZE(rv3032_trickle_resistors)) { 5392eeaa532SAlexandre Belloni dev_warn(dev, "invalid trickle resistor value\n"); 5402eeaa532SAlexandre Belloni 5412eeaa532SAlexandre Belloni return 0; 5422eeaa532SAlexandre Belloni } 5432eeaa532SAlexandre Belloni 5442eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_PMU, 5452eeaa532SAlexandre Belloni RV3032_PMU_TCR | RV3032_PMU_TCM | RV3032_PMU_BSM, 5462eeaa532SAlexandre Belloni val | FIELD_PREP(RV3032_PMU_TCR, i)); 5472eeaa532SAlexandre Belloni } 5482eeaa532SAlexandre Belloni 5492eeaa532SAlexandre Belloni #ifdef CONFIG_COMMON_CLK 5502eeaa532SAlexandre Belloni #define clkout_hw_to_rv3032(hw) container_of(hw, struct rv3032_data, clkout_hw) 5512eeaa532SAlexandre Belloni 5522eeaa532SAlexandre Belloni static int clkout_xtal_rates[] = { 5532eeaa532SAlexandre Belloni 32768, 5542eeaa532SAlexandre Belloni 1024, 5552eeaa532SAlexandre Belloni 64, 5562eeaa532SAlexandre Belloni 1, 5572eeaa532SAlexandre Belloni }; 5582eeaa532SAlexandre Belloni 5592eeaa532SAlexandre Belloni #define RV3032_HFD_STEP 8192 5602eeaa532SAlexandre Belloni 5612eeaa532SAlexandre Belloni static unsigned long rv3032_clkout_recalc_rate(struct clk_hw *hw, 5622eeaa532SAlexandre Belloni unsigned long parent_rate) 5632eeaa532SAlexandre Belloni { 5642eeaa532SAlexandre Belloni int clkout, ret; 5652eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 5662eeaa532SAlexandre Belloni 5672eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CLKOUT2, &clkout); 5682eeaa532SAlexandre Belloni if (ret < 0) 5692eeaa532SAlexandre Belloni return 0; 5702eeaa532SAlexandre Belloni 5712eeaa532SAlexandre Belloni if (clkout & RV3032_CLKOUT2_OS) { 5722eeaa532SAlexandre Belloni unsigned long rate = FIELD_GET(RV3032_CLKOUT2_HFD_MSK, clkout) << 8; 5732eeaa532SAlexandre Belloni 5742eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_CLKOUT1, &clkout); 5752eeaa532SAlexandre Belloni if (ret < 0) 5762eeaa532SAlexandre Belloni return 0; 5772eeaa532SAlexandre Belloni 5782eeaa532SAlexandre Belloni rate += clkout + 1; 5792eeaa532SAlexandre Belloni 5802eeaa532SAlexandre Belloni return rate * RV3032_HFD_STEP; 5812eeaa532SAlexandre Belloni } 5822eeaa532SAlexandre Belloni 5832eeaa532SAlexandre Belloni return clkout_xtal_rates[FIELD_GET(RV3032_CLKOUT2_FD_MSK, clkout)]; 5842eeaa532SAlexandre Belloni } 5852eeaa532SAlexandre Belloni 5862eeaa532SAlexandre Belloni static long rv3032_clkout_round_rate(struct clk_hw *hw, unsigned long rate, 5872eeaa532SAlexandre Belloni unsigned long *prate) 5882eeaa532SAlexandre Belloni { 5892eeaa532SAlexandre Belloni int i, hfd; 5902eeaa532SAlexandre Belloni 5912eeaa532SAlexandre Belloni if (rate < RV3032_HFD_STEP) 5922eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(clkout_xtal_rates); i++) 5932eeaa532SAlexandre Belloni if (clkout_xtal_rates[i] <= rate) 5942eeaa532SAlexandre Belloni return clkout_xtal_rates[i]; 5952eeaa532SAlexandre Belloni 5962eeaa532SAlexandre Belloni hfd = DIV_ROUND_CLOSEST(rate, RV3032_HFD_STEP); 5972eeaa532SAlexandre Belloni 5982eeaa532SAlexandre Belloni return RV3032_HFD_STEP * clamp(hfd, 0, 8192); 5992eeaa532SAlexandre Belloni } 6002eeaa532SAlexandre Belloni 6012eeaa532SAlexandre Belloni static int rv3032_clkout_set_rate(struct clk_hw *hw, unsigned long rate, 6022eeaa532SAlexandre Belloni unsigned long parent_rate) 6032eeaa532SAlexandre Belloni { 6042eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 6052eeaa532SAlexandre Belloni u32 status, eerd; 6062eeaa532SAlexandre Belloni int i, hfd, ret; 6072eeaa532SAlexandre Belloni 6082eeaa532SAlexandre Belloni for (i = 0; i < ARRAY_SIZE(clkout_xtal_rates); i++) { 6092eeaa532SAlexandre Belloni if (clkout_xtal_rates[i] == rate) { 6102eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_CLKOUT2, 0xff, 6112eeaa532SAlexandre Belloni FIELD_PREP(RV3032_CLKOUT2_FD_MSK, i)); 6122eeaa532SAlexandre Belloni } 6132eeaa532SAlexandre Belloni } 6142eeaa532SAlexandre Belloni 6152eeaa532SAlexandre Belloni hfd = DIV_ROUND_CLOSEST(rate, RV3032_HFD_STEP); 6162eeaa532SAlexandre Belloni hfd = clamp(hfd, 1, 8192) - 1; 6172eeaa532SAlexandre Belloni 6182eeaa532SAlexandre Belloni ret = rv3032_enter_eerd(rv3032, &eerd); 6192eeaa532SAlexandre Belloni if (ret) 6202eeaa532SAlexandre Belloni goto exit_eerd; 6212eeaa532SAlexandre Belloni 6222eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_CLKOUT1, hfd & 0xff); 6232eeaa532SAlexandre Belloni if (ret) 6242eeaa532SAlexandre Belloni return ret; 6252eeaa532SAlexandre Belloni 6262eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_CLKOUT2, RV3032_CLKOUT2_OS | 6272eeaa532SAlexandre Belloni FIELD_PREP(RV3032_CLKOUT2_HFD_MSK, hfd >> 8)); 6282eeaa532SAlexandre Belloni if (ret) 6292eeaa532SAlexandre Belloni goto exit_eerd; 6302eeaa532SAlexandre Belloni 6312eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_EEPROM_CMD, RV3032_EEPROM_CMD_UPDATE); 6322eeaa532SAlexandre Belloni if (ret) 6332eeaa532SAlexandre Belloni goto exit_eerd; 6342eeaa532SAlexandre Belloni 6352eeaa532SAlexandre Belloni usleep_range(46000, RV3032_EEBUSY_TIMEOUT); 6362eeaa532SAlexandre Belloni 6372eeaa532SAlexandre Belloni ret = regmap_read_poll_timeout(rv3032->regmap, RV3032_TLSB, status, 6382eeaa532SAlexandre Belloni !(status & RV3032_TLSB_EEBUSY), 6392eeaa532SAlexandre Belloni RV3032_EEBUSY_POLL, RV3032_EEBUSY_TIMEOUT); 6402eeaa532SAlexandre Belloni 6412eeaa532SAlexandre Belloni exit_eerd: 6422eeaa532SAlexandre Belloni rv3032_exit_eerd(rv3032, eerd); 6432eeaa532SAlexandre Belloni 6442eeaa532SAlexandre Belloni return ret; 6452eeaa532SAlexandre Belloni } 6462eeaa532SAlexandre Belloni 6472eeaa532SAlexandre Belloni static int rv3032_clkout_prepare(struct clk_hw *hw) 6482eeaa532SAlexandre Belloni { 6492eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 6502eeaa532SAlexandre Belloni 6512eeaa532SAlexandre Belloni return rv3032_update_cfg(rv3032, RV3032_PMU, RV3032_PMU_NCLKE, 0); 6522eeaa532SAlexandre Belloni } 6532eeaa532SAlexandre Belloni 6542eeaa532SAlexandre Belloni static void rv3032_clkout_unprepare(struct clk_hw *hw) 6552eeaa532SAlexandre Belloni { 6562eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 6572eeaa532SAlexandre Belloni 6582eeaa532SAlexandre Belloni rv3032_update_cfg(rv3032, RV3032_PMU, RV3032_PMU_NCLKE, RV3032_PMU_NCLKE); 6592eeaa532SAlexandre Belloni } 6602eeaa532SAlexandre Belloni 6612eeaa532SAlexandre Belloni static int rv3032_clkout_is_prepared(struct clk_hw *hw) 6622eeaa532SAlexandre Belloni { 6632eeaa532SAlexandre Belloni int val, ret; 6642eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = clkout_hw_to_rv3032(hw); 6652eeaa532SAlexandre Belloni 6662eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_PMU, &val); 6672eeaa532SAlexandre Belloni if (ret < 0) 6682eeaa532SAlexandre Belloni return ret; 6692eeaa532SAlexandre Belloni 6702eeaa532SAlexandre Belloni return !(val & RV3032_PMU_NCLKE); 6712eeaa532SAlexandre Belloni } 6722eeaa532SAlexandre Belloni 6732eeaa532SAlexandre Belloni static const struct clk_ops rv3032_clkout_ops = { 6742eeaa532SAlexandre Belloni .prepare = rv3032_clkout_prepare, 6752eeaa532SAlexandre Belloni .unprepare = rv3032_clkout_unprepare, 6762eeaa532SAlexandre Belloni .is_prepared = rv3032_clkout_is_prepared, 6772eeaa532SAlexandre Belloni .recalc_rate = rv3032_clkout_recalc_rate, 6782eeaa532SAlexandre Belloni .round_rate = rv3032_clkout_round_rate, 6792eeaa532SAlexandre Belloni .set_rate = rv3032_clkout_set_rate, 6802eeaa532SAlexandre Belloni }; 6812eeaa532SAlexandre Belloni 6822eeaa532SAlexandre Belloni static int rv3032_clkout_register_clk(struct rv3032_data *rv3032, 6832eeaa532SAlexandre Belloni struct i2c_client *client) 6842eeaa532SAlexandre Belloni { 6852eeaa532SAlexandre Belloni int ret; 6862eeaa532SAlexandre Belloni struct clk *clk; 6872eeaa532SAlexandre Belloni struct clk_init_data init; 6882eeaa532SAlexandre Belloni struct device_node *node = client->dev.of_node; 6892eeaa532SAlexandre Belloni 6902eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_TLSB, RV3032_TLSB_CLKF, 0); 6912eeaa532SAlexandre Belloni if (ret < 0) 6922eeaa532SAlexandre Belloni return ret; 6932eeaa532SAlexandre Belloni 6942eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL2, RV3032_CTRL2_CLKIE, 0); 6952eeaa532SAlexandre Belloni if (ret < 0) 6962eeaa532SAlexandre Belloni return ret; 6972eeaa532SAlexandre Belloni 6982eeaa532SAlexandre Belloni ret = regmap_write(rv3032->regmap, RV3032_CLK_IRQ, 0); 6992eeaa532SAlexandre Belloni if (ret < 0) 7002eeaa532SAlexandre Belloni return ret; 7012eeaa532SAlexandre Belloni 7022eeaa532SAlexandre Belloni init.name = "rv3032-clkout"; 7032eeaa532SAlexandre Belloni init.ops = &rv3032_clkout_ops; 7042eeaa532SAlexandre Belloni init.flags = 0; 7052eeaa532SAlexandre Belloni init.parent_names = NULL; 7062eeaa532SAlexandre Belloni init.num_parents = 0; 7072eeaa532SAlexandre Belloni rv3032->clkout_hw.init = &init; 7082eeaa532SAlexandre Belloni 7092eeaa532SAlexandre Belloni of_property_read_string(node, "clock-output-names", &init.name); 7102eeaa532SAlexandre Belloni 7112eeaa532SAlexandre Belloni clk = devm_clk_register(&client->dev, &rv3032->clkout_hw); 7122eeaa532SAlexandre Belloni if (!IS_ERR(clk)) 7132eeaa532SAlexandre Belloni of_clk_add_provider(node, of_clk_src_simple_get, clk); 7142eeaa532SAlexandre Belloni 7152eeaa532SAlexandre Belloni return 0; 7162eeaa532SAlexandre Belloni } 7172eeaa532SAlexandre Belloni #endif 7182eeaa532SAlexandre Belloni 7192eeaa532SAlexandre Belloni static int rv3032_hwmon_read_temp(struct device *dev, long *mC) 7202eeaa532SAlexandre Belloni { 7212eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 7222eeaa532SAlexandre Belloni u8 buf[2]; 7232eeaa532SAlexandre Belloni int temp, prev = 0; 7242eeaa532SAlexandre Belloni int ret; 7252eeaa532SAlexandre Belloni 7262eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_TLSB, buf, sizeof(buf)); 7272eeaa532SAlexandre Belloni if (ret) 7282eeaa532SAlexandre Belloni return ret; 7292eeaa532SAlexandre Belloni 7302eeaa532SAlexandre Belloni temp = sign_extend32(buf[1], 7) << 4; 7312eeaa532SAlexandre Belloni temp |= FIELD_GET(RV3032_TLSB_TEMP, buf[0]); 7322eeaa532SAlexandre Belloni 7332eeaa532SAlexandre Belloni /* No blocking or shadowing on RV3032_TLSB and RV3032_TMSB */ 7342eeaa532SAlexandre Belloni do { 7352eeaa532SAlexandre Belloni prev = temp; 7362eeaa532SAlexandre Belloni 7372eeaa532SAlexandre Belloni ret = regmap_bulk_read(rv3032->regmap, RV3032_TLSB, buf, sizeof(buf)); 7382eeaa532SAlexandre Belloni if (ret) 7392eeaa532SAlexandre Belloni return ret; 7402eeaa532SAlexandre Belloni 7412eeaa532SAlexandre Belloni temp = sign_extend32(buf[1], 7) << 4; 7422eeaa532SAlexandre Belloni temp |= FIELD_GET(RV3032_TLSB_TEMP, buf[0]); 7432eeaa532SAlexandre Belloni } while (temp != prev); 7442eeaa532SAlexandre Belloni 7452eeaa532SAlexandre Belloni *mC = (temp * 1000) / 16; 7462eeaa532SAlexandre Belloni 7472eeaa532SAlexandre Belloni return 0; 7482eeaa532SAlexandre Belloni } 7492eeaa532SAlexandre Belloni 7502eeaa532SAlexandre Belloni static umode_t rv3032_hwmon_is_visible(const void *data, enum hwmon_sensor_types type, 7512eeaa532SAlexandre Belloni u32 attr, int channel) 7522eeaa532SAlexandre Belloni { 7532eeaa532SAlexandre Belloni if (type != hwmon_temp) 7542eeaa532SAlexandre Belloni return 0; 7552eeaa532SAlexandre Belloni 7562eeaa532SAlexandre Belloni switch (attr) { 7572eeaa532SAlexandre Belloni case hwmon_temp_input: 7582eeaa532SAlexandre Belloni return 0444; 7592eeaa532SAlexandre Belloni default: 7602eeaa532SAlexandre Belloni return 0; 7612eeaa532SAlexandre Belloni } 7622eeaa532SAlexandre Belloni } 7632eeaa532SAlexandre Belloni 7642eeaa532SAlexandre Belloni static int rv3032_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 7652eeaa532SAlexandre Belloni u32 attr, int channel, long *temp) 7662eeaa532SAlexandre Belloni { 7672eeaa532SAlexandre Belloni int err; 7682eeaa532SAlexandre Belloni 7692eeaa532SAlexandre Belloni switch (attr) { 7702eeaa532SAlexandre Belloni case hwmon_temp_input: 7712eeaa532SAlexandre Belloni err = rv3032_hwmon_read_temp(dev, temp); 7722eeaa532SAlexandre Belloni break; 7732eeaa532SAlexandre Belloni default: 7742eeaa532SAlexandre Belloni err = -EOPNOTSUPP; 7752eeaa532SAlexandre Belloni break; 7762eeaa532SAlexandre Belloni } 7772eeaa532SAlexandre Belloni 7782eeaa532SAlexandre Belloni return err; 7792eeaa532SAlexandre Belloni } 7802eeaa532SAlexandre Belloni 7812eeaa532SAlexandre Belloni static const struct hwmon_channel_info *rv3032_hwmon_info[] = { 7822eeaa532SAlexandre Belloni HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ), 7832eeaa532SAlexandre Belloni HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST), 7842eeaa532SAlexandre Belloni NULL 7852eeaa532SAlexandre Belloni }; 7862eeaa532SAlexandre Belloni 7872eeaa532SAlexandre Belloni static const struct hwmon_ops rv3032_hwmon_hwmon_ops = { 7882eeaa532SAlexandre Belloni .is_visible = rv3032_hwmon_is_visible, 7892eeaa532SAlexandre Belloni .read = rv3032_hwmon_read, 7902eeaa532SAlexandre Belloni }; 7912eeaa532SAlexandre Belloni 7922eeaa532SAlexandre Belloni static const struct hwmon_chip_info rv3032_hwmon_chip_info = { 7932eeaa532SAlexandre Belloni .ops = &rv3032_hwmon_hwmon_ops, 7942eeaa532SAlexandre Belloni .info = rv3032_hwmon_info, 7952eeaa532SAlexandre Belloni }; 7962eeaa532SAlexandre Belloni 7972eeaa532SAlexandre Belloni static void rv3032_hwmon_register(struct device *dev) 7982eeaa532SAlexandre Belloni { 7992eeaa532SAlexandre Belloni struct rv3032_data *rv3032 = dev_get_drvdata(dev); 8002eeaa532SAlexandre Belloni 8012eeaa532SAlexandre Belloni if (!IS_REACHABLE(CONFIG_HWMON)) 8022eeaa532SAlexandre Belloni return; 8032eeaa532SAlexandre Belloni 8042eeaa532SAlexandre Belloni devm_hwmon_device_register_with_info(dev, "rv3032", rv3032, &rv3032_hwmon_chip_info, NULL); 8052eeaa532SAlexandre Belloni } 8062eeaa532SAlexandre Belloni 8072eeaa532SAlexandre Belloni static struct rtc_class_ops rv3032_rtc_ops = { 8082eeaa532SAlexandre Belloni .read_time = rv3032_get_time, 8092eeaa532SAlexandre Belloni .set_time = rv3032_set_time, 8102eeaa532SAlexandre Belloni .read_offset = rv3032_read_offset, 8112eeaa532SAlexandre Belloni .set_offset = rv3032_set_offset, 8122eeaa532SAlexandre Belloni .ioctl = rv3032_ioctl, 8132eeaa532SAlexandre Belloni }; 8142eeaa532SAlexandre Belloni 8152eeaa532SAlexandre Belloni static const struct regmap_config regmap_config = { 8162eeaa532SAlexandre Belloni .reg_bits = 8, 8172eeaa532SAlexandre Belloni .val_bits = 8, 8182eeaa532SAlexandre Belloni .max_register = 0xCA, 8192eeaa532SAlexandre Belloni }; 8202eeaa532SAlexandre Belloni 8212eeaa532SAlexandre Belloni static int rv3032_probe(struct i2c_client *client) 8222eeaa532SAlexandre Belloni { 8232eeaa532SAlexandre Belloni struct rv3032_data *rv3032; 8242eeaa532SAlexandre Belloni int ret, status; 8252eeaa532SAlexandre Belloni struct nvmem_config nvmem_cfg = { 8262eeaa532SAlexandre Belloni .name = "rv3032_nvram", 8272eeaa532SAlexandre Belloni .word_size = 1, 8282eeaa532SAlexandre Belloni .stride = 1, 8292eeaa532SAlexandre Belloni .size = 16, 8302eeaa532SAlexandre Belloni .type = NVMEM_TYPE_BATTERY_BACKED, 8312eeaa532SAlexandre Belloni .reg_read = rv3032_nvram_read, 8322eeaa532SAlexandre Belloni .reg_write = rv3032_nvram_write, 8332eeaa532SAlexandre Belloni }; 8342eeaa532SAlexandre Belloni struct nvmem_config eeprom_cfg = { 8352eeaa532SAlexandre Belloni .name = "rv3032_eeprom", 8362eeaa532SAlexandre Belloni .word_size = 1, 8372eeaa532SAlexandre Belloni .stride = 1, 8382eeaa532SAlexandre Belloni .size = 32, 8392eeaa532SAlexandre Belloni .type = NVMEM_TYPE_EEPROM, 8402eeaa532SAlexandre Belloni .reg_read = rv3032_eeprom_read, 8412eeaa532SAlexandre Belloni .reg_write = rv3032_eeprom_write, 8422eeaa532SAlexandre Belloni }; 8432eeaa532SAlexandre Belloni 8442eeaa532SAlexandre Belloni rv3032 = devm_kzalloc(&client->dev, sizeof(struct rv3032_data), 8452eeaa532SAlexandre Belloni GFP_KERNEL); 8462eeaa532SAlexandre Belloni if (!rv3032) 8472eeaa532SAlexandre Belloni return -ENOMEM; 8482eeaa532SAlexandre Belloni 8492eeaa532SAlexandre Belloni rv3032->regmap = devm_regmap_init_i2c(client, ®map_config); 8502eeaa532SAlexandre Belloni if (IS_ERR(rv3032->regmap)) 8512eeaa532SAlexandre Belloni return PTR_ERR(rv3032->regmap); 8522eeaa532SAlexandre Belloni 8532eeaa532SAlexandre Belloni i2c_set_clientdata(client, rv3032); 8542eeaa532SAlexandre Belloni 8552eeaa532SAlexandre Belloni ret = regmap_read(rv3032->regmap, RV3032_STATUS, &status); 8562eeaa532SAlexandre Belloni if (ret < 0) 8572eeaa532SAlexandre Belloni return ret; 8582eeaa532SAlexandre Belloni 8592eeaa532SAlexandre Belloni rv3032->rtc = devm_rtc_allocate_device(&client->dev); 8602eeaa532SAlexandre Belloni if (IS_ERR(rv3032->rtc)) 8612eeaa532SAlexandre Belloni return PTR_ERR(rv3032->rtc); 8622eeaa532SAlexandre Belloni 8632eeaa532SAlexandre Belloni if (client->irq > 0) { 8642eeaa532SAlexandre Belloni ret = devm_request_threaded_irq(&client->dev, client->irq, 8652eeaa532SAlexandre Belloni NULL, rv3032_handle_irq, 8662eeaa532SAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 8672eeaa532SAlexandre Belloni "rv3032", rv3032); 8682eeaa532SAlexandre Belloni if (ret) { 8692eeaa532SAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 8702eeaa532SAlexandre Belloni client->irq = 0; 8712eeaa532SAlexandre Belloni } else { 8722eeaa532SAlexandre Belloni rv3032_rtc_ops.read_alarm = rv3032_get_alarm; 8732eeaa532SAlexandre Belloni rv3032_rtc_ops.set_alarm = rv3032_set_alarm; 8742eeaa532SAlexandre Belloni rv3032_rtc_ops.alarm_irq_enable = rv3032_alarm_irq_enable; 8752eeaa532SAlexandre Belloni } 8762eeaa532SAlexandre Belloni } 8772eeaa532SAlexandre Belloni 8782eeaa532SAlexandre Belloni ret = regmap_update_bits(rv3032->regmap, RV3032_CTRL1, 8792eeaa532SAlexandre Belloni RV3032_CTRL1_WADA, RV3032_CTRL1_WADA); 8802eeaa532SAlexandre Belloni if (ret) 8812eeaa532SAlexandre Belloni return ret; 8822eeaa532SAlexandre Belloni 8832eeaa532SAlexandre Belloni rv3032_trickle_charger_setup(&client->dev, rv3032); 8842eeaa532SAlexandre Belloni 8852eeaa532SAlexandre Belloni rv3032->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 8862eeaa532SAlexandre Belloni rv3032->rtc->range_max = RTC_TIMESTAMP_END_2099; 8872eeaa532SAlexandre Belloni rv3032->rtc->ops = &rv3032_rtc_ops; 8882eeaa532SAlexandre Belloni ret = rtc_register_device(rv3032->rtc); 8892eeaa532SAlexandre Belloni if (ret) 8902eeaa532SAlexandre Belloni return ret; 8912eeaa532SAlexandre Belloni 892*767fbb71SAlexandre Belloni nvmem_cfg.priv = rv3032->regmap; 8932eeaa532SAlexandre Belloni rtc_nvmem_register(rv3032->rtc, &nvmem_cfg); 8942eeaa532SAlexandre Belloni eeprom_cfg.priv = rv3032; 8952eeaa532SAlexandre Belloni rtc_nvmem_register(rv3032->rtc, &eeprom_cfg); 8962eeaa532SAlexandre Belloni 8972eeaa532SAlexandre Belloni rv3032->rtc->max_user_freq = 1; 8982eeaa532SAlexandre Belloni 8992eeaa532SAlexandre Belloni #ifdef CONFIG_COMMON_CLK 9002eeaa532SAlexandre Belloni rv3032_clkout_register_clk(rv3032, client); 9012eeaa532SAlexandre Belloni #endif 9022eeaa532SAlexandre Belloni 9032eeaa532SAlexandre Belloni rv3032_hwmon_register(&client->dev); 9042eeaa532SAlexandre Belloni 9052eeaa532SAlexandre Belloni return 0; 9062eeaa532SAlexandre Belloni } 9072eeaa532SAlexandre Belloni 9082eeaa532SAlexandre Belloni static const struct of_device_id rv3032_of_match[] = { 9092eeaa532SAlexandre Belloni { .compatible = "microcrystal,rv3032", }, 9102eeaa532SAlexandre Belloni { } 9112eeaa532SAlexandre Belloni }; 9122eeaa532SAlexandre Belloni MODULE_DEVICE_TABLE(of, rv3032_of_match); 9132eeaa532SAlexandre Belloni 9142eeaa532SAlexandre Belloni static struct i2c_driver rv3032_driver = { 9152eeaa532SAlexandre Belloni .driver = { 9162eeaa532SAlexandre Belloni .name = "rtc-rv3032", 9172eeaa532SAlexandre Belloni .of_match_table = of_match_ptr(rv3032_of_match), 9182eeaa532SAlexandre Belloni }, 9192eeaa532SAlexandre Belloni .probe_new = rv3032_probe, 9202eeaa532SAlexandre Belloni }; 9212eeaa532SAlexandre Belloni module_i2c_driver(rv3032_driver); 9222eeaa532SAlexandre Belloni 9232eeaa532SAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 9242eeaa532SAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV3032 RTC driver"); 9252eeaa532SAlexandre Belloni MODULE_LICENSE("GPL v2"); 926