blob: e3aea00c3145746fd32abe405f091142856d6633 [file] [log] [blame]
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02001/* drivers/rtc/rtc-max6902.c
Raphael Assenat8e12ecc2006-06-25 05:48:23 -07002 *
3 * Copyright (C) 2006 8D Technologies inc.
4 * Copyright (C) 2004 Compulab Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Driver for MAX6902 spi RTC
11 *
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070012 */
13
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070014#include <linux/module.h>
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070015#include <linux/kernel.h>
16#include <linux/platform_device.h>
17#include <linux/init.h>
18#include <linux/rtc.h>
19#include <linux/spi/spi.h>
20#include <linux/bcd.h>
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070021
22#define MAX6902_REG_SECONDS 0x01
23#define MAX6902_REG_MINUTES 0x03
24#define MAX6902_REG_HOURS 0x05
25#define MAX6902_REG_DATE 0x07
26#define MAX6902_REG_MONTH 0x09
27#define MAX6902_REG_DAY 0x0B
28#define MAX6902_REG_YEAR 0x0D
29#define MAX6902_REG_CONTROL 0x0F
30#define MAX6902_REG_CENTURY 0x13
31
Alessandro Zummoa5771c62009-01-06 14:42:19 -080032static int max6902_set_reg(struct device *dev, unsigned char address,
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070033 unsigned char data)
34{
35 struct spi_device *spi = to_spi_device(dev);
36 unsigned char buf[2];
37
38 /* MSB must be '0' to write */
39 buf[0] = address & 0x7f;
40 buf[1] = data;
41
Alessandro Zummoa5771c62009-01-06 14:42:19 -080042 return spi_write_then_read(spi, buf, 2, NULL, 0);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070043}
44
45static int max6902_get_reg(struct device *dev, unsigned char address,
46 unsigned char *data)
47{
48 struct spi_device *spi = to_spi_device(dev);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070049
50 /* Set MSB to indicate read */
Alessandro Zummoa5771c62009-01-06 14:42:19 -080051 *data = address | 0x80;
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070052
Alessandro Zummoa5771c62009-01-06 14:42:19 -080053 return spi_write_then_read(spi, data, 1, data, 1);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070054}
55
Alessandro Zummoa5771c62009-01-06 14:42:19 -080056static int max6902_read_time(struct device *dev, struct rtc_time *dt)
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070057{
Alessandro Zummoa5771c62009-01-06 14:42:19 -080058 int err, century;
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070059 struct spi_device *spi = to_spi_device(dev);
Alessandro Zummoa5771c62009-01-06 14:42:19 -080060 unsigned char buf[8];
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070061
Alessandro Zummoa5771c62009-01-06 14:42:19 -080062 buf[0] = 0xbf; /* Burst read */
63
64 err = spi_write_then_read(spi, buf, 1, buf, 8);
65 if (err != 0)
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070066 return err;
67
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070068 /* The chip sends data in this order:
69 * Seconds, Minutes, Hours, Date, Month, Day, Year */
Alessandro Zummoa5771c62009-01-06 14:42:19 -080070 dt->tm_sec = bcd2bin(buf[0]);
71 dt->tm_min = bcd2bin(buf[1]);
72 dt->tm_hour = bcd2bin(buf[2]);
73 dt->tm_mday = bcd2bin(buf[3]);
74 dt->tm_mon = bcd2bin(buf[4]) - 1;
75 dt->tm_wday = bcd2bin(buf[5]);
76 dt->tm_year = bcd2bin(buf[6]);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070077
Alessandro Zummoa5771c62009-01-06 14:42:19 -080078 /* Read century */
79 err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &buf[0]);
80 if (err != 0)
81 return err;
82
83 century = bcd2bin(buf[0]) * 100;
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070084
85 dt->tm_year += century;
86 dt->tm_year -= 1900;
87
Alessandro Zummoa5771c62009-01-06 14:42:19 -080088 return rtc_valid_tm(dt);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070089}
90
Alessandro Zummoa5771c62009-01-06 14:42:19 -080091static int max6902_set_time(struct device *dev, struct rtc_time *dt)
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070092{
Alessandro Zummoa5771c62009-01-06 14:42:19 -080093 dt->tm_year = dt->tm_year + 1900;
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070094
95 /* Remove write protection */
Axel Lin08348d22013-04-29 16:20:57 -070096 max6902_set_reg(dev, MAX6902_REG_CONTROL, 0);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -070097
Axel Lin08348d22013-04-29 16:20:57 -070098 max6902_set_reg(dev, MAX6902_REG_SECONDS, bin2bcd(dt->tm_sec));
99 max6902_set_reg(dev, MAX6902_REG_MINUTES, bin2bcd(dt->tm_min));
100 max6902_set_reg(dev, MAX6902_REG_HOURS, bin2bcd(dt->tm_hour));
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700101
Axel Lin08348d22013-04-29 16:20:57 -0700102 max6902_set_reg(dev, MAX6902_REG_DATE, bin2bcd(dt->tm_mday));
103 max6902_set_reg(dev, MAX6902_REG_MONTH, bin2bcd(dt->tm_mon + 1));
104 max6902_set_reg(dev, MAX6902_REG_DAY, bin2bcd(dt->tm_wday));
105 max6902_set_reg(dev, MAX6902_REG_YEAR, bin2bcd(dt->tm_year % 100));
106 max6902_set_reg(dev, MAX6902_REG_CENTURY, bin2bcd(dt->tm_year / 100));
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700107
108 /* Compulab used a delay here. However, the datasheet
109 * does not mention a delay being required anywhere... */
110 /* delay(2000); */
111
112 /* Write protect */
Axel Lin08348d22013-04-29 16:20:57 -0700113 max6902_set_reg(dev, MAX6902_REG_CONTROL, 0x80);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700114
115 return 0;
116}
117
David Brownellff8371a2006-09-30 23:28:17 -0700118static const struct rtc_class_ops max6902_rtc_ops = {
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700119 .read_time = max6902_read_time,
120 .set_time = max6902_set_time,
121};
122
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800123static int max6902_probe(struct spi_device *spi)
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700124{
125 struct rtc_device *rtc;
126 unsigned char tmp;
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700127 int res;
128
Alessandro Zummoa5771c62009-01-06 14:42:19 -0800129 spi->mode = SPI_MODE_3;
130 spi->bits_per_word = 8;
131 spi_setup(spi);
132
133 res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
134 if (res != 0)
135 return res;
136
Jingoo Han04353a72013-04-29 16:19:43 -0700137 rtc = devm_rtc_device_register(&spi->dev, "max6902",
138 &max6902_rtc_ops, THIS_MODULE);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700139 if (IS_ERR(rtc))
140 return PTR_ERR(rtc);
141
Jingoo Hana5ef73f2013-04-29 16:20:22 -0700142 spi_set_drvdata(spi, rtc);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700143 return 0;
144}
145
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800146static int max6902_remove(struct spi_device *spi)
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700147{
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700148 return 0;
149}
150
151static struct spi_driver max6902_driver = {
152 .driver = {
David Brownell5c076fc2007-08-22 14:01:57 -0700153 .name = "rtc-max6902",
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700154 .owner = THIS_MODULE,
155 },
David Brownell5c076fc2007-08-22 14:01:57 -0700156 .probe = max6902_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800157 .remove = max6902_remove,
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700158};
159
Axel Lin109e9412012-03-23 15:02:30 -0700160module_spi_driver(max6902_driver);
Raphael Assenat8e12ecc2006-06-25 05:48:23 -0700161
162MODULE_DESCRIPTION ("max6902 spi RTC driver");
163MODULE_AUTHOR ("Raphael Assenat");
164MODULE_LICENSE ("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700165MODULE_ALIAS("spi:rtc-max6902");