1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Copyright (C) 2018 ROHM Semiconductors
5 // RTC driver for ROHM BD71828 and BD71815 PMIC
8 #include <linux/mfd/rohm-bd71815.h>
9 #include <linux/mfd/rohm-bd71828.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/rtc.h>
17 * On BD71828 and BD71815 the ALM0 MASK is 14 bytes after the ALM0
20 #define BD718XX_ALM_EN_OFFSET 14
23 * We read regs RTC_SEC => RTC_YEAR
24 * this struct is ordered according to chip registers.
25 * Keep it u8 only (or packed) to avoid padding issues.
27 struct bd70528_rtc_day
{
33 struct bd70528_rtc_data
{
34 struct bd70528_rtc_day time
;
41 struct bd71828_rtc_alm
{
42 struct bd70528_rtc_data alm0
;
43 struct bd70528_rtc_data alm1
;
49 struct rohm_regmap_dev
*parent
;
50 struct regmap
*regmap
;
53 u8 bd718xx_alm_block_start
;
56 static inline void tmday2rtc(struct rtc_time
*t
, struct bd70528_rtc_day
*d
)
58 d
->sec
&= ~BD70528_MASK_RTC_SEC
;
59 d
->min
&= ~BD70528_MASK_RTC_MINUTE
;
60 d
->hour
&= ~BD70528_MASK_RTC_HOUR
;
61 d
->sec
|= bin2bcd(t
->tm_sec
);
62 d
->min
|= bin2bcd(t
->tm_min
);
63 d
->hour
|= bin2bcd(t
->tm_hour
);
66 static inline void tm2rtc(struct rtc_time
*t
, struct bd70528_rtc_data
*r
)
68 r
->day
&= ~BD70528_MASK_RTC_DAY
;
69 r
->week
&= ~BD70528_MASK_RTC_WEEK
;
70 r
->month
&= ~BD70528_MASK_RTC_MONTH
;
72 * PM and 24H bits are not used by Wake - thus we clear them
73 * here and not in tmday2rtc() which is also used by wake.
75 r
->time
.hour
&= ~(BD70528_MASK_RTC_HOUR_PM
| BD70528_MASK_RTC_HOUR_24H
);
77 tmday2rtc(t
, &r
->time
);
79 * We do always set time in 24H mode.
81 r
->time
.hour
|= BD70528_MASK_RTC_HOUR_24H
;
82 r
->day
|= bin2bcd(t
->tm_mday
);
83 r
->week
|= bin2bcd(t
->tm_wday
);
84 r
->month
|= bin2bcd(t
->tm_mon
+ 1);
85 r
->year
= bin2bcd(t
->tm_year
- 100);
88 static inline void rtc2tm(struct bd70528_rtc_data
*r
, struct rtc_time
*t
)
90 t
->tm_sec
= bcd2bin(r
->time
.sec
& BD70528_MASK_RTC_SEC
);
91 t
->tm_min
= bcd2bin(r
->time
.min
& BD70528_MASK_RTC_MINUTE
);
92 t
->tm_hour
= bcd2bin(r
->time
.hour
& BD70528_MASK_RTC_HOUR
);
94 * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
95 * is not BCD value but tells whether it is AM or PM
97 if (!(r
->time
.hour
& BD70528_MASK_RTC_HOUR_24H
)) {
99 if (r
->time
.hour
& BD70528_MASK_RTC_HOUR_PM
)
102 t
->tm_mday
= bcd2bin(r
->day
& BD70528_MASK_RTC_DAY
);
103 t
->tm_mon
= bcd2bin(r
->month
& BD70528_MASK_RTC_MONTH
) - 1;
104 t
->tm_year
= 100 + bcd2bin(r
->year
& BD70528_MASK_RTC_YEAR
);
105 t
->tm_wday
= bcd2bin(r
->week
& BD70528_MASK_RTC_WEEK
);
108 static int bd71828_set_alarm(struct device
*dev
, struct rtc_wkalrm
*a
)
111 struct bd71828_rtc_alm alm
;
112 struct bd70528_rtc
*r
= dev_get_drvdata(dev
);
114 ret
= regmap_bulk_read(r
->regmap
, r
->bd718xx_alm_block_start
, &alm
,
117 dev_err(dev
, "Failed to read alarm regs\n");
121 tm2rtc(&a
->time
, &alm
.alm0
);
124 alm
.alm_mask
&= ~BD70528_MASK_ALM_EN
;
126 alm
.alm_mask
|= BD70528_MASK_ALM_EN
;
128 ret
= regmap_bulk_write(r
->regmap
, r
->bd718xx_alm_block_start
, &alm
,
131 dev_err(dev
, "Failed to set alarm time\n");
137 static int bd71828_read_alarm(struct device
*dev
, struct rtc_wkalrm
*a
)
140 struct bd71828_rtc_alm alm
;
141 struct bd70528_rtc
*r
= dev_get_drvdata(dev
);
143 ret
= regmap_bulk_read(r
->regmap
, r
->bd718xx_alm_block_start
, &alm
,
146 dev_err(dev
, "Failed to read alarm regs\n");
150 rtc2tm(&alm
.alm0
, &a
->time
);
151 a
->time
.tm_mday
= -1;
153 a
->time
.tm_year
= -1;
154 a
->enabled
= !!(alm
.alm_mask
& BD70528_MASK_ALM_EN
);
160 static int bd71828_set_time(struct device
*dev
, struct rtc_time
*t
)
163 struct bd70528_rtc_data rtc_data
;
164 struct bd70528_rtc
*r
= dev_get_drvdata(dev
);
166 ret
= regmap_bulk_read(r
->regmap
, r
->reg_time_start
, &rtc_data
,
169 dev_err(dev
, "Failed to read RTC time registers\n");
172 tm2rtc(t
, &rtc_data
);
174 ret
= regmap_bulk_write(r
->regmap
, r
->reg_time_start
, &rtc_data
,
177 dev_err(dev
, "Failed to set RTC time\n");
182 static int bd70528_get_time(struct device
*dev
, struct rtc_time
*t
)
184 struct bd70528_rtc
*r
= dev_get_drvdata(dev
);
185 struct bd70528_rtc_data rtc_data
;
188 /* read the RTC date and time registers all at once */
189 ret
= regmap_bulk_read(r
->regmap
, r
->reg_time_start
, &rtc_data
,
192 dev_err(dev
, "Failed to read RTC time (err %d)\n", ret
);
196 rtc2tm(&rtc_data
, t
);
201 static int bd71828_alm_enable(struct device
*dev
, unsigned int enabled
)
204 struct bd70528_rtc
*r
= dev_get_drvdata(dev
);
205 unsigned int enableval
= BD70528_MASK_ALM_EN
;
210 ret
= regmap_update_bits(r
->regmap
, r
->bd718xx_alm_block_start
+
211 BD718XX_ALM_EN_OFFSET
, BD70528_MASK_ALM_EN
,
214 dev_err(dev
, "Failed to change alarm state\n");
219 static const struct rtc_class_ops bd71828_rtc_ops
= {
220 .read_time
= bd70528_get_time
,
221 .set_time
= bd71828_set_time
,
222 .read_alarm
= bd71828_read_alarm
,
223 .set_alarm
= bd71828_set_alarm
,
224 .alarm_irq_enable
= bd71828_alm_enable
,
227 static irqreturn_t
alm_hndlr(int irq
, void *data
)
229 struct rtc_device
*rtc
= data
;
231 rtc_update_irq(rtc
, 1, RTC_IRQF
| RTC_AF
| RTC_PF
);
235 static int bd70528_probe(struct platform_device
*pdev
)
237 struct bd70528_rtc
*bd_rtc
;
238 const struct rtc_class_ops
*rtc_ops
;
240 struct rtc_device
*rtc
;
244 enum rohm_chip_type chip
= platform_get_device_id(pdev
)->driver_data
;
246 bd_rtc
= devm_kzalloc(&pdev
->dev
, sizeof(*bd_rtc
), GFP_KERNEL
);
250 bd_rtc
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
251 if (!bd_rtc
->regmap
) {
252 dev_err(&pdev
->dev
, "No regmap\n");
256 bd_rtc
->dev
= &pdev
->dev
;
257 rtc_ops
= &bd71828_rtc_ops
;
260 case ROHM_CHIP_TYPE_BD71815
:
261 bd_rtc
->reg_time_start
= BD71815_REG_RTC_START
;
264 * See also BD718XX_ALM_EN_OFFSET:
265 * This works for BD71828 and BD71815 as they have same offset
266 * between ALM0 start and ALM0_MASK. If new ICs are to be
267 * added this requires proper check as ALM0_MASK is not located
268 * at the end of ALM0 block - but after all ALM blocks so if
269 * amount of ALMs differ the offset to enable/disable is likely
270 * to be incorrect and enable/disable must be given as own
273 bd_rtc
->bd718xx_alm_block_start
= BD71815_REG_RTC_ALM_START
;
274 hour_reg
= BD71815_REG_HOUR
;
276 case ROHM_CHIP_TYPE_BD71828
:
277 bd_rtc
->reg_time_start
= BD71828_REG_RTC_START
;
278 bd_rtc
->bd718xx_alm_block_start
= BD71828_REG_RTC_ALM_START
;
279 hour_reg
= BD71828_REG_RTC_HOUR
;
282 dev_err(&pdev
->dev
, "Unknown chip\n");
286 irq
= platform_get_irq_byname(pdev
, "bd70528-rtc-alm-0");
291 platform_set_drvdata(pdev
, bd_rtc
);
293 ret
= regmap_read(bd_rtc
->regmap
, hour_reg
, &hr
);
296 dev_err(&pdev
->dev
, "Failed to reag RTC clock\n");
300 if (!(hr
& BD70528_MASK_RTC_HOUR_24H
)) {
303 ret
= rtc_ops
->read_time(&pdev
->dev
, &t
);
306 ret
= rtc_ops
->set_time(&pdev
->dev
, &t
);
310 "Setting 24H clock for RTC failed\n");
315 device_set_wakeup_capable(&pdev
->dev
, true);
316 device_wakeup_enable(&pdev
->dev
);
318 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
320 dev_err(&pdev
->dev
, "RTC device creation failed\n");
324 rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
325 rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
328 /* Request alarm IRQ prior to registerig the RTC */
329 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
, NULL
, &alm_hndlr
,
330 IRQF_ONESHOT
, "bd70528-rtc", rtc
);
334 return devm_rtc_register_device(rtc
);
337 static const struct platform_device_id bd718x7_rtc_id
[] = {
338 { "bd71828-rtc", ROHM_CHIP_TYPE_BD71828
},
339 { "bd71815-rtc", ROHM_CHIP_TYPE_BD71815
},
342 MODULE_DEVICE_TABLE(platform
, bd718x7_rtc_id
);
344 static struct platform_driver bd70528_rtc
= {
346 .name
= "bd70528-rtc"
348 .probe
= bd70528_probe
,
349 .id_table
= bd718x7_rtc_id
,
352 module_platform_driver(bd70528_rtc
);
354 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
355 MODULE_DESCRIPTION("ROHM BD71828 and BD71815 PMIC RTC driver");
356 MODULE_LICENSE("GPL");
357 MODULE_ALIAS("platform:bd70528-rtc");