Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* rtc-ds1343.c
3 *
4 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
5 * Real Time Clock
6 *
7 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
8 * Ankur Srivastava <sankurece@gmail.com> : DS1343 Nvram Support
9 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/device.h>
15#include <linux/spi/spi.h>
16#include <linux/regmap.h>
17#include <linux/rtc.h>
18#include <linux/bcd.h>
19#include <linux/pm.h>
20#include <linux/pm_wakeirq.h>
21#include <linux/slab.h>
22
23#define DALLAS_MAXIM_DS1343 0
24#define DALLAS_MAXIM_DS1344 1
25
26/* RTC DS1343 Registers */
27#define DS1343_SECONDS_REG 0x00
28#define DS1343_MINUTES_REG 0x01
29#define DS1343_HOURS_REG 0x02
30#define DS1343_DAY_REG 0x03
31#define DS1343_DATE_REG 0x04
32#define DS1343_MONTH_REG 0x05
33#define DS1343_YEAR_REG 0x06
34#define DS1343_ALM0_SEC_REG 0x07
35#define DS1343_ALM0_MIN_REG 0x08
36#define DS1343_ALM0_HOUR_REG 0x09
37#define DS1343_ALM0_DAY_REG 0x0A
38#define DS1343_ALM1_SEC_REG 0x0B
39#define DS1343_ALM1_MIN_REG 0x0C
40#define DS1343_ALM1_HOUR_REG 0x0D
41#define DS1343_ALM1_DAY_REG 0x0E
42#define DS1343_CONTROL_REG 0x0F
43#define DS1343_STATUS_REG 0x10
44#define DS1343_TRICKLE_REG 0x11
45#define DS1343_NVRAM 0x20
46
47#define DS1343_NVRAM_LEN 96
48
49/* DS1343 Control Registers bits */
50#define DS1343_EOSC 0x80
51#define DS1343_DOSF 0x20
52#define DS1343_EGFIL 0x10
53#define DS1343_SQW 0x08
54#define DS1343_INTCN 0x04
55#define DS1343_A1IE 0x02
56#define DS1343_A0IE 0x01
57
58/* DS1343 Status Registers bits */
59#define DS1343_OSF 0x80
60#define DS1343_IRQF1 0x02
61#define DS1343_IRQF0 0x01
62
63/* DS1343 Trickle Charger Registers bits */
64#define DS1343_TRICKLE_MAGIC 0xa0
65#define DS1343_TRICKLE_DS1 0x08
66#define DS1343_TRICKLE_1K 0x01
67#define DS1343_TRICKLE_2K 0x02
68#define DS1343_TRICKLE_4K 0x03
69
70static const struct spi_device_id ds1343_id[] = {
71 { "ds1343", DALLAS_MAXIM_DS1343 },
72 { "ds1344", DALLAS_MAXIM_DS1344 },
73 { }
74};
75MODULE_DEVICE_TABLE(spi, ds1343_id);
76
77struct ds1343_priv {
78 struct rtc_device *rtc;
79 struct regmap *map;
80 int irq;
81};
82
83static ssize_t ds1343_show_glitchfilter(struct device *dev,
84 struct device_attribute *attr, char *buf)
85{
86 struct ds1343_priv *priv = dev_get_drvdata(dev->parent);
87 int glitch_filt_status, data;
88 int res;
89
90 res = regmap_read(priv->map, DS1343_CONTROL_REG, &data);
91 if (res)
92 return res;
93
94 glitch_filt_status = !!(data & DS1343_EGFIL);
95
96 if (glitch_filt_status)
97 return sprintf(buf, "enabled\n");
98 else
99 return sprintf(buf, "disabled\n");
100}
101
102static ssize_t ds1343_store_glitchfilter(struct device *dev,
103 struct device_attribute *attr,
104 const char *buf, size_t count)
105{
106 struct ds1343_priv *priv = dev_get_drvdata(dev->parent);
107 int data = 0;
108 int res;
109
110 if (strncmp(buf, "enabled", 7) == 0)
111 data = DS1343_EGFIL;
112 else if (strncmp(buf, "disabled", 8))
113 return -EINVAL;
114
115 res = regmap_update_bits(priv->map, DS1343_CONTROL_REG,
116 DS1343_EGFIL, data);
117 if (res)
118 return res;
119
120 return count;
121}
122
123static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
124 ds1343_store_glitchfilter);
125
126static int ds1343_nvram_write(void *priv, unsigned int off, void *val,
127 size_t bytes)
128{
129 struct ds1343_priv *ds1343 = priv;
130
131 return regmap_bulk_write(ds1343->map, DS1343_NVRAM + off, val, bytes);
132}
133
134static int ds1343_nvram_read(void *priv, unsigned int off, void *val,
135 size_t bytes)
136{
137 struct ds1343_priv *ds1343 = priv;
138
139 return regmap_bulk_read(ds1343->map, DS1343_NVRAM + off, val, bytes);
140}
141
142static ssize_t ds1343_show_tricklecharger(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
145 struct ds1343_priv *priv = dev_get_drvdata(dev->parent);
146 int res, data;
147 char *diodes = "disabled", *resistors = " ";
148
149 res = regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
150 if (res)
151 return res;
152
153 if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
154 switch (data & 0x0c) {
155 case DS1343_TRICKLE_DS1:
156 diodes = "one diode,";
157 break;
158
159 default:
160 diodes = "no diode,";
161 break;
162 }
163
164 switch (data & 0x03) {
165 case DS1343_TRICKLE_1K:
166 resistors = "1k Ohm";
167 break;
168
169 case DS1343_TRICKLE_2K:
170 resistors = "2k Ohm";
171 break;
172
173 case DS1343_TRICKLE_4K:
174 resistors = "4k Ohm";
175 break;
176
177 default:
178 diodes = "disabled";
179 break;
180 }
181 }
182
183 return sprintf(buf, "%s %s\n", diodes, resistors);
184}
185
186static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
187
188static struct attribute *ds1343_attrs[] = {
189 &dev_attr_glitch_filter.attr,
190 &dev_attr_trickle_charger.attr,
191 NULL
192};
193
194static const struct attribute_group ds1343_attr_group = {
195 .attrs = ds1343_attrs,
196};
197
198static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
199{
200 struct ds1343_priv *priv = dev_get_drvdata(dev);
201 unsigned char buf[7];
202 int res;
203
204 res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
205 if (res)
206 return res;
207
208 dt->tm_sec = bcd2bin(buf[0]);
209 dt->tm_min = bcd2bin(buf[1]);
210 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
211 dt->tm_wday = bcd2bin(buf[3]) - 1;
212 dt->tm_mday = bcd2bin(buf[4]);
213 dt->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
214 dt->tm_year = bcd2bin(buf[6]) + 100; /* year offset from 1900 */
215
216 return 0;
217}
218
219static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
220{
221 struct ds1343_priv *priv = dev_get_drvdata(dev);
222 u8 buf[7];
223
224 buf[0] = bin2bcd(dt->tm_sec);
225 buf[1] = bin2bcd(dt->tm_min);
226 buf[2] = bin2bcd(dt->tm_hour) & 0x3F;
227 buf[3] = bin2bcd(dt->tm_wday + 1);
228 buf[4] = bin2bcd(dt->tm_mday);
229 buf[5] = bin2bcd(dt->tm_mon + 1);
230 buf[6] = bin2bcd(dt->tm_year - 100);
231
232 return regmap_bulk_write(priv->map, DS1343_SECONDS_REG,
233 buf, sizeof(buf));
234}
235
236static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
237{
238 struct ds1343_priv *priv = dev_get_drvdata(dev);
239 unsigned char buf[4];
240 unsigned int val;
241 int res;
242
243 if (priv->irq <= 0)
244 return -EINVAL;
245
246 res = regmap_read(priv->map, DS1343_STATUS_REG, &val);
247 if (res)
248 return res;
249
250 alarm->pending = !!(val & DS1343_IRQF0);
251
252 res = regmap_read(priv->map, DS1343_CONTROL_REG, &val);
253 if (res)
254 return res;
255 alarm->enabled = !!(val & DS1343_A0IE);
256
257 res = regmap_bulk_read(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
258 if (res)
259 return res;
260
261 alarm->time.tm_sec = bcd2bin(buf[0]) & 0x7f;
262 alarm->time.tm_min = bcd2bin(buf[1]) & 0x7f;
263 alarm->time.tm_hour = bcd2bin(buf[2]) & 0x3f;
264 alarm->time.tm_mday = bcd2bin(buf[3]) & 0x3f;
265
266 return 0;
267}
268
269static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
270{
271 struct ds1343_priv *priv = dev_get_drvdata(dev);
272 unsigned char buf[4];
273 int res = 0;
274
275 if (priv->irq <= 0)
276 return -EINVAL;
277
278 res = regmap_update_bits(priv->map, DS1343_CONTROL_REG, DS1343_A0IE, 0);
279 if (res)
280 return res;
281
282 buf[0] = bin2bcd(alarm->time.tm_sec);
283 buf[1] = bin2bcd(alarm->time.tm_min);
284 buf[2] = bin2bcd(alarm->time.tm_hour);
285 buf[3] = bin2bcd(alarm->time.tm_mday);
286
287 res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
288 if (res)
289 return res;
290
291 if (alarm->enabled)
292 res = regmap_update_bits(priv->map, DS1343_CONTROL_REG,
293 DS1343_A0IE, DS1343_A0IE);
294
295 return res;
296}
297
298static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
299{
300 struct ds1343_priv *priv = dev_get_drvdata(dev);
301
302 if (priv->irq <= 0)
303 return -EINVAL;
304
305 return regmap_update_bits(priv->map, DS1343_CONTROL_REG,
306 DS1343_A0IE, enabled ? DS1343_A0IE : 0);
307}
308
309static irqreturn_t ds1343_thread(int irq, void *dev_id)
310{
311 struct ds1343_priv *priv = dev_id;
312 unsigned int stat;
313 int res = 0;
314
315 rtc_lock(priv->rtc);
316
317 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
318 if (res)
319 goto out;
320
321 if (stat & DS1343_IRQF0) {
322 stat &= ~DS1343_IRQF0;
323 regmap_write(priv->map, DS1343_STATUS_REG, stat);
324
325 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
326
327 regmap_update_bits(priv->map, DS1343_CONTROL_REG,
328 DS1343_A0IE, 0);
329 }
330
331out:
332 rtc_unlock(priv->rtc);
333 return IRQ_HANDLED;
334}
335
336static const struct rtc_class_ops ds1343_rtc_ops = {
337 .read_time = ds1343_read_time,
338 .set_time = ds1343_set_time,
339 .read_alarm = ds1343_read_alarm,
340 .set_alarm = ds1343_set_alarm,
341 .alarm_irq_enable = ds1343_alarm_irq_enable,
342};
343
344static int ds1343_probe(struct spi_device *spi)
345{
346 struct ds1343_priv *priv;
347 struct regmap_config config = { .reg_bits = 8, .val_bits = 8,
348 .write_flag_mask = 0x80, };
349 unsigned int data;
350 int res;
351 struct nvmem_config nvmem_cfg = {
352 .name = "ds1343-",
353 .word_size = 1,
354 .stride = 1,
355 .size = DS1343_NVRAM_LEN,
356 .reg_read = ds1343_nvram_read,
357 .reg_write = ds1343_nvram_write,
358 };
359
360 priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
361 if (!priv)
362 return -ENOMEM;
363
364 /* RTC DS1347 works in spi mode 3 and
365 * its chip select is active high. Active high should be defined as
366 * "inverse polarity" as GPIO-based chip selects can be logically
367 * active high but inverted by the GPIO library.
368 */
369 spi->mode |= SPI_MODE_3;
370 spi->mode ^= SPI_CS_HIGH;
371 spi->bits_per_word = 8;
372 res = spi_setup(spi);
373 if (res)
374 return res;
375
376 spi_set_drvdata(spi, priv);
377
378 priv->map = devm_regmap_init_spi(spi, &config);
379
380 if (IS_ERR(priv->map)) {
381 dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n");
382 return PTR_ERR(priv->map);
383 }
384
385 res = regmap_read(priv->map, DS1343_SECONDS_REG, &data);
386 if (res)
387 return res;
388
389 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
390 data |= DS1343_INTCN;
391 data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE);
392 regmap_write(priv->map, DS1343_CONTROL_REG, data);
393
394 regmap_read(priv->map, DS1343_STATUS_REG, &data);
395 data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0);
396 regmap_write(priv->map, DS1343_STATUS_REG, data);
397
398 priv->rtc = devm_rtc_allocate_device(&spi->dev);
399 if (IS_ERR(priv->rtc))
400 return PTR_ERR(priv->rtc);
401
402 priv->rtc->ops = &ds1343_rtc_ops;
403 priv->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
404 priv->rtc->range_max = RTC_TIMESTAMP_END_2099;
405
406 res = rtc_add_group(priv->rtc, &ds1343_attr_group);
407 if (res)
408 dev_err(&spi->dev,
409 "unable to create sysfs entries for rtc ds1343\n");
410
411 res = devm_rtc_register_device(priv->rtc);
412 if (res)
413 return res;
414
415 nvmem_cfg.priv = priv;
416 devm_rtc_nvmem_register(priv->rtc, &nvmem_cfg);
417
418 priv->irq = spi->irq;
419
420 if (priv->irq >= 0) {
421 res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
422 ds1343_thread, IRQF_ONESHOT,
423 "ds1343", priv);
424 if (res) {
425 priv->irq = -1;
426 dev_err(&spi->dev,
427 "unable to request irq for rtc ds1343\n");
428 } else {
429 device_init_wakeup(&spi->dev, true);
430 dev_pm_set_wake_irq(&spi->dev, spi->irq);
431 }
432 }
433
434 return 0;
435}
436
437static void ds1343_remove(struct spi_device *spi)
438{
439 dev_pm_clear_wake_irq(&spi->dev);
440}
441
442#ifdef CONFIG_PM_SLEEP
443
444static int ds1343_suspend(struct device *dev)
445{
446 struct spi_device *spi = to_spi_device(dev);
447
448 if (spi->irq >= 0 && device_may_wakeup(dev))
449 enable_irq_wake(spi->irq);
450
451 return 0;
452}
453
454static int ds1343_resume(struct device *dev)
455{
456 struct spi_device *spi = to_spi_device(dev);
457
458 if (spi->irq >= 0 && device_may_wakeup(dev))
459 disable_irq_wake(spi->irq);
460
461 return 0;
462}
463
464#endif
465
466static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
467
468static struct spi_driver ds1343_driver = {
469 .driver = {
470 .name = "ds1343",
471 .pm = &ds1343_pm,
472 },
473 .probe = ds1343_probe,
474 .remove = ds1343_remove,
475 .id_table = ds1343_id,
476};
477
478module_spi_driver(ds1343_driver);
479
480MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
481MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>,"
482 "Ankur Srivastava <sankurece@gmail.com>");
483MODULE_LICENSE("GPL v2");
1/* rtc-ds1343.c
2 *
3 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
4 * Real Time Clock
5 *
6 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
7 * Ankur Srivastava <sankurece@gmail.com> : DS1343 Nvram Support
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/interrupt.h>
18#include <linux/device.h>
19#include <linux/spi/spi.h>
20#include <linux/regmap.h>
21#include <linux/rtc.h>
22#include <linux/bcd.h>
23#include <linux/pm.h>
24#include <linux/pm_wakeirq.h>
25#include <linux/slab.h>
26
27#define DALLAS_MAXIM_DS1343 0
28#define DALLAS_MAXIM_DS1344 1
29
30/* RTC DS1343 Registers */
31#define DS1343_SECONDS_REG 0x00
32#define DS1343_MINUTES_REG 0x01
33#define DS1343_HOURS_REG 0x02
34#define DS1343_DAY_REG 0x03
35#define DS1343_DATE_REG 0x04
36#define DS1343_MONTH_REG 0x05
37#define DS1343_YEAR_REG 0x06
38#define DS1343_ALM0_SEC_REG 0x07
39#define DS1343_ALM0_MIN_REG 0x08
40#define DS1343_ALM0_HOUR_REG 0x09
41#define DS1343_ALM0_DAY_REG 0x0A
42#define DS1343_ALM1_SEC_REG 0x0B
43#define DS1343_ALM1_MIN_REG 0x0C
44#define DS1343_ALM1_HOUR_REG 0x0D
45#define DS1343_ALM1_DAY_REG 0x0E
46#define DS1343_CONTROL_REG 0x0F
47#define DS1343_STATUS_REG 0x10
48#define DS1343_TRICKLE_REG 0x11
49#define DS1343_NVRAM 0x20
50
51#define DS1343_NVRAM_LEN 96
52
53/* DS1343 Control Registers bits */
54#define DS1343_EOSC 0x80
55#define DS1343_DOSF 0x20
56#define DS1343_EGFIL 0x10
57#define DS1343_SQW 0x08
58#define DS1343_INTCN 0x04
59#define DS1343_A1IE 0x02
60#define DS1343_A0IE 0x01
61
62/* DS1343 Status Registers bits */
63#define DS1343_OSF 0x80
64#define DS1343_IRQF1 0x02
65#define DS1343_IRQF0 0x01
66
67/* DS1343 Trickle Charger Registers bits */
68#define DS1343_TRICKLE_MAGIC 0xa0
69#define DS1343_TRICKLE_DS1 0x08
70#define DS1343_TRICKLE_1K 0x01
71#define DS1343_TRICKLE_2K 0x02
72#define DS1343_TRICKLE_4K 0x03
73
74static const struct spi_device_id ds1343_id[] = {
75 { "ds1343", DALLAS_MAXIM_DS1343 },
76 { "ds1344", DALLAS_MAXIM_DS1344 },
77 { }
78};
79MODULE_DEVICE_TABLE(spi, ds1343_id);
80
81struct ds1343_priv {
82 struct spi_device *spi;
83 struct rtc_device *rtc;
84 struct regmap *map;
85 struct mutex mutex;
86 unsigned int irqen;
87 int irq;
88 int alarm_sec;
89 int alarm_min;
90 int alarm_hour;
91 int alarm_mday;
92};
93
94static int ds1343_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
95{
96 switch (cmd) {
97#ifdef RTC_SET_CHARGE
98 case RTC_SET_CHARGE:
99 {
100 int val;
101
102 if (copy_from_user(&val, (int __user *)arg, sizeof(int)))
103 return -EFAULT;
104
105 return regmap_write(priv->map, DS1343_TRICKLE_REG, val);
106 }
107 break;
108#endif
109 }
110
111 return -ENOIOCTLCMD;
112}
113
114static ssize_t ds1343_show_glitchfilter(struct device *dev,
115 struct device_attribute *attr, char *buf)
116{
117 struct ds1343_priv *priv = dev_get_drvdata(dev);
118 int glitch_filt_status, data;
119
120 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
121
122 glitch_filt_status = !!(data & DS1343_EGFIL);
123
124 if (glitch_filt_status)
125 return sprintf(buf, "enabled\n");
126 else
127 return sprintf(buf, "disabled\n");
128}
129
130static ssize_t ds1343_store_glitchfilter(struct device *dev,
131 struct device_attribute *attr,
132 const char *buf, size_t count)
133{
134 struct ds1343_priv *priv = dev_get_drvdata(dev);
135 int data;
136
137 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
138
139 if (strncmp(buf, "enabled", 7) == 0)
140 data |= DS1343_EGFIL;
141
142 else if (strncmp(buf, "disabled", 8) == 0)
143 data &= ~(DS1343_EGFIL);
144
145 else
146 return -EINVAL;
147
148 regmap_write(priv->map, DS1343_CONTROL_REG, data);
149
150 return count;
151}
152
153static DEVICE_ATTR(glitch_filter, S_IRUGO | S_IWUSR, ds1343_show_glitchfilter,
154 ds1343_store_glitchfilter);
155
156static ssize_t ds1343_nvram_write(struct file *filp, struct kobject *kobj,
157 struct bin_attribute *attr,
158 char *buf, loff_t off, size_t count)
159{
160 int ret;
161 unsigned char address;
162 struct device *dev = kobj_to_dev(kobj);
163 struct ds1343_priv *priv = dev_get_drvdata(dev);
164
165 address = DS1343_NVRAM + off;
166
167 ret = regmap_bulk_write(priv->map, address, buf, count);
168 if (ret < 0)
169 dev_err(&priv->spi->dev, "Error in nvram write %d", ret);
170
171 return (ret < 0) ? ret : count;
172}
173
174
175static ssize_t ds1343_nvram_read(struct file *filp, struct kobject *kobj,
176 struct bin_attribute *attr,
177 char *buf, loff_t off, size_t count)
178{
179 int ret;
180 unsigned char address;
181 struct device *dev = kobj_to_dev(kobj);
182 struct ds1343_priv *priv = dev_get_drvdata(dev);
183
184 address = DS1343_NVRAM + off;
185
186 ret = regmap_bulk_read(priv->map, address, buf, count);
187 if (ret < 0)
188 dev_err(&priv->spi->dev, "Error in nvram read %d\n", ret);
189
190 return (ret < 0) ? ret : count;
191}
192
193
194static struct bin_attribute nvram_attr = {
195 .attr.name = "nvram",
196 .attr.mode = S_IRUGO | S_IWUSR,
197 .read = ds1343_nvram_read,
198 .write = ds1343_nvram_write,
199 .size = DS1343_NVRAM_LEN,
200};
201
202static ssize_t ds1343_show_alarmstatus(struct device *dev,
203 struct device_attribute *attr, char *buf)
204{
205 struct ds1343_priv *priv = dev_get_drvdata(dev);
206 int alarmstatus, data;
207
208 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
209
210 alarmstatus = !!(data & DS1343_A0IE);
211
212 if (alarmstatus)
213 return sprintf(buf, "enabled\n");
214 else
215 return sprintf(buf, "disabled\n");
216}
217
218static DEVICE_ATTR(alarm_status, S_IRUGO, ds1343_show_alarmstatus, NULL);
219
220static ssize_t ds1343_show_alarmmode(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
223 struct ds1343_priv *priv = dev_get_drvdata(dev);
224 int alarm_mode, data;
225 char *alarm_str;
226
227 regmap_read(priv->map, DS1343_ALM0_SEC_REG, &data);
228 alarm_mode = (data & 0x80) >> 4;
229
230 regmap_read(priv->map, DS1343_ALM0_MIN_REG, &data);
231 alarm_mode |= (data & 0x80) >> 5;
232
233 regmap_read(priv->map, DS1343_ALM0_HOUR_REG, &data);
234 alarm_mode |= (data & 0x80) >> 6;
235
236 regmap_read(priv->map, DS1343_ALM0_DAY_REG, &data);
237 alarm_mode |= (data & 0x80) >> 7;
238
239 switch (alarm_mode) {
240 case 15:
241 alarm_str = "each second";
242 break;
243
244 case 7:
245 alarm_str = "seconds match";
246 break;
247
248 case 3:
249 alarm_str = "minutes and seconds match";
250 break;
251
252 case 1:
253 alarm_str = "hours, minutes and seconds match";
254 break;
255
256 case 0:
257 alarm_str = "day, hours, minutes and seconds match";
258 break;
259
260 default:
261 alarm_str = "invalid";
262 break;
263 }
264
265 return sprintf(buf, "%s\n", alarm_str);
266}
267
268static DEVICE_ATTR(alarm_mode, S_IRUGO, ds1343_show_alarmmode, NULL);
269
270static ssize_t ds1343_show_tricklecharger(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
273 struct ds1343_priv *priv = dev_get_drvdata(dev);
274 int data;
275 char *diodes = "disabled", *resistors = " ";
276
277 regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
278
279 if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
280 switch (data & 0x0c) {
281 case DS1343_TRICKLE_DS1:
282 diodes = "one diode,";
283 break;
284
285 default:
286 diodes = "no diode,";
287 break;
288 }
289
290 switch (data & 0x03) {
291 case DS1343_TRICKLE_1K:
292 resistors = "1k Ohm";
293 break;
294
295 case DS1343_TRICKLE_2K:
296 resistors = "2k Ohm";
297 break;
298
299 case DS1343_TRICKLE_4K:
300 resistors = "4k Ohm";
301 break;
302
303 default:
304 diodes = "disabled";
305 break;
306 }
307 }
308
309 return sprintf(buf, "%s %s\n", diodes, resistors);
310}
311
312static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
313
314static int ds1343_sysfs_register(struct device *dev)
315{
316 struct ds1343_priv *priv = dev_get_drvdata(dev);
317 int err;
318
319 err = device_create_file(dev, &dev_attr_glitch_filter);
320 if (err)
321 return err;
322
323 err = device_create_file(dev, &dev_attr_trickle_charger);
324 if (err)
325 goto error1;
326
327 err = device_create_bin_file(dev, &nvram_attr);
328 if (err)
329 goto error2;
330
331 if (priv->irq <= 0)
332 return err;
333
334 err = device_create_file(dev, &dev_attr_alarm_mode);
335 if (err)
336 goto error3;
337
338 err = device_create_file(dev, &dev_attr_alarm_status);
339 if (!err)
340 return err;
341
342 device_remove_file(dev, &dev_attr_alarm_mode);
343
344error3:
345 device_remove_bin_file(dev, &nvram_attr);
346
347error2:
348 device_remove_file(dev, &dev_attr_trickle_charger);
349
350error1:
351 device_remove_file(dev, &dev_attr_glitch_filter);
352
353 return err;
354}
355
356static void ds1343_sysfs_unregister(struct device *dev)
357{
358 struct ds1343_priv *priv = dev_get_drvdata(dev);
359
360 device_remove_file(dev, &dev_attr_glitch_filter);
361 device_remove_file(dev, &dev_attr_trickle_charger);
362 device_remove_bin_file(dev, &nvram_attr);
363
364 if (priv->irq <= 0)
365 return;
366
367 device_remove_file(dev, &dev_attr_alarm_status);
368 device_remove_file(dev, &dev_attr_alarm_mode);
369}
370
371static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
372{
373 struct ds1343_priv *priv = dev_get_drvdata(dev);
374 unsigned char buf[7];
375 int res;
376
377 res = regmap_bulk_read(priv->map, DS1343_SECONDS_REG, buf, 7);
378 if (res)
379 return res;
380
381 dt->tm_sec = bcd2bin(buf[0]);
382 dt->tm_min = bcd2bin(buf[1]);
383 dt->tm_hour = bcd2bin(buf[2] & 0x3F);
384 dt->tm_wday = bcd2bin(buf[3]) - 1;
385 dt->tm_mday = bcd2bin(buf[4]);
386 dt->tm_mon = bcd2bin(buf[5] & 0x1F) - 1;
387 dt->tm_year = bcd2bin(buf[6]) + 100; /* year offset from 1900 */
388
389 return rtc_valid_tm(dt);
390}
391
392static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
393{
394 struct ds1343_priv *priv = dev_get_drvdata(dev);
395 int res;
396
397 res = regmap_write(priv->map, DS1343_SECONDS_REG,
398 bin2bcd(dt->tm_sec));
399 if (res)
400 return res;
401
402 res = regmap_write(priv->map, DS1343_MINUTES_REG,
403 bin2bcd(dt->tm_min));
404 if (res)
405 return res;
406
407 res = regmap_write(priv->map, DS1343_HOURS_REG,
408 bin2bcd(dt->tm_hour) & 0x3F);
409 if (res)
410 return res;
411
412 res = regmap_write(priv->map, DS1343_DAY_REG,
413 bin2bcd(dt->tm_wday + 1));
414 if (res)
415 return res;
416
417 res = regmap_write(priv->map, DS1343_DATE_REG,
418 bin2bcd(dt->tm_mday));
419 if (res)
420 return res;
421
422 res = regmap_write(priv->map, DS1343_MONTH_REG,
423 bin2bcd(dt->tm_mon + 1));
424 if (res)
425 return res;
426
427 dt->tm_year %= 100;
428
429 res = regmap_write(priv->map, DS1343_YEAR_REG,
430 bin2bcd(dt->tm_year));
431 if (res)
432 return res;
433
434 return 0;
435}
436
437static int ds1343_update_alarm(struct device *dev)
438{
439 struct ds1343_priv *priv = dev_get_drvdata(dev);
440 unsigned int control, stat;
441 unsigned char buf[4];
442 int res = 0;
443
444 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
445 if (res)
446 return res;
447
448 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
449 if (res)
450 return res;
451
452 control &= ~(DS1343_A0IE);
453 stat &= ~(DS1343_IRQF0);
454
455 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
456 if (res)
457 return res;
458
459 res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
460 if (res)
461 return res;
462
463 buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ?
464 0x80 : bin2bcd(priv->alarm_sec) & 0x7F;
465 buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ?
466 0x80 : bin2bcd(priv->alarm_min) & 0x7F;
467 buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ?
468 0x80 : bin2bcd(priv->alarm_hour) & 0x3F;
469 buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ?
470 0x80 : bin2bcd(priv->alarm_mday) & 0x7F;
471
472 res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
473 if (res)
474 return res;
475
476 if (priv->irqen) {
477 control |= DS1343_A0IE;
478 res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
479 }
480
481 return res;
482}
483
484static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
485{
486 struct ds1343_priv *priv = dev_get_drvdata(dev);
487 int res = 0;
488 unsigned int stat;
489
490 if (priv->irq <= 0)
491 return -EINVAL;
492
493 mutex_lock(&priv->mutex);
494
495 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
496 if (res)
497 goto out;
498
499 alarm->enabled = !!(priv->irqen & RTC_AF);
500 alarm->pending = !!(stat & DS1343_IRQF0);
501
502 alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec;
503 alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min;
504 alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour;
505 alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday;
506
507out:
508 mutex_unlock(&priv->mutex);
509 return res;
510}
511
512static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
513{
514 struct ds1343_priv *priv = dev_get_drvdata(dev);
515 int res = 0;
516
517 if (priv->irq <= 0)
518 return -EINVAL;
519
520 mutex_lock(&priv->mutex);
521
522 priv->alarm_sec = alarm->time.tm_sec;
523 priv->alarm_min = alarm->time.tm_min;
524 priv->alarm_hour = alarm->time.tm_hour;
525 priv->alarm_mday = alarm->time.tm_mday;
526
527 if (alarm->enabled)
528 priv->irqen |= RTC_AF;
529
530 res = ds1343_update_alarm(dev);
531
532 mutex_unlock(&priv->mutex);
533
534 return res;
535}
536
537static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
538{
539 struct ds1343_priv *priv = dev_get_drvdata(dev);
540 int res = 0;
541
542 if (priv->irq <= 0)
543 return -EINVAL;
544
545 mutex_lock(&priv->mutex);
546
547 if (enabled)
548 priv->irqen |= RTC_AF;
549 else
550 priv->irqen &= ~RTC_AF;
551
552 res = ds1343_update_alarm(dev);
553
554 mutex_unlock(&priv->mutex);
555
556 return res;
557}
558
559static irqreturn_t ds1343_thread(int irq, void *dev_id)
560{
561 struct ds1343_priv *priv = dev_id;
562 unsigned int stat, control;
563 int res = 0;
564
565 mutex_lock(&priv->mutex);
566
567 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
568 if (res)
569 goto out;
570
571 if (stat & DS1343_IRQF0) {
572 stat &= ~DS1343_IRQF0;
573 regmap_write(priv->map, DS1343_STATUS_REG, stat);
574
575 res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
576 if (res)
577 goto out;
578
579 control &= ~DS1343_A0IE;
580 regmap_write(priv->map, DS1343_CONTROL_REG, control);
581
582 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
583 }
584
585out:
586 mutex_unlock(&priv->mutex);
587 return IRQ_HANDLED;
588}
589
590static const struct rtc_class_ops ds1343_rtc_ops = {
591 .ioctl = ds1343_ioctl,
592 .read_time = ds1343_read_time,
593 .set_time = ds1343_set_time,
594 .read_alarm = ds1343_read_alarm,
595 .set_alarm = ds1343_set_alarm,
596 .alarm_irq_enable = ds1343_alarm_irq_enable,
597};
598
599static int ds1343_probe(struct spi_device *spi)
600{
601 struct ds1343_priv *priv;
602 struct regmap_config config;
603 unsigned int data;
604 int res;
605
606 memset(&config, 0, sizeof(config));
607 config.reg_bits = 8;
608 config.val_bits = 8;
609 config.write_flag_mask = 0x80;
610
611 priv = devm_kzalloc(&spi->dev, sizeof(struct ds1343_priv), GFP_KERNEL);
612 if (!priv)
613 return -ENOMEM;
614
615 priv->spi = spi;
616 mutex_init(&priv->mutex);
617
618 /* RTC DS1347 works in spi mode 3 and
619 * its chip select is active high
620 */
621 spi->mode = SPI_MODE_3 | SPI_CS_HIGH;
622 spi->bits_per_word = 8;
623 res = spi_setup(spi);
624 if (res)
625 return res;
626
627 spi_set_drvdata(spi, priv);
628
629 priv->map = devm_regmap_init_spi(spi, &config);
630
631 if (IS_ERR(priv->map)) {
632 dev_err(&spi->dev, "spi regmap init failed for rtc ds1343\n");
633 return PTR_ERR(priv->map);
634 }
635
636 res = regmap_read(priv->map, DS1343_SECONDS_REG, &data);
637 if (res)
638 return res;
639
640 regmap_read(priv->map, DS1343_CONTROL_REG, &data);
641 data |= DS1343_INTCN;
642 data &= ~(DS1343_EOSC | DS1343_A1IE | DS1343_A0IE);
643 regmap_write(priv->map, DS1343_CONTROL_REG, data);
644
645 regmap_read(priv->map, DS1343_STATUS_REG, &data);
646 data &= ~(DS1343_OSF | DS1343_IRQF1 | DS1343_IRQF0);
647 regmap_write(priv->map, DS1343_STATUS_REG, data);
648
649 priv->rtc = devm_rtc_device_register(&spi->dev, "ds1343",
650 &ds1343_rtc_ops, THIS_MODULE);
651 if (IS_ERR(priv->rtc)) {
652 dev_err(&spi->dev, "unable to register rtc ds1343\n");
653 return PTR_ERR(priv->rtc);
654 }
655
656 priv->irq = spi->irq;
657
658 if (priv->irq >= 0) {
659 res = devm_request_threaded_irq(&spi->dev, spi->irq, NULL,
660 ds1343_thread, IRQF_ONESHOT,
661 "ds1343", priv);
662 if (res) {
663 priv->irq = -1;
664 dev_err(&spi->dev,
665 "unable to request irq for rtc ds1343\n");
666 } else {
667 device_init_wakeup(&spi->dev, true);
668 dev_pm_set_wake_irq(&spi->dev, spi->irq);
669 }
670 }
671
672 res = ds1343_sysfs_register(&spi->dev);
673 if (res)
674 dev_err(&spi->dev,
675 "unable to create sysfs entries for rtc ds1343\n");
676
677 return 0;
678}
679
680static int ds1343_remove(struct spi_device *spi)
681{
682 struct ds1343_priv *priv = spi_get_drvdata(spi);
683
684 if (spi->irq) {
685 mutex_lock(&priv->mutex);
686 priv->irqen &= ~RTC_AF;
687 mutex_unlock(&priv->mutex);
688
689 dev_pm_clear_wake_irq(&spi->dev);
690 device_init_wakeup(&spi->dev, false);
691 devm_free_irq(&spi->dev, spi->irq, priv);
692 }
693
694 spi_set_drvdata(spi, NULL);
695
696 ds1343_sysfs_unregister(&spi->dev);
697
698 return 0;
699}
700
701#ifdef CONFIG_PM_SLEEP
702
703static int ds1343_suspend(struct device *dev)
704{
705 struct spi_device *spi = to_spi_device(dev);
706
707 if (spi->irq >= 0 && device_may_wakeup(dev))
708 enable_irq_wake(spi->irq);
709
710 return 0;
711}
712
713static int ds1343_resume(struct device *dev)
714{
715 struct spi_device *spi = to_spi_device(dev);
716
717 if (spi->irq >= 0 && device_may_wakeup(dev))
718 disable_irq_wake(spi->irq);
719
720 return 0;
721}
722
723#endif
724
725static SIMPLE_DEV_PM_OPS(ds1343_pm, ds1343_suspend, ds1343_resume);
726
727static struct spi_driver ds1343_driver = {
728 .driver = {
729 .name = "ds1343",
730 .pm = &ds1343_pm,
731 },
732 .probe = ds1343_probe,
733 .remove = ds1343_remove,
734 .id_table = ds1343_id,
735};
736
737module_spi_driver(ds1343_driver);
738
739MODULE_DESCRIPTION("DS1343 RTC SPI Driver");
740MODULE_AUTHOR("Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>,"
741 "Ankur Srivastava <sankurece@gmail.com>");
742MODULE_LICENSE("GPL v2");