Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for TI BQ32000 RTC.
  4 *
  5 * Copyright (C) 2009 Semihalf.
  6 * Copyright (C) 2014 Pavel Machek <pavel@denx.de>
  7 *
  8 * You can get hardware description at
  9 * https://www.ti.com/lit/ds/symlink/bq32000.pdf
 
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/i2c.h>
 14#include <linux/rtc.h>
 15#include <linux/init.h>
 16#include <linux/kstrtox.h>
 17#include <linux/errno.h>
 18#include <linux/bcd.h>
 19
 20#define BQ32K_SECONDS		0x00	/* Seconds register address */
 21#define BQ32K_SECONDS_MASK	0x7F	/* Mask over seconds value */
 22#define BQ32K_STOP		0x80	/* Oscillator Stop flat */
 23
 24#define BQ32K_MINUTES		0x01	/* Minutes register address */
 25#define BQ32K_MINUTES_MASK	0x7F	/* Mask over minutes value */
 26#define BQ32K_OF		0x80	/* Oscillator Failure flag */
 27
 28#define BQ32K_HOURS_MASK	0x3F	/* Mask over hours value */
 29#define BQ32K_CENT		0x40	/* Century flag */
 30#define BQ32K_CENT_EN		0x80	/* Century flag enable bit */
 31
 32#define BQ32K_CALIBRATION	0x07	/* CAL_CFG1, calibration and control */
 33#define BQ32K_TCH2		0x08	/* Trickle charge enable */
 34#define BQ32K_CFG2		0x09	/* Trickle charger control */
 35#define BQ32K_TCFE		BIT(6)	/* Trickle charge FET bypass */
 36
 37#define MAX_LEN			10	/* Maximum number of consecutive
 38					 * register for this particular RTC.
 39					 */
 40
 41struct bq32k_regs {
 42	uint8_t		seconds;
 43	uint8_t		minutes;
 44	uint8_t		cent_hours;
 45	uint8_t		day;
 46	uint8_t		date;
 47	uint8_t		month;
 48	uint8_t		years;
 49};
 50
 51static struct i2c_driver bq32k_driver;
 52
 53static int bq32k_read(struct device *dev, void *data, uint8_t off, uint8_t len)
 54{
 55	struct i2c_client *client = to_i2c_client(dev);
 56	struct i2c_msg msgs[] = {
 57		{
 58			.addr = client->addr,
 59			.flags = 0,
 60			.len = 1,
 61			.buf = &off,
 62		}, {
 63			.addr = client->addr,
 64			.flags = I2C_M_RD,
 65			.len = len,
 66			.buf = data,
 67		}
 68	};
 69
 70	if (i2c_transfer(client->adapter, msgs, 2) == 2)
 71		return 0;
 72
 73	return -EIO;
 74}
 75
 76static int bq32k_write(struct device *dev, void *data, uint8_t off, uint8_t len)
 77{
 78	struct i2c_client *client = to_i2c_client(dev);
 79	uint8_t buffer[MAX_LEN + 1];
 80
 81	buffer[0] = off;
 82	memcpy(&buffer[1], data, len);
 83
 84	if (i2c_master_send(client, buffer, len + 1) == len + 1)
 85		return 0;
 86
 87	return -EIO;
 88}
 89
 90static int bq32k_rtc_read_time(struct device *dev, struct rtc_time *tm)
 91{
 92	struct bq32k_regs regs;
 93	int error;
 94
 95	error = bq32k_read(dev, &regs, 0, sizeof(regs));
 96	if (error)
 97		return error;
 98
 99	/*
100	 * In case of oscillator failure, the register contents should be
101	 * considered invalid. The flag is cleared the next time the RTC is set.
102	 */
103	if (regs.minutes & BQ32K_OF)
104		return -EINVAL;
105
106	tm->tm_sec = bcd2bin(regs.seconds & BQ32K_SECONDS_MASK);
107	tm->tm_min = bcd2bin(regs.minutes & BQ32K_MINUTES_MASK);
108	tm->tm_hour = bcd2bin(regs.cent_hours & BQ32K_HOURS_MASK);
109	tm->tm_mday = bcd2bin(regs.date);
110	tm->tm_wday = bcd2bin(regs.day) - 1;
111	tm->tm_mon = bcd2bin(regs.month) - 1;
112	tm->tm_year = bcd2bin(regs.years) +
113				((regs.cent_hours & BQ32K_CENT) ? 100 : 0);
114
115	return 0;
116}
117
118static int bq32k_rtc_set_time(struct device *dev, struct rtc_time *tm)
119{
120	struct bq32k_regs regs;
121
122	regs.seconds = bin2bcd(tm->tm_sec);
123	regs.minutes = bin2bcd(tm->tm_min);
124	regs.cent_hours = bin2bcd(tm->tm_hour) | BQ32K_CENT_EN;
125	regs.day = bin2bcd(tm->tm_wday + 1);
126	regs.date = bin2bcd(tm->tm_mday);
127	regs.month = bin2bcd(tm->tm_mon + 1);
128
129	if (tm->tm_year >= 100) {
130		regs.cent_hours |= BQ32K_CENT;
131		regs.years = bin2bcd(tm->tm_year - 100);
132	} else
133		regs.years = bin2bcd(tm->tm_year);
134
135	return bq32k_write(dev, &regs, 0, sizeof(regs));
136}
137
138static const struct rtc_class_ops bq32k_rtc_ops = {
139	.read_time	= bq32k_rtc_read_time,
140	.set_time	= bq32k_rtc_set_time,
141};
142
143static int trickle_charger_of_init(struct device *dev, struct device_node *node)
144{
145	unsigned char reg;
146	int error;
147	u32 ohms = 0;
148
149	if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms))
150		return 0;
151
152	switch (ohms) {
153	case 180+940:
154		/*
155		 * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging
156		 * over diode and 940ohm resistor)
157		 */
158
159		if (of_property_read_bool(node, "trickle-diode-disable")) {
160			dev_err(dev, "diode and resistor mismatch\n");
161			return -EINVAL;
162		}
163		reg = 0x05;
164		break;
165
166	case 180+20000:
167		/* diode disabled */
168
169		if (!of_property_read_bool(node, "trickle-diode-disable")) {
170			dev_err(dev, "bq32k: diode and resistor mismatch\n");
171			return -EINVAL;
172		}
173		reg = 0x45;
174		break;
175
176	default:
177		dev_err(dev, "invalid resistor value (%d)\n", ohms);
178		return -EINVAL;
179	}
180
181	error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
182	if (error)
183		return error;
184
185	reg = 0x20;
186	error = bq32k_write(dev, &reg, BQ32K_TCH2, 1);
187	if (error)
188		return error;
189
190	dev_info(dev, "Enabled trickle RTC battery charge.\n");
191	return 0;
192}
193
194static ssize_t bq32k_sysfs_show_tricklecharge_bypass(struct device *dev,
195					       struct device_attribute *attr,
196					       char *buf)
197{
198	int reg, error;
199
200	error = bq32k_read(dev, &reg, BQ32K_CFG2, 1);
201	if (error)
202		return error;
203
204	return sprintf(buf, "%d\n", (reg & BQ32K_TCFE) ? 1 : 0);
205}
206
207static ssize_t bq32k_sysfs_store_tricklecharge_bypass(struct device *dev,
208						struct device_attribute *attr,
209						const char *buf, size_t count)
210{
211	int reg, enable, error;
212
213	if (kstrtoint(buf, 0, &enable))
214		return -EINVAL;
215
216	error = bq32k_read(dev, &reg, BQ32K_CFG2, 1);
217	if (error)
218		return error;
219
220	if (enable) {
221		reg |= BQ32K_TCFE;
222		error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
223		if (error)
224			return error;
225
226		dev_info(dev, "Enabled trickle charge FET bypass.\n");
227	} else {
228		reg &= ~BQ32K_TCFE;
229		error = bq32k_write(dev, &reg, BQ32K_CFG2, 1);
230		if (error)
231			return error;
232
233		dev_info(dev, "Disabled trickle charge FET bypass.\n");
234	}
235
236	return count;
237}
238
239static DEVICE_ATTR(trickle_charge_bypass, 0644,
240		   bq32k_sysfs_show_tricklecharge_bypass,
241		   bq32k_sysfs_store_tricklecharge_bypass);
242
243static int bq32k_sysfs_register(struct device *dev)
244{
245	return device_create_file(dev, &dev_attr_trickle_charge_bypass);
246}
247
248static void bq32k_sysfs_unregister(struct device *dev)
249{
250	device_remove_file(dev, &dev_attr_trickle_charge_bypass);
251}
252
253static int bq32k_probe(struct i2c_client *client)
254{
255	struct device *dev = &client->dev;
256	struct rtc_device *rtc;
257	uint8_t reg;
258	int error;
259
260	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
261		return -ENODEV;
262
263	/* Check Oscillator Stop flag */
264	error = bq32k_read(dev, &reg, BQ32K_SECONDS, 1);
265	if (!error && (reg & BQ32K_STOP)) {
266		dev_warn(dev, "Oscillator was halted. Restarting...\n");
267		reg &= ~BQ32K_STOP;
268		error = bq32k_write(dev, &reg, BQ32K_SECONDS, 1);
269	}
270	if (error)
271		return error;
272
273	/* Check Oscillator Failure flag */
274	error = bq32k_read(dev, &reg, BQ32K_MINUTES, 1);
 
 
 
 
 
275	if (error)
276		return error;
277	if (reg & BQ32K_OF)
278		dev_warn(dev, "Oscillator Failure. Check RTC battery.\n");
279
280	if (client->dev.of_node)
281		trickle_charger_of_init(dev, client->dev.of_node);
282
283	rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name,
284						&bq32k_rtc_ops, THIS_MODULE);
285	if (IS_ERR(rtc))
286		return PTR_ERR(rtc);
287
288	error = bq32k_sysfs_register(&client->dev);
289	if (error) {
290		dev_err(&client->dev,
291			"Unable to create sysfs entries for rtc bq32000\n");
292		return error;
293	}
294
295
296	i2c_set_clientdata(client, rtc);
297
298	return 0;
299}
300
301static void bq32k_remove(struct i2c_client *client)
302{
303	bq32k_sysfs_unregister(&client->dev);
 
 
 
304}
305
306static const struct i2c_device_id bq32k_id[] = {
307	{ "bq32000", 0 },
308	{ }
309};
310MODULE_DEVICE_TABLE(i2c, bq32k_id);
311
312static const __maybe_unused struct of_device_id bq32k_of_match[] = {
313	{ .compatible = "ti,bq32000" },
314	{ }
315};
316MODULE_DEVICE_TABLE(of, bq32k_of_match);
317
318static struct i2c_driver bq32k_driver = {
319	.driver = {
320		.name	= "bq32k",
321		.of_match_table = of_match_ptr(bq32k_of_match),
322	},
323	.probe_new	= bq32k_probe,
324	.remove		= bq32k_remove,
325	.id_table	= bq32k_id,
326};
327
328module_i2c_driver(bq32k_driver);
 
 
 
 
 
 
 
 
 
 
329
330MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
331MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
332MODULE_LICENSE("GPL");
v3.1
 
  1/*
  2 * Driver for TI BQ32000 RTC.
  3 *
  4 * Copyright (C) 2009 Semihalf.
 
  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
 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	/* Seconds register address */
 19#define BQ32K_SECONDS_MASK	0x7F	/* Mask over seconds value */
 20#define BQ32K_STOP		0x80	/* Oscillator Stop flat */
 21
 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 */
 25
 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 */
 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, &regs, 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, &regs, 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	/* Check Oscillator Stop flag */
137	error = bq32k_read(dev, &reg, 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, &reg, BQ32K_SECONDS, 1);
142	}
143	if (error)
144		return error;
145
146	/* Check Oscillator Failure flag */
147	error = bq32k_read(dev, &reg, 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, &reg, 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 __devexit 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		= __devexit_p(bq32k_remove),
187	.id_table	= bq32k_id,
188};
189
190static __init int bq32k_init(void)
191{
192	return i2c_add_driver(&bq32k_driver);
193}
194module_init(bq32k_init);
195
196static __exit void bq32k_exit(void)
197{
198	i2c_del_driver(&bq32k_driver);
199}
200module_exit(bq32k_exit);
201
202MODULE_AUTHOR("Semihalf, Piotr Ziecik <kosmo@semihalf.com>");
203MODULE_DESCRIPTION("TI BQ32000 I2C RTC driver");
204MODULE_LICENSE("GPL");