Loading...
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2022 Nuvoton Technology Corporation
3
4#include <linux/bcd.h>
5#include <linux/clk-provider.h>
6#include <linux/err.h>
7#include <linux/i2c.h>
8#include <linux/module.h>
9#include <linux/of.h>
10#include <linux/rtc.h>
11#include <linux/slab.h>
12
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 */
26#define NCT3018Y_REG_PART 0x21 /* part info */
27
28#define NCT3018Y_BIT_AF BIT(7)
29#define NCT3018Y_BIT_ST BIT(7)
30#define NCT3018Y_BIT_DM BIT(6)
31#define NCT3018Y_BIT_HF BIT(5)
32#define NCT3018Y_BIT_DSM BIT(4)
33#define NCT3018Y_BIT_AIE BIT(3)
34#define NCT3018Y_BIT_OFIE BIT(2)
35#define NCT3018Y_BIT_CIE BIT(1)
36#define NCT3018Y_BIT_TWO BIT(0)
37
38#define NCT3018Y_REG_BAT_MASK 0x07
39#define NCT3018Y_REG_CLKO_F_MASK 0x03 /* frequenc mask */
40#define NCT3018Y_REG_CLKO_CKE 0x80 /* clock out enabled */
41#define NCT3018Y_REG_PART_NCT3018Y 0x02
42
43struct nct3018y {
44 struct rtc_device *rtc;
45 struct i2c_client *client;
46 int part_num;
47#ifdef CONFIG_COMMON_CLK
48 struct clk_hw clkout_hw;
49#endif
50};
51
52static int nct3018y_set_alarm_mode(struct i2c_client *client, bool on)
53{
54 int err, flags;
55
56 dev_dbg(&client->dev, "%s:on:%d\n", __func__, on);
57
58 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
59 if (flags < 0) {
60 dev_dbg(&client->dev,
61 "Failed to read NCT3018Y_REG_CTRL\n");
62 return flags;
63 }
64
65 if (on)
66 flags |= NCT3018Y_BIT_AIE;
67 else
68 flags &= ~NCT3018Y_BIT_AIE;
69
70 flags |= NCT3018Y_BIT_CIE;
71 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
72 if (err < 0) {
73 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
74 return err;
75 }
76
77 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
78 if (flags < 0) {
79 dev_dbg(&client->dev,
80 "Failed to read NCT3018Y_REG_ST\n");
81 return flags;
82 }
83
84 flags &= ~(NCT3018Y_BIT_AF);
85 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
86 if (err < 0) {
87 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_ST\n");
88 return err;
89 }
90
91 return 0;
92}
93
94static int nct3018y_get_alarm_mode(struct i2c_client *client, unsigned char *alarm_enable,
95 unsigned char *alarm_flag)
96{
97 int flags;
98
99 if (alarm_enable) {
100 dev_dbg(&client->dev, "%s:NCT3018Y_REG_CTRL\n", __func__);
101 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
102 if (flags < 0)
103 return flags;
104 *alarm_enable = flags & NCT3018Y_BIT_AIE;
105 }
106
107 if (alarm_flag) {
108 dev_dbg(&client->dev, "%s:NCT3018Y_REG_ST\n", __func__);
109 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
110 if (flags < 0)
111 return flags;
112 *alarm_flag = flags & NCT3018Y_BIT_AF;
113 }
114
115 dev_dbg(&client->dev, "%s:alarm_enable:%x alarm_flag:%x\n",
116 __func__, *alarm_enable, *alarm_flag);
117
118 return 0;
119}
120
121static irqreturn_t nct3018y_irq(int irq, void *dev_id)
122{
123 struct nct3018y *nct3018y = i2c_get_clientdata(dev_id);
124 struct i2c_client *client = nct3018y->client;
125 int err;
126 unsigned char alarm_flag;
127 unsigned char alarm_enable;
128
129 dev_dbg(&client->dev, "%s:irq:%d\n", __func__, irq);
130 err = nct3018y_get_alarm_mode(nct3018y->client, &alarm_enable, &alarm_flag);
131 if (err)
132 return IRQ_NONE;
133
134 if (alarm_flag) {
135 dev_dbg(&client->dev, "%s:alarm flag:%x\n",
136 __func__, alarm_flag);
137 rtc_update_irq(nct3018y->rtc, 1, RTC_IRQF | RTC_AF);
138 nct3018y_set_alarm_mode(nct3018y->client, 0);
139 dev_dbg(&client->dev, "%s:IRQ_HANDLED\n", __func__);
140 return IRQ_HANDLED;
141 }
142
143 return IRQ_NONE;
144}
145
146/*
147 * In the routines that deal directly with the nct3018y hardware, we use
148 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
149 */
150static int nct3018y_rtc_read_time(struct device *dev, struct rtc_time *tm)
151{
152 struct i2c_client *client = to_i2c_client(dev);
153 unsigned char buf[10];
154 int err;
155
156 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_ST, 1, buf);
157 if (err < 0)
158 return err;
159
160 if (!buf[0]) {
161 dev_dbg(&client->dev, " voltage <=1.7, date/time is not reliable.\n");
162 return -EINVAL;
163 }
164
165 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SC, sizeof(buf), buf);
166 if (err < 0)
167 return err;
168
169 tm->tm_sec = bcd2bin(buf[0] & 0x7F);
170 tm->tm_min = bcd2bin(buf[2] & 0x7F);
171 tm->tm_hour = bcd2bin(buf[4] & 0x3F);
172 tm->tm_wday = buf[6] & 0x07;
173 tm->tm_mday = bcd2bin(buf[7] & 0x3F);
174 tm->tm_mon = bcd2bin(buf[8] & 0x1F) - 1;
175 tm->tm_year = bcd2bin(buf[9]) + 100;
176
177 return 0;
178}
179
180static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
181{
182 struct i2c_client *client = to_i2c_client(dev);
183 struct nct3018y *nct3018y = dev_get_drvdata(dev);
184 unsigned char buf[4] = {0};
185 int err, flags;
186 int restore_flags = 0;
187
188 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
189 if (flags < 0) {
190 dev_dbg(&client->dev, "Failed to read NCT3018Y_REG_CTRL.\n");
191 return flags;
192 }
193
194 /* Check and set TWO bit */
195 if (nct3018y->part_num == NCT3018Y_REG_PART_NCT3018Y && !(flags & NCT3018Y_BIT_TWO)) {
196 restore_flags = 1;
197 flags |= NCT3018Y_BIT_TWO;
198 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
199 if (err < 0) {
200 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL.\n");
201 return err;
202 }
203 }
204
205 buf[0] = bin2bcd(tm->tm_sec);
206 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SC, buf[0]);
207 if (err < 0) {
208 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SC\n");
209 return err;
210 }
211
212 buf[0] = bin2bcd(tm->tm_min);
213 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MN, buf[0]);
214 if (err < 0) {
215 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MN\n");
216 return err;
217 }
218
219 buf[0] = bin2bcd(tm->tm_hour);
220 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HR, buf[0]);
221 if (err < 0) {
222 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HR\n");
223 return err;
224 }
225
226 buf[0] = tm->tm_wday & 0x07;
227 buf[1] = bin2bcd(tm->tm_mday);
228 buf[2] = bin2bcd(tm->tm_mon + 1);
229 buf[3] = bin2bcd(tm->tm_year - 100);
230 err = i2c_smbus_write_i2c_block_data(client, NCT3018Y_REG_DW,
231 sizeof(buf), buf);
232 if (err < 0) {
233 dev_dbg(&client->dev, "Unable to write for day and mon and year\n");
234 return -EIO;
235 }
236
237 /* Restore TWO bit */
238 if (restore_flags) {
239 if (nct3018y->part_num == NCT3018Y_REG_PART_NCT3018Y)
240 flags &= ~NCT3018Y_BIT_TWO;
241
242 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
243 if (err < 0) {
244 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL.\n");
245 return err;
246 }
247 }
248
249 return err;
250}
251
252static int nct3018y_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
253{
254 struct i2c_client *client = to_i2c_client(dev);
255 unsigned char buf[5];
256 int err;
257
258 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SCA,
259 sizeof(buf), buf);
260 if (err < 0) {
261 dev_dbg(&client->dev, "Unable to read date\n");
262 return -EIO;
263 }
264
265 dev_dbg(&client->dev, "%s: raw data is sec=%02x, min=%02x hr=%02x\n",
266 __func__, buf[0], buf[2], buf[4]);
267
268 tm->time.tm_sec = bcd2bin(buf[0] & 0x7F);
269 tm->time.tm_min = bcd2bin(buf[2] & 0x7F);
270 tm->time.tm_hour = bcd2bin(buf[4] & 0x3F);
271
272 err = nct3018y_get_alarm_mode(client, &tm->enabled, &tm->pending);
273 if (err < 0)
274 return err;
275
276 dev_dbg(&client->dev, "%s:s=%d m=%d, hr=%d, enabled=%d, pending=%d\n",
277 __func__, tm->time.tm_sec, tm->time.tm_min,
278 tm->time.tm_hour, tm->enabled, tm->pending);
279
280 return 0;
281}
282
283static int nct3018y_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
284{
285 struct i2c_client *client = to_i2c_client(dev);
286 int err;
287
288 dev_dbg(dev, "%s, sec=%d, min=%d hour=%d tm->enabled:%d\n",
289 __func__, tm->time.tm_sec, tm->time.tm_min, tm->time.tm_hour,
290 tm->enabled);
291
292 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SCA, bin2bcd(tm->time.tm_sec));
293 if (err < 0) {
294 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SCA\n");
295 return err;
296 }
297
298 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MNA, bin2bcd(tm->time.tm_min));
299 if (err < 0) {
300 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MNA\n");
301 return err;
302 }
303
304 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HRA, bin2bcd(tm->time.tm_hour));
305 if (err < 0) {
306 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HRA\n");
307 return err;
308 }
309
310 return nct3018y_set_alarm_mode(client, tm->enabled);
311}
312
313static int nct3018y_irq_enable(struct device *dev, unsigned int enabled)
314{
315 dev_dbg(dev, "%s: alarm enable=%d\n", __func__, enabled);
316
317 return nct3018y_set_alarm_mode(to_i2c_client(dev), enabled);
318}
319
320static int nct3018y_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
321{
322 struct i2c_client *client = to_i2c_client(dev);
323 int status, flags = 0;
324
325 switch (cmd) {
326 case RTC_VL_READ:
327 status = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
328 if (status < 0)
329 return status;
330
331 if (!(status & NCT3018Y_REG_BAT_MASK))
332 flags |= RTC_VL_DATA_INVALID;
333
334 return put_user(flags, (unsigned int __user *)arg);
335
336 default:
337 return -ENOIOCTLCMD;
338 }
339}
340
341#ifdef CONFIG_COMMON_CLK
342/*
343 * Handling of the clkout
344 */
345
346#define clkout_hw_to_nct3018y(_hw) container_of(_hw, struct nct3018y, clkout_hw)
347
348static const int clkout_rates[] = {
349 32768,
350 1024,
351 32,
352 1,
353};
354
355static unsigned long nct3018y_clkout_recalc_rate(struct clk_hw *hw,
356 unsigned long parent_rate)
357{
358 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
359 struct i2c_client *client = nct3018y->client;
360 int flags;
361
362 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
363 if (flags < 0)
364 return 0;
365
366 flags &= NCT3018Y_REG_CLKO_F_MASK;
367 return clkout_rates[flags];
368}
369
370static long nct3018y_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
371 unsigned long *prate)
372{
373 int i;
374
375 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
376 if (clkout_rates[i] <= rate)
377 return clkout_rates[i];
378
379 return 0;
380}
381
382static int nct3018y_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
383 unsigned long parent_rate)
384{
385 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
386 struct i2c_client *client = nct3018y->client;
387 int i, flags;
388
389 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
390 if (flags < 0)
391 return flags;
392
393 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
394 if (clkout_rates[i] == rate) {
395 flags &= ~NCT3018Y_REG_CLKO_F_MASK;
396 flags |= i;
397 return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
398 }
399
400 return -EINVAL;
401}
402
403static int nct3018y_clkout_control(struct clk_hw *hw, bool enable)
404{
405 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
406 struct i2c_client *client = nct3018y->client;
407 int flags;
408
409 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
410 if (flags < 0)
411 return flags;
412
413 if (enable)
414 flags |= NCT3018Y_REG_CLKO_CKE;
415 else
416 flags &= ~NCT3018Y_REG_CLKO_CKE;
417
418 return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
419}
420
421static int nct3018y_clkout_prepare(struct clk_hw *hw)
422{
423 return nct3018y_clkout_control(hw, 1);
424}
425
426static void nct3018y_clkout_unprepare(struct clk_hw *hw)
427{
428 nct3018y_clkout_control(hw, 0);
429}
430
431static int nct3018y_clkout_is_prepared(struct clk_hw *hw)
432{
433 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
434 struct i2c_client *client = nct3018y->client;
435 int flags;
436
437 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
438 if (flags < 0)
439 return flags;
440
441 return flags & NCT3018Y_REG_CLKO_CKE;
442}
443
444static const struct clk_ops nct3018y_clkout_ops = {
445 .prepare = nct3018y_clkout_prepare,
446 .unprepare = nct3018y_clkout_unprepare,
447 .is_prepared = nct3018y_clkout_is_prepared,
448 .recalc_rate = nct3018y_clkout_recalc_rate,
449 .round_rate = nct3018y_clkout_round_rate,
450 .set_rate = nct3018y_clkout_set_rate,
451};
452
453static struct clk *nct3018y_clkout_register_clk(struct nct3018y *nct3018y)
454{
455 struct i2c_client *client = nct3018y->client;
456 struct device_node *node = client->dev.of_node;
457 struct clk *clk;
458 struct clk_init_data init;
459
460 init.name = "nct3018y-clkout";
461 init.ops = &nct3018y_clkout_ops;
462 init.flags = 0;
463 init.parent_names = NULL;
464 init.num_parents = 0;
465 nct3018y->clkout_hw.init = &init;
466
467 /* optional override of the clockname */
468 of_property_read_string(node, "clock-output-names", &init.name);
469
470 /* register the clock */
471 clk = devm_clk_register(&client->dev, &nct3018y->clkout_hw);
472
473 if (!IS_ERR(clk))
474 of_clk_add_provider(node, of_clk_src_simple_get, clk);
475
476 return clk;
477}
478#endif
479
480static const struct rtc_class_ops nct3018y_rtc_ops = {
481 .read_time = nct3018y_rtc_read_time,
482 .set_time = nct3018y_rtc_set_time,
483 .read_alarm = nct3018y_rtc_read_alarm,
484 .set_alarm = nct3018y_rtc_set_alarm,
485 .alarm_irq_enable = nct3018y_irq_enable,
486 .ioctl = nct3018y_ioctl,
487};
488
489static int nct3018y_probe(struct i2c_client *client)
490{
491 struct nct3018y *nct3018y;
492 int err, flags;
493
494 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
495 I2C_FUNC_SMBUS_BYTE |
496 I2C_FUNC_SMBUS_BLOCK_DATA))
497 return -ENODEV;
498
499 nct3018y = devm_kzalloc(&client->dev, sizeof(struct nct3018y),
500 GFP_KERNEL);
501 if (!nct3018y)
502 return -ENOMEM;
503
504 i2c_set_clientdata(client, nct3018y);
505 nct3018y->client = client;
506 device_set_wakeup_capable(&client->dev, 1);
507
508 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
509 if (flags < 0) {
510 dev_dbg(&client->dev, "%s: read error\n", __func__);
511 return flags;
512 } else if (flags & NCT3018Y_BIT_TWO) {
513 dev_dbg(&client->dev, "%s: NCT3018Y_BIT_TWO is set\n", __func__);
514 }
515
516 nct3018y->part_num = i2c_smbus_read_byte_data(client, NCT3018Y_REG_PART);
517 if (nct3018y->part_num < 0) {
518 dev_dbg(&client->dev, "Failed to read NCT3018Y_REG_PART.\n");
519 return nct3018y->part_num;
520 } else if (nct3018y->part_num == NCT3018Y_REG_PART_NCT3018Y) {
521 flags = NCT3018Y_BIT_HF;
522 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
523 if (err < 0) {
524 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL.\n");
525 return err;
526 }
527 }
528
529 flags = 0;
530 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
531 if (err < 0) {
532 dev_dbg(&client->dev, "%s: write error\n", __func__);
533 return err;
534 }
535
536 nct3018y->rtc = devm_rtc_allocate_device(&client->dev);
537 if (IS_ERR(nct3018y->rtc))
538 return PTR_ERR(nct3018y->rtc);
539
540 nct3018y->rtc->ops = &nct3018y_rtc_ops;
541 nct3018y->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
542 nct3018y->rtc->range_max = RTC_TIMESTAMP_END_2099;
543
544 if (client->irq > 0) {
545 err = devm_request_threaded_irq(&client->dev, client->irq,
546 NULL, nct3018y_irq,
547 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
548 "nct3018y", client);
549 if (err) {
550 dev_dbg(&client->dev, "unable to request IRQ %d\n", client->irq);
551 return err;
552 }
553 } else {
554 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, nct3018y->rtc->features);
555 clear_bit(RTC_FEATURE_ALARM, nct3018y->rtc->features);
556 }
557
558#ifdef CONFIG_COMMON_CLK
559 /* register clk in common clk framework */
560 nct3018y_clkout_register_clk(nct3018y);
561#endif
562
563 return devm_rtc_register_device(nct3018y->rtc);
564}
565
566static const struct i2c_device_id nct3018y_id[] = {
567 { "nct3018y", 0 },
568 { }
569};
570MODULE_DEVICE_TABLE(i2c, nct3018y_id);
571
572static const struct of_device_id nct3018y_of_match[] = {
573 { .compatible = "nuvoton,nct3018y" },
574 {}
575};
576MODULE_DEVICE_TABLE(of, nct3018y_of_match);
577
578static struct i2c_driver nct3018y_driver = {
579 .driver = {
580 .name = "rtc-nct3018y",
581 .of_match_table = nct3018y_of_match,
582 },
583 .probe = nct3018y_probe,
584 .id_table = nct3018y_id,
585};
586
587module_i2c_driver(nct3018y_driver);
588
589MODULE_AUTHOR("Medad CChien <ctcchien@nuvoton.com>");
590MODULE_AUTHOR("Mia Lin <mimi05633@gmail.com>");
591MODULE_DESCRIPTION("Nuvoton NCT3018Y RTC driver");
592MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2022 Nuvoton Technology Corporation
3
4#include <linux/bcd.h>
5#include <linux/clk-provider.h>
6#include <linux/err.h>
7#include <linux/i2c.h>
8#include <linux/module.h>
9#include <linux/of.h>
10#include <linux/rtc.h>
11#include <linux/slab.h>
12
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 */
26
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)
36
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 */
40
41struct nct3018y {
42 struct rtc_device *rtc;
43 struct i2c_client *client;
44#ifdef CONFIG_COMMON_CLK
45 struct clk_hw clkout_hw;
46#endif
47};
48
49static int nct3018y_set_alarm_mode(struct i2c_client *client, bool on)
50{
51 int err, flags;
52
53 dev_dbg(&client->dev, "%s:on:%d\n", __func__, on);
54
55 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
56 if (flags < 0) {
57 dev_dbg(&client->dev,
58 "Failed to read NCT3018Y_REG_CTRL\n");
59 return flags;
60 }
61
62 if (on)
63 flags |= NCT3018Y_BIT_AIE;
64 else
65 flags &= ~NCT3018Y_BIT_AIE;
66
67 flags |= NCT3018Y_BIT_CIE;
68 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
69 if (err < 0) {
70 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
71 return err;
72 }
73
74 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
75 if (flags < 0) {
76 dev_dbg(&client->dev,
77 "Failed to read NCT3018Y_REG_ST\n");
78 return flags;
79 }
80
81 flags &= ~(NCT3018Y_BIT_AF);
82 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
83 if (err < 0) {
84 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_ST\n");
85 return err;
86 }
87
88 return 0;
89}
90
91static int nct3018y_get_alarm_mode(struct i2c_client *client, unsigned char *alarm_enable,
92 unsigned char *alarm_flag)
93{
94 int flags;
95
96 if (alarm_enable) {
97 dev_dbg(&client->dev, "%s:NCT3018Y_REG_CTRL\n", __func__);
98 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
99 if (flags < 0)
100 return flags;
101 *alarm_enable = flags & NCT3018Y_BIT_AIE;
102 }
103
104 if (alarm_flag) {
105 dev_dbg(&client->dev, "%s:NCT3018Y_REG_ST\n", __func__);
106 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
107 if (flags < 0)
108 return flags;
109 *alarm_flag = flags & NCT3018Y_BIT_AF;
110 }
111
112 dev_dbg(&client->dev, "%s:alarm_enable:%x alarm_flag:%x\n",
113 __func__, *alarm_enable, *alarm_flag);
114
115 return 0;
116}
117
118static irqreturn_t nct3018y_irq(int irq, void *dev_id)
119{
120 struct nct3018y *nct3018y = i2c_get_clientdata(dev_id);
121 struct i2c_client *client = nct3018y->client;
122 int err;
123 unsigned char alarm_flag;
124 unsigned char alarm_enable;
125
126 dev_dbg(&client->dev, "%s:irq:%d\n", __func__, irq);
127 err = nct3018y_get_alarm_mode(nct3018y->client, &alarm_enable, &alarm_flag);
128 if (err)
129 return IRQ_NONE;
130
131 if (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__);
137 return IRQ_HANDLED;
138 }
139
140 return IRQ_NONE;
141}
142
143/*
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.
146 */
147static int nct3018y_rtc_read_time(struct device *dev, struct rtc_time *tm)
148{
149 struct i2c_client *client = to_i2c_client(dev);
150 unsigned char buf[10];
151 int err;
152
153 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_ST, 1, buf);
154 if (err < 0)
155 return err;
156
157 if (!buf[0]) {
158 dev_dbg(&client->dev, " voltage <=1.7, date/time is not reliable.\n");
159 return -EINVAL;
160 }
161
162 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SC, sizeof(buf), buf);
163 if (err < 0)
164 return err;
165
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;
173
174 return 0;
175}
176
177static int nct3018y_rtc_set_time(struct device *dev, struct rtc_time *tm)
178{
179 struct i2c_client *client = to_i2c_client(dev);
180 unsigned char buf[4] = {0};
181 int err;
182
183 buf[0] = bin2bcd(tm->tm_sec);
184 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SC, buf[0]);
185 if (err < 0) {
186 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SC\n");
187 return err;
188 }
189
190 buf[0] = bin2bcd(tm->tm_min);
191 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MN, buf[0]);
192 if (err < 0) {
193 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MN\n");
194 return err;
195 }
196
197 buf[0] = bin2bcd(tm->tm_hour);
198 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HR, buf[0]);
199 if (err < 0) {
200 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HR\n");
201 return err;
202 }
203
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,
209 sizeof(buf), buf);
210 if (err < 0) {
211 dev_dbg(&client->dev, "Unable to write for day and mon and year\n");
212 return -EIO;
213 }
214
215 return err;
216}
217
218static int nct3018y_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
219{
220 struct i2c_client *client = to_i2c_client(dev);
221 unsigned char buf[5];
222 int err;
223
224 err = i2c_smbus_read_i2c_block_data(client, NCT3018Y_REG_SCA,
225 sizeof(buf), buf);
226 if (err < 0) {
227 dev_dbg(&client->dev, "Unable to read date\n");
228 return -EIO;
229 }
230
231 dev_dbg(&client->dev, "%s: raw data is sec=%02x, min=%02x hr=%02x\n",
232 __func__, buf[0], buf[2], buf[4]);
233
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);
237
238 err = nct3018y_get_alarm_mode(client, &tm->enabled, &tm->pending);
239 if (err < 0)
240 return err;
241
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);
245
246 return 0;
247}
248
249static int nct3018y_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
250{
251 struct i2c_client *client = to_i2c_client(dev);
252 int err;
253
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,
256 tm->enabled);
257
258 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_SCA, bin2bcd(tm->time.tm_sec));
259 if (err < 0) {
260 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_SCA\n");
261 return err;
262 }
263
264 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_MNA, bin2bcd(tm->time.tm_min));
265 if (err < 0) {
266 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_MNA\n");
267 return err;
268 }
269
270 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_HRA, bin2bcd(tm->time.tm_hour));
271 if (err < 0) {
272 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_HRA\n");
273 return err;
274 }
275
276 return nct3018y_set_alarm_mode(client, tm->enabled);
277}
278
279static int nct3018y_irq_enable(struct device *dev, unsigned int enabled)
280{
281 dev_dbg(dev, "%s: alarm enable=%d\n", __func__, enabled);
282
283 return nct3018y_set_alarm_mode(to_i2c_client(dev), enabled);
284}
285
286static int nct3018y_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
287{
288 struct i2c_client *client = to_i2c_client(dev);
289 int status, flags = 0;
290
291 switch (cmd) {
292 case RTC_VL_READ:
293 status = i2c_smbus_read_byte_data(client, NCT3018Y_REG_ST);
294 if (status < 0)
295 return status;
296
297 if (!(status & NCT3018Y_REG_BAT_MASK))
298 flags |= RTC_VL_DATA_INVALID;
299
300 return put_user(flags, (unsigned int __user *)arg);
301
302 default:
303 return -ENOIOCTLCMD;
304 }
305}
306
307#ifdef CONFIG_COMMON_CLK
308/*
309 * Handling of the clkout
310 */
311
312#define clkout_hw_to_nct3018y(_hw) container_of(_hw, struct nct3018y, clkout_hw)
313
314static const int clkout_rates[] = {
315 32768,
316 1024,
317 32,
318 1,
319};
320
321static unsigned long nct3018y_clkout_recalc_rate(struct clk_hw *hw,
322 unsigned long parent_rate)
323{
324 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
325 struct i2c_client *client = nct3018y->client;
326 int flags;
327
328 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
329 if (flags < 0)
330 return 0;
331
332 flags &= NCT3018Y_REG_CLKO_F_MASK;
333 return clkout_rates[flags];
334}
335
336static long nct3018y_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
337 unsigned long *prate)
338{
339 int i;
340
341 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
342 if (clkout_rates[i] <= rate)
343 return clkout_rates[i];
344
345 return 0;
346}
347
348static int nct3018y_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
349 unsigned long parent_rate)
350{
351 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
352 struct i2c_client *client = nct3018y->client;
353 int i, flags;
354
355 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
356 if (flags < 0)
357 return flags;
358
359 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
360 if (clkout_rates[i] == rate) {
361 flags &= ~NCT3018Y_REG_CLKO_F_MASK;
362 flags |= i;
363 return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
364 }
365
366 return -EINVAL;
367}
368
369static int nct3018y_clkout_control(struct clk_hw *hw, bool enable)
370{
371 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
372 struct i2c_client *client = nct3018y->client;
373 int flags;
374
375 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
376 if (flags < 0)
377 return flags;
378
379 if (enable)
380 flags |= NCT3018Y_REG_CLKO_CKE;
381 else
382 flags &= ~NCT3018Y_REG_CLKO_CKE;
383
384 return i2c_smbus_write_byte_data(client, NCT3018Y_REG_CLKO, flags);
385}
386
387static int nct3018y_clkout_prepare(struct clk_hw *hw)
388{
389 return nct3018y_clkout_control(hw, 1);
390}
391
392static void nct3018y_clkout_unprepare(struct clk_hw *hw)
393{
394 nct3018y_clkout_control(hw, 0);
395}
396
397static int nct3018y_clkout_is_prepared(struct clk_hw *hw)
398{
399 struct nct3018y *nct3018y = clkout_hw_to_nct3018y(hw);
400 struct i2c_client *client = nct3018y->client;
401 int flags;
402
403 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CLKO);
404 if (flags < 0)
405 return flags;
406
407 return flags & NCT3018Y_REG_CLKO_CKE;
408}
409
410static 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,
417};
418
419static struct clk *nct3018y_clkout_register_clk(struct nct3018y *nct3018y)
420{
421 struct i2c_client *client = nct3018y->client;
422 struct device_node *node = client->dev.of_node;
423 struct clk *clk;
424 struct clk_init_data init;
425
426 init.name = "nct3018y-clkout";
427 init.ops = &nct3018y_clkout_ops;
428 init.flags = 0;
429 init.parent_names = NULL;
430 init.num_parents = 0;
431 nct3018y->clkout_hw.init = &init;
432
433 /* optional override of the clockname */
434 of_property_read_string(node, "clock-output-names", &init.name);
435
436 /* register the clock */
437 clk = devm_clk_register(&client->dev, &nct3018y->clkout_hw);
438
439 if (!IS_ERR(clk))
440 of_clk_add_provider(node, of_clk_src_simple_get, clk);
441
442 return clk;
443}
444#endif
445
446static 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,
453};
454
455static int nct3018y_probe(struct i2c_client *client)
456{
457 struct nct3018y *nct3018y;
458 int err, flags;
459
460 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
461 I2C_FUNC_SMBUS_BYTE |
462 I2C_FUNC_SMBUS_BLOCK_DATA))
463 return -ENODEV;
464
465 nct3018y = devm_kzalloc(&client->dev, sizeof(struct nct3018y),
466 GFP_KERNEL);
467 if (!nct3018y)
468 return -ENOMEM;
469
470 i2c_set_clientdata(client, nct3018y);
471 nct3018y->client = client;
472 device_set_wakeup_capable(&client->dev, 1);
473
474 flags = i2c_smbus_read_byte_data(client, NCT3018Y_REG_CTRL);
475 if (flags < 0) {
476 dev_dbg(&client->dev, "%s: read error\n", __func__);
477 return flags;
478 } else if (flags & NCT3018Y_BIT_TWO) {
479 dev_dbg(&client->dev, "%s: NCT3018Y_BIT_TWO is set\n", __func__);
480 }
481
482 flags = NCT3018Y_BIT_TWO;
483 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_CTRL, flags);
484 if (err < 0) {
485 dev_dbg(&client->dev, "Unable to write NCT3018Y_REG_CTRL\n");
486 return err;
487 }
488
489 flags = 0;
490 err = i2c_smbus_write_byte_data(client, NCT3018Y_REG_ST, flags);
491 if (err < 0) {
492 dev_dbg(&client->dev, "%s: write error\n", __func__);
493 return err;
494 }
495
496 nct3018y->rtc = devm_rtc_allocate_device(&client->dev);
497 if (IS_ERR(nct3018y->rtc))
498 return PTR_ERR(nct3018y->rtc);
499
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;
503
504 if (client->irq > 0) {
505 err = devm_request_threaded_irq(&client->dev, client->irq,
506 NULL, nct3018y_irq,
507 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
508 "nct3018y", client);
509 if (err) {
510 dev_dbg(&client->dev, "unable to request IRQ %d\n", client->irq);
511 return err;
512 }
513 } else {
514 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, nct3018y->rtc->features);
515 clear_bit(RTC_FEATURE_ALARM, nct3018y->rtc->features);
516 }
517
518#ifdef CONFIG_COMMON_CLK
519 /* register clk in common clk framework */
520 nct3018y_clkout_register_clk(nct3018y);
521#endif
522
523 return devm_rtc_register_device(nct3018y->rtc);
524}
525
526static const struct i2c_device_id nct3018y_id[] = {
527 { "nct3018y", 0 },
528 { }
529};
530MODULE_DEVICE_TABLE(i2c, nct3018y_id);
531
532static const struct of_device_id nct3018y_of_match[] = {
533 { .compatible = "nuvoton,nct3018y" },
534 {}
535};
536MODULE_DEVICE_TABLE(of, nct3018y_of_match);
537
538static struct i2c_driver nct3018y_driver = {
539 .driver = {
540 .name = "rtc-nct3018y",
541 .of_match_table = of_match_ptr(nct3018y_of_match),
542 },
543 .probe_new = nct3018y_probe,
544 .id_table = nct3018y_id,
545};
546
547module_i2c_driver(nct3018y_driver);
548
549MODULE_AUTHOR("Medad CChien <ctcchien@nuvoton.com>");
550MODULE_AUTHOR("Mia Lin <mimi05633@gmail.com>");
551MODULE_DESCRIPTION("Nuvoton NCT3018Y RTC driver");
552MODULE_LICENSE("GPL");