rtc-da9052.c 7.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Real time clock driver for DA9052
 *
 * Copyright(c) 2012 Dialog Semiconductor Ltd.
 *
 * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
18
#include <linux/err.h>
19
#include <linux/delay.h>
20 21 22 23

#include <linux/mfd/da9052/da9052.h>
#include <linux/mfd/da9052/reg.h>

24 25
#define rtc_err(rtc, fmt, ...) \
		dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
26

27 28
#define DA9052_GET_TIME_RETRIES 5

29 30 31 32 33
struct da9052_rtc {
	struct rtc_device *rtc;
	struct da9052 *da9052;
};

34
static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
35 36 37
{
	int ret;
	if (enable) {
38 39 40
		ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
				DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON,
				DA9052_ALARM_Y_ALARM_ON);
41
		if (ret != 0)
42
			rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
43
	} else {
44 45
		ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
			DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
46
		if (ret != 0)
47
			rtc_err(rtc, "Write error: %d\n", ret);
48 49 50 51 52 53 54 55
	}
	return ret;
}

static irqreturn_t da9052_rtc_irq(int irq, void *data)
{
	struct da9052_rtc *rtc = data;

56
	rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
57 58 59 60

	return IRQ_HANDLED;
}

61
static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
62 63
{
	int ret;
64 65 66
	uint8_t v[2][5];
	int idx = 1;
	int timeout = DA9052_GET_TIME_RETRIES;
67

68 69
	ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, &v[0][0]);
	if (ret) {
70
		rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
71 72 73
		return ret;
	}

74 75 76 77 78 79 80
	do {
		ret = da9052_group_read(rtc->da9052,
					DA9052_ALARM_MI_REG, 5, &v[idx][0]);
		if (ret) {
			rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
			return ret;
		}
81

82 83 84 85 86 87
		if (memcmp(&v[0][0], &v[1][0], 5) == 0) {
			rtc_tm->tm_year = (v[0][4] & DA9052_RTC_YEAR) + 100;
			rtc_tm->tm_mon  = (v[0][3] & DA9052_RTC_MONTH) - 1;
			rtc_tm->tm_mday = v[0][2] & DA9052_RTC_DAY;
			rtc_tm->tm_hour = v[0][1] & DA9052_RTC_HOUR;
			rtc_tm->tm_min  = v[0][0] & DA9052_RTC_MIN;
88
			rtc_tm->tm_sec = 0;
89 90 91 92 93 94 95 96 97 98 99 100 101

			ret = rtc_valid_tm(rtc_tm);
			return ret;
		}

		idx = (1-idx);
		msleep(20);

	} while (timeout--);

	rtc_err(rtc, "Timed out reading alarm time\n");

	return -EIO;
102 103
}

104
static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
105
{
106 107
	struct da9052 *da9052 = rtc->da9052;
	unsigned long alm_time;
108 109 110
	int ret;
	uint8_t v[3];

111 112 113 114 115 116 117 118 119 120
	ret = rtc_tm_to_time(rtc_tm, &alm_time);
	if (ret != 0)
		return ret;

	if (rtc_tm->tm_sec > 0) {
		alm_time += 60 - rtc_tm->tm_sec;
		rtc_time_to_tm(alm_time, rtc_tm);
	}
	BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */

121 122 123 124 125 126
	rtc_tm->tm_year -= 100;
	rtc_tm->tm_mon += 1;

	ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
				DA9052_RTC_MIN, rtc_tm->tm_min);
	if (ret != 0) {
127
		rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
128 129 130 131 132 133 134 135 136 137 138 139 140 141
		return ret;
	}

	v[0] = rtc_tm->tm_hour;
	v[1] = rtc_tm->tm_mday;
	v[2] = rtc_tm->tm_mon;

	ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
	if (ret < 0)
		return ret;

	ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
				DA9052_RTC_YEAR, rtc_tm->tm_year);
	if (ret != 0)
142
		rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
143 144 145 146

	return ret;
}

147
static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
148 149 150
{
	int ret;

151
	ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
152
	if (ret < 0) {
153
		rtc_err(rtc, "Failed to read ALM: %d\n", ret);
154 155
		return ret;
	}
156 157

	return !!(ret&DA9052_ALARM_Y_ALARM_ON);
158 159 160 161 162 163
}

static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
{
	struct da9052_rtc *rtc = dev_get_drvdata(dev);
	int ret;
164 165 166
	uint8_t v[2][6];
	int idx = 1;
	int timeout = DA9052_GET_TIME_RETRIES;
167

168 169
	ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, &v[0][0]);
	if (ret) {
170
		rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
171 172 173
		return ret;
	}

174 175 176 177 178 179 180
	do {
		ret = da9052_group_read(rtc->da9052,
					DA9052_COUNT_S_REG, 6, &v[idx][0]);
		if (ret) {
			rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
			return ret;
		}
181

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
		if (memcmp(&v[0][0], &v[1][0], 6) == 0) {
			rtc_tm->tm_year = (v[0][5] & DA9052_RTC_YEAR) + 100;
			rtc_tm->tm_mon  = (v[0][4] & DA9052_RTC_MONTH) - 1;
			rtc_tm->tm_mday = v[0][3] & DA9052_RTC_DAY;
			rtc_tm->tm_hour = v[0][2] & DA9052_RTC_HOUR;
			rtc_tm->tm_min  = v[0][1] & DA9052_RTC_MIN;
			rtc_tm->tm_sec  = v[0][0] & DA9052_RTC_SEC;

			ret = rtc_valid_tm(rtc_tm);
			return ret;
		}

		idx = (1-idx);
		msleep(20);

	} while (timeout--);

	rtc_err(rtc, "Timed out reading time\n");

	return -EIO;
202 203 204 205 206 207
}

static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
	struct da9052_rtc *rtc;
	uint8_t v[6];
208
	int ret;
209

210 211 212 213
	/* DA9052 only has 6 bits for year - to represent 2000-2063 */
	if ((tm->tm_year < 100) || (tm->tm_year > 163))
		return -EINVAL;

214 215 216 217 218 219 220 221 222
	rtc = dev_get_drvdata(dev);

	v[0] = tm->tm_sec;
	v[1] = tm->tm_min;
	v[2] = tm->tm_hour;
	v[3] = tm->tm_mday;
	v[4] = tm->tm_mon + 1;
	v[5] = tm->tm_year - 100;

223 224 225 226
	ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
	if (ret < 0)
		rtc_err(rtc, "failed to set RTC time: %d\n", ret);
	return ret;
227 228 229 230 231 232 233 234
}

static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	int ret;
	struct rtc_time *tm = &alrm->time;
	struct da9052_rtc *rtc = dev_get_drvdata(dev);

235 236 237
	ret = da9052_read_alarm(rtc, tm);
	if (ret < 0) {
		rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
238
		return ret;
239
	}
240

241
	alrm->enabled = da9052_rtc_get_alarm_status(rtc);
242 243 244 245 246 247 248 249 250
	return 0;
}

static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
{
	int ret;
	struct rtc_time *tm = &alrm->time;
	struct da9052_rtc *rtc = dev_get_drvdata(dev);

251 252 253 254
	/* DA9052 only has 6 bits for year - to represent 2000-2063 */
	if ((tm->tm_year < 100) || (tm->tm_year > 163))
		return -EINVAL;

255
	ret = da9052_rtc_enable_alarm(rtc, 0);
256 257 258
	if (ret < 0)
		return ret;

259 260
	ret = da9052_set_alarm(rtc, tm);
	if (ret < 0)
261 262
		return ret;

263
	ret = da9052_rtc_enable_alarm(rtc, 1);
264 265 266 267 268 269 270
	return ret;
}

static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
{
	struct da9052_rtc *rtc = dev_get_drvdata(dev);

271
	return da9052_rtc_enable_alarm(rtc, enabled);
272 273 274 275 276 277 278 279 280 281
}

static const struct rtc_class_ops da9052_rtc_ops = {
	.read_time	= da9052_rtc_read_time,
	.set_time	= da9052_rtc_set_time,
	.read_alarm	= da9052_rtc_read_alarm,
	.set_alarm	= da9052_rtc_set_alarm,
	.alarm_irq_enable = da9052_rtc_alarm_irq_enable,
};

282
static int da9052_rtc_probe(struct platform_device *pdev)
283 284 285 286 287 288 289 290 291 292
{
	struct da9052_rtc *rtc;
	int ret;

	rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
	if (!rtc)
		return -ENOMEM;

	rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
	platform_set_drvdata(pdev, rtc);
293 294 295 296 297 298 299 300 301 302 303 304 305

	ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE);
	if (ret < 0) {
		rtc_err(rtc,
			"Failed to setup RTC battery charging: %d\n", ret);
		return ret;
	}

	ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
				DA9052_ALARM_Y_TICK_ON, 0);
	if (ret != 0)
		rtc_err(rtc, "Failed to disable TICKS: %d\n", ret);

306 307 308 309 310 311 312
	device_init_wakeup(&pdev->dev, true);
	rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
				       &da9052_rtc_ops, THIS_MODULE);

	if (IS_ERR(rtc->rtc))
		return PTR_ERR(rtc->rtc);

313
	ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
314
				da9052_rtc_irq, rtc);
315
	if (ret != 0) {
316
		rtc_err(rtc, "irq registration failed: %d\n", ret);
317
		return ret;
318 319
	}

320
	return 0;
321 322 323 324 325 326 327 328 329 330 331
}

static struct platform_driver da9052_rtc_driver = {
	.probe	= da9052_rtc_probe,
	.driver = {
		.name	= "da9052-rtc",
	},
};

module_platform_driver(da9052_rtc_driver);

332
MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
333 334 335
MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:da9052-rtc");