Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * A driver for the I2C members of the Abracon AB x8xx RTC family,
  3 * and compatible: AB 1805 and AB 0805
  4 *
  5 * Copyright 2014-2015 Macq S.A.
  6 *
  7 * Author: Philippe De Muyter <phdm@macqel.be>
  8 * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com>
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License version 2 as
 12 * published by the Free Software Foundation.
 13 *
 14 */
 15
 16#include <linux/bcd.h>
 17#include <linux/i2c.h>
 18#include <linux/module.h>
 
 19#include <linux/rtc.h>
 
 20
 21#define ABX8XX_REG_HTH		0x00
 22#define ABX8XX_REG_SC		0x01
 23#define ABX8XX_REG_MN		0x02
 24#define ABX8XX_REG_HR		0x03
 25#define ABX8XX_REG_DA		0x04
 26#define ABX8XX_REG_MO		0x05
 27#define ABX8XX_REG_YR		0x06
 28#define ABX8XX_REG_WD		0x07
 29
 30#define ABX8XX_REG_AHTH		0x08
 31#define ABX8XX_REG_ASC		0x09
 32#define ABX8XX_REG_AMN		0x0a
 33#define ABX8XX_REG_AHR		0x0b
 34#define ABX8XX_REG_ADA		0x0c
 35#define ABX8XX_REG_AMO		0x0d
 36#define ABX8XX_REG_AWD		0x0e
 37
 38#define ABX8XX_REG_STATUS	0x0f
 39#define ABX8XX_STATUS_AF	BIT(2)
 
 
 40
 41#define ABX8XX_REG_CTRL1	0x10
 42#define ABX8XX_CTRL_WRITE	BIT(0)
 43#define ABX8XX_CTRL_ARST	BIT(2)
 44#define ABX8XX_CTRL_12_24	BIT(6)
 45
 
 
 
 46#define ABX8XX_REG_IRQ		0x12
 47#define ABX8XX_IRQ_AIE		BIT(2)
 48#define ABX8XX_IRQ_IM_1_4	(0x3 << 5)
 49
 50#define ABX8XX_REG_CD_TIMER_CTL	0x18
 51
 52#define ABX8XX_REG_OSC		0x1c
 53#define ABX8XX_OSC_FOS		BIT(3)
 54#define ABX8XX_OSC_BOS		BIT(4)
 55#define ABX8XX_OSC_ACAL_512	BIT(5)
 56#define ABX8XX_OSC_ACAL_1024	BIT(6)
 57
 58#define ABX8XX_OSC_OSEL		BIT(7)
 59
 60#define ABX8XX_REG_OSS		0x1d
 61#define ABX8XX_OSS_OF		BIT(1)
 62#define ABX8XX_OSS_OMODE	BIT(4)
 63
 
 
 
 
 
 
 
 
 64#define ABX8XX_REG_CFG_KEY	0x1f
 65#define ABX8XX_CFG_KEY_OSC	0xa1
 66#define ABX8XX_CFG_KEY_MISC	0x9d
 67
 68#define ABX8XX_REG_ID0		0x28
 69
 
 
 
 70#define ABX8XX_REG_TRICKLE	0x20
 71#define ABX8XX_TRICKLE_CHARGE_ENABLE	0xa0
 72#define ABX8XX_TRICKLE_STANDARD_DIODE	0x8
 73#define ABX8XX_TRICKLE_SCHOTTKY_DIODE	0x4
 74
 75static u8 trickle_resistors[] = {0, 3, 6, 11};
 76
 77enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
 78	AB1801, AB1803, AB1804, AB1805, ABX80X};
 79
 80struct abx80x_cap {
 81	u16 pn;
 82	bool has_tc;
 
 83};
 84
 85static struct abx80x_cap abx80x_caps[] = {
 86	[AB0801] = {.pn = 0x0801},
 87	[AB0803] = {.pn = 0x0803},
 88	[AB0804] = {.pn = 0x0804, .has_tc = true},
 89	[AB0805] = {.pn = 0x0805, .has_tc = true},
 90	[AB1801] = {.pn = 0x1801},
 91	[AB1803] = {.pn = 0x1803},
 92	[AB1804] = {.pn = 0x1804, .has_tc = true},
 93	[AB1805] = {.pn = 0x1805, .has_tc = true},
 
 94	[ABX80X] = {.pn = 0}
 95};
 96
 
 
 
 
 
 
 97static int abx80x_is_rc_mode(struct i2c_client *client)
 98{
 99	int flags = 0;
100
101	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
102	if (flags < 0) {
103		dev_err(&client->dev,
104			"Failed to read autocalibration attribute\n");
105		return flags;
106	}
107
108	return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
109}
110
111static int abx80x_enable_trickle_charger(struct i2c_client *client,
112					 u8 trickle_cfg)
113{
114	int err;
115
116	/*
117	 * Write the configuration key register to enable access to the Trickle
118	 * register
119	 */
120	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
121					ABX8XX_CFG_KEY_MISC);
122	if (err < 0) {
123		dev_err(&client->dev, "Unable to write configuration key\n");
124		return -EIO;
125	}
126
127	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
128					ABX8XX_TRICKLE_CHARGE_ENABLE |
129					trickle_cfg);
130	if (err < 0) {
131		dev_err(&client->dev, "Unable to write trickle register\n");
132		return -EIO;
133	}
134
135	return 0;
136}
137
138static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
139{
140	struct i2c_client *client = to_i2c_client(dev);
141	unsigned char buf[8];
142	int err, flags, rc_mode = 0;
143
144	/* Read the Oscillator Failure only in XT mode */
145	rc_mode = abx80x_is_rc_mode(client);
146	if (rc_mode < 0)
147		return rc_mode;
148
149	if (!rc_mode) {
150		flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
151		if (flags < 0)
152			return flags;
153
154		if (flags & ABX8XX_OSS_OF) {
155			dev_err(dev, "Oscillator failure, data is invalid.\n");
156			return -EINVAL;
157		}
158	}
159
160	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
161					    sizeof(buf), buf);
162	if (err < 0) {
163		dev_err(&client->dev, "Unable to read date\n");
164		return -EIO;
165	}
166
167	tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
168	tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
169	tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
170	tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
171	tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
172	tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
173	tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
174
175	err = rtc_valid_tm(tm);
176	if (err < 0)
177		dev_err(&client->dev, "retrieved date/time is not valid.\n");
178
179	return err;
180}
181
182static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
183{
184	struct i2c_client *client = to_i2c_client(dev);
185	unsigned char buf[8];
186	int err, flags;
187
188	if (tm->tm_year < 100)
189		return -EINVAL;
190
191	buf[ABX8XX_REG_HTH] = 0;
192	buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
193	buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
194	buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
195	buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
196	buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
197	buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
198	buf[ABX8XX_REG_WD] = tm->tm_wday;
199
200	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
201					     sizeof(buf), buf);
202	if (err < 0) {
203		dev_err(&client->dev, "Unable to write to date registers\n");
204		return -EIO;
205	}
206
207	/* Clear the OF bit of Oscillator Status Register */
208	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
209	if (flags < 0)
210		return flags;
211
212	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
213					flags & ~ABX8XX_OSS_OF);
214	if (err < 0) {
215		dev_err(&client->dev, "Unable to write oscillator status register\n");
216		return err;
217	}
218
219	return 0;
220}
221
222static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
223{
224	struct i2c_client *client = dev_id;
225	struct rtc_device *rtc = i2c_get_clientdata(client);
 
226	int status;
227
228	status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
229	if (status < 0)
230		return IRQ_NONE;
231
232	if (status & ABX8XX_STATUS_AF)
233		rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
234
 
 
 
 
 
 
 
235	i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
236
237	return IRQ_HANDLED;
238}
239
240static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
241{
242	struct i2c_client *client = to_i2c_client(dev);
243	unsigned char buf[7];
244
245	int irq_mask, err;
246
247	if (client->irq <= 0)
248		return -EINVAL;
249
250	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
251					    sizeof(buf), buf);
252	if (err)
253		return err;
254
255	irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
256	if (irq_mask < 0)
257		return irq_mask;
258
259	t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
260	t->time.tm_min = bcd2bin(buf[1] & 0x7F);
261	t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
262	t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
263	t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
264	t->time.tm_wday = buf[5] & 0x7;
265
266	t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
267	t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
268
269	return err;
270}
271
272static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
273{
274	struct i2c_client *client = to_i2c_client(dev);
275	u8 alarm[6];
276	int err;
277
278	if (client->irq <= 0)
279		return -EINVAL;
280
281	alarm[0] = 0x0;
282	alarm[1] = bin2bcd(t->time.tm_sec);
283	alarm[2] = bin2bcd(t->time.tm_min);
284	alarm[3] = bin2bcd(t->time.tm_hour);
285	alarm[4] = bin2bcd(t->time.tm_mday);
286	alarm[5] = bin2bcd(t->time.tm_mon + 1);
287
288	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
289					     sizeof(alarm), alarm);
290	if (err < 0) {
291		dev_err(&client->dev, "Unable to write alarm registers\n");
292		return -EIO;
293	}
294
295	if (t->enabled) {
296		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
297						(ABX8XX_IRQ_IM_1_4 |
298						 ABX8XX_IRQ_AIE));
299		if (err)
300			return err;
301	}
302
303	return 0;
304}
305
306static int abx80x_rtc_set_autocalibration(struct device *dev,
307					  int autocalibration)
308{
309	struct i2c_client *client = to_i2c_client(dev);
310	int retval, flags = 0;
311
312	if ((autocalibration != 0) && (autocalibration != 1024) &&
313	    (autocalibration != 512)) {
314		dev_err(dev, "autocalibration value outside permitted range\n");
315		return -EINVAL;
316	}
317
318	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
319	if (flags < 0)
320		return flags;
321
322	if (autocalibration == 0) {
323		flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
324	} else if (autocalibration == 1024) {
325		/* 1024 autocalibration is 0x10 */
326		flags |= ABX8XX_OSC_ACAL_1024;
327		flags &= ~(ABX8XX_OSC_ACAL_512);
328	} else {
329		/* 512 autocalibration is 0x11 */
330		flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
331	}
332
333	/* Unlock write access to Oscillator Control Register */
334	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
335					   ABX8XX_CFG_KEY_OSC);
336	if (retval < 0) {
337		dev_err(dev, "Failed to write CONFIG_KEY register\n");
338		return retval;
339	}
340
341	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
342
343	return retval;
344}
345
346static int abx80x_rtc_get_autocalibration(struct device *dev)
347{
348	struct i2c_client *client = to_i2c_client(dev);
349	int flags = 0, autocalibration;
350
351	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
352	if (flags < 0)
353		return flags;
354
355	if (flags & ABX8XX_OSC_ACAL_512)
356		autocalibration = 512;
357	else if (flags & ABX8XX_OSC_ACAL_1024)
358		autocalibration = 1024;
359	else
360		autocalibration = 0;
361
362	return autocalibration;
363}
364
365static ssize_t autocalibration_store(struct device *dev,
366				     struct device_attribute *attr,
367				     const char *buf, size_t count)
368{
369	int retval;
370	unsigned long autocalibration = 0;
371
372	retval = kstrtoul(buf, 10, &autocalibration);
373	if (retval < 0) {
374		dev_err(dev, "Failed to store RTC autocalibration attribute\n");
375		return -EINVAL;
376	}
377
378	retval = abx80x_rtc_set_autocalibration(dev, autocalibration);
379
380	return retval ? retval : count;
381}
382
383static ssize_t autocalibration_show(struct device *dev,
384				    struct device_attribute *attr, char *buf)
385{
386	int autocalibration = 0;
387
388	autocalibration = abx80x_rtc_get_autocalibration(dev);
389	if (autocalibration < 0) {
390		dev_err(dev, "Failed to read RTC autocalibration\n");
391		sprintf(buf, "0\n");
392		return autocalibration;
393	}
394
395	return sprintf(buf, "%d\n", autocalibration);
396}
397
398static DEVICE_ATTR_RW(autocalibration);
399
400static ssize_t oscillator_store(struct device *dev,
401				struct device_attribute *attr,
402				const char *buf, size_t count)
403{
404	struct i2c_client *client = to_i2c_client(dev);
405	int retval, flags, rc_mode = 0;
406
407	if (strncmp(buf, "rc", 2) == 0) {
408		rc_mode = 1;
409	} else if (strncmp(buf, "xtal", 4) == 0) {
410		rc_mode = 0;
411	} else {
412		dev_err(dev, "Oscillator selection value outside permitted ones\n");
413		return -EINVAL;
414	}
415
416	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
417	if (flags < 0)
418		return flags;
419
420	if (rc_mode == 0)
421		flags &= ~(ABX8XX_OSC_OSEL);
422	else
423		flags |= (ABX8XX_OSC_OSEL);
424
425	/* Unlock write access on Oscillator Control register */
426	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
427					   ABX8XX_CFG_KEY_OSC);
428	if (retval < 0) {
429		dev_err(dev, "Failed to write CONFIG_KEY register\n");
430		return retval;
431	}
432
433	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
434	if (retval < 0) {
435		dev_err(dev, "Failed to write Oscillator Control register\n");
436		return retval;
437	}
438
439	return retval ? retval : count;
440}
441
442static ssize_t oscillator_show(struct device *dev,
443			       struct device_attribute *attr, char *buf)
444{
445	int rc_mode = 0;
446	struct i2c_client *client = to_i2c_client(dev);
447
448	rc_mode = abx80x_is_rc_mode(client);
449
450	if (rc_mode < 0) {
451		dev_err(dev, "Failed to read RTC oscillator selection\n");
452		sprintf(buf, "\n");
453		return rc_mode;
454	}
455
456	if (rc_mode)
457		return sprintf(buf, "rc\n");
458	else
459		return sprintf(buf, "xtal\n");
460}
461
462static DEVICE_ATTR_RW(oscillator);
463
464static struct attribute *rtc_calib_attrs[] = {
465	&dev_attr_autocalibration.attr,
466	&dev_attr_oscillator.attr,
467	NULL,
468};
469
470static const struct attribute_group rtc_calib_attr_group = {
471	.attrs		= rtc_calib_attrs,
472};
473
474static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
475{
476	struct i2c_client *client = to_i2c_client(dev);
477	int err;
478
479	if (enabled)
480		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
481						(ABX8XX_IRQ_IM_1_4 |
482						 ABX8XX_IRQ_AIE));
483	else
484		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
485						ABX8XX_IRQ_IM_1_4);
486	return err;
487}
488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489static const struct rtc_class_ops abx80x_rtc_ops = {
490	.read_time	= abx80x_rtc_read_time,
491	.set_time	= abx80x_rtc_set_time,
492	.read_alarm	= abx80x_read_alarm,
493	.set_alarm	= abx80x_set_alarm,
494	.alarm_irq_enable = abx80x_alarm_irq_enable,
 
495};
496
497static int abx80x_dt_trickle_cfg(struct device_node *np)
498{
 
499	const char *diode;
500	int trickle_cfg = 0;
501	int i, ret;
502	u32 tmp;
503
504	ret = of_property_read_string(np, "abracon,tc-diode", &diode);
505	if (ret)
506		return ret;
507
508	if (!strcmp(diode, "standard"))
509		trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
510	else if (!strcmp(diode, "schottky"))
511		trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
512	else
 
513		return -EINVAL;
 
514
515	ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
516	if (ret)
517		return ret;
518
519	for (i = 0; i < sizeof(trickle_resistors); i++)
520		if (trickle_resistors[i] == tmp)
521			break;
522
523	if (i == sizeof(trickle_resistors))
 
524		return -EINVAL;
 
525
526	return (trickle_cfg | i);
527}
528
529static void rtc_calib_remove_sysfs_group(void *_dev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
530{
531	struct device *dev = _dev;
 
 
 
 
 
532
533	sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group);
 
 
534}
 
 
 
 
 
 
535
536static int abx80x_probe(struct i2c_client *client,
537			const struct i2c_device_id *id)
538{
539	struct device_node *np = client->dev.of_node;
540	struct rtc_device *rtc;
541	int i, data, err, trickle_cfg = -EINVAL;
542	char buf[7];
543	unsigned int part = id->driver_data;
544	unsigned int partnumber;
545	unsigned int majrev, minrev;
546	unsigned int lot;
547	unsigned int wafer;
548	unsigned int uid;
549
550	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
551		return -ENODEV;
552
553	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
554					    sizeof(buf), buf);
555	if (err < 0) {
556		dev_err(&client->dev, "Unable to read partnumber\n");
557		return -EIO;
558	}
559
560	partnumber = (buf[0] << 8) | buf[1];
561	majrev = buf[2] >> 3;
562	minrev = buf[2] & 0x7;
563	lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
564	uid = ((buf[4] & 0x7f) << 8) | buf[5];
565	wafer = (buf[6] & 0x7c) >> 2;
566	dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
567		 partnumber, majrev, minrev, lot, wafer, uid);
568
569	data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
570	if (data < 0) {
571		dev_err(&client->dev, "Unable to read control register\n");
572		return -EIO;
573	}
574
575	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
576					((data & ~(ABX8XX_CTRL_12_24 |
577						   ABX8XX_CTRL_ARST)) |
578					 ABX8XX_CTRL_WRITE));
579	if (err < 0) {
580		dev_err(&client->dev, "Unable to write control register\n");
581		return -EIO;
582	}
583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584	/* part autodetection */
585	if (part == ABX80X) {
586		for (i = 0; abx80x_caps[i].pn; i++)
587			if (partnumber == abx80x_caps[i].pn)
588				break;
589		if (abx80x_caps[i].pn == 0) {
590			dev_err(&client->dev, "Unknown part: %04x\n",
591				partnumber);
592			return -EINVAL;
593		}
594		part = i;
595	}
596
597	if (partnumber != abx80x_caps[part].pn) {
598		dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
599			partnumber, abx80x_caps[part].pn);
600		return -EINVAL;
601	}
602
603	if (np && abx80x_caps[part].has_tc)
604		trickle_cfg = abx80x_dt_trickle_cfg(np);
605
606	if (trickle_cfg > 0) {
607		dev_info(&client->dev, "Enabling trickle charger: %02x\n",
608			 trickle_cfg);
609		abx80x_enable_trickle_charger(client, trickle_cfg);
610	}
611
612	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
613					BIT(2));
614	if (err)
615		return err;
616
617	rtc = devm_rtc_device_register(&client->dev, "abx8xx",
618				       &abx80x_rtc_ops, THIS_MODULE);
 
 
 
 
 
 
 
 
619
620	if (IS_ERR(rtc))
621		return PTR_ERR(rtc);
622
623	i2c_set_clientdata(client, rtc);
 
 
 
 
624
625	if (client->irq > 0) {
626		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
627		err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
628						abx80x_handle_irq,
629						IRQF_SHARED | IRQF_ONESHOT,
630						"abx8xx",
631						client);
632		if (err) {
633			dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
634			client->irq = 0;
635		}
636	}
637
638	/* Export sysfs entries */
639	err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group);
640	if (err) {
641		dev_err(&client->dev, "Failed to create sysfs group: %d\n",
642			err);
643		return err;
644	}
645
646	err = devm_add_action(&client->dev, rtc_calib_remove_sysfs_group,
647			      &client->dev);
648	if (err) {
649		rtc_calib_remove_sysfs_group(&client->dev);
650		dev_err(&client->dev,
651			"Failed to add sysfs cleanup action: %d\n",
652			err);
653		return err;
654	}
655
656	return 0;
657}
658
659static int abx80x_remove(struct i2c_client *client)
660{
661	return 0;
662}
663
664static const struct i2c_device_id abx80x_id[] = {
665	{ "abx80x", ABX80X },
666	{ "ab0801", AB0801 },
667	{ "ab0803", AB0803 },
668	{ "ab0804", AB0804 },
669	{ "ab0805", AB0805 },
670	{ "ab1801", AB1801 },
671	{ "ab1803", AB1803 },
672	{ "ab1804", AB1804 },
673	{ "ab1805", AB1805 },
674	{ "rv1805", AB1805 },
675	{ }
676};
677MODULE_DEVICE_TABLE(i2c, abx80x_id);
678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
679static struct i2c_driver abx80x_driver = {
680	.driver		= {
681		.name	= "rtc-abx80x",
 
682	},
683	.probe		= abx80x_probe,
684	.remove		= abx80x_remove,
685	.id_table	= abx80x_id,
686};
687
688module_i2c_driver(abx80x_driver);
689
690MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
691MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
692MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
693MODULE_LICENSE("GPL v2");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * A driver for the I2C members of the Abracon AB x8xx RTC family,
  4 * and compatible: AB 1805 and AB 0805
  5 *
  6 * Copyright 2014-2015 Macq S.A.
  7 *
  8 * Author: Philippe De Muyter <phdm@macqel.be>
  9 * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
 
 
 
 
 10 *
 11 */
 12
 13#include <linux/bcd.h>
 14#include <linux/i2c.h>
 15#include <linux/module.h>
 16#include <linux/of_device.h>
 17#include <linux/rtc.h>
 18#include <linux/watchdog.h>
 19
 20#define ABX8XX_REG_HTH		0x00
 21#define ABX8XX_REG_SC		0x01
 22#define ABX8XX_REG_MN		0x02
 23#define ABX8XX_REG_HR		0x03
 24#define ABX8XX_REG_DA		0x04
 25#define ABX8XX_REG_MO		0x05
 26#define ABX8XX_REG_YR		0x06
 27#define ABX8XX_REG_WD		0x07
 28
 29#define ABX8XX_REG_AHTH		0x08
 30#define ABX8XX_REG_ASC		0x09
 31#define ABX8XX_REG_AMN		0x0a
 32#define ABX8XX_REG_AHR		0x0b
 33#define ABX8XX_REG_ADA		0x0c
 34#define ABX8XX_REG_AMO		0x0d
 35#define ABX8XX_REG_AWD		0x0e
 36
 37#define ABX8XX_REG_STATUS	0x0f
 38#define ABX8XX_STATUS_AF	BIT(2)
 39#define ABX8XX_STATUS_BLF	BIT(4)
 40#define ABX8XX_STATUS_WDT	BIT(6)
 41
 42#define ABX8XX_REG_CTRL1	0x10
 43#define ABX8XX_CTRL_WRITE	BIT(0)
 44#define ABX8XX_CTRL_ARST	BIT(2)
 45#define ABX8XX_CTRL_12_24	BIT(6)
 46
 47#define ABX8XX_REG_CTRL2	0x11
 48#define ABX8XX_CTRL2_RSVD	BIT(5)
 49
 50#define ABX8XX_REG_IRQ		0x12
 51#define ABX8XX_IRQ_AIE		BIT(2)
 52#define ABX8XX_IRQ_IM_1_4	(0x3 << 5)
 53
 54#define ABX8XX_REG_CD_TIMER_CTL	0x18
 55
 56#define ABX8XX_REG_OSC		0x1c
 57#define ABX8XX_OSC_FOS		BIT(3)
 58#define ABX8XX_OSC_BOS		BIT(4)
 59#define ABX8XX_OSC_ACAL_512	BIT(5)
 60#define ABX8XX_OSC_ACAL_1024	BIT(6)
 61
 62#define ABX8XX_OSC_OSEL		BIT(7)
 63
 64#define ABX8XX_REG_OSS		0x1d
 65#define ABX8XX_OSS_OF		BIT(1)
 66#define ABX8XX_OSS_OMODE	BIT(4)
 67
 68#define ABX8XX_REG_WDT		0x1b
 69#define ABX8XX_WDT_WDS		BIT(7)
 70#define ABX8XX_WDT_BMB_MASK	0x7c
 71#define ABX8XX_WDT_BMB_SHIFT	2
 72#define ABX8XX_WDT_MAX_TIME	(ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT)
 73#define ABX8XX_WDT_WRB_MASK	0x03
 74#define ABX8XX_WDT_WRB_1HZ	0x02
 75
 76#define ABX8XX_REG_CFG_KEY	0x1f
 77#define ABX8XX_CFG_KEY_OSC	0xa1
 78#define ABX8XX_CFG_KEY_MISC	0x9d
 79
 80#define ABX8XX_REG_ID0		0x28
 81
 82#define ABX8XX_REG_OUT_CTRL	0x30
 83#define ABX8XX_OUT_CTRL_EXDS	BIT(4)
 84
 85#define ABX8XX_REG_TRICKLE	0x20
 86#define ABX8XX_TRICKLE_CHARGE_ENABLE	0xa0
 87#define ABX8XX_TRICKLE_STANDARD_DIODE	0x8
 88#define ABX8XX_TRICKLE_SCHOTTKY_DIODE	0x4
 89
 90static u8 trickle_resistors[] = {0, 3, 6, 11};
 91
 92enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
 93	AB1801, AB1803, AB1804, AB1805, RV1805, ABX80X};
 94
 95struct abx80x_cap {
 96	u16 pn;
 97	bool has_tc;
 98	bool has_wdog;
 99};
100
101static struct abx80x_cap abx80x_caps[] = {
102	[AB0801] = {.pn = 0x0801},
103	[AB0803] = {.pn = 0x0803},
104	[AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true},
105	[AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true},
106	[AB1801] = {.pn = 0x1801},
107	[AB1803] = {.pn = 0x1803},
108	[AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true},
109	[AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
110	[RV1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
111	[ABX80X] = {.pn = 0}
112};
113
114struct abx80x_priv {
115	struct rtc_device *rtc;
116	struct i2c_client *client;
117	struct watchdog_device wdog;
118};
119
120static int abx80x_is_rc_mode(struct i2c_client *client)
121{
122	int flags = 0;
123
124	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
125	if (flags < 0) {
126		dev_err(&client->dev,
127			"Failed to read autocalibration attribute\n");
128		return flags;
129	}
130
131	return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
132}
133
134static int abx80x_enable_trickle_charger(struct i2c_client *client,
135					 u8 trickle_cfg)
136{
137	int err;
138
139	/*
140	 * Write the configuration key register to enable access to the Trickle
141	 * register
142	 */
143	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
144					ABX8XX_CFG_KEY_MISC);
145	if (err < 0) {
146		dev_err(&client->dev, "Unable to write configuration key\n");
147		return -EIO;
148	}
149
150	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
151					ABX8XX_TRICKLE_CHARGE_ENABLE |
152					trickle_cfg);
153	if (err < 0) {
154		dev_err(&client->dev, "Unable to write trickle register\n");
155		return -EIO;
156	}
157
158	return 0;
159}
160
161static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
162{
163	struct i2c_client *client = to_i2c_client(dev);
164	unsigned char buf[8];
165	int err, flags, rc_mode = 0;
166
167	/* Read the Oscillator Failure only in XT mode */
168	rc_mode = abx80x_is_rc_mode(client);
169	if (rc_mode < 0)
170		return rc_mode;
171
172	if (!rc_mode) {
173		flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
174		if (flags < 0)
175			return flags;
176
177		if (flags & ABX8XX_OSS_OF) {
178			dev_err(dev, "Oscillator failure, data is invalid.\n");
179			return -EINVAL;
180		}
181	}
182
183	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
184					    sizeof(buf), buf);
185	if (err < 0) {
186		dev_err(&client->dev, "Unable to read date\n");
187		return -EIO;
188	}
189
190	tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
191	tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
192	tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
193	tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
194	tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
195	tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
196	tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
197
198	return 0;
 
 
 
 
199}
200
201static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
202{
203	struct i2c_client *client = to_i2c_client(dev);
204	unsigned char buf[8];
205	int err, flags;
206
207	if (tm->tm_year < 100)
208		return -EINVAL;
209
210	buf[ABX8XX_REG_HTH] = 0;
211	buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
212	buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
213	buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
214	buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
215	buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
216	buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
217	buf[ABX8XX_REG_WD] = tm->tm_wday;
218
219	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
220					     sizeof(buf), buf);
221	if (err < 0) {
222		dev_err(&client->dev, "Unable to write to date registers\n");
223		return -EIO;
224	}
225
226	/* Clear the OF bit of Oscillator Status Register */
227	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
228	if (flags < 0)
229		return flags;
230
231	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
232					flags & ~ABX8XX_OSS_OF);
233	if (err < 0) {
234		dev_err(&client->dev, "Unable to write oscillator status register\n");
235		return err;
236	}
237
238	return 0;
239}
240
241static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
242{
243	struct i2c_client *client = dev_id;
244	struct abx80x_priv *priv = i2c_get_clientdata(client);
245	struct rtc_device *rtc = priv->rtc;
246	int status;
247
248	status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
249	if (status < 0)
250		return IRQ_NONE;
251
252	if (status & ABX8XX_STATUS_AF)
253		rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
254
255	/*
256	 * It is unclear if we'll get an interrupt before the external
257	 * reset kicks in.
258	 */
259	if (status & ABX8XX_STATUS_WDT)
260		dev_alert(&client->dev, "watchdog timeout interrupt.\n");
261
262	i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
263
264	return IRQ_HANDLED;
265}
266
267static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
268{
269	struct i2c_client *client = to_i2c_client(dev);
270	unsigned char buf[7];
271
272	int irq_mask, err;
273
274	if (client->irq <= 0)
275		return -EINVAL;
276
277	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
278					    sizeof(buf), buf);
279	if (err)
280		return err;
281
282	irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
283	if (irq_mask < 0)
284		return irq_mask;
285
286	t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
287	t->time.tm_min = bcd2bin(buf[1] & 0x7F);
288	t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
289	t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
290	t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
291	t->time.tm_wday = buf[5] & 0x7;
292
293	t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
294	t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
295
296	return err;
297}
298
299static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
300{
301	struct i2c_client *client = to_i2c_client(dev);
302	u8 alarm[6];
303	int err;
304
305	if (client->irq <= 0)
306		return -EINVAL;
307
308	alarm[0] = 0x0;
309	alarm[1] = bin2bcd(t->time.tm_sec);
310	alarm[2] = bin2bcd(t->time.tm_min);
311	alarm[3] = bin2bcd(t->time.tm_hour);
312	alarm[4] = bin2bcd(t->time.tm_mday);
313	alarm[5] = bin2bcd(t->time.tm_mon + 1);
314
315	err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
316					     sizeof(alarm), alarm);
317	if (err < 0) {
318		dev_err(&client->dev, "Unable to write alarm registers\n");
319		return -EIO;
320	}
321
322	if (t->enabled) {
323		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
324						(ABX8XX_IRQ_IM_1_4 |
325						 ABX8XX_IRQ_AIE));
326		if (err)
327			return err;
328	}
329
330	return 0;
331}
332
333static int abx80x_rtc_set_autocalibration(struct device *dev,
334					  int autocalibration)
335{
336	struct i2c_client *client = to_i2c_client(dev);
337	int retval, flags = 0;
338
339	if ((autocalibration != 0) && (autocalibration != 1024) &&
340	    (autocalibration != 512)) {
341		dev_err(dev, "autocalibration value outside permitted range\n");
342		return -EINVAL;
343	}
344
345	flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
346	if (flags < 0)
347		return flags;
348
349	if (autocalibration == 0) {
350		flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
351	} else if (autocalibration == 1024) {
352		/* 1024 autocalibration is 0x10 */
353		flags |= ABX8XX_OSC_ACAL_1024;
354		flags &= ~(ABX8XX_OSC_ACAL_512);
355	} else {
356		/* 512 autocalibration is 0x11 */
357		flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
358	}
359
360	/* Unlock write access to Oscillator Control Register */
361	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
362					   ABX8XX_CFG_KEY_OSC);
363	if (retval < 0) {
364		dev_err(dev, "Failed to write CONFIG_KEY register\n");
365		return retval;
366	}
367
368	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
369
370	return retval;
371}
372
373static int abx80x_rtc_get_autocalibration(struct device *dev)
374{
375	struct i2c_client *client = to_i2c_client(dev);
376	int flags = 0, autocalibration;
377
378	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
379	if (flags < 0)
380		return flags;
381
382	if (flags & ABX8XX_OSC_ACAL_512)
383		autocalibration = 512;
384	else if (flags & ABX8XX_OSC_ACAL_1024)
385		autocalibration = 1024;
386	else
387		autocalibration = 0;
388
389	return autocalibration;
390}
391
392static ssize_t autocalibration_store(struct device *dev,
393				     struct device_attribute *attr,
394				     const char *buf, size_t count)
395{
396	int retval;
397	unsigned long autocalibration = 0;
398
399	retval = kstrtoul(buf, 10, &autocalibration);
400	if (retval < 0) {
401		dev_err(dev, "Failed to store RTC autocalibration attribute\n");
402		return -EINVAL;
403	}
404
405	retval = abx80x_rtc_set_autocalibration(dev->parent, autocalibration);
406
407	return retval ? retval : count;
408}
409
410static ssize_t autocalibration_show(struct device *dev,
411				    struct device_attribute *attr, char *buf)
412{
413	int autocalibration = 0;
414
415	autocalibration = abx80x_rtc_get_autocalibration(dev->parent);
416	if (autocalibration < 0) {
417		dev_err(dev, "Failed to read RTC autocalibration\n");
418		sprintf(buf, "0\n");
419		return autocalibration;
420	}
421
422	return sprintf(buf, "%d\n", autocalibration);
423}
424
425static DEVICE_ATTR_RW(autocalibration);
426
427static ssize_t oscillator_store(struct device *dev,
428				struct device_attribute *attr,
429				const char *buf, size_t count)
430{
431	struct i2c_client *client = to_i2c_client(dev->parent);
432	int retval, flags, rc_mode = 0;
433
434	if (strncmp(buf, "rc", 2) == 0) {
435		rc_mode = 1;
436	} else if (strncmp(buf, "xtal", 4) == 0) {
437		rc_mode = 0;
438	} else {
439		dev_err(dev, "Oscillator selection value outside permitted ones\n");
440		return -EINVAL;
441	}
442
443	flags =  i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
444	if (flags < 0)
445		return flags;
446
447	if (rc_mode == 0)
448		flags &= ~(ABX8XX_OSC_OSEL);
449	else
450		flags |= (ABX8XX_OSC_OSEL);
451
452	/* Unlock write access on Oscillator Control register */
453	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
454					   ABX8XX_CFG_KEY_OSC);
455	if (retval < 0) {
456		dev_err(dev, "Failed to write CONFIG_KEY register\n");
457		return retval;
458	}
459
460	retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
461	if (retval < 0) {
462		dev_err(dev, "Failed to write Oscillator Control register\n");
463		return retval;
464	}
465
466	return retval ? retval : count;
467}
468
469static ssize_t oscillator_show(struct device *dev,
470			       struct device_attribute *attr, char *buf)
471{
472	int rc_mode = 0;
473	struct i2c_client *client = to_i2c_client(dev->parent);
474
475	rc_mode = abx80x_is_rc_mode(client);
476
477	if (rc_mode < 0) {
478		dev_err(dev, "Failed to read RTC oscillator selection\n");
479		sprintf(buf, "\n");
480		return rc_mode;
481	}
482
483	if (rc_mode)
484		return sprintf(buf, "rc\n");
485	else
486		return sprintf(buf, "xtal\n");
487}
488
489static DEVICE_ATTR_RW(oscillator);
490
491static struct attribute *rtc_calib_attrs[] = {
492	&dev_attr_autocalibration.attr,
493	&dev_attr_oscillator.attr,
494	NULL,
495};
496
497static const struct attribute_group rtc_calib_attr_group = {
498	.attrs		= rtc_calib_attrs,
499};
500
501static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
502{
503	struct i2c_client *client = to_i2c_client(dev);
504	int err;
505
506	if (enabled)
507		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
508						(ABX8XX_IRQ_IM_1_4 |
509						 ABX8XX_IRQ_AIE));
510	else
511		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
512						ABX8XX_IRQ_IM_1_4);
513	return err;
514}
515
516static int abx80x_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
517{
518	struct i2c_client *client = to_i2c_client(dev);
519	int status, tmp;
520
521	switch (cmd) {
522	case RTC_VL_READ:
523		status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
524		if (status < 0)
525			return status;
526
527		tmp = status & ABX8XX_STATUS_BLF ? RTC_VL_BACKUP_LOW : 0;
528
529		return put_user(tmp, (unsigned int __user *)arg);
530
531	case RTC_VL_CLR:
532		status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
533		if (status < 0)
534			return status;
535
536		status &= ~ABX8XX_STATUS_BLF;
537
538		tmp = i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
539		if (tmp < 0)
540			return tmp;
541
542		return 0;
543
544	default:
545		return -ENOIOCTLCMD;
546	}
547}
548
549static const struct rtc_class_ops abx80x_rtc_ops = {
550	.read_time	= abx80x_rtc_read_time,
551	.set_time	= abx80x_rtc_set_time,
552	.read_alarm	= abx80x_read_alarm,
553	.set_alarm	= abx80x_set_alarm,
554	.alarm_irq_enable = abx80x_alarm_irq_enable,
555	.ioctl		= abx80x_ioctl,
556};
557
558static int abx80x_dt_trickle_cfg(struct i2c_client *client)
559{
560	struct device_node *np = client->dev.of_node;
561	const char *diode;
562	int trickle_cfg = 0;
563	int i, ret;
564	u32 tmp;
565
566	ret = of_property_read_string(np, "abracon,tc-diode", &diode);
567	if (ret)
568		return ret;
569
570	if (!strcmp(diode, "standard")) {
571		trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
572	} else if (!strcmp(diode, "schottky")) {
573		trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
574	} else {
575		dev_dbg(&client->dev, "Invalid tc-diode value: %s\n", diode);
576		return -EINVAL;
577	}
578
579	ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
580	if (ret)
581		return ret;
582
583	for (i = 0; i < sizeof(trickle_resistors); i++)
584		if (trickle_resistors[i] == tmp)
585			break;
586
587	if (i == sizeof(trickle_resistors)) {
588		dev_dbg(&client->dev, "Invalid tc-resistor value: %u\n", tmp);
589		return -EINVAL;
590	}
591
592	return (trickle_cfg | i);
593}
594
595#ifdef CONFIG_WATCHDOG
596
597static inline u8 timeout_bits(unsigned int timeout)
598{
599	return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) |
600		 ABX8XX_WDT_WRB_1HZ;
601}
602
603static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog,
604				     unsigned int timeout)
605{
606	struct abx80x_priv *priv = watchdog_get_drvdata(wdog);
607	u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout);
608
609	/*
610	 * Writing any timeout to the WDT register resets the watchdog timer.
611	 * Writing 0 disables it.
612	 */
613	return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val);
614}
615
616static int abx80x_wdog_set_timeout(struct watchdog_device *wdog,
617				   unsigned int new_timeout)
618{
619	int err = 0;
620
621	if (watchdog_hw_running(wdog))
622		err = __abx80x_wdog_set_timeout(wdog, new_timeout);
623
624	if (err == 0)
625		wdog->timeout = new_timeout;
626
627	return err;
628}
629
630static int abx80x_wdog_ping(struct watchdog_device *wdog)
631{
632	return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
633}
634
635static int abx80x_wdog_start(struct watchdog_device *wdog)
636{
637	return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
638}
639
640static int abx80x_wdog_stop(struct watchdog_device *wdog)
641{
642	return __abx80x_wdog_set_timeout(wdog, 0);
643}
644
645static const struct watchdog_info abx80x_wdog_info = {
646	.identity = "abx80x watchdog",
647	.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
648};
649
650static const struct watchdog_ops abx80x_wdog_ops = {
651	.owner = THIS_MODULE,
652	.start = abx80x_wdog_start,
653	.stop = abx80x_wdog_stop,
654	.ping = abx80x_wdog_ping,
655	.set_timeout = abx80x_wdog_set_timeout,
656};
657
658static int abx80x_setup_watchdog(struct abx80x_priv *priv)
659{
660	priv->wdog.parent = &priv->client->dev;
661	priv->wdog.ops = &abx80x_wdog_ops;
662	priv->wdog.info = &abx80x_wdog_info;
663	priv->wdog.min_timeout = 1;
664	priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME;
665	priv->wdog.timeout = ABX8XX_WDT_MAX_TIME;
666
667	watchdog_set_drvdata(&priv->wdog, priv);
668
669	return devm_watchdog_register_device(&priv->client->dev, &priv->wdog);
670}
671#else
672static int abx80x_setup_watchdog(struct abx80x_priv *priv)
673{
674	return 0;
675}
676#endif
677
678static int abx80x_probe(struct i2c_client *client,
679			const struct i2c_device_id *id)
680{
681	struct device_node *np = client->dev.of_node;
682	struct abx80x_priv *priv;
683	int i, data, err, trickle_cfg = -EINVAL;
684	char buf[7];
685	unsigned int part = id->driver_data;
686	unsigned int partnumber;
687	unsigned int majrev, minrev;
688	unsigned int lot;
689	unsigned int wafer;
690	unsigned int uid;
691
692	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
693		return -ENODEV;
694
695	err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
696					    sizeof(buf), buf);
697	if (err < 0) {
698		dev_err(&client->dev, "Unable to read partnumber\n");
699		return -EIO;
700	}
701
702	partnumber = (buf[0] << 8) | buf[1];
703	majrev = buf[2] >> 3;
704	minrev = buf[2] & 0x7;
705	lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
706	uid = ((buf[4] & 0x7f) << 8) | buf[5];
707	wafer = (buf[6] & 0x7c) >> 2;
708	dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
709		 partnumber, majrev, minrev, lot, wafer, uid);
710
711	data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
712	if (data < 0) {
713		dev_err(&client->dev, "Unable to read control register\n");
714		return -EIO;
715	}
716
717	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
718					((data & ~(ABX8XX_CTRL_12_24 |
719						   ABX8XX_CTRL_ARST)) |
720					 ABX8XX_CTRL_WRITE));
721	if (err < 0) {
722		dev_err(&client->dev, "Unable to write control register\n");
723		return -EIO;
724	}
725
726	/* Configure RV1805 specifics */
727	if (part == RV1805) {
728		/*
729		 * Avoid accidentally entering test mode. This can happen
730		 * on the RV1805 in case the reserved bit 5 in control2
731		 * register is set. RV-1805-C3 datasheet indicates that
732		 * the bit should be cleared in section 11h - Control2.
733		 */
734		data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL2);
735		if (data < 0) {
736			dev_err(&client->dev,
737				"Unable to read control2 register\n");
738			return -EIO;
739		}
740
741		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL2,
742						data & ~ABX8XX_CTRL2_RSVD);
743		if (err < 0) {
744			dev_err(&client->dev,
745				"Unable to write control2 register\n");
746			return -EIO;
747		}
748
749		/*
750		 * Avoid extra power leakage. The RV1805 uses smaller
751		 * 10pin package and the EXTI input is not present.
752		 * Disable it to avoid leakage.
753		 */
754		data = i2c_smbus_read_byte_data(client, ABX8XX_REG_OUT_CTRL);
755		if (data < 0) {
756			dev_err(&client->dev,
757				"Unable to read output control register\n");
758			return -EIO;
759		}
760
761		/*
762		 * Write the configuration key register to enable access to
763		 * the config2 register
764		 */
765		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
766						ABX8XX_CFG_KEY_MISC);
767		if (err < 0) {
768			dev_err(&client->dev,
769				"Unable to write configuration key\n");
770			return -EIO;
771		}
772
773		err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OUT_CTRL,
774						data | ABX8XX_OUT_CTRL_EXDS);
775		if (err < 0) {
776			dev_err(&client->dev,
777				"Unable to write output control register\n");
778			return -EIO;
779		}
780	}
781
782	/* part autodetection */
783	if (part == ABX80X) {
784		for (i = 0; abx80x_caps[i].pn; i++)
785			if (partnumber == abx80x_caps[i].pn)
786				break;
787		if (abx80x_caps[i].pn == 0) {
788			dev_err(&client->dev, "Unknown part: %04x\n",
789				partnumber);
790			return -EINVAL;
791		}
792		part = i;
793	}
794
795	if (partnumber != abx80x_caps[part].pn) {
796		dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
797			partnumber, abx80x_caps[part].pn);
798		return -EINVAL;
799	}
800
801	if (np && abx80x_caps[part].has_tc)
802		trickle_cfg = abx80x_dt_trickle_cfg(client);
803
804	if (trickle_cfg > 0) {
805		dev_info(&client->dev, "Enabling trickle charger: %02x\n",
806			 trickle_cfg);
807		abx80x_enable_trickle_charger(client, trickle_cfg);
808	}
809
810	err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
811					BIT(2));
812	if (err)
813		return err;
814
815	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
816	if (priv == NULL)
817		return -ENOMEM;
818
819	priv->rtc = devm_rtc_allocate_device(&client->dev);
820	if (IS_ERR(priv->rtc))
821		return PTR_ERR(priv->rtc);
822
823	priv->rtc->ops = &abx80x_rtc_ops;
824	priv->client = client;
825
826	i2c_set_clientdata(client, priv);
 
827
828	if (abx80x_caps[part].has_wdog) {
829		err = abx80x_setup_watchdog(priv);
830		if (err)
831			return err;
832	}
833
834	if (client->irq > 0) {
835		dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
836		err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
837						abx80x_handle_irq,
838						IRQF_SHARED | IRQF_ONESHOT,
839						"abx8xx",
840						client);
841		if (err) {
842			dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
843			client->irq = 0;
844		}
845	}
846
847	err = rtc_add_group(priv->rtc, &rtc_calib_attr_group);
 
848	if (err) {
849		dev_err(&client->dev, "Failed to create sysfs group: %d\n",
850			err);
851		return err;
852	}
853
854	return rtc_register_device(priv->rtc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
855}
856
857static const struct i2c_device_id abx80x_id[] = {
858	{ "abx80x", ABX80X },
859	{ "ab0801", AB0801 },
860	{ "ab0803", AB0803 },
861	{ "ab0804", AB0804 },
862	{ "ab0805", AB0805 },
863	{ "ab1801", AB1801 },
864	{ "ab1803", AB1803 },
865	{ "ab1804", AB1804 },
866	{ "ab1805", AB1805 },
867	{ "rv1805", RV1805 },
868	{ }
869};
870MODULE_DEVICE_TABLE(i2c, abx80x_id);
871
872#ifdef CONFIG_OF
873static const struct of_device_id abx80x_of_match[] = {
874	{
875		.compatible = "abracon,abx80x",
876		.data = (void *)ABX80X
877	},
878	{
879		.compatible = "abracon,ab0801",
880		.data = (void *)AB0801
881	},
882	{
883		.compatible = "abracon,ab0803",
884		.data = (void *)AB0803
885	},
886	{
887		.compatible = "abracon,ab0804",
888		.data = (void *)AB0804
889	},
890	{
891		.compatible = "abracon,ab0805",
892		.data = (void *)AB0805
893	},
894	{
895		.compatible = "abracon,ab1801",
896		.data = (void *)AB1801
897	},
898	{
899		.compatible = "abracon,ab1803",
900		.data = (void *)AB1803
901	},
902	{
903		.compatible = "abracon,ab1804",
904		.data = (void *)AB1804
905	},
906	{
907		.compatible = "abracon,ab1805",
908		.data = (void *)AB1805
909	},
910	{
911		.compatible = "microcrystal,rv1805",
912		.data = (void *)RV1805
913	},
914	{ }
915};
916MODULE_DEVICE_TABLE(of, abx80x_of_match);
917#endif
918
919static struct i2c_driver abx80x_driver = {
920	.driver		= {
921		.name	= "rtc-abx80x",
922		.of_match_table = of_match_ptr(abx80x_of_match),
923	},
924	.probe		= abx80x_probe,
 
925	.id_table	= abx80x_id,
926};
927
928module_i2c_driver(abx80x_driver);
929
930MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
931MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
932MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
933MODULE_LICENSE("GPL v2");