2 * Driver for TI BQ32000 RTC.
4 * Copyright (C) 2009 Semihalf.
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.
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/rtc.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/bcd.h>
18 #define BQ32K_SECONDS 0x00 /* Seconds register address */
19 #define BQ32K_SECONDS_MASK 0x7F /* Mask over seconds value */
20 #define BQ32K_STOP 0x80 /* Oscillator Stop flat */
22 #define BQ32K_MINUTES 0x01 /* Minutes register address */
23 #define BQ32K_MINUTES_MASK 0x7F /* Mask over minutes value */
24 #define BQ32K_OF 0x80 /* Oscillator Failure flag */
26 #define BQ32K_HOURS_MASK 0x3F /* Mask over hours value */
27 #define BQ32K_CENT 0x40 /* Century flag */
28 #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */
40 static struct i2c_driver bq32k_driver;
42 static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
44 struct i2c_client *client = to_i2c_client(dev);
45 struct i2c_msg msgs[] = {
59 if (i2c_transfer(client->adapter, msgs, 2) == 2)
65 static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
67 struct i2c_client *client = to_i2c_client(dev);
68 uint8_t buffer[len + 1];
71 memcpy(&buffer[1], data, len);
73 if (i2c_master_send(client, buffer, len + 1) == len + 1)
79 static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
81 struct bq32k_regs regs;
84 error = bq32k_read(dev, ®s, 0, sizeof(regs));
88 tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
89 tm->tm_min = bcd2bin(regs.minutes & BQ32K_SECONDS_MASK);
90 tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
91 tm->tm_mday = bcd2bin(regs.date);
92 tm->tm_wday = bcd2bin(regs.day) - 1;
93 tm->tm_mon = bcd2bin(regs.month) - 1;
94 tm->tm_year = bcd2bin(regs.years) +
95 ((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
97 return rtc_valid_tm(tm);
100 static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
102 struct bq32k_regs regs;
104 regs.seconds = bin2bcd(tm->tm_sec);
105 regs.minutes = bin2bcd(tm->tm_min);
106 regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
107 regs.day = bin2bcd(tm->tm_wday + 1);
108 regs.date = bin2bcd(tm->tm_mday);
109 regs.month = bin2bcd(tm->tm_mon + 1);
111 if (tm->tm_year >= 100) {
112 regs.cent_hours |= BQ32K_CENT;
113 regs.years = bin2bcd(tm->tm_year - 100);
115 regs.years = bin2bcd(tm->tm_year);
117 return bq32k_write(dev, ®s, 0, sizeof(regs));
120 static const struct rtc_class_ops bq32k_rtc_ops = {
121 .read_time = bq32k_rtc_read_time,
122 .set_time = bq32k_rtc_set_time,
125 static int bq32k_probe(struct i2c_client *client,
126 const struct i2c_device_id *id)
128 struct device *dev = &client->dev;
129 struct rtc_device *rtc;
133 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
136 /* Check Oscillator Stop flag */
137 error = bq32k_read(dev, ®, BQ32K_SECONDS, 1);
138 if (!error && (reg & BQ32K_STOP)) {
139 dev_warn(dev, "Oscillator was halted. Restarting...\n");
141 error = bq32k_write(dev, ®, BQ32K_SECONDS, 1);
146 /* Check Oscillator Failure flag */
147 error = bq32k_read(dev, ®, BQ32K_MINUTES, 1);
148 if (!error && (reg & BQ32K_OF)) {
149 dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
151 error = bq32k_write(dev, ®, BQ32K_MINUTES, 1);
156 rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
157 &bq32k_rtc_ops, THIS_MODULE);
161 i2c_set_clientdata(client, rtc);
166 static const struct i2c_device_id bq32k_id[] = {
170 MODULE_DEVICE_TABLE(i2c, bq32k_id);
172 static struct i2c_driver bq32k_driver = {
175 .owner = THIS_MODULE,
177 .probe = bq32k_probe,
178 .id_table = bq32k_id,
181 module_i2c_driver(bq32k_driver);
183 MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
184 MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
185 MODULE_LICENSE("GPL");