1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2022 Nuvoton Technology Corporation
5 #include <linux/clk-provider.h>
8 #include <linux/module.h>
10 #include <linux/rtc.h>
11 #include <linux/slab.h>
13 #define NCT3018Y_REG_SC 0x00 /* seconds */
14 #define NCT3018Y_REG_SCA 0x01 /* alarm */
15 #define NCT3018Y_REG_MN 0x02
16 #define NCT3018Y_REG_MNA 0x03 /* alarm */
17 #define NCT3018Y_REG_HR 0x04
18 #define NCT3018Y_REG_HRA 0x05 /* alarm */
19 #define NCT3018Y_REG_DW 0x06
20 #define NCT3018Y_REG_DM 0x07
21 #define NCT3018Y_REG_MO 0x08
22 #define NCT3018Y_REG_YR 0x09
23 #define NCT3018Y_REG_CTRL 0x0A /* timer control */
24 #define NCT3018Y_REG_ST 0x0B /* status */
25 #define NCT3018Y_REG_CLKO 0x0C /* clock out */
27 #define NCT3018Y_BIT_AF BIT(7)
28 #define NCT3018Y_BIT_ST BIT(7)
29 #define NCT3018Y_BIT_DM BIT(6)
30 #define NCT3018Y_BIT_HF BIT(5)
31 #define NCT3018Y_BIT_DSM BIT(4)
32 #define NCT3018Y_BIT_AIE BIT(3)
33 #define NCT3018Y_BIT_OFIE BIT(2)
34 #define NCT3018Y_BIT_CIE BIT(1)
35 #define NCT3018Y_BIT_TWO BIT(0)
37 #define NCT3018Y_REG_BAT_MASK 0x07
38 #define NCT3018Y_REG_CLKO_F_MASK 0x03 /* frequenc mask */
39 #define NCT3018Y_REG_CLKO_CKE 0x80 /* clock out enabled */
42 struct rtc_device *rtc;
43 struct i2c_client *client;
44 #ifdef CONFIG_COMMON_CLK
45 struct clk_hw clkout_hw;
49 static int nct3018y_set_alarm_mode(struct i2c_client *client, bool on)
53 dev_dbg(&client->dev, "%s:on:%d\n", __func__, on);
55 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
58 "Failed to read NCT3018Y_REG_CTRL\n");
63 flags |= NCT3018Y_BIT_AIE;
65 flags &= ~NCT3018Y_BIT_AIE;
67 flags |= NCT3018Y_BIT_CIE;
68 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
70 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
74 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
77 "Failed to read NCT3018Y_REG_ST\n");
81 flags &= ~(NCT3018Y_BIT_AF);
82 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
84 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_ST\n");
91 static int nct3018y_get_alarm_mode(struct i2c_client *client, unsigned char *alarm_enable,
92 unsigned char *alarm_flag)
97 dev_dbg(&client->dev, "%s:NCT3018Y_REG_CTRL\n", __func__);
98 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
101 *alarm_enable = flags & NCT3018Y_BIT_AIE;
105 dev_dbg(&client->dev, "%s:NCT3018Y_REG_ST\n", __func__);
106 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
109 *alarm_flag = flags & NCT3018Y_BIT_AF;
112 dev_dbg(&client->dev, "%s:alarm_enable:%x alarm_flag:%x\n",
113 __func__, *alarm_enable, *alarm_flag);
118 static irqreturn_t nct3018y_irq(int irq, void *dev_id)
120 struct nct3018y *nct3018y = i2c_get_clientdata(dev_id);
121 struct i2c_client *client = nct3018y->client;
123 unsigned char alarm_flag;
124 unsigned char alarm_enable;
126 dev_dbg(&client->dev, "%s:irq:%d\n", __func__, irq);
127 err = nct3018y_get_alarm_mode(nct3018y->client, &alarm_enable, &alarm_flag);
132 dev_dbg(&client->dev, "%s:alarm flag:%x\n",
133 __func__, alarm_flag);
134 rtc_update_irq(nct3018y->rtc, 1, RTC_IRQF | RTC_AF);
135 nct3018y_set_alarm_mode(nct3018y->client, 0);
136 dev_dbg(&client->dev, "%s:IRQ_HANDLED\n", __func__);
144 * In the routines that deal directly with the nct3018y hardware, we use
145 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
147 static int nct3018y_rtc_read_time(struct device *dev, struct rtc_time *tm)
149 struct i2c_client *client = to_i2c_client(dev);
150 unsigned char buf[10];
153 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_ST, 1, buf);
158 dev_dbg(&client->dev, " voltage <=1.7, date/time is not reliable.\n");
162 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SC, sizeof(buf), buf);
166 tm->tm_sec = bcd2bin(buf[0] & 0x7F);
167 tm->tm_min = bcd2bin(buf[2] & 0x7F);
168 tm->tm_hour = bcd2bin(buf[4] & 0x3F);
169 tm->tm_wday = buf[6] & 0x07;
170 tm->tm_mday = bcd2bin(buf[7] & 0x3F);
171 tm->tm_mon = bcd2bin(buf[8] & 0x1F) - 1;
172 tm->tm_year = bcd2bin(buf[9]) + 100;
177 static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
179 struct i2c_client *client = to_i2c_client(dev);
180 unsigned char buf[4] = {0};
183 buf[0] = bin2bcd(tm->tm_sec);
184 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SC, buf[0]);
186 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SC\n");
190 buf[0] = bin2bcd(tm->tm_min);
191 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MN, buf[0]);
193 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MN\n");
197 buf[0] = bin2bcd(tm->tm_hour);
198 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HR, buf[0]);
200 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HR\n");
204 buf[0] = tm->tm_wday & 0x07;
205 buf[1] = bin2bcd(tm->tm_mday);
206 buf[2] = bin2bcd(tm->tm_mon + 1);
207 buf[3] = bin2bcd(tm->tm_year - 100);
208 err = i2c_smbus_write_i2c_block_data(client, NCT3018Y_REG_DW,
211 dev_dbg(&client->dev, "Unable to write for day and mon and year\n");
218 static int nct3018y_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
220 struct i2c_client *client = to_i2c_client(dev);
221 unsigned char buf[5];
224 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SCA,
227 dev_dbg(&client->dev, "Unable to read date\n");
231 dev_dbg(&client->dev, "%s: raw data is sec=%02x, min=%02x hr=%02x\n",
232 __func__, buf[0], buf[2], buf[4]);
234 tm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
235 tm->time.tm_min = bcd2bin(buf[2] & 0x7F);
236 tm->time.tm_hour = bcd2bin(buf[4] & 0x3F);
238 err = nct3018y_get_alarm_mode(client, &tm->enabled, &tm->pending);
242 dev_dbg(&client->dev, "%s:s=%d m=%d, hr=%d, enabled=%d, pending=%d\n",
243 __func__, tm->time.tm_sec, tm->time.tm_min,
244 tm->time.tm_hour, tm->enabled, tm->pending);
249 static int nct3018y_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
251 struct i2c_client *client = to_i2c_client(dev);
254 dev_dbg(dev, "%s, sec=%d, min=%d hour=%d tm->enabled:%d\n",
255 __func__, tm->time.tm_sec, tm->time.tm_min, tm->time.tm_hour,
258 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SCA, bin2bcd(tm->time.tm_sec));
260 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SCA\n");
264 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MNA, bin2bcd(tm->time.tm_min));
266 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MNA\n");
270 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HRA, bin2bcd(tm->time.tm_hour));
272 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HRA\n");
276 return nct3018y_set_alarm_mode(client, tm->enabled);
279 static int nct3018y_irq_enable(struct device *dev, unsigned int enabled)
281 dev_dbg(dev, "%s: alarm enable=%d\n", __func__, enabled);
283 return nct3018y_set_alarm_mode(to_i2c_client(dev), enabled);
286 static int nct3018y_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
288 struct i2c_client *client = to_i2c_client(dev);
289 int status, flags = 0;
293 status = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
297 if (!(status & NCT3018Y_REG_BAT_MASK))
298 flags |= RTC_VL_DATA_INVALID;
300 return put_user(flags, (unsigned int __user *)arg);
307 #ifdef CONFIG_COMMON_CLK
309 * Handling of the clkout
312 #define clkout_hw_to_nct3018y(_hw) container_of(_hw, struct nct3018y, clkout_hw)
314 static const int clkout_rates[] = {
321 static unsigned long nct3018y_clkout_recalc_rate(struct clk_hw *hw,
322 unsigned long parent_rate)
324 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
325 struct i2c_client *client = nct3018y->client;
328 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
332 flags &= NCT3018Y_REG_CLKO_F_MASK;
333 return clkout_rates[flags];
336 static long nct3018y_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
337 unsigned long *prate)
341 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
342 if (clkout_rates[i] <= rate)
343 return clkout_rates[i];
348 static int nct3018y_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
349 unsigned long parent_rate)
351 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
352 struct i2c_client *client = nct3018y->client;
355 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
359 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
360 if (clkout_rates[i] == rate) {
361 flags &= ~NCT3018Y_REG_CLKO_F_MASK;
363 return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
369 static int nct3018y_clkout_control(struct clk_hw *hw, bool enable)
371 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
372 struct i2c_client *client = nct3018y->client;
375 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
380 flags |= NCT3018Y_REG_CLKO_CKE;
382 flags &= ~NCT3018Y_REG_CLKO_CKE;
384 return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
387 static int nct3018y_clkout_prepare(struct clk_hw *hw)
389 return nct3018y_clkout_control(hw, 1);
392 static void nct3018y_clkout_unprepare(struct clk_hw *hw)
394 nct3018y_clkout_control(hw, 0);
397 static int nct3018y_clkout_is_prepared(struct clk_hw *hw)
399 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
400 struct i2c_client *client = nct3018y->client;
403 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
407 return flags & NCT3018Y_REG_CLKO_CKE;
410 static const struct clk_ops nct3018y_clkout_ops = {
411 .prepare = nct3018y_clkout_prepare,
412 .unprepare = nct3018y_clkout_unprepare,
413 .is_prepared = nct3018y_clkout_is_prepared,
414 .recalc_rate = nct3018y_clkout_recalc_rate,
415 .round_rate = nct3018y_clkout_round_rate,
416 .set_rate = nct3018y_clkout_set_rate,
419 static struct clk *nct3018y_clkout_register_clk(struct nct3018y *nct3018y)
421 struct i2c_client *client = nct3018y->client;
422 struct device_node *node = client->dev.of_node;
424 struct clk_init_data init;
426 init.name = "nct3018y-clkout";
427 init.ops = &nct3018y_clkout_ops;
429 init.parent_names = NULL;
430 init.num_parents = 0;
431 nct3018y->clkout_hw.init = &init;
433 /* optional override of the clockname */
434 of_property_read_string(node, "clock-output-names", &init.name);
436 /* register the clock */
437 clk = devm_clk_register(&client->dev, &nct3018y->clkout_hw);
440 of_clk_add_provider(node, of_clk_src_simple_get, clk);
446 static const struct rtc_class_ops nct3018y_rtc_ops = {
447 .read_time = nct3018y_rtc_read_time,
448 .set_time = nct3018y_rtc_set_time,
449 .read_alarm = nct3018y_rtc_read_alarm,
450 .set_alarm = nct3018y_rtc_set_alarm,
451 .alarm_irq_enable = nct3018y_irq_enable,
452 .ioctl = nct3018y_ioctl,
455 static int nct3018y_probe(struct i2c_client *client)
457 struct nct3018y *nct3018y;
460 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
461 I2C_FUNC_SMBUS_BYTE |
462 I2C_FUNC_SMBUS_BLOCK_DATA))
465 nct3018y = devm_kzalloc(&client->dev, sizeof(struct nct3018y),
470 i2c_set_clientdata(client, nct3018y);
471 nct3018y->client = client;
472 device_set_wakeup_capable(&client->dev, 1);
474 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
476 dev_dbg(&client->dev, "%s: read error\n", __func__);
478 } else if (flags & NCT3018Y_BIT_TWO) {
479 dev_dbg(&client->dev, "%s: NCT3018Y_BIT_TWO is set\n", __func__);
482 flags = NCT3018Y_BIT_TWO;
483 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
485 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
490 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
492 dev_dbg(&client->dev, "%s: write error\n", __func__);
496 nct3018y->rtc = devm_rtc_allocate_device(&client->dev);
497 if (IS_ERR(nct3018y->rtc))
498 return PTR_ERR(nct3018y->rtc);
500 nct3018y->rtc->ops = &nct3018y_rtc_ops;
501 nct3018y->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
502 nct3018y->rtc->range_max = RTC_TIMESTAMP_END_2099;
504 if (client->irq > 0) {
505 err = devm_request_threaded_irq(&client->dev, client->irq,
507 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
510 dev_dbg(&client->dev, "unable to request IRQ %d\n", client->irq);
514 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, nct3018y->rtc->features);
515 clear_bit(RTC_FEATURE_ALARM, nct3018y->rtc->features);
518 #ifdef CONFIG_COMMON_CLK
519 /* register clk in common clk framework */
520 nct3018y_clkout_register_clk(nct3018y);
523 return devm_rtc_register_device(nct3018y->rtc);
526 static const struct i2c_device_id nct3018y_id[] = {
530 MODULE_DEVICE_TABLE(i2c, nct3018y_id);
532 static const struct of_device_id nct3018y_of_match[] = {
533 { .compatible = "nuvoton,nct3018y" },
536 MODULE_DEVICE_TABLE(of, nct3018y_of_match);
538 static struct i2c_driver nct3018y_driver = {
540 .name = "rtc-nct3018y",
541 .of_match_table = nct3018y_of_match,
543 .probe = nct3018y_probe,
544 .id_table = nct3018y_id,
547 module_i2c_driver(nct3018y_driver);
549 MODULE_AUTHOR("Medad CChien <ctcchien@nuvoton.com>");
550 MODULE_AUTHOR("Mia Lin <mimi05633@gmail.com>");
551 MODULE_DESCRIPTION("Nuvoton NCT3018Y RTC driver");
552 MODULE_LICENSE("GPL");