Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2//
  3// Copyright (C) 2018 ROHM Semiconductors
  4//
  5// RTC driver for ROHM BD71828 and BD71815 PMIC
  6
  7#include <linux/bcd.h>
  8#include <linux/mfd/rohm-bd71815.h>
  9#include <linux/mfd/rohm-bd71828.h>
 10#include <linux/module.h>
 11#include <linux/of.h>
 12#include <linux/platform_device.h>
 13#include <linux/regmap.h>
 14#include <linux/rtc.h>
 15
 16/*
 17 * On BD71828 and BD71815 the ALM0 MASK is 14 bytes after the ALM0
 18 * block start
 19 */
 20#define BD718XX_ALM_EN_OFFSET 14
 21
 22/*
 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.
 26 */
 27struct bd70528_rtc_day {
 28	u8 sec;
 29	u8 min;
 30	u8 hour;
 31} __packed;
 32
 33struct bd70528_rtc_data {
 34	struct bd70528_rtc_day time;
 35	u8 week;
 36	u8 day;
 37	u8 month;
 38	u8 year;
 39} __packed;
 40
 41struct bd71828_rtc_alm {
 42	struct bd70528_rtc_data alm0;
 43	struct bd70528_rtc_data alm1;
 
 
 
 
 44	u8 alm_mask;
 45	u8 alm1_mask;
 46} __packed;
 47
 48struct bd70528_rtc {
 49	struct rohm_regmap_dev *parent;
 50	struct regmap *regmap;
 51	struct device *dev;
 52	u8 reg_time_start;
 53	u8 bd718xx_alm_block_start;
 54};
 55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
 57{
 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);
 64}
 65
 66static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
 67{
 68	r->day &= ~BD70528_MASK_RTC_DAY;
 69	r->week &= ~BD70528_MASK_RTC_WEEK;
 70	r->month &= ~BD70528_MASK_RTC_MONTH;
 71	/*
 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.
 74	 */
 75	r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
 76
 77	tmday2rtc(t, &r->time);
 78	/*
 79	 * We do always set time in 24H mode.
 80	 */
 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);
 86}
 87
 88static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
 89{
 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);
 93	/*
 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
 96	 */
 97	if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
 98		t->tm_hour %= 12;
 99		if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
100			t->tm_hour += 12;
101	}
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);
106}
107
108static int bd71828_set_alarm(struct device *dev, struct rtc_wkalrm *a)
109{
 
 
110	int ret;
111	struct bd71828_rtc_alm alm;
112	struct bd70528_rtc *r = dev_get_drvdata(dev);
 
113
114	ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
115			       sizeof(alm));
 
 
 
 
 
 
 
116	if (ret) {
117		dev_err(dev, "Failed to read alarm regs\n");
118		return ret;
119	}
120
121	tm2rtc(&a->time, &alm.alm0);
 
122
123	if (!a->enabled)
124		alm.alm_mask &= ~BD70528_MASK_ALM_EN;
125	else
 
126		alm.alm_mask |= BD70528_MASK_ALM_EN;
 
 
127
128	ret = regmap_bulk_write(r->regmap, r->bd718xx_alm_block_start, &alm,
129				sizeof(alm));
 
 
 
 
 
 
 
130	if (ret)
131		dev_err(dev, "Failed to set alarm time\n");
132
133	return ret;
134
135}
136
137static int bd71828_read_alarm(struct device *dev, struct rtc_wkalrm *a)
138{
 
139	int ret;
140	struct bd71828_rtc_alm alm;
141	struct bd70528_rtc *r = dev_get_drvdata(dev);
 
142
143	ret = regmap_bulk_read(r->regmap, r->bd718xx_alm_block_start, &alm,
144			       sizeof(alm));
145	if (ret) {
146		dev_err(dev, "Failed to read alarm regs\n");
147		return ret;
148	}
149
150	rtc2tm(&alm.alm0, &a->time);
151	a->time.tm_mday = -1;
152	a->time.tm_mon = -1;
153	a->time.tm_year = -1;
154	a->enabled = !!(alm.alm_mask & BD70528_MASK_ALM_EN);
155	a->pending = 0;
156
157	return 0;
158}
159
160static int bd71828_set_time(struct device *dev, struct rtc_time *t)
161{
162	int ret;
163	struct bd70528_rtc_data rtc_data;
164	struct bd70528_rtc *r = dev_get_drvdata(dev);
 
165
166	ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
167			       sizeof(rtc_data));
168	if (ret) {
169		dev_err(dev, "Failed to read RTC time registers\n");
170		return ret;
 
 
 
 
 
 
 
171	}
172	tm2rtc(t, &rtc_data);
173
174	ret = regmap_bulk_write(r->regmap, r->reg_time_start, &rtc_data,
175				sizeof(rtc_data));
176	if (ret)
 
177		dev_err(dev, "Failed to set RTC time\n");
 
 
 
 
 
 
 
178
179	return ret;
180}
181
 
 
 
 
 
 
 
 
 
 
 
182static int bd70528_get_time(struct device *dev, struct rtc_time *t)
183{
184	struct bd70528_rtc *r = dev_get_drvdata(dev);
 
185	struct bd70528_rtc_data rtc_data;
186	int ret;
187
188	/* read the RTC date and time registers all at once */
189	ret = regmap_bulk_read(r->regmap, r->reg_time_start, &rtc_data,
 
190			       sizeof(rtc_data));
191	if (ret) {
192		dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
193		return ret;
194	}
195
196	rtc2tm(&rtc_data, t);
197
198	return 0;
199}
200
201static int bd71828_alm_enable(struct device *dev, unsigned int enabled)
202{
203	int ret;
204	struct bd70528_rtc *r = dev_get_drvdata(dev);
205	unsigned int enableval = BD70528_MASK_ALM_EN;
 
206
207	if (!enabled)
208		enableval = 0;
209
210	ret = regmap_update_bits(r->regmap, r->bd718xx_alm_block_start +
211				 BD718XX_ALM_EN_OFFSET, BD70528_MASK_ALM_EN,
212				 enableval);
 
 
 
 
 
213	if (ret)
214		dev_err(dev, "Failed to change alarm state\n");
215
 
 
216	return ret;
217}
218
219static 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,
225};
226
227static irqreturn_t alm_hndlr(int irq, void *data)
228{
229	struct rtc_device *rtc = data;
230
231	rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
232	return IRQ_HANDLED;
233}
234
235static int bd70528_probe(struct platform_device *pdev)
236{
237	struct bd70528_rtc *bd_rtc;
238	const struct rtc_class_ops *rtc_ops;
239	const char *irq_name;
240	int ret;
241	struct rtc_device *rtc;
242	int irq;
243	unsigned int hr;
244	u8 hour_reg;
245	enum rohm_chip_type chip = platform_get_device_id(pdev)->driver_data;
246
 
 
 
 
 
247	bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
248	if (!bd_rtc)
249		return -ENOMEM;
250
251	bd_rtc->regmap = dev_get_regmap(pdev->dev.parent, NULL);
252	if (!bd_rtc->regmap) {
253		dev_err(&pdev->dev, "No regmap\n");
254		return -EINVAL;
255	}
256
257	bd_rtc->dev = &pdev->dev;
258	rtc_ops = &bd71828_rtc_ops;
259
260	switch (chip) {
261	case ROHM_CHIP_TYPE_BD71815:
262		irq_name = "bd71815-rtc-alm-0";
263		bd_rtc->reg_time_start = BD71815_REG_RTC_START;
264
265		/*
266		 * See also BD718XX_ALM_EN_OFFSET:
267		 * This works for BD71828 and BD71815 as they have same offset
268		 * between ALM0 start and ALM0_MASK. If new ICs are to be
269		 * added this requires proper check as ALM0_MASK is not located
270		 * at the end of ALM0 block - but after all ALM blocks so if
271		 * amount of ALMs differ the offset to enable/disable is likely
272		 * to be incorrect and enable/disable must be given as own
273		 * reg address here.
274		 */
275		bd_rtc->bd718xx_alm_block_start = BD71815_REG_RTC_ALM_START;
276		hour_reg = BD71815_REG_HOUR;
277		break;
278	case ROHM_CHIP_TYPE_BD71828:
279		irq_name = "bd71828-rtc-alm-0";
280		bd_rtc->reg_time_start = BD71828_REG_RTC_START;
281		bd_rtc->bd718xx_alm_block_start = BD71828_REG_RTC_ALM_START;
282		hour_reg = BD71828_REG_RTC_HOUR;
283		break;
284	default:
285		dev_err(&pdev->dev, "Unknown chip\n");
286		return -ENOENT;
287	}
288
289	irq = platform_get_irq_byname(pdev, irq_name);
290
 
291	if (irq < 0)
292		return irq;
293
294	platform_set_drvdata(pdev, bd_rtc);
295
296	ret = regmap_read(bd_rtc->regmap, hour_reg, &hr);
297
298	if (ret) {
299		dev_err(&pdev->dev, "Failed to reag RTC clock\n");
300		return ret;
301	}
302
303	if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
304		struct rtc_time t;
305
306		ret = rtc_ops->read_time(&pdev->dev, &t);
307
308		if (!ret)
309			ret = rtc_ops->set_time(&pdev->dev, &t);
310
311		if (ret) {
312			dev_err(&pdev->dev,
313				"Setting 24H clock for RTC failed\n");
314			return ret;
315		}
316	}
317
318	device_set_wakeup_capable(&pdev->dev, true);
319	device_wakeup_enable(&pdev->dev);
320
321	rtc = devm_rtc_allocate_device(&pdev->dev);
322	if (IS_ERR(rtc)) {
323		dev_err(&pdev->dev, "RTC device creation failed\n");
324		return PTR_ERR(rtc);
325	}
326
327	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
328	rtc->range_max = RTC_TIMESTAMP_END_2099;
329	rtc->ops = rtc_ops;
330
331	/* Request alarm IRQ prior to registerig the RTC */
332	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
333					IRQF_ONESHOT, "bd70528-rtc", rtc);
334	if (ret)
335		return ret;
336
337	return devm_rtc_register_device(rtc);
338}
 
 
 
 
 
 
 
 
 
 
 
339
340static const struct platform_device_id bd718x7_rtc_id[] = {
341	{ "bd71828-rtc", ROHM_CHIP_TYPE_BD71828 },
342	{ "bd71815-rtc", ROHM_CHIP_TYPE_BD71815 },
343	{ },
344};
345MODULE_DEVICE_TABLE(platform, bd718x7_rtc_id);
346
347static struct platform_driver bd70528_rtc = {
348	.driver = {
349		.name = "bd70528-rtc"
350	},
351	.probe = bd70528_probe,
352	.id_table = bd718x7_rtc_id,
353};
354
355module_platform_driver(bd70528_rtc);
356
357MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
358MODULE_DESCRIPTION("ROHM BD71828 and BD71815 PMIC RTC driver");
359MODULE_LICENSE("GPL");
360MODULE_ALIAS("platform:bd70528-rtc");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2//
  3// Copyright (C) 2018 ROHM Semiconductors
  4//
  5// RTC driver for ROHM BD70528 PMIC
  6
  7#include <linux/bcd.h>
  8#include <linux/mfd/rohm-bd70528.h>
 
  9#include <linux/module.h>
 10#include <linux/of.h>
 11#include <linux/platform_device.h>
 12#include <linux/regmap.h>
 13#include <linux/rtc.h>
 14
 15/*
 
 
 
 
 
 
 16 * We read regs RTC_SEC => RTC_YEAR
 17 * this struct is ordered according to chip registers.
 18 * Keep it u8 only to avoid padding issues.
 19 */
 20struct bd70528_rtc_day {
 21	u8 sec;
 22	u8 min;
 23	u8 hour;
 24} __packed;
 25
 26struct bd70528_rtc_data {
 27	struct bd70528_rtc_day time;
 28	u8 week;
 29	u8 day;
 30	u8 month;
 31	u8 year;
 32} __packed;
 33
 34struct bd70528_rtc_wake {
 35	struct bd70528_rtc_day time;
 36	u8 ctrl;
 37} __packed;
 38
 39struct bd70528_rtc_alm {
 40	struct bd70528_rtc_data data;
 41	u8 alm_mask;
 42	u8 alm_repeat;
 43} __packed;
 44
 45struct bd70528_rtc {
 46	struct rohm_regmap_dev *mfd;
 
 47	struct device *dev;
 
 
 48};
 49
 50static int bd70528_set_wake(struct rohm_regmap_dev *bd70528,
 51			    int enable, int *old_state)
 52{
 53	int ret;
 54	unsigned int ctrl_reg;
 55
 56	ret = regmap_read(bd70528->regmap, BD70528_REG_WAKE_EN, &ctrl_reg);
 57	if (ret)
 58		return ret;
 59
 60	if (old_state) {
 61		if (ctrl_reg & BD70528_MASK_WAKE_EN)
 62			*old_state |= BD70528_WAKE_STATE_BIT;
 63		else
 64			*old_state &= ~BD70528_WAKE_STATE_BIT;
 65
 66		if (!enable == !(*old_state & BD70528_WAKE_STATE_BIT))
 67			return 0;
 68	}
 69
 70	if (enable)
 71		ctrl_reg |= BD70528_MASK_WAKE_EN;
 72	else
 73		ctrl_reg &= ~BD70528_MASK_WAKE_EN;
 74
 75	return regmap_write(bd70528->regmap, BD70528_REG_WAKE_EN,
 76			    ctrl_reg);
 77}
 78
 79static int bd70528_set_elapsed_tmr(struct rohm_regmap_dev *bd70528,
 80				   int enable, int *old_state)
 81{
 82	int ret;
 83	unsigned int ctrl_reg;
 84
 85	/*
 86	 * TBD
 87	 * What is the purpose of elapsed timer ?
 88	 * Is the timeout registers counting down, or is the disable - re-enable
 89	 * going to restart the elapsed-time counting? If counting is restarted
 90	 * the timeout should be decreased by the amount of time that has
 91	 * elapsed since starting the timer. Maybe we should store the monotonic
 92	 * clock value when timer is started so that if RTC is set while timer
 93	 * is armed we could do the compensation. This is a hack if RTC/system
 94	 * clk are drifting. OTOH, RTC controlled via I2C is in any case
 95	 * inaccurate...
 96	 */
 97	ret = regmap_read(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
 98			  &ctrl_reg);
 99	if (ret)
100		return ret;
101
102	if (old_state) {
103		if (ctrl_reg & BD70528_MASK_ELAPSED_TIMER_EN)
104			*old_state |= BD70528_ELAPSED_STATE_BIT;
105		else
106			*old_state &= ~BD70528_ELAPSED_STATE_BIT;
107
108		if ((!enable) == (!(*old_state & BD70528_ELAPSED_STATE_BIT)))
109			return 0;
110	}
111
112	if (enable)
113		ctrl_reg |= BD70528_MASK_ELAPSED_TIMER_EN;
114	else
115		ctrl_reg &= ~BD70528_MASK_ELAPSED_TIMER_EN;
116
117	return regmap_write(bd70528->regmap, BD70528_REG_ELAPSED_TIMER_EN,
118			    ctrl_reg);
119}
120
121static int bd70528_set_rtc_based_timers(struct bd70528_rtc *r, int new_state,
122					int *old_state)
123{
124	int ret;
125
126	ret = bd70528_wdt_set(r->mfd, new_state & BD70528_WDT_STATE_BIT,
127			      old_state);
128	if (ret) {
129		dev_err(r->dev,
130			"Failed to disable WDG for RTC setting (%d)\n", ret);
131		return ret;
132	}
133	ret = bd70528_set_elapsed_tmr(r->mfd,
134				      new_state & BD70528_ELAPSED_STATE_BIT,
135				      old_state);
136	if (ret) {
137		dev_err(r->dev,
138			"Failed to disable 'elapsed timer' for RTC setting\n");
139		return ret;
140	}
141	ret = bd70528_set_wake(r->mfd, new_state & BD70528_WAKE_STATE_BIT,
142			       old_state);
143	if (ret) {
144		dev_err(r->dev,
145			"Failed to disable 'wake timer' for RTC setting\n");
146		return ret;
147	}
148
149	return ret;
150}
151
152static int bd70528_re_enable_rtc_based_timers(struct bd70528_rtc *r,
153					      int old_state)
154{
155	return bd70528_set_rtc_based_timers(r, old_state, NULL);
156}
157
158static int bd70528_disable_rtc_based_timers(struct bd70528_rtc *r,
159					    int *old_state)
160{
161	return bd70528_set_rtc_based_timers(r, 0, old_state);
162}
163
164static inline void tmday2rtc(struct rtc_time *t, struct bd70528_rtc_day *d)
165{
166	d->sec &= ~BD70528_MASK_RTC_SEC;
167	d->min &= ~BD70528_MASK_RTC_MINUTE;
168	d->hour &= ~BD70528_MASK_RTC_HOUR;
169	d->sec |= bin2bcd(t->tm_sec);
170	d->min |= bin2bcd(t->tm_min);
171	d->hour |= bin2bcd(t->tm_hour);
172}
173
174static inline void tm2rtc(struct rtc_time *t, struct bd70528_rtc_data *r)
175{
176	r->day &= ~BD70528_MASK_RTC_DAY;
177	r->week &= ~BD70528_MASK_RTC_WEEK;
178	r->month &= ~BD70528_MASK_RTC_MONTH;
179	/*
180	 * PM and 24H bits are not used by Wake - thus we clear them
181	 * here and not in tmday2rtc() which is also used by wake.
182	 */
183	r->time.hour &= ~(BD70528_MASK_RTC_HOUR_PM | BD70528_MASK_RTC_HOUR_24H);
184
185	tmday2rtc(t, &r->time);
186	/*
187	 * We do always set time in 24H mode.
188	 */
189	r->time.hour |= BD70528_MASK_RTC_HOUR_24H;
190	r->day |= bin2bcd(t->tm_mday);
191	r->week |= bin2bcd(t->tm_wday);
192	r->month |= bin2bcd(t->tm_mon + 1);
193	r->year = bin2bcd(t->tm_year - 100);
194}
195
196static inline void rtc2tm(struct bd70528_rtc_data *r, struct rtc_time *t)
197{
198	t->tm_sec = bcd2bin(r->time.sec & BD70528_MASK_RTC_SEC);
199	t->tm_min = bcd2bin(r->time.min & BD70528_MASK_RTC_MINUTE);
200	t->tm_hour = bcd2bin(r->time.hour & BD70528_MASK_RTC_HOUR);
201	/*
202	 * If RTC is in 12H mode, then bit BD70528_MASK_RTC_HOUR_PM
203	 * is not BCD value but tells whether it is AM or PM
204	 */
205	if (!(r->time.hour & BD70528_MASK_RTC_HOUR_24H)) {
206		t->tm_hour %= 12;
207		if (r->time.hour & BD70528_MASK_RTC_HOUR_PM)
208			t->tm_hour += 12;
209	}
210	t->tm_mday = bcd2bin(r->day & BD70528_MASK_RTC_DAY);
211	t->tm_mon = bcd2bin(r->month & BD70528_MASK_RTC_MONTH) - 1;
212	t->tm_year = 100 + bcd2bin(r->year & BD70528_MASK_RTC_YEAR);
213	t->tm_wday = bcd2bin(r->week & BD70528_MASK_RTC_WEEK);
214}
215
216static int bd70528_set_alarm(struct device *dev, struct rtc_wkalrm *a)
217{
218	struct bd70528_rtc_wake wake;
219	struct bd70528_rtc_alm alm;
220	int ret;
 
221	struct bd70528_rtc *r = dev_get_drvdata(dev);
222	struct rohm_regmap_dev *bd70528 = r->mfd;
223
224	ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_WAKE_START,
225			       &wake, sizeof(wake));
226	if (ret) {
227		dev_err(dev, "Failed to read wake regs\n");
228		return ret;
229	}
230
231	ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_ALM_START,
232			       &alm, sizeof(alm));
233	if (ret) {
234		dev_err(dev, "Failed to read alarm regs\n");
235		return ret;
236	}
237
238	tm2rtc(&a->time, &alm.data);
239	tmday2rtc(&a->time, &wake.time);
240
241	if (a->enabled) {
242		alm.alm_mask &= ~BD70528_MASK_ALM_EN;
243		wake.ctrl |= BD70528_MASK_WAKE_EN;
244	} else {
245		alm.alm_mask |= BD70528_MASK_ALM_EN;
246		wake.ctrl &= ~BD70528_MASK_WAKE_EN;
247	}
248
249	ret = regmap_bulk_write(bd70528->regmap,
250				BD70528_REG_RTC_WAKE_START, &wake,
251				sizeof(wake));
252	if (ret) {
253		dev_err(dev, "Failed to set wake time\n");
254		return ret;
255	}
256	ret = regmap_bulk_write(bd70528->regmap, BD70528_REG_RTC_ALM_START,
257				&alm, sizeof(alm));
258	if (ret)
259		dev_err(dev, "Failed to set alarm time\n");
260
261	return ret;
 
262}
263
264static int bd70528_read_alarm(struct device *dev, struct rtc_wkalrm *a)
265{
266	struct bd70528_rtc_alm alm;
267	int ret;
 
268	struct bd70528_rtc *r = dev_get_drvdata(dev);
269	struct rohm_regmap_dev *bd70528 = r->mfd;
270
271	ret = regmap_bulk_read(bd70528->regmap, BD70528_REG_RTC_ALM_START,
272			       &alm, sizeof(alm));
273	if (ret) {
274		dev_err(dev, "Failed to read alarm regs\n");
275		return ret;
276	}
277
278	rtc2tm(&alm.data, &a->time);
279	a->time.tm_mday = -1;
280	a->time.tm_mon = -1;
281	a->time.tm_year = -1;
282	a->enabled = !(alm.alm_mask & BD70528_MASK_ALM_EN);
283	a->pending = 0;
284
285	return 0;
286}
287
288static int bd70528_set_time_locked(struct device *dev, struct rtc_time *t)
289{
290	int ret, tmpret, old_states;
291	struct bd70528_rtc_data rtc_data;
292	struct bd70528_rtc *r = dev_get_drvdata(dev);
293	struct rohm_regmap_dev *bd70528 = r->mfd;
294
295	ret = bd70528_disable_rtc_based_timers(r, &old_states);
296	if (ret)
 
 
297		return ret;
298
299	tmpret = regmap_bulk_read(bd70528->regmap,
300				  BD70528_REG_RTC_START, &rtc_data,
301				  sizeof(rtc_data));
302	if (tmpret) {
303		dev_err(dev, "Failed to read RTC time registers\n");
304		goto renable_out;
305	}
306	tm2rtc(t, &rtc_data);
307
308	tmpret = regmap_bulk_write(bd70528->regmap,
309				   BD70528_REG_RTC_START, &rtc_data,
310				   sizeof(rtc_data));
311	if (tmpret) {
312		dev_err(dev, "Failed to set RTC time\n");
313		goto renable_out;
314	}
315
316renable_out:
317	ret = bd70528_re_enable_rtc_based_timers(r, old_states);
318	if (tmpret)
319		ret = tmpret;
320
321	return ret;
322}
323
324static int bd70528_set_time(struct device *dev, struct rtc_time *t)
325{
326	int ret;
327	struct bd70528_rtc *r = dev_get_drvdata(dev);
328
329	bd70528_wdt_lock(r->mfd);
330	ret = bd70528_set_time_locked(dev, t);
331	bd70528_wdt_unlock(r->mfd);
332	return ret;
333}
334
335static int bd70528_get_time(struct device *dev, struct rtc_time *t)
336{
337	struct bd70528_rtc *r = dev_get_drvdata(dev);
338	struct rohm_regmap_dev *bd70528 = r->mfd;
339	struct bd70528_rtc_data rtc_data;
340	int ret;
341
342	/* read the RTC date and time registers all at once */
343	ret = regmap_bulk_read(bd70528->regmap,
344			       BD70528_REG_RTC_START, &rtc_data,
345			       sizeof(rtc_data));
346	if (ret) {
347		dev_err(dev, "Failed to read RTC time (err %d)\n", ret);
348		return ret;
349	}
350
351	rtc2tm(&rtc_data, t);
352
353	return 0;
354}
355
356static int bd70528_alm_enable(struct device *dev, unsigned int enabled)
357{
358	int ret;
 
359	unsigned int enableval = BD70528_MASK_ALM_EN;
360	struct bd70528_rtc *r = dev_get_drvdata(dev);
361
362	if (enabled)
363		enableval = 0;
364
365	bd70528_wdt_lock(r->mfd);
366	ret = bd70528_set_wake(r->mfd, enabled, NULL);
367	if (ret) {
368		dev_err(dev, "Failed to change wake state\n");
369		goto out_unlock;
370	}
371	ret = regmap_update_bits(r->mfd->regmap, BD70528_REG_RTC_ALM_MASK,
372				 BD70528_MASK_ALM_EN, enableval);
373	if (ret)
374		dev_err(dev, "Failed to change alarm state\n");
375
376out_unlock:
377	bd70528_wdt_unlock(r->mfd);
378	return ret;
379}
380
381static const struct rtc_class_ops bd70528_rtc_ops = {
382	.read_time		= bd70528_get_time,
383	.set_time		= bd70528_set_time,
384	.read_alarm		= bd70528_read_alarm,
385	.set_alarm		= bd70528_set_alarm,
386	.alarm_irq_enable	= bd70528_alm_enable,
387};
388
389static irqreturn_t alm_hndlr(int irq, void *data)
390{
391	struct rtc_device *rtc = data;
392
393	rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF | RTC_PF);
394	return IRQ_HANDLED;
395}
396
397static int bd70528_probe(struct platform_device *pdev)
398{
399	struct bd70528_rtc *bd_rtc;
400	struct rohm_regmap_dev *mfd;
 
401	int ret;
402	struct rtc_device *rtc;
403	int irq;
404	unsigned int hr;
 
 
405
406	mfd = dev_get_drvdata(pdev->dev.parent);
407	if (!mfd) {
408		dev_err(&pdev->dev, "No MFD driver data\n");
409		return -EINVAL;
410	}
411	bd_rtc = devm_kzalloc(&pdev->dev, sizeof(*bd_rtc), GFP_KERNEL);
412	if (!bd_rtc)
413		return -ENOMEM;
414
415	bd_rtc->mfd = mfd;
 
 
 
 
 
416	bd_rtc->dev = &pdev->dev;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
418	irq = platform_get_irq_byname(pdev, "bd70528-rtc-alm");
419	if (irq < 0)
420		return irq;
421
422	platform_set_drvdata(pdev, bd_rtc);
423
424	ret = regmap_read(mfd->regmap, BD70528_REG_RTC_HOUR, &hr);
425
426	if (ret) {
427		dev_err(&pdev->dev, "Failed to reag RTC clock\n");
428		return ret;
429	}
430
431	if (!(hr & BD70528_MASK_RTC_HOUR_24H)) {
432		struct rtc_time t;
433
434		ret = bd70528_get_time(&pdev->dev, &t);
435
436		if (!ret)
437			ret = bd70528_set_time(&pdev->dev, &t);
438
439		if (ret) {
440			dev_err(&pdev->dev,
441				"Setting 24H clock for RTC failed\n");
442			return ret;
443		}
444	}
445
446	device_set_wakeup_capable(&pdev->dev, true);
447	device_wakeup_enable(&pdev->dev);
448
449	rtc = devm_rtc_allocate_device(&pdev->dev);
450	if (IS_ERR(rtc)) {
451		dev_err(&pdev->dev, "RTC device creation failed\n");
452		return PTR_ERR(rtc);
453	}
454
455	rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
456	rtc->range_max = RTC_TIMESTAMP_END_2099;
457	rtc->ops = &bd70528_rtc_ops;
458
459	/* Request alarm IRQ prior to registerig the RTC */
460	ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, &alm_hndlr,
461					IRQF_ONESHOT, "bd70528-rtc", rtc);
462	if (ret)
463		return ret;
464
465	/*
466	 *  BD70528 irq controller is not touching the main mask register.
467	 *  So enable the RTC block interrupts at main level. We can just
468	 *  leave them enabled as irq-controller should disable irqs
469	 *  from sub-registers when IRQ is disabled or freed.
470	 */
471	ret = regmap_update_bits(mfd->regmap,
472				 BD70528_REG_INT_MAIN_MASK,
473				 BD70528_INT_RTC_MASK, 0);
474	if (ret) {
475		dev_err(&pdev->dev, "Failed to enable RTC interrupts\n");
476		return ret;
477	}
478
479	return rtc_register_device(rtc);
480}
 
 
 
 
481
482static struct platform_driver bd70528_rtc = {
483	.driver = {
484		.name = "bd70528-rtc"
485	},
486	.probe = bd70528_probe,
 
487};
488
489module_platform_driver(bd70528_rtc);
490
491MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
492MODULE_DESCRIPTION("BD70528 RTC driver");
493MODULE_LICENSE("GPL");