Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for the Epson RTC module RX-6110 SA
  4 *
  5 * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
  6 * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
  7 */
  8
  9#include <linux/bcd.h>
 10#include <linux/init.h>
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/of_gpio.h>
 14#include <linux/regmap.h>
 15#include <linux/rtc.h>
 16#include <linux/of.h>
 17#include <linux/of_device.h>
 18#include <linux/spi/spi.h>
 19#include <linux/i2c.h>
 20
 21/* RX-6110 Register definitions */
 22#define RX6110_REG_SEC		0x10
 23#define RX6110_REG_MIN		0x11
 24#define RX6110_REG_HOUR		0x12
 25#define RX6110_REG_WDAY		0x13
 26#define RX6110_REG_MDAY		0x14
 27#define RX6110_REG_MONTH	0x15
 28#define RX6110_REG_YEAR		0x16
 29#define RX6110_REG_RES1		0x17
 30#define RX6110_REG_ALMIN	0x18
 31#define RX6110_REG_ALHOUR	0x19
 32#define RX6110_REG_ALWDAY	0x1A
 33#define RX6110_REG_TCOUNT0	0x1B
 34#define RX6110_REG_TCOUNT1	0x1C
 35#define RX6110_REG_EXT		0x1D
 36#define RX6110_REG_FLAG		0x1E
 37#define RX6110_REG_CTRL		0x1F
 38#define RX6110_REG_USER0	0x20
 39#define RX6110_REG_USER1	0x21
 40#define RX6110_REG_USER2	0x22
 41#define RX6110_REG_USER3	0x23
 42#define RX6110_REG_USER4	0x24
 43#define RX6110_REG_USER5	0x25
 44#define RX6110_REG_USER6	0x26
 45#define RX6110_REG_USER7	0x27
 46#define RX6110_REG_USER8	0x28
 47#define RX6110_REG_USER9	0x29
 48#define RX6110_REG_USERA	0x2A
 49#define RX6110_REG_USERB	0x2B
 50#define RX6110_REG_USERC	0x2C
 51#define RX6110_REG_USERD	0x2D
 52#define RX6110_REG_USERE	0x2E
 53#define RX6110_REG_USERF	0x2F
 54#define RX6110_REG_RES2		0x30
 55#define RX6110_REG_RES3		0x31
 56#define RX6110_REG_IRQ		0x32
 57
 58#define RX6110_BIT_ALARM_EN		BIT(7)
 59
 60/* Extension Register (1Dh) bit positions */
 61#define RX6110_BIT_EXT_TSEL0		BIT(0)
 62#define RX6110_BIT_EXT_TSEL1		BIT(1)
 63#define RX6110_BIT_EXT_TSEL2		BIT(2)
 64#define RX6110_BIT_EXT_WADA		BIT(3)
 65#define RX6110_BIT_EXT_TE		BIT(4)
 66#define RX6110_BIT_EXT_USEL		BIT(5)
 67#define RX6110_BIT_EXT_FSEL0		BIT(6)
 68#define RX6110_BIT_EXT_FSEL1		BIT(7)
 69
 70/* Flag Register (1Eh) bit positions */
 71#define RX6110_BIT_FLAG_VLF		BIT(1)
 72#define RX6110_BIT_FLAG_AF		BIT(3)
 73#define RX6110_BIT_FLAG_TF		BIT(4)
 74#define RX6110_BIT_FLAG_UF		BIT(5)
 75
 76/* Control Register (1Fh) bit positions */
 77#define RX6110_BIT_CTRL_TBKE		BIT(0)
 78#define RX6110_BIT_CTRL_TBKON		BIT(1)
 79#define RX6110_BIT_CTRL_TSTP		BIT(2)
 80#define RX6110_BIT_CTRL_AIE		BIT(3)
 81#define RX6110_BIT_CTRL_TIE		BIT(4)
 82#define RX6110_BIT_CTRL_UIE		BIT(5)
 83#define RX6110_BIT_CTRL_STOP		BIT(6)
 84#define RX6110_BIT_CTRL_TEST		BIT(7)
 85
 86enum {
 87	RTC_SEC = 0,
 88	RTC_MIN,
 89	RTC_HOUR,
 90	RTC_WDAY,
 91	RTC_MDAY,
 92	RTC_MONTH,
 93	RTC_YEAR,
 94	RTC_NR_TIME
 95};
 96
 97#define RX6110_DRIVER_NAME		"rx6110"
 98
 99struct rx6110_data {
100	struct rtc_device *rtc;
101	struct regmap *regmap;
102};
103
104/**
105 * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
106 *
107 * @tm: holds date and time
108 * @data: holds the encoding in rx6110 native form
109 */
110static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
111{
112	pr_debug("%s: date %ptRr\n", __func__, tm);
113
114	/*
115	 * The year in the RTC is a value between 0 and 99.
116	 * Assume that this represents the current century
117	 * and disregard all other values.
118	 */
119	if (tm->tm_year < 100 || tm->tm_year >= 200)
120		return -EINVAL;
121
122	data[RTC_SEC] = bin2bcd(tm->tm_sec);
123	data[RTC_MIN] = bin2bcd(tm->tm_min);
124	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
125	data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
126	data[RTC_MDAY] = bin2bcd(tm->tm_mday);
127	data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
128	data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
129
130	return 0;
131}
132
133/**
134 * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
135 *
136 * @data: holds the encoding in rx6110 native form
137 * @tm: holds date and time
138 */
139static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
140{
141	tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
142	tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
143	/* only 24-hour clock */
144	tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
145	tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
146	tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
147	tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
148	tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
149
150	pr_debug("%s: date %ptRr\n", __func__, tm);
151
152	/*
153	 * The year in the RTC is a value between 0 and 99.
154	 * Assume that this represents the current century
155	 * and disregard all other values.
156	 */
157	if (tm->tm_year < 100 || tm->tm_year >= 200)
158		return -EINVAL;
159
160	return 0;
161}
162
163/**
164 * rx6110_set_time - set the current time in the rx6110 registers
165 *
166 * @dev: the rtc device in use
167 * @tm: holds date and time
168 *
169 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
170 * year. Next time this is wrong is 2100, which will not be a leap year
171 *
172 * Note: If STOP is not set/cleared, the clock will start when the seconds
173 *       register is written
174 *
175 */
176static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
177{
178	struct rx6110_data *rx6110 = dev_get_drvdata(dev);
179	u8 data[RTC_NR_TIME];
180	int ret;
181
182	ret = rx6110_rtc_tm_to_data(tm, data);
183	if (ret < 0)
184		return ret;
185
186	/* set STOP bit before changing clock/calendar */
187	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
188				 RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
189	if (ret)
190		return ret;
191
192	ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
193				RTC_NR_TIME);
194	if (ret)
195		return ret;
196
197	/* The time in the RTC is valid. Be sure to have VLF cleared. */
198	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
199				 RX6110_BIT_FLAG_VLF, 0);
200	if (ret)
201		return ret;
202
203	/* clear STOP bit after changing clock/calendar */
204	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
205				 RX6110_BIT_CTRL_STOP, 0);
206
207	return ret;
208}
209
210/**
211 * rx6110_get_time - get the current time from the rx6110 registers
212 * @dev: the rtc device in use
213 * @tm: holds date and time
214 */
215static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
216{
217	struct rx6110_data *rx6110 = dev_get_drvdata(dev);
218	u8 data[RTC_NR_TIME];
219	int flags;
220	int ret;
221
222	ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
223	if (ret)
224		return -EINVAL;
225
226	/* check for VLF Flag (set at power-on) */
227	if ((flags & RX6110_BIT_FLAG_VLF)) {
228		dev_warn(dev, "Voltage low, data is invalid.\n");
229		return -EINVAL;
230	}
231
232	/* read registers to date */
233	ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
234			       RTC_NR_TIME);
235	if (ret)
236		return ret;
237
238	ret = rx6110_data_to_rtc_tm(data, tm);
239	if (ret)
240		return ret;
241
242	dev_dbg(dev, "%s: date %ptRr\n", __func__, tm);
243
244	return 0;
245}
246
247static const struct reg_sequence rx6110_default_regs[] = {
248	{ RX6110_REG_RES1,   0xB8 },
249	{ RX6110_REG_RES2,   0x00 },
250	{ RX6110_REG_RES3,   0x10 },
251	{ RX6110_REG_IRQ,    0x00 },
252	{ RX6110_REG_ALMIN,  0x00 },
253	{ RX6110_REG_ALHOUR, 0x00 },
254	{ RX6110_REG_ALWDAY, 0x00 },
255};
256
257/**
258 * rx6110_init - initialize the rx6110 registers
259 *
260 * @rx6110: pointer to the rx6110 struct in use
261 *
262 */
263static int rx6110_init(struct rx6110_data *rx6110)
264{
265	struct rtc_device *rtc = rx6110->rtc;
266	int flags;
267	int ret;
268
269	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
270				 RX6110_BIT_EXT_TE, 0);
271	if (ret)
272		return ret;
273
274	ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
275				    ARRAY_SIZE(rx6110_default_regs));
276	if (ret)
277		return ret;
278
279	ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
280	if (ret)
281		return ret;
282
283	/* check for VLF Flag (set at power-on) */
284	if ((flags & RX6110_BIT_FLAG_VLF))
285		dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
286
287	/* check for Alarm Flag */
288	if (flags & RX6110_BIT_FLAG_AF)
289		dev_warn(&rtc->dev, "An alarm may have been missed.\n");
290
291	/* check for Periodic Timer Flag */
292	if (flags & RX6110_BIT_FLAG_TF)
293		dev_warn(&rtc->dev, "Periodic timer was detected\n");
294
295	/* check for Update Timer Flag */
296	if (flags & RX6110_BIT_FLAG_UF)
297		dev_warn(&rtc->dev, "Update timer was detected\n");
298
299	/* clear all flags BUT VLF */
300	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
301				 RX6110_BIT_FLAG_AF |
302				 RX6110_BIT_FLAG_UF |
303				 RX6110_BIT_FLAG_TF,
304				 0);
305
306	return ret;
307}
308
309static const struct rtc_class_ops rx6110_rtc_ops = {
310	.read_time = rx6110_get_time,
311	.set_time = rx6110_set_time,
312};
313
314static int rx6110_probe(struct rx6110_data *rx6110, struct device *dev)
315{
316	int err;
317
318	rx6110->rtc = devm_rtc_device_register(dev,
319					       RX6110_DRIVER_NAME,
320					       &rx6110_rtc_ops, THIS_MODULE);
321
322	if (IS_ERR(rx6110->rtc))
323		return PTR_ERR(rx6110->rtc);
324
325	err = rx6110_init(rx6110);
326	if (err)
327		return err;
328
329	rx6110->rtc->max_user_freq = 1;
330
331	return 0;
332}
333
334#if IS_ENABLED(CONFIG_SPI_MASTER)
335static struct regmap_config regmap_spi_config = {
336	.reg_bits = 8,
337	.val_bits = 8,
338	.max_register = RX6110_REG_IRQ,
339	.read_flag_mask = 0x80,
340};
341
342/**
343 * rx6110_spi_probe - initialize rtc driver
344 * @spi: pointer to spi device
345 */
346static int rx6110_spi_probe(struct spi_device *spi)
347{
348	struct rx6110_data *rx6110;
 
349
350	if ((spi->bits_per_word && spi->bits_per_word != 8) ||
351	    (spi->max_speed_hz > 2000000) ||
352	    (spi->mode != (SPI_CS_HIGH | SPI_CPOL | SPI_CPHA))) {
353		dev_warn(&spi->dev, "SPI settings: bits_per_word: %d, max_speed_hz: %d, mode: %xh\n",
354			 spi->bits_per_word, spi->max_speed_hz, spi->mode);
355		dev_warn(&spi->dev, "driving device in an unsupported mode");
356	}
357
358	rx6110 = devm_kzalloc(&spi->dev, sizeof(*rx6110), GFP_KERNEL);
359	if (!rx6110)
360		return -ENOMEM;
361
362	rx6110->regmap = devm_regmap_init_spi(spi, &regmap_spi_config);
363	if (IS_ERR(rx6110->regmap)) {
364		dev_err(&spi->dev, "regmap init failed for rtc rx6110\n");
365		return PTR_ERR(rx6110->regmap);
366	}
367
368	spi_set_drvdata(spi, rx6110);
369
370	return rx6110_probe(rx6110, &spi->dev);
371}
372
373static const struct spi_device_id rx6110_spi_id[] = {
374	{ "rx6110", 0 },
375	{ }
376};
377MODULE_DEVICE_TABLE(spi, rx6110_spi_id);
378
379static const __maybe_unused struct of_device_id rx6110_spi_of_match[] = {
380	{ .compatible = "epson,rx6110" },
381	{ },
382};
383MODULE_DEVICE_TABLE(of, rx6110_spi_of_match);
384
385static struct spi_driver rx6110_spi_driver = {
386	.driver = {
387		.name = RX6110_DRIVER_NAME,
388		.of_match_table = of_match_ptr(rx6110_spi_of_match),
389	},
390	.probe		= rx6110_spi_probe,
391	.id_table	= rx6110_spi_id,
392};
393
394static int rx6110_spi_register(void)
395{
396	return spi_register_driver(&rx6110_spi_driver);
397}
398
399static void rx6110_spi_unregister(void)
400{
401	spi_unregister_driver(&rx6110_spi_driver);
402}
403#else
404static int rx6110_spi_register(void)
405{
406	return 0;
407}
408
409static void rx6110_spi_unregister(void)
410{
411}
412#endif /* CONFIG_SPI_MASTER */
413
414#if IS_ENABLED(CONFIG_I2C)
415static struct regmap_config regmap_i2c_config = {
416	.reg_bits = 8,
417	.val_bits = 8,
418	.max_register = RX6110_REG_IRQ,
419	.read_flag_mask = 0x80,
420};
421
422static int rx6110_i2c_probe(struct i2c_client *client)
423{
424	struct i2c_adapter *adapter = client->adapter;
425	struct rx6110_data *rx6110;
426
427	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
428				| I2C_FUNC_SMBUS_I2C_BLOCK)) {
429		dev_err(&adapter->dev,
430			"doesn't support required functionality\n");
431		return -EIO;
432	}
433
434	rx6110 = devm_kzalloc(&client->dev, sizeof(*rx6110), GFP_KERNEL);
435	if (!rx6110)
436		return -ENOMEM;
437
438	rx6110->regmap = devm_regmap_init_i2c(client, &regmap_i2c_config);
439	if (IS_ERR(rx6110->regmap)) {
440		dev_err(&client->dev, "regmap init failed for rtc rx6110\n");
441		return PTR_ERR(rx6110->regmap);
442	}
443
444	i2c_set_clientdata(client, rx6110);
445
446	return rx6110_probe(rx6110, &client->dev);
447}
448
449static const struct acpi_device_id rx6110_i2c_acpi_match[] = {
450	{ "SECC6110" },
451	{ }
452};
453MODULE_DEVICE_TABLE(acpi, rx6110_i2c_acpi_match);
454
455static const struct i2c_device_id rx6110_i2c_id[] = {
456	{ "rx6110", 0 },
457	{ }
458};
459MODULE_DEVICE_TABLE(i2c, rx6110_i2c_id);
460
461static struct i2c_driver rx6110_i2c_driver = {
462	.driver = {
463		.name = RX6110_DRIVER_NAME,
464		.acpi_match_table = rx6110_i2c_acpi_match,
465	},
466	.probe_new	= rx6110_i2c_probe,
467	.id_table	= rx6110_i2c_id,
468};
469
470static int rx6110_i2c_register(void)
471{
472	return i2c_add_driver(&rx6110_i2c_driver);
473}
474
475static void rx6110_i2c_unregister(void)
476{
477	i2c_del_driver(&rx6110_i2c_driver);
478}
479#else
480static int rx6110_i2c_register(void)
481{
482	return 0;
483}
484
485static void rx6110_i2c_unregister(void)
486{
487}
488#endif /* CONFIG_I2C */
489
490static int __init rx6110_module_init(void)
491{
492	int ret;
493
494	ret = rx6110_spi_register();
495	if (ret)
496		return ret;
497
498	ret = rx6110_i2c_register();
499	if (ret)
500		rx6110_spi_unregister();
501
502	return ret;
503}
504module_init(rx6110_module_init);
505
506static void __exit rx6110_module_exit(void)
507{
508	rx6110_spi_unregister();
509	rx6110_i2c_unregister();
510}
511module_exit(rx6110_module_exit);
512
513MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
514MODULE_DESCRIPTION("RX-6110 SA RTC driver");
515MODULE_LICENSE("GPL");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for the Epson RTC module RX-6110 SA
  4 *
  5 * Copyright(C) 2015 Pengutronix, Steffen Trumtrar <kernel@pengutronix.de>
  6 * Copyright(C) SEIKO EPSON CORPORATION 2013. All rights reserved.
  7 */
  8
  9#include <linux/bcd.h>
 10#include <linux/init.h>
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/of_gpio.h>
 14#include <linux/regmap.h>
 15#include <linux/rtc.h>
 16#include <linux/of.h>
 17#include <linux/of_device.h>
 18#include <linux/spi/spi.h>
 
 19
 20/* RX-6110 Register definitions */
 21#define RX6110_REG_SEC		0x10
 22#define RX6110_REG_MIN		0x11
 23#define RX6110_REG_HOUR		0x12
 24#define RX6110_REG_WDAY		0x13
 25#define RX6110_REG_MDAY		0x14
 26#define RX6110_REG_MONTH	0x15
 27#define RX6110_REG_YEAR		0x16
 28#define RX6110_REG_RES1		0x17
 29#define RX6110_REG_ALMIN	0x18
 30#define RX6110_REG_ALHOUR	0x19
 31#define RX6110_REG_ALWDAY	0x1A
 32#define RX6110_REG_TCOUNT0	0x1B
 33#define RX6110_REG_TCOUNT1	0x1C
 34#define RX6110_REG_EXT		0x1D
 35#define RX6110_REG_FLAG		0x1E
 36#define RX6110_REG_CTRL		0x1F
 37#define RX6110_REG_USER0	0x20
 38#define RX6110_REG_USER1	0x21
 39#define RX6110_REG_USER2	0x22
 40#define RX6110_REG_USER3	0x23
 41#define RX6110_REG_USER4	0x24
 42#define RX6110_REG_USER5	0x25
 43#define RX6110_REG_USER6	0x26
 44#define RX6110_REG_USER7	0x27
 45#define RX6110_REG_USER8	0x28
 46#define RX6110_REG_USER9	0x29
 47#define RX6110_REG_USERA	0x2A
 48#define RX6110_REG_USERB	0x2B
 49#define RX6110_REG_USERC	0x2C
 50#define RX6110_REG_USERD	0x2D
 51#define RX6110_REG_USERE	0x2E
 52#define RX6110_REG_USERF	0x2F
 53#define RX6110_REG_RES2		0x30
 54#define RX6110_REG_RES3		0x31
 55#define RX6110_REG_IRQ		0x32
 56
 57#define RX6110_BIT_ALARM_EN		BIT(7)
 58
 59/* Extension Register (1Dh) bit positions */
 60#define RX6110_BIT_EXT_TSEL0		BIT(0)
 61#define RX6110_BIT_EXT_TSEL1		BIT(1)
 62#define RX6110_BIT_EXT_TSEL2		BIT(2)
 63#define RX6110_BIT_EXT_WADA		BIT(3)
 64#define RX6110_BIT_EXT_TE		BIT(4)
 65#define RX6110_BIT_EXT_USEL		BIT(5)
 66#define RX6110_BIT_EXT_FSEL0		BIT(6)
 67#define RX6110_BIT_EXT_FSEL1		BIT(7)
 68
 69/* Flag Register (1Eh) bit positions */
 70#define RX6110_BIT_FLAG_VLF		BIT(1)
 71#define RX6110_BIT_FLAG_AF		BIT(3)
 72#define RX6110_BIT_FLAG_TF		BIT(4)
 73#define RX6110_BIT_FLAG_UF		BIT(5)
 74
 75/* Control Register (1Fh) bit positions */
 76#define RX6110_BIT_CTRL_TBKE		BIT(0)
 77#define RX6110_BIT_CTRL_TBKON		BIT(1)
 78#define RX6110_BIT_CTRL_TSTP		BIT(2)
 79#define RX6110_BIT_CTRL_AIE		BIT(3)
 80#define RX6110_BIT_CTRL_TIE		BIT(4)
 81#define RX6110_BIT_CTRL_UIE		BIT(5)
 82#define RX6110_BIT_CTRL_STOP		BIT(6)
 83#define RX6110_BIT_CTRL_TEST		BIT(7)
 84
 85enum {
 86	RTC_SEC = 0,
 87	RTC_MIN,
 88	RTC_HOUR,
 89	RTC_WDAY,
 90	RTC_MDAY,
 91	RTC_MONTH,
 92	RTC_YEAR,
 93	RTC_NR_TIME
 94};
 95
 96#define RX6110_DRIVER_NAME		"rx6110"
 97
 98struct rx6110_data {
 99	struct rtc_device *rtc;
100	struct regmap *regmap;
101};
102
103/**
104 * rx6110_rtc_tm_to_data - convert rtc_time to native time encoding
105 *
106 * @tm: holds date and time
107 * @data: holds the encoding in rx6110 native form
108 */
109static int rx6110_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
110{
111	pr_debug("%s: date %ptRr\n", __func__, tm);
112
113	/*
114	 * The year in the RTC is a value between 0 and 99.
115	 * Assume that this represents the current century
116	 * and disregard all other values.
117	 */
118	if (tm->tm_year < 100 || tm->tm_year >= 200)
119		return -EINVAL;
120
121	data[RTC_SEC] = bin2bcd(tm->tm_sec);
122	data[RTC_MIN] = bin2bcd(tm->tm_min);
123	data[RTC_HOUR] = bin2bcd(tm->tm_hour);
124	data[RTC_WDAY] = BIT(bin2bcd(tm->tm_wday));
125	data[RTC_MDAY] = bin2bcd(tm->tm_mday);
126	data[RTC_MONTH] = bin2bcd(tm->tm_mon + 1);
127	data[RTC_YEAR] = bin2bcd(tm->tm_year % 100);
128
129	return 0;
130}
131
132/**
133 * rx6110_data_to_rtc_tm - convert native time encoding to rtc_time
134 *
135 * @data: holds the encoding in rx6110 native form
136 * @tm: holds date and time
137 */
138static int rx6110_data_to_rtc_tm(u8 *data, struct rtc_time *tm)
139{
140	tm->tm_sec = bcd2bin(data[RTC_SEC] & 0x7f);
141	tm->tm_min = bcd2bin(data[RTC_MIN] & 0x7f);
142	/* only 24-hour clock */
143	tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
144	tm->tm_wday = ffs(data[RTC_WDAY] & 0x7f);
145	tm->tm_mday = bcd2bin(data[RTC_MDAY] & 0x3f);
146	tm->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1f) - 1;
147	tm->tm_year = bcd2bin(data[RTC_YEAR]) + 100;
148
149	pr_debug("%s: date %ptRr\n", __func__, tm);
150
151	/*
152	 * The year in the RTC is a value between 0 and 99.
153	 * Assume that this represents the current century
154	 * and disregard all other values.
155	 */
156	if (tm->tm_year < 100 || tm->tm_year >= 200)
157		return -EINVAL;
158
159	return 0;
160}
161
162/**
163 * rx6110_set_time - set the current time in the rx6110 registers
164 *
165 * @dev: the rtc device in use
166 * @tm: holds date and time
167 *
168 * BUG: The HW assumes every year that is a multiple of 4 to be a leap
169 * year. Next time this is wrong is 2100, which will not be a leap year
170 *
171 * Note: If STOP is not set/cleared, the clock will start when the seconds
172 *       register is written
173 *
174 */
175static int rx6110_set_time(struct device *dev, struct rtc_time *tm)
176{
177	struct rx6110_data *rx6110 = dev_get_drvdata(dev);
178	u8 data[RTC_NR_TIME];
179	int ret;
180
181	ret = rx6110_rtc_tm_to_data(tm, data);
182	if (ret < 0)
183		return ret;
184
185	/* set STOP bit before changing clock/calendar */
186	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
187				 RX6110_BIT_CTRL_STOP, RX6110_BIT_CTRL_STOP);
188	if (ret)
189		return ret;
190
191	ret = regmap_bulk_write(rx6110->regmap, RX6110_REG_SEC, data,
192				RTC_NR_TIME);
193	if (ret)
194		return ret;
195
196	/* The time in the RTC is valid. Be sure to have VLF cleared. */
197	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
198				 RX6110_BIT_FLAG_VLF, 0);
199	if (ret)
200		return ret;
201
202	/* clear STOP bit after changing clock/calendar */
203	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_CTRL,
204				 RX6110_BIT_CTRL_STOP, 0);
205
206	return ret;
207}
208
209/**
210 * rx6110_get_time - get the current time from the rx6110 registers
211 * @dev: the rtc device in use
212 * @tm: holds date and time
213 */
214static int rx6110_get_time(struct device *dev, struct rtc_time *tm)
215{
216	struct rx6110_data *rx6110 = dev_get_drvdata(dev);
217	u8 data[RTC_NR_TIME];
218	int flags;
219	int ret;
220
221	ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
222	if (ret)
223		return -EINVAL;
224
225	/* check for VLF Flag (set at power-on) */
226	if ((flags & RX6110_BIT_FLAG_VLF)) {
227		dev_warn(dev, "Voltage low, data is invalid.\n");
228		return -EINVAL;
229	}
230
231	/* read registers to date */
232	ret = regmap_bulk_read(rx6110->regmap, RX6110_REG_SEC, data,
233			       RTC_NR_TIME);
234	if (ret)
235		return ret;
236
237	ret = rx6110_data_to_rtc_tm(data, tm);
238	if (ret)
239		return ret;
240
241	dev_dbg(dev, "%s: date %ptRr\n", __func__, tm);
242
243	return 0;
244}
245
246static const struct reg_sequence rx6110_default_regs[] = {
247	{ RX6110_REG_RES1,   0xB8 },
248	{ RX6110_REG_RES2,   0x00 },
249	{ RX6110_REG_RES3,   0x10 },
250	{ RX6110_REG_IRQ,    0x00 },
251	{ RX6110_REG_ALMIN,  0x00 },
252	{ RX6110_REG_ALHOUR, 0x00 },
253	{ RX6110_REG_ALWDAY, 0x00 },
254};
255
256/**
257 * rx6110_init - initialize the rx6110 registers
258 *
259 * @rx6110: pointer to the rx6110 struct in use
260 *
261 */
262static int rx6110_init(struct rx6110_data *rx6110)
263{
264	struct rtc_device *rtc = rx6110->rtc;
265	int flags;
266	int ret;
267
268	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_EXT,
269				 RX6110_BIT_EXT_TE, 0);
270	if (ret)
271		return ret;
272
273	ret = regmap_register_patch(rx6110->regmap, rx6110_default_regs,
274				    ARRAY_SIZE(rx6110_default_regs));
275	if (ret)
276		return ret;
277
278	ret = regmap_read(rx6110->regmap, RX6110_REG_FLAG, &flags);
279	if (ret)
280		return ret;
281
282	/* check for VLF Flag (set at power-on) */
283	if ((flags & RX6110_BIT_FLAG_VLF))
284		dev_warn(&rtc->dev, "Voltage low, data loss detected.\n");
285
286	/* check for Alarm Flag */
287	if (flags & RX6110_BIT_FLAG_AF)
288		dev_warn(&rtc->dev, "An alarm may have been missed.\n");
289
290	/* check for Periodic Timer Flag */
291	if (flags & RX6110_BIT_FLAG_TF)
292		dev_warn(&rtc->dev, "Periodic timer was detected\n");
293
294	/* check for Update Timer Flag */
295	if (flags & RX6110_BIT_FLAG_UF)
296		dev_warn(&rtc->dev, "Update timer was detected\n");
297
298	/* clear all flags BUT VLF */
299	ret = regmap_update_bits(rx6110->regmap, RX6110_REG_FLAG,
300				 RX6110_BIT_FLAG_AF |
301				 RX6110_BIT_FLAG_UF |
302				 RX6110_BIT_FLAG_TF,
303				 0);
304
305	return ret;
306}
307
308static const struct rtc_class_ops rx6110_rtc_ops = {
309	.read_time = rx6110_get_time,
310	.set_time = rx6110_set_time,
311};
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313static struct regmap_config regmap_spi_config = {
314	.reg_bits = 8,
315	.val_bits = 8,
316	.max_register = RX6110_REG_IRQ,
317	.read_flag_mask = 0x80,
318};
319
320/**
321 * rx6110_probe - initialize rtc driver
322 * @spi: pointer to spi device
323 */
324static int rx6110_probe(struct spi_device *spi)
325{
326	struct rx6110_data *rx6110;
327	int err;
328
329	if ((spi->bits_per_word && spi->bits_per_word != 8) ||
330	    (spi->max_speed_hz > 2000000) ||
331	    (spi->mode != (SPI_CS_HIGH | SPI_CPOL | SPI_CPHA))) {
332		dev_warn(&spi->dev, "SPI settings: bits_per_word: %d, max_speed_hz: %d, mode: %xh\n",
333			 spi->bits_per_word, spi->max_speed_hz, spi->mode);
334		dev_warn(&spi->dev, "driving device in an unsupported mode");
335	}
336
337	rx6110 = devm_kzalloc(&spi->dev, sizeof(*rx6110), GFP_KERNEL);
338	if (!rx6110)
339		return -ENOMEM;
340
341	rx6110->regmap = devm_regmap_init_spi(spi, &regmap_spi_config);
342	if (IS_ERR(rx6110->regmap)) {
343		dev_err(&spi->dev, "regmap init failed for rtc rx6110\n");
344		return PTR_ERR(rx6110->regmap);
345	}
346
347	spi_set_drvdata(spi, rx6110);
348
349	rx6110->rtc = devm_rtc_device_register(&spi->dev,
350					       RX6110_DRIVER_NAME,
351					       &rx6110_rtc_ops, THIS_MODULE);
 
 
 
 
 
352
353	if (IS_ERR(rx6110->rtc))
354		return PTR_ERR(rx6110->rtc);
 
 
 
355
356	err = rx6110_init(rx6110);
357	if (err)
358		return err;
 
 
 
 
 
359
360	rx6110->rtc->max_user_freq = 1;
 
 
 
361
 
 
 
 
 
 
 
362	return 0;
363}
364
365static const struct spi_device_id rx6110_id[] = {
366	{ "rx6110", 0 },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367	{ }
368};
369MODULE_DEVICE_TABLE(spi, rx6110_id);
370
371static const struct of_device_id rx6110_spi_of_match[] = {
372	{ .compatible = "epson,rx6110" },
373	{ },
374};
375MODULE_DEVICE_TABLE(of, rx6110_spi_of_match);
376
377static struct spi_driver rx6110_driver = {
378	.driver = {
379		.name = RX6110_DRIVER_NAME,
380		.of_match_table = of_match_ptr(rx6110_spi_of_match),
381	},
382	.probe		= rx6110_probe,
383	.id_table	= rx6110_id,
384};
385
386module_spi_driver(rx6110_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
388MODULE_AUTHOR("Val Krutov <val.krutov@erd.epson.com>");
389MODULE_DESCRIPTION("RX-6110 SA RTC driver");
390MODULE_LICENSE("GPL");