Blame view

drivers/rtc/rtc-em3027.c 3.66 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
2
3
4
5
6
7
8
  /*
   * An rtc/i2c driver for the EM Microelectronic EM3027
   * Copyright 2011 CompuLab, Ltd.
   *
   * Author: Mike Rapoport <mike@compulab.co.il>
   *
   * Based on rtc-ds1672.c by Alessandro Zummo <a.zummo@towertech.it>
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
9
10
11
12
13
   */
  
  #include <linux/i2c.h>
  #include <linux/rtc.h>
  #include <linux/bcd.h>
2113852b2   Paul Gortmaker   rtc: Add module.h...
14
  #include <linux/module.h>
e706974dc   Peter Robinson   drivers/rtc/rtc-e...
15
  #include <linux/of.h>
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  
  /* Registers */
  #define EM3027_REG_ON_OFF_CTRL	0x00
  #define EM3027_REG_IRQ_CTRL	0x01
  #define EM3027_REG_IRQ_FLAGS	0x02
  #define EM3027_REG_STATUS	0x03
  #define EM3027_REG_RST_CTRL	0x04
  
  #define EM3027_REG_WATCH_SEC	0x08
  #define EM3027_REG_WATCH_MIN	0x09
  #define EM3027_REG_WATCH_HOUR	0x0a
  #define EM3027_REG_WATCH_DATE	0x0b
  #define EM3027_REG_WATCH_DAY	0x0c
  #define EM3027_REG_WATCH_MON	0x0d
  #define EM3027_REG_WATCH_YEAR	0x0e
  
  #define EM3027_REG_ALARM_SEC	0x10
  #define EM3027_REG_ALARM_MIN	0x11
  #define EM3027_REG_ALARM_HOUR	0x12
  #define EM3027_REG_ALARM_DATE	0x13
  #define EM3027_REG_ALARM_DAY	0x14
  #define EM3027_REG_ALARM_MON	0x15
  #define EM3027_REG_ALARM_YEAR	0x16
  
  static struct i2c_driver em3027_driver;
  
  static int em3027_get_time(struct device *dev, struct rtc_time *tm)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  
  	unsigned char addr = EM3027_REG_WATCH_SEC;
  	unsigned char buf[7];
  
  	struct i2c_msg msgs[] = {
84cab86cb   Shubhrajyoti D   drivers/rtc/rtc-e...
50
51
52
53
54
55
56
57
58
59
60
  		{/* setup read addr */
  			.addr = client->addr,
  			.len = 1,
  			.buf = &addr
  		},
  		{/* read time/date */
  			.addr = client->addr,
  			.flags = I2C_M_RD,
  			.len = 7,
  			.buf = buf
  		},
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  	};
  
  	/* read time/date registers */
  	if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
  		dev_err(&client->dev, "%s: read error
  ", __func__);
  		return -EIO;
  	}
  
  	tm->tm_sec	= bcd2bin(buf[0]);
  	tm->tm_min	= bcd2bin(buf[1]);
  	tm->tm_hour	= bcd2bin(buf[2]);
  	tm->tm_mday	= bcd2bin(buf[3]);
  	tm->tm_wday	= bcd2bin(buf[4]);
394c051d0   Ilya Ledvich   rtc: em3027: corr...
75
  	tm->tm_mon	= bcd2bin(buf[5]) - 1;
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
76
77
78
79
80
81
82
83
84
85
86
  	tm->tm_year	= bcd2bin(buf[6]) + 100;
  
  	return 0;
  }
  
  static int em3027_set_time(struct device *dev, struct rtc_time *tm)
  {
  	struct i2c_client *client = to_i2c_client(dev);
  	unsigned char buf[8];
  
  	struct i2c_msg msg = {
84cab86cb   Shubhrajyoti D   drivers/rtc/rtc-e...
87
88
89
  		.addr = client->addr,
  		.len = 8,
  		.buf = buf,	/* write time/date */
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
90
91
92
93
94
95
96
97
  	};
  
  	buf[0] = EM3027_REG_WATCH_SEC;
  	buf[1] = bin2bcd(tm->tm_sec);
  	buf[2] = bin2bcd(tm->tm_min);
  	buf[3] = bin2bcd(tm->tm_hour);
  	buf[4] = bin2bcd(tm->tm_mday);
  	buf[5] = bin2bcd(tm->tm_wday);
394c051d0   Ilya Ledvich   rtc: em3027: corr...
98
  	buf[6] = bin2bcd(tm->tm_mon + 1);
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	buf[7] = bin2bcd(tm->tm_year % 100);
  
  	/* write time/date registers */
  	if ((i2c_transfer(client->adapter, &msg, 1)) != 1) {
  		dev_err(&client->dev, "%s: write error
  ", __func__);
  		return -EIO;
  	}
  
  	return 0;
  }
  
  static const struct rtc_class_ops em3027_rtc_ops = {
  	.read_time = em3027_get_time,
  	.set_time = em3027_set_time,
  };
  
  static int em3027_probe(struct i2c_client *client,
  			const struct i2c_device_id *id)
  {
  	struct rtc_device *rtc;
  
  	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  		return -ENODEV;
019b21d07   Jingoo Han   rtc: rtc-em3027: ...
123
  	rtc = devm_rtc_device_register(&client->dev, em3027_driver.driver.name,
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
124
125
126
127
128
129
130
131
  				  &em3027_rtc_ops, THIS_MODULE);
  	if (IS_ERR(rtc))
  		return PTR_ERR(rtc);
  
  	i2c_set_clientdata(client, rtc);
  
  	return 0;
  }
45a635184   Arvind Yadav   rtc: constify i2c...
132
  static const struct i2c_device_id em3027_id[] = {
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
133
134
135
  	{ "em3027", 0 },
  	{ }
  };
e706974dc   Peter Robinson   drivers/rtc/rtc-e...
136
137
138
139
140
141
142
143
144
  MODULE_DEVICE_TABLE(i2c, em3027_id);
  
  #ifdef CONFIG_OF
  static const struct of_device_id em3027_of_match[] = {
  	{ .compatible = "emmicro,em3027", },
  	{}
  };
  MODULE_DEVICE_TABLE(of, em3027_of_match);
  #endif
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
145
146
147
148
  
  static struct i2c_driver em3027_driver = {
  	.driver = {
  		   .name = "rtc-em3027",
e706974dc   Peter Robinson   drivers/rtc/rtc-e...
149
  		   .of_match_table = of_match_ptr(em3027_of_match),
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
150
151
  	},
  	.probe = &em3027_probe,
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
152
153
  	.id_table = em3027_id,
  };
0abc92011   Axel Lin   rtc: convert rtc ...
154
  module_i2c_driver(em3027_driver);
ae3551f9c   Mike Rapoport   rtc: add EM3027 r...
155
156
157
158
  
  MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  MODULE_DESCRIPTION("EM Microelectronic EM3027 RTC driver");
  MODULE_LICENSE("GPL");