Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:05

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup shared_tod_mcp7940m_rtc
0007  *
0008  * @brief This file provides the interfaces of @ref shared_tod_mcp7940m_rtc.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2023 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 /*
0037  * Only 24 hour format is supported. If this driver is the only ones who write
0038  * the RTC, that shouldn't be a problem.
0039  *
0040  * The weekday register is not used. It has a user-defined representation anyway
0041  * and therefore doesn't really matter.
0042  */
0043 
0044 #include <libchip/mcp7940m-rtc.h>
0045 
0046 #define REG_MCP7940M_RTCSEC 0x00u
0047 #define REG_MCP7940M_RTCMIN 0x01u
0048 #define REG_MCP7940M_RTCHOUR 0x02u
0049 #define REG_MCP7940M_RTCWKDAY 0x03u
0050 #define REG_MCP7940M_RTCDATE 0x04u
0051 #define REG_MCP7940M_RTCMTH 0x05u
0052 #define REG_MCP7940M_RTCYEAR 0x06u
0053 
0054 #define MCP7940M_RTCSEC_ST   (0x01u << 7)
0055 #define MCP7940M_RTCMTH_LPYR   (0x01u << 5)
0056 
0057 #define REG_MCP7940M_CONTROL 0x07
0058 
0059 #define MCP7940M_CONTROL_OUT     (0x1u << 7)
0060 #define MCP7940M_CONTROL_SQWEN   (0x1u << 6)
0061 #define MCP7940M_CONTROL_ALM1EN  (0x1u << 5)
0062 #define MCP7940M_CONTROL_ALM0EN  (0x1u << 4)
0063 #define MCP7940M_CONTROL_EXTOSC  (0x1u << 3)
0064 #define MCP7940M_CONTROL_CRSTRIM (0x1u << 2)
0065 #define MCP7940M_CONTROL_SQWFS1  (0x1u << 1)
0066 #define MCP7940M_CONTROL_SQWFS0  (0x1u << 0)
0067 
0068 int mcp7940m_hw_init(struct i2c_rtc_base *base)
0069 {
0070   uint8_t reg;
0071   ssize_t rv;
0072   struct mcp7940m_rtc *ctx =
0073       RTEMS_CONTAINER_OF(base, struct mcp7940m_rtc, base);
0074 
0075   /*
0076    * Make sure that all alarms and outputs are disabled. Enable or disable
0077    * oscillator.
0078    *
0079    * This makes sure that we can start with an uninitialized device that has a
0080    * random value in the control register.
0081    */
0082   reg = 0;
0083   if (!ctx->crystal) {
0084     reg |= MCP7940M_CONTROL_EXTOSC;
0085   }
0086   rv = i2c_rtc_write(&ctx->base, REG_MCP7940M_CONTROL, &reg, 1);
0087 
0088   if (rv == 0 && ctx->crystal) {
0089     rv = i2c_rtc_read(&ctx->base, REG_MCP7940M_RTCSEC, &reg, 1);
0090     if (rv == 0 && (reg & MCP7940M_RTCSEC_ST) == 0) {
0091       reg |= MCP7940M_RTCSEC_ST;
0092       rv = i2c_rtc_write(&ctx->base, REG_MCP7940M_RTCSEC, &reg, 1);
0093     }
0094   }
0095 
0096   return rv;
0097 }