Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * rtc-tps80031.c -- TI TPS80031/TPS80032 RTC driver
  4 *
  5 * RTC driver for TI TPS80031/TPS80032 Fully Integrated
  6 * Power Management with Power Path and Battery Charger
  7 *
  8 * Copyright (c) 2012, NVIDIA Corporation.
  9 *
 10 * Author: Laxman Dewangan <ldewangan@nvidia.com>
 11 */
 12
 13#include <linux/bcd.h>
 14#include <linux/device.h>
 15#include <linux/err.h>
 16#include <linux/init.h>
 17#include <linux/kernel.h>
 18#include <linux/module.h>
 19#include <linux/mfd/tps80031.h>
 20#include <linux/platform_device.h>
 21#include <linux/pm.h>
 22#include <linux/rtc.h>
 23#include <linux/slab.h>
 24
 25#define ENABLE_ALARM_INT			0x08
 26#define ALARM_INT_STATUS			0x40
 27
 28/**
 29 * Setting bit to 1 in STOP_RTC will run the RTC and
 30 * setting this bit to 0 will freeze RTC.
 31 */
 32#define STOP_RTC				0x1
 33
 34/* Power on reset Values of RTC registers */
 35#define TPS80031_RTC_POR_YEAR			0
 36#define TPS80031_RTC_POR_MONTH			1
 37#define TPS80031_RTC_POR_DAY			1
 38
 39/* Numbers of registers for time and alarms */
 40#define TPS80031_RTC_TIME_NUM_REGS		7
 41#define TPS80031_RTC_ALARM_NUM_REGS		6
 42
 43/**
 44 * PMU RTC have only 2 nibbles to store year information, so using an
 45 * offset of 100 to set the base year as 2000 for our driver.
 46 */
 47#define RTC_YEAR_OFFSET 100
 48
 49struct tps80031_rtc {
 50	struct rtc_device	*rtc;
 51	int			irq;
 52};
 53
 54static int tps80031_rtc_read_time(struct device *dev, struct rtc_time *tm)
 55{
 56	u8 buff[TPS80031_RTC_TIME_NUM_REGS];
 57	int ret;
 58
 59	ret = tps80031_reads(dev->parent, TPS80031_SLAVE_ID1,
 60			TPS80031_SECONDS_REG, TPS80031_RTC_TIME_NUM_REGS, buff);
 61	if (ret < 0) {
 62		dev_err(dev, "reading RTC_SECONDS_REG failed, err = %d\n", ret);
 63		return ret;
 64	}
 65
 66	tm->tm_sec = bcd2bin(buff[0]);
 67	tm->tm_min = bcd2bin(buff[1]);
 68	tm->tm_hour = bcd2bin(buff[2]);
 69	tm->tm_mday = bcd2bin(buff[3]);
 70	tm->tm_mon = bcd2bin(buff[4]) - 1;
 71	tm->tm_year = bcd2bin(buff[5]) + RTC_YEAR_OFFSET;
 72	tm->tm_wday = bcd2bin(buff[6]);
 73	return 0;
 74}
 75
 76static int tps80031_rtc_set_time(struct device *dev, struct rtc_time *tm)
 77{
 78	u8 buff[7];
 79	int ret;
 80
 81	buff[0] = bin2bcd(tm->tm_sec);
 82	buff[1] = bin2bcd(tm->tm_min);
 83	buff[2] = bin2bcd(tm->tm_hour);
 84	buff[3] = bin2bcd(tm->tm_mday);
 85	buff[4] = bin2bcd(tm->tm_mon + 1);
 86	buff[5] = bin2bcd(tm->tm_year % RTC_YEAR_OFFSET);
 87	buff[6] = bin2bcd(tm->tm_wday);
 88
 89	/* Stop RTC while updating the RTC time registers */
 90	ret = tps80031_clr_bits(dev->parent, TPS80031_SLAVE_ID1,
 91				TPS80031_RTC_CTRL_REG, STOP_RTC);
 92	if (ret < 0) {
 93		dev_err(dev->parent, "Stop RTC failed, err = %d\n", ret);
 94		return ret;
 95	}
 96
 97	ret = tps80031_writes(dev->parent, TPS80031_SLAVE_ID1,
 98			TPS80031_SECONDS_REG,
 99			TPS80031_RTC_TIME_NUM_REGS, buff);
100	if (ret < 0) {
101		dev_err(dev, "writing RTC_SECONDS_REG failed, err %d\n", ret);
102		return ret;
103	}
104
105	ret = tps80031_set_bits(dev->parent, TPS80031_SLAVE_ID1,
106				TPS80031_RTC_CTRL_REG, STOP_RTC);
107	if (ret < 0)
108		dev_err(dev->parent, "Start RTC failed, err = %d\n", ret);
109	return ret;
110}
111
112static int tps80031_rtc_alarm_irq_enable(struct device *dev,
113					 unsigned int enable)
114{
115	int ret;
116
117	if (enable)
118		ret = tps80031_set_bits(dev->parent, TPS80031_SLAVE_ID1,
119				TPS80031_RTC_INTERRUPTS_REG, ENABLE_ALARM_INT);
120	else
121		ret = tps80031_clr_bits(dev->parent, TPS80031_SLAVE_ID1,
122				TPS80031_RTC_INTERRUPTS_REG, ENABLE_ALARM_INT);
123	if (ret < 0) {
124		dev_err(dev, "Update on RTC_INT failed, err = %d\n", ret);
125		return ret;
126	}
127	return 0;
128}
129
130static int tps80031_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
131{
132	u8 buff[TPS80031_RTC_ALARM_NUM_REGS];
133	int ret;
134
135	buff[0] = bin2bcd(alrm->time.tm_sec);
136	buff[1] = bin2bcd(alrm->time.tm_min);
137	buff[2] = bin2bcd(alrm->time.tm_hour);
138	buff[3] = bin2bcd(alrm->time.tm_mday);
139	buff[4] = bin2bcd(alrm->time.tm_mon + 1);
140	buff[5] = bin2bcd(alrm->time.tm_year % RTC_YEAR_OFFSET);
141	ret = tps80031_writes(dev->parent, TPS80031_SLAVE_ID1,
142			TPS80031_ALARM_SECONDS_REG,
143			TPS80031_RTC_ALARM_NUM_REGS, buff);
144	if (ret < 0) {
145		dev_err(dev, "Writing RTC_ALARM failed, err %d\n", ret);
146		return ret;
147	}
148	return tps80031_rtc_alarm_irq_enable(dev, alrm->enabled);
149}
150
151static int tps80031_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
152{
153	u8 buff[6];
154	int ret;
155
156	ret = tps80031_reads(dev->parent, TPS80031_SLAVE_ID1,
157			TPS80031_ALARM_SECONDS_REG,
158			TPS80031_RTC_ALARM_NUM_REGS, buff);
159	if (ret < 0) {
160		dev_err(dev->parent,
161			"reading RTC_ALARM failed, err = %d\n", ret);
162		return ret;
163	}
164
165	alrm->time.tm_sec = bcd2bin(buff[0]);
166	alrm->time.tm_min = bcd2bin(buff[1]);
167	alrm->time.tm_hour = bcd2bin(buff[2]);
168	alrm->time.tm_mday = bcd2bin(buff[3]);
169	alrm->time.tm_mon = bcd2bin(buff[4]) - 1;
170	alrm->time.tm_year = bcd2bin(buff[5]) + RTC_YEAR_OFFSET;
171	return 0;
172}
173
174static int clear_alarm_int_status(struct device *dev, struct tps80031_rtc *rtc)
175{
176	int ret;
177	u8 buf;
178
179	/**
180	 * As per datasheet, A dummy read of this  RTC_STATUS_REG register
181	 * is necessary before each I2C read in order to update the status
182	 * register value.
183	 */
184	ret = tps80031_read(dev->parent, TPS80031_SLAVE_ID1,
185				TPS80031_RTC_STATUS_REG, &buf);
186	if (ret < 0) {
187		dev_err(dev, "reading RTC_STATUS failed. err = %d\n", ret);
188		return ret;
189	}
190
191	/* clear Alarm status bits.*/
192	ret = tps80031_set_bits(dev->parent, TPS80031_SLAVE_ID1,
193			TPS80031_RTC_STATUS_REG, ALARM_INT_STATUS);
194	if (ret < 0) {
195		dev_err(dev, "clear Alarm INT failed, err = %d\n", ret);
196		return ret;
197	}
198	return 0;
199}
200
201static irqreturn_t tps80031_rtc_irq(int irq, void *data)
202{
203	struct device *dev = data;
204	struct tps80031_rtc *rtc = dev_get_drvdata(dev);
205	int ret;
206
207	ret = clear_alarm_int_status(dev, rtc);
208	if (ret < 0)
209		return ret;
210
211	rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
212	return IRQ_HANDLED;
213}
214
215static const struct rtc_class_ops tps80031_rtc_ops = {
216	.read_time = tps80031_rtc_read_time,
217	.set_time = tps80031_rtc_set_time,
218	.set_alarm = tps80031_rtc_set_alarm,
219	.read_alarm = tps80031_rtc_read_alarm,
220	.alarm_irq_enable = tps80031_rtc_alarm_irq_enable,
221};
222
223static int tps80031_rtc_probe(struct platform_device *pdev)
224{
225	struct tps80031_rtc *rtc;
226	struct rtc_time tm;
227	int ret;
228
229	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
230	if (!rtc)
231		return -ENOMEM;
232
233	rtc->irq = platform_get_irq(pdev, 0);
234	platform_set_drvdata(pdev, rtc);
235
236	/* Start RTC */
237	ret = tps80031_set_bits(pdev->dev.parent, TPS80031_SLAVE_ID1,
238			TPS80031_RTC_CTRL_REG, STOP_RTC);
239	if (ret < 0) {
240		dev_err(&pdev->dev, "failed to start RTC. err = %d\n", ret);
241		return ret;
242	}
243
244	/* If RTC have POR values, set time 01:01:2000 */
245	tps80031_rtc_read_time(&pdev->dev, &tm);
246	if ((tm.tm_year == RTC_YEAR_OFFSET + TPS80031_RTC_POR_YEAR) &&
247		(tm.tm_mon == (TPS80031_RTC_POR_MONTH - 1)) &&
248		(tm.tm_mday == TPS80031_RTC_POR_DAY)) {
249		tm.tm_year = 2000;
250		tm.tm_mday = 1;
251		tm.tm_mon = 1;
252		ret = tps80031_rtc_set_time(&pdev->dev, &tm);
253		if (ret < 0) {
254			dev_err(&pdev->dev,
255				"RTC set time failed, err = %d\n", ret);
256			return ret;
257		}
258	}
259
260	/* Clear alarm intretupt status if it is there */
261	ret = clear_alarm_int_status(&pdev->dev, rtc);
262	if (ret < 0) {
263		dev_err(&pdev->dev, "Clear alarm int failed, err = %d\n", ret);
264		return ret;
265	}
266
267	rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
268			       &tps80031_rtc_ops, THIS_MODULE);
269	if (IS_ERR(rtc->rtc)) {
270		ret = PTR_ERR(rtc->rtc);
271		dev_err(&pdev->dev, "RTC registration failed, err %d\n", ret);
272		return ret;
273	}
274
275	ret = devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
276			tps80031_rtc_irq,
277			IRQF_ONESHOT,
278			dev_name(&pdev->dev), rtc);
279	if (ret < 0) {
280		dev_err(&pdev->dev, "request IRQ:%d failed, err = %d\n",
281			 rtc->irq, ret);
282		return ret;
283	}
284	device_set_wakeup_capable(&pdev->dev, 1);
285	return 0;
286}
287
288#ifdef CONFIG_PM_SLEEP
289static int tps80031_rtc_suspend(struct device *dev)
290{
291	struct tps80031_rtc *rtc = dev_get_drvdata(dev);
292
293	if (device_may_wakeup(dev))
294		enable_irq_wake(rtc->irq);
295	return 0;
296}
297
298static int tps80031_rtc_resume(struct device *dev)
299{
300	struct tps80031_rtc *rtc = dev_get_drvdata(dev);
301
302	if (device_may_wakeup(dev))
303		disable_irq_wake(rtc->irq);
304	return 0;
305};
306#endif
307
308static SIMPLE_DEV_PM_OPS(tps80031_pm_ops, tps80031_rtc_suspend,
309			tps80031_rtc_resume);
310
311static struct platform_driver tps80031_rtc_driver = {
312	.driver	= {
313		.name	= "tps80031-rtc",
314		.pm	= &tps80031_pm_ops,
315	},
316	.probe	= tps80031_rtc_probe,
317};
318
319module_platform_driver(tps80031_rtc_driver);
320
321MODULE_ALIAS("platform:tps80031-rtc");
322MODULE_DESCRIPTION("TI TPS80031/TPS80032 RTC driver");
323MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
324MODULE_LICENSE("GPL v2");