diff mbox series

[v2,1/2] rtc: rx8025: implement RX-8035 support

Message ID 20210709044518.28769-2-matt@traverse.com.au
State New
Headers show
Series rtc: Implement support for EPSON RX-8035 | expand

Commit Message

Mathew McBride July 9, 2021, 4:45 a.m. UTC
The RX-8035 is a newer RTC from EPSON that is very
similar to the RX-8025.

The key difference is in the oscillation stop (XSTP)
bit which is inverted on the RX-8035.

Signed-off-by: Mathew McBride <matt@traverse.com.au>

---
 drivers/rtc/rtc-rx8025.c | 58 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 53 insertions(+), 5 deletions(-)

-- 
2.30.1

Comments

Nobuhiro Iwamatsu July 19, 2021, 8:15 a.m. UTC | #1
Hi,

2021年7月9日(金) 13:45 Mathew McBride <matt@traverse.com.au>:
>

> The RX-8035 is a newer RTC from EPSON that is very

> similar to the RX-8025.

>

> The key difference is in the oscillation stop (XSTP)

> bit which is inverted on the RX-8035.

>

> Signed-off-by: Mathew McBride <matt@traverse.com.au>


Reviewed- by: Nobuhiro Iwamatsu <iwamatsu@nigauri.org>

Best regards,
  Nobuhiro

> ---

>  drivers/rtc/rtc-rx8025.c | 58 ++++++++++++++++++++++++++++++++++++----

>  1 file changed, 53 insertions(+), 5 deletions(-)

>

> diff --git a/drivers/rtc/rtc-rx8025.c b/drivers/rtc/rtc-rx8025.c

> index c914091819ba..7a0258fe4b8e 100644

> --- a/drivers/rtc/rtc-rx8025.c

> +++ b/drivers/rtc/rtc-rx8025.c

> @@ -60,14 +60,23 @@

>  #define RX8025_ADJ_DATA_MAX    62

>  #define RX8025_ADJ_DATA_MIN    -62

>

> +enum rx_model {

> +       model_rx_unknown,

> +       model_rx_8025,

> +       model_rx_8035,

> +       model_last

> +};

> +

>  static const struct i2c_device_id rx8025_id[] = {

> -       { "rx8025", 0 },

> +       { "rx8025", model_rx_8025 },

> +       { "rx8035", model_rx_8035 },

>         { }

>  };

>  MODULE_DEVICE_TABLE(i2c, rx8025_id);

>

>  struct rx8025_data {

>         struct rtc_device *rtc;

> +       enum rx_model model;

>         u8 ctrl1;

>  };

>

> @@ -100,10 +109,26 @@ static s32 rx8025_write_regs(const struct i2c_client *client,

>                                               length, values);

>  }

>

> +static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)

> +{

> +       int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;

> +       /* XSTP bit has different polarity on RX-8025 vs RX-8035.

> +        * RX-8025: 0 == oscillator stopped

> +        * RX-8035: 1 == oscillator stopped

> +        */

> +

> +       if (model == model_rx_8025)

> +               xstp = !xstp;

> +

> +       return xstp;

> +}

> +

>  static int rx8025_check_validity(struct device *dev)

>  {

>         struct i2c_client *client = to_i2c_client(dev);

> +       struct rx8025_data *drvdata = dev_get_drvdata(dev);

>         int ctrl2;

> +       int xstp;

>

>         ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);

>         if (ctrl2 < 0)

> @@ -117,7 +142,8 @@ static int rx8025_check_validity(struct device *dev)

>                 return -EINVAL;

>         }

>

> -       if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {

> +       xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);

> +       if (xstp) {

>                 dev_warn(dev, "crystal stopped, date is invalid\n");

>                 return -EINVAL;

>         }

> @@ -127,6 +153,7 @@ static int rx8025_check_validity(struct device *dev)

>

>  static int rx8025_reset_validity(struct i2c_client *client)

>  {

> +       struct rx8025_data *drvdata = i2c_get_clientdata(client);

>         int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);

>

>         if (ctrl2 < 0)

> @@ -134,22 +161,28 @@ static int rx8025_reset_validity(struct i2c_client *client)

>

>         ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);

>

> +       if (drvdata->model == model_rx_8025)

> +               ctrl2 |= RX8025_BIT_CTRL2_XST;

> +       else

> +               ctrl2 &= ~(RX8025_BIT_CTRL2_XST);

> +

>         return rx8025_write_reg(client, RX8025_REG_CTRL2,

> -                               ctrl2 | RX8025_BIT_CTRL2_XST);

> +                               ctrl2);

>  }

>

>  static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)

>  {

>         struct i2c_client *client = dev_id;

>         struct rx8025_data *rx8025 = i2c_get_clientdata(client);

> -       int status;

> +       int status, xstp;

>

>         rtc_lock(rx8025->rtc);

>         status = rx8025_read_reg(client, RX8025_REG_CTRL2);

>         if (status < 0)

>                 goto out;

>

> -       if (!(status & RX8025_BIT_CTRL2_XST))

> +       xstp = rx8025_is_osc_stopped(rx8025->model, status);

> +       if (xstp)

>                 dev_warn(&client->dev, "Oscillation stop was detected,"

>                          "you may have to readjust the clock\n");

>

> @@ -519,6 +552,21 @@ static int rx8025_probe(struct i2c_client *client,

>

>         i2c_set_clientdata(client, rx8025);

>

> +       if (id) {

> +               rx8025->model = id->driver_data;

> +               switch (rx8025->model) {

> +               case model_rx_8025:

> +                       dev_info(&client->dev, "Model RX-8025");

> +                       break;

> +               case model_rx_8035:

> +                       dev_info(&client->dev, "Model RX-8035");

> +                       break;

> +               default:

> +                       dev_warn(&client->dev, "Unknown model: %d\n", rx8025->model);

> +                       break;

> +               }

> +       }

> +

>         err = rx8025_init_client(client);

>         if (err)

>                 return err;

> --

> 2.30.1

>



-- 
Nobuhiro Iwamatsu
   iwamatsu at {nigauri.org / debian.org}
   GPG ID: 40AD1FA6
diff mbox series

Patch

diff --git a/drivers/rtc/rtc-rx8025.c b/drivers/rtc/rtc-rx8025.c
index c914091819ba..7a0258fe4b8e 100644
--- a/drivers/rtc/rtc-rx8025.c
+++ b/drivers/rtc/rtc-rx8025.c
@@ -60,14 +60,23 @@ 
 #define RX8025_ADJ_DATA_MAX	62
 #define RX8025_ADJ_DATA_MIN	-62
 
+enum rx_model {
+	model_rx_unknown,
+	model_rx_8025,
+	model_rx_8035,
+	model_last
+};
+
 static const struct i2c_device_id rx8025_id[] = {
-	{ "rx8025", 0 },
+	{ "rx8025", model_rx_8025 },
+	{ "rx8035", model_rx_8035 },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, rx8025_id);
 
 struct rx8025_data {
 	struct rtc_device *rtc;
+	enum rx_model model;
 	u8 ctrl1;
 };
 
@@ -100,10 +109,26 @@  static s32 rx8025_write_regs(const struct i2c_client *client,
 					      length, values);
 }
 
+static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
+{
+	int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
+	/* XSTP bit has different polarity on RX-8025 vs RX-8035.
+	 * RX-8025: 0 == oscillator stopped
+	 * RX-8035: 1 == oscillator stopped
+	 */
+
+	if (model == model_rx_8025)
+		xstp = !xstp;
+
+	return xstp;
+}
+
 static int rx8025_check_validity(struct device *dev)
 {
 	struct i2c_client *client = to_i2c_client(dev);
+	struct rx8025_data *drvdata = dev_get_drvdata(dev);
 	int ctrl2;
+	int xstp;
 
 	ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
 	if (ctrl2 < 0)
@@ -117,7 +142,8 @@  static int rx8025_check_validity(struct device *dev)
 		return -EINVAL;
 	}
 
-	if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
+	xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
+	if (xstp) {
 		dev_warn(dev, "crystal stopped, date is invalid\n");
 		return -EINVAL;
 	}
@@ -127,6 +153,7 @@  static int rx8025_check_validity(struct device *dev)
 
 static int rx8025_reset_validity(struct i2c_client *client)
 {
+	struct rx8025_data *drvdata = i2c_get_clientdata(client);
 	int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
 
 	if (ctrl2 < 0)
@@ -134,22 +161,28 @@  static int rx8025_reset_validity(struct i2c_client *client)
 
 	ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
 
+	if (drvdata->model == model_rx_8025)
+		ctrl2 |= RX8025_BIT_CTRL2_XST;
+	else
+		ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
+
 	return rx8025_write_reg(client, RX8025_REG_CTRL2,
-				ctrl2 | RX8025_BIT_CTRL2_XST);
+				ctrl2);
 }
 
 static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
 {
 	struct i2c_client *client = dev_id;
 	struct rx8025_data *rx8025 = i2c_get_clientdata(client);
-	int status;
+	int status, xstp;
 
 	rtc_lock(rx8025->rtc);
 	status = rx8025_read_reg(client, RX8025_REG_CTRL2);
 	if (status < 0)
 		goto out;
 
-	if (!(status & RX8025_BIT_CTRL2_XST))
+	xstp = rx8025_is_osc_stopped(rx8025->model, status);
+	if (xstp)
 		dev_warn(&client->dev, "Oscillation stop was detected,"
 			 "you may have to readjust the clock\n");
 
@@ -519,6 +552,21 @@  static int rx8025_probe(struct i2c_client *client,
 
 	i2c_set_clientdata(client, rx8025);
 
+	if (id) {
+		rx8025->model = id->driver_data;
+		switch (rx8025->model) {
+		case model_rx_8025:
+			dev_info(&client->dev, "Model RX-8025");
+			break;
+		case model_rx_8035:
+			dev_info(&client->dev, "Model RX-8035");
+			break;
+		default:
+			dev_warn(&client->dev, "Unknown model: %d\n", rx8025->model);
+			break;
+		}
+	}
+
 	err = rx8025_init_client(client);
 	if (err)
 		return err;