hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/rtc/rtc-ds1343.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* rtc-ds1343.c
23 *
34 * Driver for Dallas Semiconductor DS1343 Low Current, SPI Compatible
....@@ -5,11 +6,6 @@
56 *
67 * Author : Raghavendra Chandra Ganiga <ravi23ganiga@gmail.com>
78 * 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
- *
139 */
1410
1511 #include <linux/init.h>
....@@ -79,45 +75,21 @@
7975 MODULE_DEVICE_TABLE(spi, ds1343_id);
8076
8177 struct ds1343_priv {
82
- struct spi_device *spi;
8378 struct rtc_device *rtc;
8479 struct regmap *map;
85
- struct mutex mutex;
86
- unsigned int irqen;
8780 int irq;
88
- int alarm_sec;
89
- int alarm_min;
90
- int alarm_hour;
91
- int alarm_mday;
9281 };
93
-
94
-static 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
-}
11382
11483 static ssize_t ds1343_show_glitchfilter(struct device *dev,
11584 struct device_attribute *attr, char *buf)
11685 {
117
- struct ds1343_priv *priv = dev_get_drvdata(dev);
86
+ struct ds1343_priv *priv = dev_get_drvdata(dev->parent);
11887 int glitch_filt_status, data;
88
+ int res;
11989
120
- regmap_read(priv->map, DS1343_CONTROL_REG, &data);
90
+ res = regmap_read(priv->map, DS1343_CONTROL_REG, &data);
91
+ if (res)
92
+ return res;
12193
12294 glitch_filt_status = !!(data & DS1343_EGFIL);
12395
....@@ -131,21 +103,19 @@
131103 struct device_attribute *attr,
132104 const char *buf, size_t count)
133105 {
134
- struct ds1343_priv *priv = dev_get_drvdata(dev);
135
- int data;
136
-
137
- regmap_read(priv->map, DS1343_CONTROL_REG, &data);
106
+ struct ds1343_priv *priv = dev_get_drvdata(dev->parent);
107
+ int data = 0;
108
+ int res;
138109
139110 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
111
+ data = DS1343_EGFIL;
112
+ else if (strncmp(buf, "disabled", 8))
146113 return -EINVAL;
147114
148
- regmap_write(priv->map, DS1343_CONTROL_REG, data);
115
+ res = regmap_update_bits(priv->map, DS1343_CONTROL_REG,
116
+ DS1343_EGFIL, data);
117
+ if (res)
118
+ return res;
149119
150120 return count;
151121 }
....@@ -172,11 +142,13 @@
172142 static ssize_t ds1343_show_tricklecharger(struct device *dev,
173143 struct device_attribute *attr, char *buf)
174144 {
175
- struct ds1343_priv *priv = dev_get_drvdata(dev);
176
- int data;
145
+ struct ds1343_priv *priv = dev_get_drvdata(dev->parent);
146
+ int res, data;
177147 char *diodes = "disabled", *resistors = " ";
178148
179
- regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
149
+ res = regmap_read(priv->map, DS1343_TRICKLE_REG, &data);
150
+ if (res)
151
+ return res;
180152
181153 if ((data & 0xf0) == DS1343_TRICKLE_MAGIC) {
182154 switch (data & 0x0c) {
....@@ -213,28 +185,15 @@
213185
214186 static DEVICE_ATTR(trickle_charger, S_IRUGO, ds1343_show_tricklecharger, NULL);
215187
216
-static int ds1343_sysfs_register(struct device *dev)
217
-{
218
- int err;
188
+static struct attribute *ds1343_attrs[] = {
189
+ &dev_attr_glitch_filter.attr,
190
+ &dev_attr_trickle_charger.attr,
191
+ NULL
192
+};
219193
220
- err = device_create_file(dev, &dev_attr_glitch_filter);
221
- if (err)
222
- return err;
223
-
224
- err = device_create_file(dev, &dev_attr_trickle_charger);
225
- if (!err)
226
- return 0;
227
-
228
- device_remove_file(dev, &dev_attr_glitch_filter);
229
-
230
- return err;
231
-}
232
-
233
-static void ds1343_sysfs_unregister(struct device *dev)
234
-{
235
- device_remove_file(dev, &dev_attr_glitch_filter);
236
- device_remove_file(dev, &dev_attr_trickle_charger);
237
-}
194
+static const struct attribute_group ds1343_attr_group = {
195
+ .attrs = ds1343_attrs,
196
+};
238197
239198 static int ds1343_read_time(struct device *dev, struct rtc_time *dt)
240199 {
....@@ -260,144 +219,78 @@
260219 static int ds1343_set_time(struct device *dev, struct rtc_time *dt)
261220 {
262221 struct ds1343_priv *priv = dev_get_drvdata(dev);
263
- int res;
222
+ u8 buf[7];
264223
265
- res = regmap_write(priv->map, DS1343_SECONDS_REG,
266
- bin2bcd(dt->tm_sec));
267
- if (res)
268
- return res;
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);
269231
270
- res = regmap_write(priv->map, DS1343_MINUTES_REG,
271
- bin2bcd(dt->tm_min));
272
- if (res)
273
- return res;
274
-
275
- res = regmap_write(priv->map, DS1343_HOURS_REG,
276
- bin2bcd(dt->tm_hour) & 0x3F);
277
- if (res)
278
- return res;
279
-
280
- res = regmap_write(priv->map, DS1343_DAY_REG,
281
- bin2bcd(dt->tm_wday + 1));
282
- if (res)
283
- return res;
284
-
285
- res = regmap_write(priv->map, DS1343_DATE_REG,
286
- bin2bcd(dt->tm_mday));
287
- if (res)
288
- return res;
289
-
290
- res = regmap_write(priv->map, DS1343_MONTH_REG,
291
- bin2bcd(dt->tm_mon + 1));
292
- if (res)
293
- return res;
294
-
295
- dt->tm_year %= 100;
296
-
297
- res = regmap_write(priv->map, DS1343_YEAR_REG,
298
- bin2bcd(dt->tm_year));
299
- if (res)
300
- return res;
301
-
302
- return 0;
303
-}
304
-
305
-static int ds1343_update_alarm(struct device *dev)
306
-{
307
- struct ds1343_priv *priv = dev_get_drvdata(dev);
308
- unsigned int control, stat;
309
- unsigned char buf[4];
310
- int res = 0;
311
-
312
- res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
313
- if (res)
314
- return res;
315
-
316
- res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
317
- if (res)
318
- return res;
319
-
320
- control &= ~(DS1343_A0IE);
321
- stat &= ~(DS1343_IRQF0);
322
-
323
- res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
324
- if (res)
325
- return res;
326
-
327
- res = regmap_write(priv->map, DS1343_STATUS_REG, stat);
328
- if (res)
329
- return res;
330
-
331
- buf[0] = priv->alarm_sec < 0 || (priv->irqen & RTC_UF) ?
332
- 0x80 : bin2bcd(priv->alarm_sec) & 0x7F;
333
- buf[1] = priv->alarm_min < 0 || (priv->irqen & RTC_UF) ?
334
- 0x80 : bin2bcd(priv->alarm_min) & 0x7F;
335
- buf[2] = priv->alarm_hour < 0 || (priv->irqen & RTC_UF) ?
336
- 0x80 : bin2bcd(priv->alarm_hour) & 0x3F;
337
- buf[3] = priv->alarm_mday < 0 || (priv->irqen & RTC_UF) ?
338
- 0x80 : bin2bcd(priv->alarm_mday) & 0x7F;
339
-
340
- res = regmap_bulk_write(priv->map, DS1343_ALM0_SEC_REG, buf, 4);
341
- if (res)
342
- return res;
343
-
344
- if (priv->irqen) {
345
- control |= DS1343_A0IE;
346
- res = regmap_write(priv->map, DS1343_CONTROL_REG, control);
347
- }
348
-
349
- return res;
232
+ return regmap_bulk_write(priv->map, DS1343_SECONDS_REG,
233
+ buf, sizeof(buf));
350234 }
351235
352236 static int ds1343_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
353237 {
354238 struct ds1343_priv *priv = dev_get_drvdata(dev);
355
- int res = 0;
356
- unsigned int stat;
239
+ unsigned char buf[4];
240
+ unsigned int val;
241
+ int res;
357242
358243 if (priv->irq <= 0)
359244 return -EINVAL;
360245
361
- mutex_lock(&priv->mutex);
362
-
363
- res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
246
+ res = regmap_read(priv->map, DS1343_STATUS_REG, &val);
364247 if (res)
365
- goto out;
248
+ return res;
366249
367
- alarm->enabled = !!(priv->irqen & RTC_AF);
368
- alarm->pending = !!(stat & DS1343_IRQF0);
250
+ alarm->pending = !!(val & DS1343_IRQF0);
369251
370
- alarm->time.tm_sec = priv->alarm_sec < 0 ? 0 : priv->alarm_sec;
371
- alarm->time.tm_min = priv->alarm_min < 0 ? 0 : priv->alarm_min;
372
- alarm->time.tm_hour = priv->alarm_hour < 0 ? 0 : priv->alarm_hour;
373
- alarm->time.tm_mday = priv->alarm_mday < 0 ? 0 : priv->alarm_mday;
252
+ res = regmap_read(priv->map, DS1343_CONTROL_REG, &val);
253
+ if (res)
254
+ return res;
255
+ alarm->enabled = !!(val & DS1343_A0IE);
374256
375
-out:
376
- mutex_unlock(&priv->mutex);
377
- return res;
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;
378267 }
379268
380269 static int ds1343_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
381270 {
382271 struct ds1343_priv *priv = dev_get_drvdata(dev);
272
+ unsigned char buf[4];
383273 int res = 0;
384274
385275 if (priv->irq <= 0)
386276 return -EINVAL;
387277
388
- mutex_lock(&priv->mutex);
278
+ res = regmap_update_bits(priv->map, DS1343_CONTROL_REG, DS1343_A0IE, 0);
279
+ if (res)
280
+ return res;
389281
390
- priv->alarm_sec = alarm->time.tm_sec;
391
- priv->alarm_min = alarm->time.tm_min;
392
- priv->alarm_hour = alarm->time.tm_hour;
393
- priv->alarm_mday = alarm->time.tm_mday;
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;
394290
395291 if (alarm->enabled)
396
- priv->irqen |= RTC_AF;
397
-
398
- res = ds1343_update_alarm(dev);
399
-
400
- mutex_unlock(&priv->mutex);
292
+ res = regmap_update_bits(priv->map, DS1343_CONTROL_REG,
293
+ DS1343_A0IE, DS1343_A0IE);
401294
402295 return res;
403296 }
....@@ -405,32 +298,21 @@
405298 static int ds1343_alarm_irq_enable(struct device *dev, unsigned int enabled)
406299 {
407300 struct ds1343_priv *priv = dev_get_drvdata(dev);
408
- int res = 0;
409301
410302 if (priv->irq <= 0)
411303 return -EINVAL;
412304
413
- mutex_lock(&priv->mutex);
414
-
415
- if (enabled)
416
- priv->irqen |= RTC_AF;
417
- else
418
- priv->irqen &= ~RTC_AF;
419
-
420
- res = ds1343_update_alarm(dev);
421
-
422
- mutex_unlock(&priv->mutex);
423
-
424
- return res;
305
+ return regmap_update_bits(priv->map, DS1343_CONTROL_REG,
306
+ DS1343_A0IE, enabled ? DS1343_A0IE : 0);
425307 }
426308
427309 static irqreturn_t ds1343_thread(int irq, void *dev_id)
428310 {
429311 struct ds1343_priv *priv = dev_id;
430
- unsigned int stat, control;
312
+ unsigned int stat;
431313 int res = 0;
432314
433
- mutex_lock(&priv->mutex);
315
+ rtc_lock(priv->rtc);
434316
435317 res = regmap_read(priv->map, DS1343_STATUS_REG, &stat);
436318 if (res)
....@@ -440,23 +322,18 @@
440322 stat &= ~DS1343_IRQF0;
441323 regmap_write(priv->map, DS1343_STATUS_REG, stat);
442324
443
- res = regmap_read(priv->map, DS1343_CONTROL_REG, &control);
444
- if (res)
445
- goto out;
446
-
447
- control &= ~DS1343_A0IE;
448
- regmap_write(priv->map, DS1343_CONTROL_REG, control);
449
-
450325 rtc_update_irq(priv->rtc, 1, RTC_AF | RTC_IRQF);
326
+
327
+ regmap_update_bits(priv->map, DS1343_CONTROL_REG,
328
+ DS1343_A0IE, 0);
451329 }
452330
453331 out:
454
- mutex_unlock(&priv->mutex);
332
+ rtc_unlock(priv->rtc);
455333 return IRQ_HANDLED;
456334 }
457335
458336 static const struct rtc_class_ops ds1343_rtc_ops = {
459
- .ioctl = ds1343_ioctl,
460337 .read_time = ds1343_read_time,
461338 .set_time = ds1343_set_time,
462339 .read_alarm = ds1343_read_alarm,
....@@ -484,13 +361,13 @@
484361 if (!priv)
485362 return -ENOMEM;
486363
487
- priv->spi = spi;
488
- mutex_init(&priv->mutex);
489
-
490364 /* RTC DS1347 works in spi mode 3 and
491
- * its chip select is active high
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.
492368 */
493
- spi->mode = SPI_MODE_3 | SPI_CS_HIGH;
369
+ spi->mode |= SPI_MODE_3;
370
+ spi->mode ^= SPI_CS_HIGH;
494371 spi->bits_per_word = 8;
495372 res = spi_setup(spi);
496373 if (res)
....@@ -524,6 +401,13 @@
524401
525402 priv->rtc->nvram_old_abi = true;
526403 priv->rtc->ops = &ds1343_rtc_ops;
404
+ priv->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
405
+ priv->rtc->range_max = RTC_TIMESTAMP_END_2099;
406
+
407
+ res = rtc_add_group(priv->rtc, &ds1343_attr_group);
408
+ if (res)
409
+ dev_err(&spi->dev,
410
+ "unable to create sysfs entries for rtc ds1343\n");
527411
528412 res = rtc_register_device(priv->rtc);
529413 if (res)
....@@ -548,31 +432,12 @@
548432 }
549433 }
550434
551
- res = ds1343_sysfs_register(&spi->dev);
552
- if (res)
553
- dev_err(&spi->dev,
554
- "unable to create sysfs entries for rtc ds1343\n");
555
-
556435 return 0;
557436 }
558437
559438 static int ds1343_remove(struct spi_device *spi)
560439 {
561
- struct ds1343_priv *priv = spi_get_drvdata(spi);
562
-
563
- if (spi->irq) {
564
- mutex_lock(&priv->mutex);
565
- priv->irqen &= ~RTC_AF;
566
- mutex_unlock(&priv->mutex);
567
-
568
- dev_pm_clear_wake_irq(&spi->dev);
569
- device_init_wakeup(&spi->dev, false);
570
- devm_free_irq(&spi->dev, spi->irq, priv);
571
- }
572
-
573
- spi_set_drvdata(spi, NULL);
574
-
575
- ds1343_sysfs_unregister(&spi->dev);
440
+ dev_pm_clear_wake_irq(&spi->dev);
576441
577442 return 0;
578443 }