xref: /openbmc/linux/drivers/rtc/rtc-pcf2127.c (revision 22652ba72453d35c8a637d5c0f06b3dc29ff9eb0)
118cb6368SRenaud Cerrato /*
2cee2cc21SAkinobu Mita  * An I2C and SPI driver for the NXP PCF2127/29 RTC
318cb6368SRenaud Cerrato  * Copyright 2013 Til-Technologies
418cb6368SRenaud Cerrato  *
518cb6368SRenaud Cerrato  * Author: Renaud Cerrato <r.cerrato@til-technologies.fr>
618cb6368SRenaud Cerrato  *
718cb6368SRenaud Cerrato  * based on the other drivers in this same directory.
818cb6368SRenaud Cerrato  *
9cee2cc21SAkinobu Mita  * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf
1018cb6368SRenaud Cerrato  *
1118cb6368SRenaud Cerrato  * This program is free software; you can redistribute it and/or modify
1218cb6368SRenaud Cerrato  * it under the terms of the GNU General Public License version 2 as
1318cb6368SRenaud Cerrato  * published by the Free Software Foundation.
1418cb6368SRenaud Cerrato  */
1518cb6368SRenaud Cerrato 
1618cb6368SRenaud Cerrato #include <linux/i2c.h>
179408ec1aSAkinobu Mita #include <linux/spi/spi.h>
1818cb6368SRenaud Cerrato #include <linux/bcd.h>
1918cb6368SRenaud Cerrato #include <linux/rtc.h>
2018cb6368SRenaud Cerrato #include <linux/slab.h>
2118cb6368SRenaud Cerrato #include <linux/module.h>
2218cb6368SRenaud Cerrato #include <linux/of.h>
23907b3262SAkinobu Mita #include <linux/regmap.h>
2418cb6368SRenaud Cerrato 
2518cb6368SRenaud Cerrato #define PCF2127_REG_CTRL1       (0x00)  /* Control Register 1 */
2618cb6368SRenaud Cerrato #define PCF2127_REG_CTRL2       (0x01)  /* Control Register 2 */
27f97cfddcSUwe Kleine-König 
2818cb6368SRenaud Cerrato #define PCF2127_REG_CTRL3       (0x02)  /* Control Register 3 */
29f97cfddcSUwe Kleine-König #define PCF2127_REG_CTRL3_BLF		BIT(2)
30f97cfddcSUwe Kleine-König 
3118cb6368SRenaud Cerrato #define PCF2127_REG_SC          (0x03)  /* datetime */
3218cb6368SRenaud Cerrato #define PCF2127_REG_MN          (0x04)
3318cb6368SRenaud Cerrato #define PCF2127_REG_HR          (0x05)
3418cb6368SRenaud Cerrato #define PCF2127_REG_DM          (0x06)
3518cb6368SRenaud Cerrato #define PCF2127_REG_DW          (0x07)
3618cb6368SRenaud Cerrato #define PCF2127_REG_MO          (0x08)
3718cb6368SRenaud Cerrato #define PCF2127_REG_YR          (0x09)
3818cb6368SRenaud Cerrato 
39653ebd75SAndrea Scian #define PCF2127_OSF             BIT(7)  /* Oscillator Fail flag */
40653ebd75SAndrea Scian 
4118cb6368SRenaud Cerrato struct pcf2127 {
4218cb6368SRenaud Cerrato 	struct rtc_device *rtc;
43907b3262SAkinobu Mita 	struct regmap *regmap;
4418cb6368SRenaud Cerrato };
4518cb6368SRenaud Cerrato 
4618cb6368SRenaud Cerrato /*
4718cb6368SRenaud Cerrato  * In the routines that deal directly with the pcf2127 hardware, we use
4818cb6368SRenaud Cerrato  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
4918cb6368SRenaud Cerrato  */
50907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm)
5118cb6368SRenaud Cerrato {
52907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
53907b3262SAkinobu Mita 	unsigned char buf[10];
54907b3262SAkinobu Mita 	int ret;
553769a375SSean Nyekjaer 	int i;
5618cb6368SRenaud Cerrato 
573769a375SSean Nyekjaer 	for (i = 0; i <= PCF2127_REG_CTRL3; i++) {
583769a375SSean Nyekjaer 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1 + i,
593769a375SSean Nyekjaer 				  (unsigned int *)(buf + i));
603769a375SSean Nyekjaer 		if (ret) {
613769a375SSean Nyekjaer 			dev_err(dev, "%s: read error\n", __func__);
623769a375SSean Nyekjaer 			return ret;
633769a375SSean Nyekjaer 		}
643769a375SSean Nyekjaer 	}
653769a375SSean Nyekjaer 
663769a375SSean Nyekjaer 	ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_SC,
673769a375SSean Nyekjaer 			       (buf + PCF2127_REG_SC),
683769a375SSean Nyekjaer 			       ARRAY_SIZE(buf) - PCF2127_REG_SC);
69907b3262SAkinobu Mita 	if (ret) {
70907b3262SAkinobu Mita 		dev_err(dev, "%s: read error\n", __func__);
71907b3262SAkinobu Mita 		return ret;
7218cb6368SRenaud Cerrato 	}
7318cb6368SRenaud Cerrato 
74f97cfddcSUwe Kleine-König 	if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF)
75907b3262SAkinobu Mita 		dev_info(dev,
76653ebd75SAndrea Scian 			"low voltage detected, check/replace RTC battery.\n");
77653ebd75SAndrea Scian 
78653ebd75SAndrea Scian 	if (buf[PCF2127_REG_SC] & PCF2127_OSF) {
79653ebd75SAndrea Scian 		/*
80653ebd75SAndrea Scian 		 * no need clear the flag here,
81653ebd75SAndrea Scian 		 * it will be cleared once the new date is saved
82653ebd75SAndrea Scian 		 */
83907b3262SAkinobu Mita 		dev_warn(dev,
84653ebd75SAndrea Scian 			 "oscillator stop detected, date/time is not reliable\n");
85653ebd75SAndrea Scian 		return -EINVAL;
8618cb6368SRenaud Cerrato 	}
8718cb6368SRenaud Cerrato 
88907b3262SAkinobu Mita 	dev_dbg(dev,
8918cb6368SRenaud Cerrato 		"%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, "
9018cb6368SRenaud Cerrato 		"sec=%02x, min=%02x, hr=%02x, "
9118cb6368SRenaud Cerrato 		"mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
9218cb6368SRenaud Cerrato 		__func__,
9318cb6368SRenaud Cerrato 		buf[0], buf[1], buf[2],
9418cb6368SRenaud Cerrato 		buf[3], buf[4], buf[5],
9518cb6368SRenaud Cerrato 		buf[6], buf[7], buf[8], buf[9]);
9618cb6368SRenaud Cerrato 
9718cb6368SRenaud Cerrato 
9818cb6368SRenaud Cerrato 	tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F);
9918cb6368SRenaud Cerrato 	tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F);
10018cb6368SRenaud Cerrato 	tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */
10118cb6368SRenaud Cerrato 	tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F);
10218cb6368SRenaud Cerrato 	tm->tm_wday = buf[PCF2127_REG_DW] & 0x07;
10318cb6368SRenaud Cerrato 	tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
10418cb6368SRenaud Cerrato 	tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]);
10518cb6368SRenaud Cerrato 	if (tm->tm_year < 70)
10618cb6368SRenaud Cerrato 		tm->tm_year += 100;	/* assume we are in 1970...2069 */
10718cb6368SRenaud Cerrato 
108907b3262SAkinobu Mita 	dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
10918cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
11018cb6368SRenaud Cerrato 		__func__,
11118cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
11218cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
11318cb6368SRenaud Cerrato 
114*22652ba7SAlexandre Belloni 	return 0;
11518cb6368SRenaud Cerrato }
11618cb6368SRenaud Cerrato 
117907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm)
11818cb6368SRenaud Cerrato {
119907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
120907b3262SAkinobu Mita 	unsigned char buf[7];
12118cb6368SRenaud Cerrato 	int i = 0, err;
12218cb6368SRenaud Cerrato 
123907b3262SAkinobu Mita 	dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
12418cb6368SRenaud Cerrato 		"mday=%d, mon=%d, year=%d, wday=%d\n",
12518cb6368SRenaud Cerrato 		__func__,
12618cb6368SRenaud Cerrato 		tm->tm_sec, tm->tm_min, tm->tm_hour,
12718cb6368SRenaud Cerrato 		tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
12818cb6368SRenaud Cerrato 
12918cb6368SRenaud Cerrato 	/* hours, minutes and seconds */
130653ebd75SAndrea Scian 	buf[i++] = bin2bcd(tm->tm_sec);	/* this will also clear OSF flag */
13118cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_min);
13218cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_hour);
13318cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mday);
13418cb6368SRenaud Cerrato 	buf[i++] = tm->tm_wday & 0x07;
13518cb6368SRenaud Cerrato 
13618cb6368SRenaud Cerrato 	/* month, 1 - 12 */
13718cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_mon + 1);
13818cb6368SRenaud Cerrato 
13918cb6368SRenaud Cerrato 	/* year */
14018cb6368SRenaud Cerrato 	buf[i++] = bin2bcd(tm->tm_year % 100);
14118cb6368SRenaud Cerrato 
14218cb6368SRenaud Cerrato 	/* write register's data */
143907b3262SAkinobu Mita 	err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i);
144907b3262SAkinobu Mita 	if (err) {
145907b3262SAkinobu Mita 		dev_err(dev,
14618cb6368SRenaud Cerrato 			"%s: err=%d", __func__, err);
147907b3262SAkinobu Mita 		return err;
14818cb6368SRenaud Cerrato 	}
14918cb6368SRenaud Cerrato 
15018cb6368SRenaud Cerrato 	return 0;
15118cb6368SRenaud Cerrato }
15218cb6368SRenaud Cerrato 
15318cb6368SRenaud Cerrato #ifdef CONFIG_RTC_INTF_DEV
15418cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev,
15518cb6368SRenaud Cerrato 				unsigned int cmd, unsigned long arg)
15618cb6368SRenaud Cerrato {
157907b3262SAkinobu Mita 	struct pcf2127 *pcf2127 = dev_get_drvdata(dev);
158f97cfddcSUwe Kleine-König 	int touser;
159f97cfddcSUwe Kleine-König 	int ret;
16018cb6368SRenaud Cerrato 
16118cb6368SRenaud Cerrato 	switch (cmd) {
16218cb6368SRenaud Cerrato 	case RTC_VL_READ:
163907b3262SAkinobu Mita 		ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser);
164907b3262SAkinobu Mita 		if (ret)
165f97cfddcSUwe Kleine-König 			return ret;
16618cb6368SRenaud Cerrato 
167907b3262SAkinobu Mita 		touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0;
168f97cfddcSUwe Kleine-König 
169f97cfddcSUwe Kleine-König 		if (copy_to_user((void __user *)arg, &touser, sizeof(int)))
17018cb6368SRenaud Cerrato 			return -EFAULT;
17118cb6368SRenaud Cerrato 		return 0;
17218cb6368SRenaud Cerrato 	default:
17318cb6368SRenaud Cerrato 		return -ENOIOCTLCMD;
17418cb6368SRenaud Cerrato 	}
17518cb6368SRenaud Cerrato }
17618cb6368SRenaud Cerrato #else
17718cb6368SRenaud Cerrato #define pcf2127_rtc_ioctl NULL
17818cb6368SRenaud Cerrato #endif
17918cb6368SRenaud Cerrato 
18018cb6368SRenaud Cerrato static const struct rtc_class_ops pcf2127_rtc_ops = {
18118cb6368SRenaud Cerrato 	.ioctl		= pcf2127_rtc_ioctl,
18218cb6368SRenaud Cerrato 	.read_time	= pcf2127_rtc_read_time,
18318cb6368SRenaud Cerrato 	.set_time	= pcf2127_rtc_set_time,
18418cb6368SRenaud Cerrato };
18518cb6368SRenaud Cerrato 
186907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap,
187907b3262SAkinobu Mita 			const char *name)
18818cb6368SRenaud Cerrato {
18918cb6368SRenaud Cerrato 	struct pcf2127 *pcf2127;
19018cb6368SRenaud Cerrato 
191907b3262SAkinobu Mita 	dev_dbg(dev, "%s\n", __func__);
19218cb6368SRenaud Cerrato 
193907b3262SAkinobu Mita 	pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL);
19418cb6368SRenaud Cerrato 	if (!pcf2127)
19518cb6368SRenaud Cerrato 		return -ENOMEM;
19618cb6368SRenaud Cerrato 
197907b3262SAkinobu Mita 	pcf2127->regmap = regmap;
19818cb6368SRenaud Cerrato 
199907b3262SAkinobu Mita 	dev_set_drvdata(dev, pcf2127);
200907b3262SAkinobu Mita 
201907b3262SAkinobu Mita 	pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops,
202907b3262SAkinobu Mita 						THIS_MODULE);
20318cb6368SRenaud Cerrato 
2047ab26cd1SDuan Jiong 	return PTR_ERR_OR_ZERO(pcf2127->rtc);
20518cb6368SRenaud Cerrato }
20618cb6368SRenaud Cerrato 
20718cb6368SRenaud Cerrato #ifdef CONFIG_OF
20818cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = {
20918cb6368SRenaud Cerrato 	{ .compatible = "nxp,pcf2127" },
210cee2cc21SAkinobu Mita 	{ .compatible = "nxp,pcf2129" },
21118cb6368SRenaud Cerrato 	{}
21218cb6368SRenaud Cerrato };
21318cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match);
21418cb6368SRenaud Cerrato #endif
21518cb6368SRenaud Cerrato 
2169408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C)
2179408ec1aSAkinobu Mita 
218907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count)
219907b3262SAkinobu Mita {
220907b3262SAkinobu Mita 	struct device *dev = context;
221907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
222907b3262SAkinobu Mita 	int ret;
223907b3262SAkinobu Mita 
224907b3262SAkinobu Mita 	ret = i2c_master_send(client, data, count);
225907b3262SAkinobu Mita 	if (ret != count)
226907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
227907b3262SAkinobu Mita 
228907b3262SAkinobu Mita 	return 0;
229907b3262SAkinobu Mita }
230907b3262SAkinobu Mita 
231907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context,
232907b3262SAkinobu Mita 				const void *reg, size_t reg_size,
233907b3262SAkinobu Mita 				const void *val, size_t val_size)
234907b3262SAkinobu Mita {
235907b3262SAkinobu Mita 	struct device *dev = context;
236907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
237907b3262SAkinobu Mita 	int ret;
238907b3262SAkinobu Mita 	void *buf;
239907b3262SAkinobu Mita 
240907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
241907b3262SAkinobu Mita 		return -EINVAL;
242907b3262SAkinobu Mita 
243907b3262SAkinobu Mita 	buf = kmalloc(val_size + 1, GFP_KERNEL);
244907b3262SAkinobu Mita 	if (!buf)
245907b3262SAkinobu Mita 		return -ENOMEM;
246907b3262SAkinobu Mita 
247907b3262SAkinobu Mita 	memcpy(buf, reg, 1);
248907b3262SAkinobu Mita 	memcpy(buf + 1, val, val_size);
249907b3262SAkinobu Mita 
250907b3262SAkinobu Mita 	ret = i2c_master_send(client, buf, val_size + 1);
251907b3262SAkinobu Mita 	if (ret != val_size + 1)
252907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
253907b3262SAkinobu Mita 
254907b3262SAkinobu Mita 	return 0;
255907b3262SAkinobu Mita }
256907b3262SAkinobu Mita 
257907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size,
258907b3262SAkinobu Mita 				void *val, size_t val_size)
259907b3262SAkinobu Mita {
260907b3262SAkinobu Mita 	struct device *dev = context;
261907b3262SAkinobu Mita 	struct i2c_client *client = to_i2c_client(dev);
262907b3262SAkinobu Mita 	int ret;
263907b3262SAkinobu Mita 
264907b3262SAkinobu Mita 	if (WARN_ON(reg_size != 1))
265907b3262SAkinobu Mita 		return -EINVAL;
266907b3262SAkinobu Mita 
267907b3262SAkinobu Mita 	ret = i2c_master_send(client, reg, 1);
268907b3262SAkinobu Mita 	if (ret != 1)
269907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
270907b3262SAkinobu Mita 
271907b3262SAkinobu Mita 	ret = i2c_master_recv(client, val, val_size);
272907b3262SAkinobu Mita 	if (ret != val_size)
273907b3262SAkinobu Mita 		return ret < 0 ? ret : -EIO;
274907b3262SAkinobu Mita 
275907b3262SAkinobu Mita 	return 0;
276907b3262SAkinobu Mita }
277907b3262SAkinobu Mita 
278907b3262SAkinobu Mita /*
279907b3262SAkinobu Mita  * The reason we need this custom regmap_bus instead of using regmap_init_i2c()
280907b3262SAkinobu Mita  * is that the STOP condition is required between set register address and
281907b3262SAkinobu Mita  * read register data when reading from registers.
282907b3262SAkinobu Mita  */
283907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = {
284907b3262SAkinobu Mita 	.write = pcf2127_i2c_write,
285907b3262SAkinobu Mita 	.gather_write = pcf2127_i2c_gather_write,
286907b3262SAkinobu Mita 	.read = pcf2127_i2c_read,
28718cb6368SRenaud Cerrato };
28818cb6368SRenaud Cerrato 
289907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver;
290907b3262SAkinobu Mita 
291907b3262SAkinobu Mita static int pcf2127_i2c_probe(struct i2c_client *client,
292907b3262SAkinobu Mita 				const struct i2c_device_id *id)
293907b3262SAkinobu Mita {
294907b3262SAkinobu Mita 	struct regmap *regmap;
295907b3262SAkinobu Mita 	static const struct regmap_config config = {
296907b3262SAkinobu Mita 		.reg_bits = 8,
297907b3262SAkinobu Mita 		.val_bits = 8,
298907b3262SAkinobu Mita 	};
299907b3262SAkinobu Mita 
300907b3262SAkinobu Mita 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
301907b3262SAkinobu Mita 		return -ENODEV;
302907b3262SAkinobu Mita 
303907b3262SAkinobu Mita 	regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap,
304907b3262SAkinobu Mita 					&client->dev, &config);
305907b3262SAkinobu Mita 	if (IS_ERR(regmap)) {
306907b3262SAkinobu Mita 		dev_err(&client->dev, "%s: regmap allocation failed: %ld\n",
307907b3262SAkinobu Mita 			__func__, PTR_ERR(regmap));
308907b3262SAkinobu Mita 		return PTR_ERR(regmap);
309907b3262SAkinobu Mita 	}
310907b3262SAkinobu Mita 
311907b3262SAkinobu Mita 	return pcf2127_probe(&client->dev, regmap,
312907b3262SAkinobu Mita 				pcf2127_i2c_driver.driver.name);
313907b3262SAkinobu Mita }
314907b3262SAkinobu Mita 
315907b3262SAkinobu Mita static const struct i2c_device_id pcf2127_i2c_id[] = {
316907b3262SAkinobu Mita 	{ "pcf2127", 0 },
317cee2cc21SAkinobu Mita 	{ "pcf2129", 0 },
318907b3262SAkinobu Mita 	{ }
319907b3262SAkinobu Mita };
320907b3262SAkinobu Mita MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id);
321907b3262SAkinobu Mita 
322907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = {
323907b3262SAkinobu Mita 	.driver		= {
324907b3262SAkinobu Mita 		.name	= "rtc-pcf2127-i2c",
325907b3262SAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
326907b3262SAkinobu Mita 	},
327907b3262SAkinobu Mita 	.probe		= pcf2127_i2c_probe,
328907b3262SAkinobu Mita 	.id_table	= pcf2127_i2c_id,
329907b3262SAkinobu Mita };
3309408ec1aSAkinobu Mita 
3319408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
3329408ec1aSAkinobu Mita {
3339408ec1aSAkinobu Mita 	return i2c_add_driver(&pcf2127_i2c_driver);
3349408ec1aSAkinobu Mita }
3359408ec1aSAkinobu Mita 
3369408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
3379408ec1aSAkinobu Mita {
3389408ec1aSAkinobu Mita 	i2c_del_driver(&pcf2127_i2c_driver);
3399408ec1aSAkinobu Mita }
3409408ec1aSAkinobu Mita 
3419408ec1aSAkinobu Mita #else
3429408ec1aSAkinobu Mita 
3439408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void)
3449408ec1aSAkinobu Mita {
3459408ec1aSAkinobu Mita 	return 0;
3469408ec1aSAkinobu Mita }
3479408ec1aSAkinobu Mita 
3489408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void)
3499408ec1aSAkinobu Mita {
3509408ec1aSAkinobu Mita }
3519408ec1aSAkinobu Mita 
3529408ec1aSAkinobu Mita #endif
3539408ec1aSAkinobu Mita 
3549408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER)
3559408ec1aSAkinobu Mita 
3569408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver;
3579408ec1aSAkinobu Mita 
3589408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi)
3599408ec1aSAkinobu Mita {
3609408ec1aSAkinobu Mita 	static const struct regmap_config config = {
3619408ec1aSAkinobu Mita 		.reg_bits = 8,
3629408ec1aSAkinobu Mita 		.val_bits = 8,
3639408ec1aSAkinobu Mita 		.read_flag_mask = 0xa0,
3649408ec1aSAkinobu Mita 		.write_flag_mask = 0x20,
3659408ec1aSAkinobu Mita 	};
3669408ec1aSAkinobu Mita 	struct regmap *regmap;
3679408ec1aSAkinobu Mita 
3689408ec1aSAkinobu Mita 	regmap = devm_regmap_init_spi(spi, &config);
3699408ec1aSAkinobu Mita 	if (IS_ERR(regmap)) {
3709408ec1aSAkinobu Mita 		dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
3719408ec1aSAkinobu Mita 			__func__, PTR_ERR(regmap));
3729408ec1aSAkinobu Mita 		return PTR_ERR(regmap);
3739408ec1aSAkinobu Mita 	}
3749408ec1aSAkinobu Mita 
3759408ec1aSAkinobu Mita 	return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name);
3769408ec1aSAkinobu Mita }
3779408ec1aSAkinobu Mita 
3789408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = {
3799408ec1aSAkinobu Mita 	{ "pcf2127", 0 },
380cee2cc21SAkinobu Mita 	{ "pcf2129", 0 },
3819408ec1aSAkinobu Mita 	{ }
3829408ec1aSAkinobu Mita };
3839408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id);
3849408ec1aSAkinobu Mita 
3859408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = {
3869408ec1aSAkinobu Mita 	.driver		= {
3879408ec1aSAkinobu Mita 		.name	= "rtc-pcf2127-spi",
3889408ec1aSAkinobu Mita 		.of_match_table = of_match_ptr(pcf2127_of_match),
3899408ec1aSAkinobu Mita 	},
3909408ec1aSAkinobu Mita 	.probe		= pcf2127_spi_probe,
3919408ec1aSAkinobu Mita 	.id_table	= pcf2127_spi_id,
3929408ec1aSAkinobu Mita };
3939408ec1aSAkinobu Mita 
3949408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
3959408ec1aSAkinobu Mita {
3969408ec1aSAkinobu Mita 	return spi_register_driver(&pcf2127_spi_driver);
3979408ec1aSAkinobu Mita }
3989408ec1aSAkinobu Mita 
3999408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
4009408ec1aSAkinobu Mita {
4019408ec1aSAkinobu Mita 	spi_unregister_driver(&pcf2127_spi_driver);
4029408ec1aSAkinobu Mita }
4039408ec1aSAkinobu Mita 
4049408ec1aSAkinobu Mita #else
4059408ec1aSAkinobu Mita 
4069408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void)
4079408ec1aSAkinobu Mita {
4089408ec1aSAkinobu Mita 	return 0;
4099408ec1aSAkinobu Mita }
4109408ec1aSAkinobu Mita 
4119408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void)
4129408ec1aSAkinobu Mita {
4139408ec1aSAkinobu Mita }
4149408ec1aSAkinobu Mita 
4159408ec1aSAkinobu Mita #endif
4169408ec1aSAkinobu Mita 
4179408ec1aSAkinobu Mita static int __init pcf2127_init(void)
4189408ec1aSAkinobu Mita {
4199408ec1aSAkinobu Mita 	int ret;
4209408ec1aSAkinobu Mita 
4219408ec1aSAkinobu Mita 	ret = pcf2127_i2c_register_driver();
4229408ec1aSAkinobu Mita 	if (ret) {
4239408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 i2c driver: %d\n", ret);
4249408ec1aSAkinobu Mita 		return ret;
4259408ec1aSAkinobu Mita 	}
4269408ec1aSAkinobu Mita 
4279408ec1aSAkinobu Mita 	ret = pcf2127_spi_register_driver();
4289408ec1aSAkinobu Mita 	if (ret) {
4299408ec1aSAkinobu Mita 		pr_err("Failed to register pcf2127 spi driver: %d\n", ret);
4309408ec1aSAkinobu Mita 		pcf2127_i2c_unregister_driver();
4319408ec1aSAkinobu Mita 	}
4329408ec1aSAkinobu Mita 
4339408ec1aSAkinobu Mita 	return ret;
4349408ec1aSAkinobu Mita }
4359408ec1aSAkinobu Mita module_init(pcf2127_init)
4369408ec1aSAkinobu Mita 
4379408ec1aSAkinobu Mita static void __exit pcf2127_exit(void)
4389408ec1aSAkinobu Mita {
4399408ec1aSAkinobu Mita 	pcf2127_spi_unregister_driver();
4409408ec1aSAkinobu Mita 	pcf2127_i2c_unregister_driver();
4419408ec1aSAkinobu Mita }
4429408ec1aSAkinobu Mita module_exit(pcf2127_exit)
44318cb6368SRenaud Cerrato 
44418cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>");
445cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver");
4464d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2");
447