1
2
3
4
5
6
7
8
9
10
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>
17
18#define BQ32K_SECONDS 0x00
19#define BQ32K_SECONDS_MASK 0x7F
20#define BQ32K_STOP 0x80
21
22#define BQ32K_MINUTES 0x01
23#define BQ32K_MINUTES_MASK 0x7F
24#define BQ32K_OF 0x80
25
26#define BQ32K_HOURS_MASK 0x3F
27#define BQ32K_CENT 0x40
28#define BQ32K_CENT_EN 0x80
29
30struct bq32k_regs {
31 uint8_t seconds;
32 uint8_t minutes;
33 uint8_t cent_hours;
34 uint8_t day;
35 uint8_t date;
36 uint8_t month;
37 uint8_t years;
38};
39
40static struct i2c_driver bq32k_driver;
41
42static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
43{
44 struct i2c_client *client = to_i2c_client(dev);
45 struct i2c_msg msgs[] = {
46 {
47 .addr = client->addr,
48 .flags = 0,
49 .len = 1,
50 .buf = &off,
51 }, {
52 .addr = client->addr,
53 .flags = I2C_M_RD,
54 .len = len,
55 .buf = data,
56 }
57 };
58
59 if (i2c_transfer(client->adapter, msgs, 2) == 2)
60 return 0;
61
62 return -EIO;
63}
64
65static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
66{
67 struct i2c_client *client = to_i2c_client(dev);
68 uint8_t buffer[len + 1];
69
70 buffer[0] = off;
71 memcpy(&buffer[1], data, len);
72
73 if (i2c_master_send(client, buffer, len + 1) == len + 1)
74 return 0;
75
76 return -EIO;
77}
78
79static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
80{
81 struct bq32k_regs regs;
82 int error;
83
84 error = bq32k_read(dev, ®s, 0, sizeof(regs));
85 if (error)
86 return error;
87
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);
96
97 return rtc_valid_tm(tm);
98}
99
100static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
101{
102 struct bq32k_regs regs;
103
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);
110
111 if (tm->tm_year >= 100) {
112 regs.cent_hours |= BQ32K_CENT;
113 regs.years = bin2bcd(tm->tm_year - 100);
114 } else
115 regs.years = bin2bcd(tm->tm_year);
116
117 return bq32k_write(dev, ®s, 0, sizeof(regs));
118}
119
120static const struct rtc_class_ops bq32k_rtc_ops = {
121 .read_time = bq32k_rtc_read_time,
122 .set_time = bq32k_rtc_set_time,
123};
124
125static int bq32k_probe(struct i2c_client *client,
126 const struct i2c_device_id *id)
127{
128 struct device *dev = &client->dev;
129 struct rtc_device *rtc;
130 uint8_t reg;
131 int error;
132
133 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
134 return -ENODEV;
135
136
137 error = bq32k_read(dev, ®, BQ32K_SECONDS, 1);
138 if (!error && (reg & BQ32K_STOP)) {
139 dev_warn(dev, "Oscillator was halted. Restarting...\n");
140 reg &= ~BQ32K_STOP;
141 error = bq32k_write(dev, ®, BQ32K_SECONDS, 1);
142 }
143 if (error)
144 return error;
145
146
147 error = bq32k_read(dev, ®, BQ32K_MINUTES, 1);
148 if (!error && (reg & BQ32K_OF)) {
149 dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
150 reg &= ~BQ32K_OF;
151 error = bq32k_write(dev, ®, BQ32K_MINUTES, 1);
152 }
153 if (error)
154 return error;
155
156 rtc = rtc_device_register(bq32k_driver.driver.name, &client->dev,
157 &bq32k_rtc_ops, THIS_MODULE);
158 if (IS_ERR(rtc))
159 return PTR_ERR(rtc);
160
161 i2c_set_clientdata(client, rtc);
162
163 return 0;
164}
165
166static int bq32k_remove(struct i2c_client *client)
167{
168 struct rtc_device *rtc = i2c_get_clientdata(client);
169
170 rtc_device_unregister(rtc);
171 return 0;
172}
173
174static const struct i2c_device_id bq32k_id[] = {
175 { "bq32000", 0 },
176 { }
177};
178MODULE_DEVICE_TABLE(i2c, bq32k_id);
179
180static struct i2c_driver bq32k_driver = {
181 .driver = {
182 .name = "bq32k",
183 .owner = THIS_MODULE,
184 },
185 .probe = bq32k_probe,
186 .remove = bq32k_remove,
187 .id_table = bq32k_id,
188};
189
190module_i2c_driver(bq32k_driver);
191
192MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
193MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
194MODULE_LICENSE("GPL");
195