aboutsummaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-rv8803.c
diff options
context:
space:
mode:
authorLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
committerLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
commit5b7c4cabbb65f5c469464da6c5f614cbd7f730f2 (patch)
treecc5c2d0a898769fd59549594fedb3ee6f84e59a0 /drivers/rtc/rtc-rv8803.c
downloadlinux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.tar.gz
linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.zip
Merge tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-nextgrafted
Pull networking updates from Jakub Kicinski: "Core: - Add dedicated kmem_cache for typical/small skb->head, avoid having to access struct page at kfree time, and improve memory use. - Introduce sysctl to set default RPS configuration for new netdevs. - Define Netlink protocol specification format which can be used to describe messages used by each family and auto-generate parsers. Add tools for generating kernel data structures and uAPI headers. - Expose all net/core sysctls inside netns. - Remove 4s sleep in netpoll if carrier is instantly detected on boot. - Add configurable limit of MDB entries per port, and port-vlan. - Continue populating drop reasons throughout the stack. - Retire a handful of legacy Qdiscs and classifiers. Protocols: - Support IPv4 big TCP (TSO frames larger than 64kB). - Add IP_LOCAL_PORT_RANGE socket option, to control local port range on socket by socket basis. - Track and report in procfs number of MPTCP sockets used. - Support mixing IPv4 and IPv6 flows in the in-kernel MPTCP path manager. - IPv6: don't check net.ipv6.route.max_size and rely on garbage collection to free memory (similarly to IPv4). - Support Penultimate Segment Pop (PSP) flavor in SRv6 (RFC8986). - ICMP: add per-rate limit counters. - Add support for user scanning requests in ieee802154. - Remove static WEP support. - Support minimal Wi-Fi 7 Extremely High Throughput (EHT) rate reporting. - WiFi 7 EHT channel puncturing support (client & AP). BPF: - Add a rbtree data structure following the "next-gen data structure" precedent set by recently added linked list, that is, by using kfunc + kptr instead of adding a new BPF map type. - Expose XDP hints via kfuncs with initial support for RX hash and timestamp metadata. - Add BPF_F_NO_TUNNEL_KEY extension to bpf_skb_set_tunnel_key to better support decap on GRE tunnel devices not operating in collect metadata. - Improve x86 JIT's codegen for PROBE_MEM runtime error checks. - Remove the need for trace_printk_lock for bpf_trace_printk and bpf_trace_vprintk helpers. - Extend libbpf's bpf_tracing.h support for tracing arguments of kprobes/uprobes and syscall as a special case. - Significantly reduce the search time for module symbols by livepatch and BPF. - Enable cpumasks to be used as kptrs, which is useful for tracing programs tracking which tasks end up running on which CPUs in different time intervals. - Add support for BPF trampoline on s390x and riscv64. - Add capability to export the XDP features supported by the NIC. - Add __bpf_kfunc tag for marking kernel functions as kfuncs. - Add cgroup.memory=nobpf kernel parameter option to disable BPF memory accounting for container environments. Netfilter: - Remove the CLUSTERIP target. It has been marked as obsolete for years, and we still have WARN splats wrt races of the out-of-band /proc interface installed by this target. - Add 'destroy' commands to nf_tables. They are identical to the existing 'delete' commands, but do not return an error if the referenced object (set, chain, rule...) did not exist. Driver API: - Improve cpumask_local_spread() locality to help NICs set the right IRQ affinity on AMD platforms. - Separate C22 and C45 MDIO bus transactions more clearly. - Introduce new DCB table to control DSCP rewrite on egress. - Support configuration of Physical Layer Collision Avoidance (PLCA) Reconciliation Sublayer (RS) (802.3cg-2019). Modern version of shared medium Ethernet. - Support for MAC Merge layer (IEEE 802.3-2018 clause 99). Allowing preemption of low priority frames by high priority frames. - Add support for controlling MACSec offload using netlink SET. - Rework devlink instance refcounts to allow registration and de-registration under the instance lock. Split the code into multiple files, drop some of the unnecessarily granular locks and factor out common parts of netlink operation handling. - Add TX frame aggregation parameters (for USB drivers). - Add a new attr TCA_EXT_WARN_MSG to report TC (offload) warning messages with notifications for debug. - Allow offloading of UDP NEW connections via act_ct. - Add support for per action HW stats in TC. - Support hardware miss to TC action (continue processing in SW from a specific point in the action chain). - Warn if old Wireless Extension user space interface is used with modern cfg80211/mac80211 drivers. Do not support Wireless Extensions for Wi-Fi 7 devices at all. Everyone should switch to using nl80211 interface instead. - Improve the CAN bit timing configuration. Use extack to return error messages directly to user space, update the SJW handling, including the definition of a new default value that will benefit CAN-FD controllers, by increasing their oscillator tolerance. New hardware / drivers: - Ethernet: - nVidia BlueField-3 support (control traffic driver) - Ethernet support for imx93 SoCs - Motorcomm yt8531 gigabit Ethernet PHY - onsemi NCN26000 10BASE-T1S PHY (with support for PLCA) - Microchip LAN8841 PHY (incl. cable diagnostics and PTP) - Amlogic gxl MDIO mux - WiFi: - RealTek RTL8188EU (rtl8xxxu) - Qualcomm Wi-Fi 7 devices (ath12k) - CAN: - Renesas R-Car V4H Drivers: - Bluetooth: - Set Per Platform Antenna Gain (PPAG) for Intel controllers. - Ethernet NICs: - Intel (1G, igc): - support TSN / Qbv / packet scheduling features of i226 model - Intel (100G, ice): - use GNSS subsystem instead of TTY - multi-buffer XDP support - extend support for GPIO pins to E823 devices - nVidia/Mellanox: - update the shared buffer configuration on PFC commands - implement PTP adjphase function for HW offset control - TC support for Geneve and GRE with VF tunnel offload - more efficient crypto key management method - multi-port eswitch support - Netronome/Corigine: - add DCB IEEE support - support IPsec offloading for NFP3800 - Freescale/NXP (enetc): - support XDP_REDIRECT for XDP non-linear buffers - improve reconfig, avoid link flap and waiting for idle - support MAC Merge layer - Other NICs: - sfc/ef100: add basic devlink support for ef100 - ionic: rx_push mode operation (writing descriptors via MMIO) - bnxt: use the auxiliary bus abstraction for RDMA - r8169: disable ASPM and reset bus in case of tx timeout - cpsw: support QSGMII mode for J721e CPSW9G - cpts: support pulse-per-second output - ngbe: add an mdio bus driver - usbnet: optimize usbnet_bh() by avoiding unnecessary queuing - r8152: handle devices with FW with NCM support - amd-xgbe: support 10Mbps, 2.5GbE speeds and rx-adaptation - virtio-net: support multi buffer XDP - virtio/vsock: replace virtio_vsock_pkt with sk_buff - tsnep: XDP support - Ethernet high-speed switches: - nVidia/Mellanox (mlxsw): - add support for latency TLV (in FW control messages) - Microchip (sparx5): - separate explicit and implicit traffic forwarding rules, make the implicit rules always active - add support for egress DSCP rewrite - IS0 VCAP support (Ingress Classification) - IS2 VCAP filters (protos, L3 addrs, L4 ports, flags, ToS etc.) - ES2 VCAP support (Egress Access Control) - support for Per-Stream Filtering and Policing (802.1Q, 8.6.5.1) - Ethernet embedded switches: - Marvell (mv88e6xxx): - add MAB (port auth) offload support - enable PTP receive for mv88e6390 - NXP (ocelot): - support MAC Merge layer - support for the the vsc7512 internal copper phys - Microchip: - lan9303: convert to PHYLINK - lan966x: support TC flower filter statistics - lan937x: PTP support for KSZ9563/KSZ8563 and LAN937x - lan937x: support Credit Based Shaper configuration - ksz9477: support Energy Efficient Ethernet - other: - qca8k: convert to regmap read/write API, use bulk operations - rswitch: Improve TX timestamp accuracy - Intel WiFi (iwlwifi): - EHT (Wi-Fi 7) rate reporting - STEP equalizer support: transfer some STEP (connection to radio on platforms with integrated wifi) related parameters from the BIOS to the firmware. - Qualcomm 802.11ax WiFi (ath11k): - IPQ5018 support - Fine Timing Measurement (FTM) responder role support - channel 177 support - MediaTek WiFi (mt76): - per-PHY LED support - mt7996: EHT (Wi-Fi 7) support - Wireless Ethernet Dispatch (WED) reset support - switch to using page pool allocator - RealTek WiFi (rtw89): - support new version of Bluetooth co-existance - Mobile: - rmnet: support TX aggregation" * tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (1872 commits) page_pool: add a comment explaining the fragment counter usage net: ethtool: fix __ethtool_dev_mm_supported() implementation ethtool: pse-pd: Fix double word in comments xsk: add linux/vmalloc.h to xsk.c sefltests: netdevsim: wait for devlink instance after netns removal selftest: fib_tests: Always cleanup before exit net/mlx5e: Align IPsec ASO result memory to be as required by hardware net/mlx5e: TC, Set CT miss to the specific ct action instance net/mlx5e: Rename CHAIN_TO_REG to MAPPED_OBJ_TO_REG net/mlx5: Refactor tc miss handling to a single function net/mlx5: Kconfig: Make tc offload depend on tc skb extension net/sched: flower: Support hardware miss to tc action net/sched: flower: Move filter handle initialization earlier net/sched: cls_api: Support hardware miss to tc action net/sched: Rename user cookie and act cookie sfc: fix builds without CONFIG_RTC_LIB sfc: clean up some inconsistent indentings net/mlx4_en: Introduce flexible array to silence overflow warning net: lan966x: Fix possible deadlock inside PTP net/ulp: Remove redundant ->clone() test in inet_clone_ulp(). ...
Diffstat (limited to 'drivers/rtc/rtc-rv8803.c')
-rw-r--r--drivers/rtc/rtc-rv8803.c713
1 files changed, 713 insertions, 0 deletions
diff --git a/drivers/rtc/rtc-rv8803.c b/drivers/rtc/rtc-rv8803.c
new file mode 100644
index 000000000..b581b6d5a
--- /dev/null
+++ b/drivers/rtc/rtc-rv8803.c
@@ -0,0 +1,713 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * RTC driver for the Micro Crystal RV8803
+ *
+ * Copyright (C) 2015 Micro Crystal SA
+ * Alexandre Belloni <alexandre.belloni@bootlin.com>
+ *
+ */
+
+#include <linux/bcd.h>
+#include <linux/bitops.h>
+#include <linux/bitfield.h>
+#include <linux/log2.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/rtc.h>
+
+#define RV8803_I2C_TRY_COUNT 4
+
+#define RV8803_SEC 0x00
+#define RV8803_MIN 0x01
+#define RV8803_HOUR 0x02
+#define RV8803_WEEK 0x03
+#define RV8803_DAY 0x04
+#define RV8803_MONTH 0x05
+#define RV8803_YEAR 0x06
+#define RV8803_RAM 0x07
+#define RV8803_ALARM_MIN 0x08
+#define RV8803_ALARM_HOUR 0x09
+#define RV8803_ALARM_WEEK_OR_DAY 0x0A
+#define RV8803_EXT 0x0D
+#define RV8803_FLAG 0x0E
+#define RV8803_CTRL 0x0F
+#define RV8803_OSC_OFFSET 0x2C
+
+#define RV8803_EXT_WADA BIT(6)
+
+#define RV8803_FLAG_V1F BIT(0)
+#define RV8803_FLAG_V2F BIT(1)
+#define RV8803_FLAG_AF BIT(3)
+#define RV8803_FLAG_TF BIT(4)
+#define RV8803_FLAG_UF BIT(5)
+
+#define RV8803_CTRL_RESET BIT(0)
+
+#define RV8803_CTRL_EIE BIT(2)
+#define RV8803_CTRL_AIE BIT(3)
+#define RV8803_CTRL_TIE BIT(4)
+#define RV8803_CTRL_UIE BIT(5)
+
+#define RX8803_CTRL_CSEL GENMASK(7, 6)
+
+#define RX8900_BACKUP_CTRL 0x18
+#define RX8900_FLAG_SWOFF BIT(2)
+#define RX8900_FLAG_VDETOFF BIT(3)
+
+enum rv8803_type {
+ rv_8803,
+ rx_8803,
+ rx_8804,
+ rx_8900
+};
+
+struct rv8803_data {
+ struct i2c_client *client;
+ struct rtc_device *rtc;
+ struct mutex flags_lock;
+ u8 ctrl;
+ u8 backup;
+ enum rv8803_type type;
+};
+
+static int rv8803_read_reg(const struct i2c_client *client, u8 reg)
+{
+ int try = RV8803_I2C_TRY_COUNT;
+ s32 ret;
+
+ /*
+ * There is a 61µs window during which the RTC does not acknowledge I2C
+ * transfers. In that case, ensure that there are multiple attempts.
+ */
+ do
+ ret = i2c_smbus_read_byte_data(client, reg);
+ while ((ret == -ENXIO || ret == -EIO) && --try);
+ if (ret < 0)
+ dev_err(&client->dev, "Unable to read register 0x%02x\n", reg);
+
+ return ret;
+}
+
+static int rv8803_read_regs(const struct i2c_client *client,
+ u8 reg, u8 count, u8 *values)
+{
+ int try = RV8803_I2C_TRY_COUNT;
+ s32 ret;
+
+ do
+ ret = i2c_smbus_read_i2c_block_data(client, reg, count, values);
+ while ((ret == -ENXIO || ret == -EIO) && --try);
+ if (ret != count) {
+ dev_err(&client->dev,
+ "Unable to read registers 0x%02x..0x%02x\n",
+ reg, reg + count - 1);
+ return ret < 0 ? ret : -EIO;
+ }
+
+ return 0;
+}
+
+static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value)
+{
+ int try = RV8803_I2C_TRY_COUNT;
+ s32 ret;
+
+ do
+ ret = i2c_smbus_write_byte_data(client, reg, value);
+ while ((ret == -ENXIO || ret == -EIO) && --try);
+ if (ret)
+ dev_err(&client->dev, "Unable to write register 0x%02x\n", reg);
+
+ return ret;
+}
+
+static int rv8803_write_regs(const struct i2c_client *client,
+ u8 reg, u8 count, const u8 *values)
+{
+ int try = RV8803_I2C_TRY_COUNT;
+ s32 ret;
+
+ do
+ ret = i2c_smbus_write_i2c_block_data(client, reg, count,
+ values);
+ while ((ret == -ENXIO || ret == -EIO) && --try);
+ if (ret)
+ dev_err(&client->dev,
+ "Unable to write registers 0x%02x..0x%02x\n",
+ reg, reg + count - 1);
+
+ return ret;
+}
+
+static int rv8803_regs_init(struct rv8803_data *rv8803)
+{
+ int ret;
+
+ ret = rv8803_write_reg(rv8803->client, RV8803_OSC_OFFSET, 0x00);
+ if (ret)
+ return ret;
+
+ ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
+ FIELD_PREP(RX8803_CTRL_CSEL, 1)); /* 2s */
+ if (ret)
+ return ret;
+
+ ret = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3,
+ (u8[]){ 0, 0, 0 });
+ if (ret)
+ return ret;
+
+ return rv8803_write_reg(rv8803->client, RV8803_RAM, 0x00);
+}
+
+static int rv8803_regs_configure(struct rv8803_data *rv8803);
+
+static int rv8803_regs_reset(struct rv8803_data *rv8803)
+{
+ /*
+ * The RV-8803 resets all registers to POR defaults after voltage-loss,
+ * the Epson RTCs don't, so we manually reset the remainder here.
+ */
+ if (rv8803->type == rx_8803 || rv8803->type == rx_8900) {
+ int ret = rv8803_regs_init(rv8803);
+ if (ret)
+ return ret;
+ }
+
+ return rv8803_regs_configure(rv8803);
+}
+
+static irqreturn_t rv8803_handle_irq(int irq, void *dev_id)
+{
+ struct i2c_client *client = dev_id;
+ struct rv8803_data *rv8803 = i2c_get_clientdata(client);
+ unsigned long events = 0;
+ int flags;
+
+ mutex_lock(&rv8803->flags_lock);
+
+ flags = rv8803_read_reg(client, RV8803_FLAG);
+ if (flags <= 0) {
+ mutex_unlock(&rv8803->flags_lock);
+ return IRQ_NONE;
+ }
+
+ if (flags & RV8803_FLAG_V1F)
+ dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
+
+ if (flags & RV8803_FLAG_V2F)
+ dev_warn(&client->dev, "Voltage low, data loss detected.\n");
+
+ if (flags & RV8803_FLAG_TF) {
+ flags &= ~RV8803_FLAG_TF;
+ rv8803->ctrl &= ~RV8803_CTRL_TIE;
+ events |= RTC_PF;
+ }
+
+ if (flags & RV8803_FLAG_AF) {
+ flags &= ~RV8803_FLAG_AF;
+ rv8803->ctrl &= ~RV8803_CTRL_AIE;
+ events |= RTC_AF;
+ }
+
+ if (flags & RV8803_FLAG_UF) {
+ flags &= ~RV8803_FLAG_UF;
+ rv8803->ctrl &= ~RV8803_CTRL_UIE;
+ events |= RTC_UF;
+ }
+
+ if (events) {
+ rtc_update_irq(rv8803->rtc, 1, events);
+ rv8803_write_reg(client, RV8803_FLAG, flags);
+ rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl);
+ }
+
+ mutex_unlock(&rv8803->flags_lock);
+
+ return IRQ_HANDLED;
+}
+
+static int rv8803_get_time(struct device *dev, struct rtc_time *tm)
+{
+ struct rv8803_data *rv8803 = dev_get_drvdata(dev);
+ u8 date1[7];
+ u8 date2[7];
+ u8 *date = date1;
+ int ret, flags;
+
+ flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
+ if (flags < 0)
+ return flags;
+
+ if (flags & RV8803_FLAG_V2F) {
+ dev_warn(dev, "Voltage low, data is invalid.\n");
+ return -EINVAL;
+ }
+
+ ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date);
+ if (ret)
+ return ret;
+
+ if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) {
+ ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2);
+ if (ret)
+ return ret;
+
+ if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59))
+ date = date2;
+ }
+
+ tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f);
+ tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f);
+ tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f);
+ tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f);
+ tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f);
+ tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
+ tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100;
+
+ return 0;
+}
+
+static int rv8803_set_time(struct device *dev, struct rtc_time *tm)
+{
+ struct rv8803_data *rv8803 = dev_get_drvdata(dev);
+ u8 date[7];
+ int ctrl, flags, ret;
+
+ ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL);
+ if (ctrl < 0)
+ return ctrl;
+
+ /* Stop the clock */
+ ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
+ ctrl | RV8803_CTRL_RESET);
+ if (ret)
+ return ret;
+
+ date[RV8803_SEC] = bin2bcd(tm->tm_sec);
+ date[RV8803_MIN] = bin2bcd(tm->tm_min);
+ date[RV8803_HOUR] = bin2bcd(tm->tm_hour);
+ date[RV8803_WEEK] = 1 << (tm->tm_wday);
+ date[RV8803_DAY] = bin2bcd(tm->tm_mday);
+ date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
+ date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100);
+
+ ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date);
+ if (ret)
+ return ret;
+
+ /* Restart the clock */
+ ret = rv8803_write_reg(rv8803->client, RV8803_CTRL,
+ ctrl & ~RV8803_CTRL_RESET);
+ if (ret)
+ return ret;
+
+ mutex_lock(&rv8803->flags_lock);
+
+ flags = rv8803_read_reg(rv8803->client, RV8803_FLAG);
+ if (flags < 0) {
+ mutex_unlock(&rv8803->flags_lock);
+ return flags;
+ }
+
+ if (flags & RV8803_FLAG_V2F) {
+ ret = rv8803_regs_reset(rv8803);
+ if (ret) {
+ mutex_unlock(&rv8803->flags_lock);
+ return ret;
+ }
+ }
+
+ ret = rv8803_write_reg(rv8803->client, RV8803_FLAG,
+ flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F));
+
+ mutex_unlock(&rv8803->flags_lock);
+
+ return ret;
+}
+
+static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+ struct rv8803_data *rv8803 = dev_get_drvdata(dev);
+ struct i2c_client *client = rv8803->client;
+ u8 alarmvals[3];
+ int flags, ret;
+
+ ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals);
+ if (ret)
+ return ret;
+
+ flags = rv8803_read_reg(client, RV8803_FLAG);
+ if (flags < 0)
+ return flags;
+
+ alrm->time.tm_sec = 0;
+ alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
+ alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
+ alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
+
+ alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE);
+ alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled;
+
+ return 0;
+}
+
+static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct rv8803_data *rv8803 = dev_get_drvdata(dev);
+ u8 alarmvals[3];
+ u8 ctrl[2];
+ int ret, err;
+
+ /* The alarm has no seconds, round up to nearest minute */
+ if (alrm->time.tm_sec) {
+ time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
+
+ alarm_time += 60 - alrm->time.tm_sec;
+ rtc_time64_to_tm(alarm_time, &alrm->time);
+ }
+
+ mutex_lock(&rv8803->flags_lock);
+
+ ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl);
+ if (ret) {
+ mutex_unlock(&rv8803->flags_lock);
+ return ret;
+ }
+
+ alarmvals[0] = bin2bcd(alrm->time.tm_min);
+ alarmvals[1] = bin2bcd(alrm->time.tm_hour);
+ alarmvals[2] = bin2bcd(alrm->time.tm_mday);
+
+ if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) {
+ rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE);
+ err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
+ rv8803->ctrl);
+ if (err) {
+ mutex_unlock(&rv8803->flags_lock);
+ return err;
+ }
+ }
+
+ ctrl[0] &= ~RV8803_FLAG_AF;
+ err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[0]);
+ mutex_unlock(&rv8803->flags_lock);
+ if (err)
+ return err;
+
+ err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals);
+ if (err)
+ return err;
+
+ if (alrm->enabled) {
+ if (rv8803->rtc->uie_rtctimer.enabled)
+ rv8803->ctrl |= RV8803_CTRL_UIE;
+ if (rv8803->rtc->aie_timer.enabled)
+ rv8803->ctrl |= RV8803_CTRL_AIE;
+
+ err = rv8803_write_reg(rv8803->client, RV8803_CTRL,
+ rv8803->ctrl);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
+static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct rv8803_data *rv8803 = dev_get_drvdata(dev);
+ int ctrl, flags, err;
+
+ ctrl = rv8803->ctrl;
+
+ if (enabled) {
+ if (rv8803->rtc->uie_rtctimer.enabled)
+ ctrl |= RV8803_CTRL_UIE;
+ if (rv8803->rtc->aie_timer.enabled)
+ ctrl |= RV8803_CTRL_AIE;
+ } else {
+ if (!rv8803->rtc->uie_rtctimer.enabled)
+ ctrl &= ~RV8803_CTRL_UIE;
+ if (!rv8803->rtc->aie_timer.enabled)
+ ctrl &= ~RV8803_CTRL_AIE;
+ }
+
+ mutex_lock(&rv8803->flags_lock);
+ flags = rv8803_read_reg(client, RV8803_FLAG);
+ if (flags < 0) {
+ mutex_unlock(&rv8803->flags_lock);
+ return flags;
+ }
+ flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF);
+ err = rv8803_write_reg(client, RV8803_FLAG, flags);
+ mutex_unlock(&rv8803->flags_lock);
+ if (err)
+ return err;
+
+ if (ctrl != rv8803->ctrl) {
+ rv8803->ctrl = ctrl;
+ err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
+static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+ struct i2c_client *client = to_i2c_client(dev);
+ struct rv8803_data *rv8803 = dev_get_drvdata(dev);
+ unsigned int vl = 0;
+ int flags, ret = 0;
+
+ switch (cmd) {
+ case RTC_VL_READ:
+ flags = rv8803_read_reg(client, RV8803_FLAG);
+ if (flags < 0)
+ return flags;
+
+ if (flags & RV8803_FLAG_V1F) {
+ dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
+ vl = RTC_VL_ACCURACY_LOW;
+ }
+
+ if (flags & RV8803_FLAG_V2F)
+ vl |= RTC_VL_DATA_INVALID;
+
+ return put_user(vl, (unsigned int __user *)arg);
+
+ case RTC_VL_CLR:
+ mutex_lock(&rv8803->flags_lock);
+ flags = rv8803_read_reg(client, RV8803_FLAG);
+ if (flags < 0) {
+ mutex_unlock(&rv8803->flags_lock);
+ return flags;
+ }
+
+ flags &= ~RV8803_FLAG_V1F;
+ ret = rv8803_write_reg(client, RV8803_FLAG, flags);
+ mutex_unlock(&rv8803->flags_lock);
+ if (ret)
+ return ret;
+
+ return 0;
+
+ default:
+ return -ENOIOCTLCMD;
+ }
+}
+
+static int rv8803_nvram_write(void *priv, unsigned int offset, void *val,
+ size_t bytes)
+{
+ return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val);
+}
+
+static int rv8803_nvram_read(void *priv, unsigned int offset,
+ void *val, size_t bytes)
+{
+ int ret;
+
+ ret = rv8803_read_reg(priv, RV8803_RAM);
+ if (ret < 0)
+ return ret;
+
+ *(u8 *)val = ret;
+
+ return 0;
+}
+
+static const struct rtc_class_ops rv8803_rtc_ops = {
+ .read_time = rv8803_get_time,
+ .set_time = rv8803_set_time,
+ .ioctl = rv8803_ioctl,
+ .read_alarm = rv8803_get_alarm,
+ .set_alarm = rv8803_set_alarm,
+ .alarm_irq_enable = rv8803_alarm_irq_enable,
+};
+
+static int rx8900_trickle_charger_init(struct rv8803_data *rv8803)
+{
+ struct i2c_client *client = rv8803->client;
+ struct device_node *node = client->dev.of_node;
+ int err;
+ u8 flags;
+
+ if (!node)
+ return 0;
+
+ if (rv8803->type != rx_8900)
+ return 0;
+
+ err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL);
+ if (err < 0)
+ return err;
+
+ flags = (u8)err;
+ flags &= ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF);
+ flags |= rv8803->backup;
+
+ return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL,
+ flags);
+}
+
+/* configure registers with values different than the Power-On reset defaults */
+static int rv8803_regs_configure(struct rv8803_data *rv8803)
+{
+ int err;
+
+ err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA);
+ if (err)
+ return err;
+
+ err = rx8900_trickle_charger_init(rv8803);
+ if (err) {
+ dev_err(&rv8803->client->dev, "failed to init charger\n");
+ return err;
+ }
+
+ return 0;
+}
+
+static const struct i2c_device_id rv8803_id[] = {
+ { "rv8803", rv_8803 },
+ { "rv8804", rx_8804 },
+ { "rx8803", rx_8803 },
+ { "rx8900", rx_8900 },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, rv8803_id);
+
+static int rv8803_probe(struct i2c_client *client)
+{
+ struct i2c_adapter *adapter = client->adapter;
+ struct rv8803_data *rv8803;
+ int err, flags;
+ struct nvmem_config nvmem_cfg = {
+ .name = "rv8803_nvram",
+ .word_size = 1,
+ .stride = 1,
+ .size = 1,
+ .reg_read = rv8803_nvram_read,
+ .reg_write = rv8803_nvram_write,
+ .priv = client,
+ };
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
+ I2C_FUNC_SMBUS_I2C_BLOCK)) {
+ dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n");
+ return -EIO;
+ }
+
+ rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data),
+ GFP_KERNEL);
+ if (!rv8803)
+ return -ENOMEM;
+
+ mutex_init(&rv8803->flags_lock);
+ rv8803->client = client;
+ if (client->dev.of_node) {
+ rv8803->type = (enum rv8803_type)
+ of_device_get_match_data(&client->dev);
+ } else {
+ const struct i2c_device_id *id = i2c_match_id(rv8803_id, client);
+
+ rv8803->type = id->driver_data;
+ }
+ i2c_set_clientdata(client, rv8803);
+
+ flags = rv8803_read_reg(client, RV8803_FLAG);
+ if (flags < 0)
+ return flags;
+
+ if (flags & RV8803_FLAG_V1F)
+ dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n");
+
+ if (flags & RV8803_FLAG_V2F)
+ dev_warn(&client->dev, "Voltage low, data loss detected.\n");
+
+ if (flags & RV8803_FLAG_AF)
+ dev_warn(&client->dev, "An alarm maybe have been missed.\n");
+
+ rv8803->rtc = devm_rtc_allocate_device(&client->dev);
+ if (IS_ERR(rv8803->rtc))
+ return PTR_ERR(rv8803->rtc);
+
+ if (client->irq > 0) {
+ err = devm_request_threaded_irq(&client->dev, client->irq,
+ NULL, rv8803_handle_irq,
+ IRQF_TRIGGER_LOW | IRQF_ONESHOT,
+ "rv8803", client);
+ if (err) {
+ dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
+ client->irq = 0;
+ }
+ }
+ if (!client->irq)
+ clear_bit(RTC_FEATURE_ALARM, rv8803->rtc->features);
+
+ if (of_property_read_bool(client->dev.of_node, "epson,vdet-disable"))
+ rv8803->backup |= RX8900_FLAG_VDETOFF;
+
+ if (of_property_read_bool(client->dev.of_node, "trickle-diode-disable"))
+ rv8803->backup |= RX8900_FLAG_SWOFF;
+
+ err = rv8803_regs_configure(rv8803);
+ if (err)
+ return err;
+
+ rv8803->rtc->ops = &rv8803_rtc_ops;
+ rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
+ rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099;
+ err = devm_rtc_register_device(rv8803->rtc);
+ if (err)
+ return err;
+
+ devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg);
+
+ rv8803->rtc->max_user_freq = 1;
+
+ return 0;
+}
+
+static const __maybe_unused struct of_device_id rv8803_of_match[] = {
+ {
+ .compatible = "microcrystal,rv8803",
+ .data = (void *)rv_8803
+ },
+ {
+ .compatible = "epson,rx8803",
+ .data = (void *)rx_8803
+ },
+ {
+ .compatible = "epson,rx8804",
+ .data = (void *)rx_8804
+ },
+ {
+ .compatible = "epson,rx8900",
+ .data = (void *)rx_8900
+ },
+ { }
+};
+MODULE_DEVICE_TABLE(of, rv8803_of_match);
+
+static struct i2c_driver rv8803_driver = {
+ .driver = {
+ .name = "rtc-rv8803",
+ .of_match_table = of_match_ptr(rv8803_of_match),
+ },
+ .probe_new = rv8803_probe,
+ .id_table = rv8803_id,
+};
+module_i2c_driver(rv8803_driver);
+
+MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
+MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver");
+MODULE_LICENSE("GPL v2");