Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2022 Nuvoton Technology Corporation
0003 
0004 #include <linux/bcd.h>
0005 #include <linux/clk-provider.h>
0006 #include <linux/err.h>
0007 #include <linux/i2c.h>
0008 #include <linux/module.h>
0009 #include <linux/of.h>
0010 #include <linux/rtc.h>
0011 #include <linux/slab.h>
0012 
0013 #define NCT3018Y_REG_SC     0x00 /* seconds */
0014 #define NCT3018Y_REG_SCA    0x01 /* alarm */
0015 #define NCT3018Y_REG_MN     0x02
0016 #define NCT3018Y_REG_MNA    0x03 /* alarm */
0017 #define NCT3018Y_REG_HR     0x04
0018 #define NCT3018Y_REG_HRA    0x05 /* alarm */
0019 #define NCT3018Y_REG_DW     0x06
0020 #define NCT3018Y_REG_DM     0x07
0021 #define NCT3018Y_REG_MO     0x08
0022 #define NCT3018Y_REG_YR     0x09
0023 #define NCT3018Y_REG_CTRL   0x0A /* timer control */
0024 #define NCT3018Y_REG_ST     0x0B /* status */
0025 #define NCT3018Y_REG_CLKO   0x0C /* clock out */
0026 
0027 #define NCT3018Y_BIT_AF     BIT(7)
0028 #define NCT3018Y_BIT_ST     BIT(7)
0029 #define NCT3018Y_BIT_DM     BIT(6)
0030 #define NCT3018Y_BIT_HF     BIT(5)
0031 #define NCT3018Y_BIT_DSM    BIT(4)
0032 #define NCT3018Y_BIT_AIE    BIT(3)
0033 #define NCT3018Y_BIT_OFIE   BIT(2)
0034 #define NCT3018Y_BIT_CIE    BIT(1)
0035 #define NCT3018Y_BIT_TWO    BIT(0)
0036 
0037 #define NCT3018Y_REG_BAT_MASK       0x07
0038 #define NCT3018Y_REG_CLKO_F_MASK    0x03 /* frequenc mask */
0039 #define NCT3018Y_REG_CLKO_CKE       0x80 /* clock out enabled */
0040 
0041 struct nct3018y {
0042     struct rtc_device *rtc;
0043     struct i2c_client *client;
0044 #ifdef CONFIG_COMMON_CLK
0045     struct clk_hw clkout_hw;
0046 #endif
0047 };
0048 
0049 static int nct3018y_set_alarm_mode(struct i2c_client *client, bool on)
0050 {
0051     int err, flags;
0052 
0053     dev_dbg(&client->dev, "%s:on:%d\n", __func__, on);
0054 
0055     flags =  i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
0056     if (flags < 0) {
0057         dev_dbg(&client->dev,
0058             "Failed to read NCT3018Y_REG_CTRL\n");
0059         return flags;
0060     }
0061 
0062     if (on)
0063         flags |= NCT3018Y_BIT_AIE;
0064     else
0065         flags &= ~NCT3018Y_BIT_AIE;
0066 
0067     flags |= NCT3018Y_BIT_CIE;
0068     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
0069     if (err < 0) {
0070         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
0071         return err;
0072     }
0073 
0074     flags =  i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
0075     if (flags < 0) {
0076         dev_dbg(&client->dev,
0077             "Failed to read NCT3018Y_REG_ST\n");
0078         return flags;
0079     }
0080 
0081     flags &= ~(NCT3018Y_BIT_AF);
0082     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
0083     if (err < 0) {
0084         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_ST\n");
0085         return err;
0086     }
0087 
0088     return 0;
0089 }
0090 
0091 static int nct3018y_get_alarm_mode(struct i2c_client *client, unsigned char *alarm_enable,
0092                    unsigned char *alarm_flag)
0093 {
0094     int flags;
0095 
0096     if (alarm_enable) {
0097         dev_dbg(&client->dev, "%s:NCT3018Y_REG_CTRL\n", __func__);
0098         flags =  i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
0099         if (flags < 0)
0100             return flags;
0101         *alarm_enable = flags & NCT3018Y_BIT_AIE;
0102     }
0103 
0104     if (alarm_flag) {
0105         dev_dbg(&client->dev, "%s:NCT3018Y_REG_ST\n", __func__);
0106         flags =  i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
0107         if (flags < 0)
0108             return flags;
0109         *alarm_flag = flags & NCT3018Y_BIT_AF;
0110     }
0111 
0112     dev_dbg(&client->dev, "%s:alarm_enable:%x alarm_flag:%x\n",
0113         __func__, *alarm_enable, *alarm_flag);
0114 
0115     return 0;
0116 }
0117 
0118 static irqreturn_t nct3018y_irq(int irq, void *dev_id)
0119 {
0120     struct nct3018y *nct3018y = i2c_get_clientdata(dev_id);
0121     struct i2c_client *client = nct3018y->client;
0122     int err;
0123     unsigned char alarm_flag;
0124     unsigned char alarm_enable;
0125 
0126     dev_dbg(&client->dev, "%s:irq:%d\n", __func__, irq);
0127     err = nct3018y_get_alarm_mode(nct3018y->client, &alarm_enable, &alarm_flag);
0128     if (err)
0129         return IRQ_NONE;
0130 
0131     if (alarm_flag) {
0132         dev_dbg(&client->dev, "%s:alarm flag:%x\n",
0133             __func__, alarm_flag);
0134         rtc_update_irq(nct3018y->rtc, 1, RTC_IRQF | RTC_AF);
0135         nct3018y_set_alarm_mode(nct3018y->client, 0);
0136         dev_dbg(&client->dev, "%s:IRQ_HANDLED\n", __func__);
0137         return IRQ_HANDLED;
0138     }
0139 
0140     return IRQ_NONE;
0141 }
0142 
0143 /*
0144  * In the routines that deal directly with the nct3018y hardware, we use
0145  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
0146  */
0147 static int nct3018y_rtc_read_time(struct device *dev, struct rtc_time *tm)
0148 {
0149     struct i2c_client *client = to_i2c_client(dev);
0150     unsigned char buf[10];
0151     int err;
0152 
0153     err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_ST, 1, buf);
0154     if (err < 0)
0155         return err;
0156 
0157     if (!buf[0]) {
0158         dev_dbg(&client->dev, " voltage <=1.7, date/time is not reliable.\n");
0159         return -EINVAL;
0160     }
0161 
0162     err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SC, sizeof(buf), buf);
0163     if (err < 0)
0164         return err;
0165 
0166     tm->tm_sec = bcd2bin(buf[0] & 0x7F);
0167     tm->tm_min = bcd2bin(buf[2] & 0x7F);
0168     tm->tm_hour = bcd2bin(buf[4] & 0x3F);
0169     tm->tm_wday = buf[6] & 0x07;
0170     tm->tm_mday = bcd2bin(buf[7] & 0x3F);
0171     tm->tm_mon = bcd2bin(buf[8] & 0x1F) - 1;
0172     tm->tm_year = bcd2bin(buf[9]) + 100;
0173 
0174     return 0;
0175 }
0176 
0177 static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
0178 {
0179     struct i2c_client *client = to_i2c_client(dev);
0180     unsigned char buf[4] = {0};
0181     int err;
0182 
0183     buf[0] = bin2bcd(tm->tm_sec);
0184     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SC, buf[0]);
0185     if (err < 0) {
0186         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SC\n");
0187         return err;
0188     }
0189 
0190     buf[0] = bin2bcd(tm->tm_min);
0191     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MN, buf[0]);
0192     if (err < 0) {
0193         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MN\n");
0194         return err;
0195     }
0196 
0197     buf[0] = bin2bcd(tm->tm_hour);
0198     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HR, buf[0]);
0199     if (err < 0) {
0200         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HR\n");
0201         return err;
0202     }
0203 
0204     buf[0] = tm->tm_wday & 0x07;
0205     buf[1] = bin2bcd(tm->tm_mday);
0206     buf[2] = bin2bcd(tm->tm_mon + 1);
0207     buf[3] = bin2bcd(tm->tm_year - 100);
0208     err = i2c_smbus_write_i2c_block_data(client, NCT3018Y_REG_DW,
0209                          sizeof(buf), buf);
0210     if (err < 0) {
0211         dev_dbg(&client->dev, "Unable to write for day and mon and year\n");
0212         return -EIO;
0213     }
0214 
0215     return err;
0216 }
0217 
0218 static int nct3018y_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
0219 {
0220     struct i2c_client *client = to_i2c_client(dev);
0221     unsigned char buf[5];
0222     int err;
0223 
0224     err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SCA,
0225                         sizeof(buf), buf);
0226     if (err < 0) {
0227         dev_dbg(&client->dev, "Unable to read date\n");
0228         return -EIO;
0229     }
0230 
0231     dev_dbg(&client->dev, "%s: raw data is sec=%02x, min=%02x hr=%02x\n",
0232         __func__, buf[0], buf[2], buf[4]);
0233 
0234     tm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
0235     tm->time.tm_min = bcd2bin(buf[2] & 0x7F);
0236     tm->time.tm_hour = bcd2bin(buf[4] & 0x3F);
0237 
0238     err = nct3018y_get_alarm_mode(client, &tm->enabled, &tm->pending);
0239     if (err < 0)
0240         return err;
0241 
0242     dev_dbg(&client->dev, "%s:s=%d m=%d, hr=%d, enabled=%d, pending=%d\n",
0243         __func__, tm->time.tm_sec, tm->time.tm_min,
0244         tm->time.tm_hour, tm->enabled, tm->pending);
0245 
0246     return 0;
0247 }
0248 
0249 static int nct3018y_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
0250 {
0251     struct i2c_client *client = to_i2c_client(dev);
0252     int err;
0253 
0254     dev_dbg(dev, "%s, sec=%d, min=%d hour=%d tm->enabled:%d\n",
0255         __func__, tm->time.tm_sec, tm->time.tm_min, tm->time.tm_hour,
0256         tm->enabled);
0257 
0258     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SCA, bin2bcd(tm->time.tm_sec));
0259     if (err < 0) {
0260         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SCA\n");
0261         return err;
0262     }
0263 
0264     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MNA, bin2bcd(tm->time.tm_min));
0265     if (err < 0) {
0266         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MNA\n");
0267         return err;
0268     }
0269 
0270     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HRA, bin2bcd(tm->time.tm_hour));
0271     if (err < 0) {
0272         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HRA\n");
0273         return err;
0274     }
0275 
0276     return nct3018y_set_alarm_mode(client, tm->enabled);
0277 }
0278 
0279 static int nct3018y_irq_enable(struct device *dev, unsigned int enabled)
0280 {
0281     dev_dbg(dev, "%s: alarm enable=%d\n", __func__, enabled);
0282 
0283     return nct3018y_set_alarm_mode(to_i2c_client(dev), enabled);
0284 }
0285 
0286 static int nct3018y_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
0287 {
0288     struct i2c_client *client = to_i2c_client(dev);
0289     int status, flags = 0;
0290 
0291     switch (cmd) {
0292     case RTC_VL_READ:
0293         status = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
0294         if (status < 0)
0295             return status;
0296 
0297         if (!(status & NCT3018Y_REG_BAT_MASK))
0298             flags |= RTC_VL_DATA_INVALID;
0299 
0300         return put_user(flags, (unsigned int __user *)arg);
0301 
0302     default:
0303         return -ENOIOCTLCMD;
0304     }
0305 }
0306 
0307 #ifdef CONFIG_COMMON_CLK
0308 /*
0309  * Handling of the clkout
0310  */
0311 
0312 #define clkout_hw_to_nct3018y(_hw) container_of(_hw, struct nct3018y, clkout_hw)
0313 
0314 static const int clkout_rates[] = {
0315     32768,
0316     1024,
0317     32,
0318     1,
0319 };
0320 
0321 static unsigned long nct3018y_clkout_recalc_rate(struct clk_hw *hw,
0322                          unsigned long parent_rate)
0323 {
0324     struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
0325     struct i2c_client *client = nct3018y->client;
0326     int flags;
0327 
0328     flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
0329     if (flags < 0)
0330         return 0;
0331 
0332     flags &= NCT3018Y_REG_CLKO_F_MASK;
0333     return clkout_rates[flags];
0334 }
0335 
0336 static long nct3018y_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
0337                        unsigned long *prate)
0338 {
0339     int i;
0340 
0341     for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
0342         if (clkout_rates[i] <= rate)
0343             return clkout_rates[i];
0344 
0345     return 0;
0346 }
0347 
0348 static int nct3018y_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
0349                     unsigned long parent_rate)
0350 {
0351     struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
0352     struct i2c_client *client = nct3018y->client;
0353     int i, flags;
0354 
0355     flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
0356     if (flags < 0)
0357         return flags;
0358 
0359     for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
0360         if (clkout_rates[i] == rate) {
0361             flags &= ~NCT3018Y_REG_CLKO_F_MASK;
0362             flags |= i;
0363             return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
0364         }
0365 
0366     return -EINVAL;
0367 }
0368 
0369 static int nct3018y_clkout_control(struct clk_hw *hw, bool enable)
0370 {
0371     struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
0372     struct i2c_client *client = nct3018y->client;
0373     int flags;
0374 
0375     flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
0376     if (flags < 0)
0377         return flags;
0378 
0379     if (enable)
0380         flags |= NCT3018Y_REG_CLKO_CKE;
0381     else
0382         flags &= ~NCT3018Y_REG_CLKO_CKE;
0383 
0384     return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
0385 }
0386 
0387 static int nct3018y_clkout_prepare(struct clk_hw *hw)
0388 {
0389     return nct3018y_clkout_control(hw, 1);
0390 }
0391 
0392 static void nct3018y_clkout_unprepare(struct clk_hw *hw)
0393 {
0394     nct3018y_clkout_control(hw, 0);
0395 }
0396 
0397 static int nct3018y_clkout_is_prepared(struct clk_hw *hw)
0398 {
0399     struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
0400     struct i2c_client *client = nct3018y->client;
0401     int flags;
0402 
0403     flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
0404     if (flags < 0)
0405         return flags;
0406 
0407     return flags & NCT3018Y_REG_CLKO_CKE;
0408 }
0409 
0410 static const struct clk_ops nct3018y_clkout_ops = {
0411     .prepare = nct3018y_clkout_prepare,
0412     .unprepare = nct3018y_clkout_unprepare,
0413     .is_prepared = nct3018y_clkout_is_prepared,
0414     .recalc_rate = nct3018y_clkout_recalc_rate,
0415     .round_rate = nct3018y_clkout_round_rate,
0416     .set_rate = nct3018y_clkout_set_rate,
0417 };
0418 
0419 static struct clk *nct3018y_clkout_register_clk(struct nct3018y *nct3018y)
0420 {
0421     struct i2c_client *client = nct3018y->client;
0422     struct device_node *node = client->dev.of_node;
0423     struct clk *clk;
0424     struct clk_init_data init;
0425 
0426     init.name = "nct3018y-clkout";
0427     init.ops = &nct3018y_clkout_ops;
0428     init.flags = 0;
0429     init.parent_names = NULL;
0430     init.num_parents = 0;
0431     nct3018y->clkout_hw.init = &init;
0432 
0433     /* optional override of the clockname */
0434     of_property_read_string(node, "clock-output-names", &init.name);
0435 
0436     /* register the clock */
0437     clk = devm_clk_register(&client->dev, &nct3018y->clkout_hw);
0438 
0439     if (!IS_ERR(clk))
0440         of_clk_add_provider(node, of_clk_src_simple_get, clk);
0441 
0442     return clk;
0443 }
0444 #endif
0445 
0446 static const struct rtc_class_ops nct3018y_rtc_ops = {
0447     .read_time  = nct3018y_rtc_read_time,
0448     .set_time   = nct3018y_rtc_set_time,
0449     .read_alarm = nct3018y_rtc_read_alarm,
0450     .set_alarm  = nct3018y_rtc_set_alarm,
0451     .alarm_irq_enable = nct3018y_irq_enable,
0452     .ioctl      = nct3018y_ioctl,
0453 };
0454 
0455 static int nct3018y_probe(struct i2c_client *client,
0456               const struct i2c_device_id *id)
0457 {
0458     struct nct3018y *nct3018y;
0459     int err, flags;
0460 
0461     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
0462                      I2C_FUNC_SMBUS_BYTE |
0463                      I2C_FUNC_SMBUS_BLOCK_DATA))
0464         return -ENODEV;
0465 
0466     nct3018y = devm_kzalloc(&client->dev, sizeof(struct nct3018y),
0467                 GFP_KERNEL);
0468     if (!nct3018y)
0469         return -ENOMEM;
0470 
0471     i2c_set_clientdata(client, nct3018y);
0472     nct3018y->client = client;
0473     device_set_wakeup_capable(&client->dev, 1);
0474 
0475     flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
0476     if (flags < 0) {
0477         dev_dbg(&client->dev, "%s: read error\n", __func__);
0478         return flags;
0479     } else if (flags & NCT3018Y_BIT_TWO) {
0480         dev_dbg(&client->dev, "%s: NCT3018Y_BIT_TWO is set\n", __func__);
0481     }
0482 
0483     flags = NCT3018Y_BIT_TWO;
0484     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
0485     if (err < 0) {
0486         dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
0487         return err;
0488     }
0489 
0490     flags = 0;
0491     err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
0492     if (err < 0) {
0493         dev_dbg(&client->dev, "%s: write error\n", __func__);
0494         return err;
0495     }
0496 
0497     nct3018y->rtc = devm_rtc_allocate_device(&client->dev);
0498     if (IS_ERR(nct3018y->rtc))
0499         return PTR_ERR(nct3018y->rtc);
0500 
0501     nct3018y->rtc->ops = &nct3018y_rtc_ops;
0502     nct3018y->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
0503     nct3018y->rtc->range_max = RTC_TIMESTAMP_END_2099;
0504 
0505     if (client->irq > 0) {
0506         err = devm_request_threaded_irq(&client->dev, client->irq,
0507                         NULL, nct3018y_irq,
0508                         IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
0509                         "nct3018y", client);
0510         if (err) {
0511             dev_dbg(&client->dev, "unable to request IRQ %d\n", client->irq);
0512             return err;
0513         }
0514     } else {
0515         clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, nct3018y->rtc->features);
0516         clear_bit(RTC_FEATURE_ALARM, nct3018y->rtc->features);
0517     }
0518 
0519 #ifdef CONFIG_COMMON_CLK
0520     /* register clk in common clk framework */
0521     nct3018y_clkout_register_clk(nct3018y);
0522 #endif
0523 
0524     return devm_rtc_register_device(nct3018y->rtc);
0525 }
0526 
0527 static const struct i2c_device_id nct3018y_id[] = {
0528     { "nct3018y", 0 },
0529     { }
0530 };
0531 MODULE_DEVICE_TABLE(i2c, nct3018y_id);
0532 
0533 static const struct of_device_id nct3018y_of_match[] = {
0534     { .compatible = "nuvoton,nct3018y" },
0535     {}
0536 };
0537 MODULE_DEVICE_TABLE(of, nct3018y_of_match);
0538 
0539 static struct i2c_driver nct3018y_driver = {
0540     .driver     = {
0541         .name   = "rtc-nct3018y",
0542         .of_match_table = of_match_ptr(nct3018y_of_match),
0543     },
0544     .probe      = nct3018y_probe,
0545     .id_table   = nct3018y_id,
0546 };
0547 
0548 module_i2c_driver(nct3018y_driver);
0549 
0550 MODULE_AUTHOR("Medad CChien <ctcchien@nuvoton.com>");
0551 MODULE_AUTHOR("Mia Lin <mimi05633@gmail.com>");
0552 MODULE_DESCRIPTION("Nuvoton NCT3018Y RTC driver");
0553 MODULE_LICENSE("GPL");