1 /*
2  *  linux/drivers/acorn/char/pcf8583.c
3  *
4  *  Copyright (C) 2000 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Driver for PCF8583 RTC & RAM chip
11  */
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/init.h>
17 
18 #include "pcf8583.h"
19 
20 static struct i2c_driver pcf8583_driver;
21 
22 static unsigned short ignore[] = { I2C_CLIENT_END };
23 static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END };
24 
25 static struct i2c_client_address_data addr_data = {
26 	normal_i2c:		normal_addr,
27 	normal_i2c_range:	ignore,
28 	probe:			ignore,
29 	probe_range:		ignore,
30 	ignore:			ignore,
31 	ignore_range:		ignore,
32 	force:			ignore,
33 };
34 
35 #define DAT(x) ((unsigned int)(x->data))
36 
37 static int
pcf8583_attach(struct i2c_adapter * adap,int addr,unsigned short flags,int kind)38 pcf8583_attach(struct i2c_adapter *adap, int addr, unsigned short flags,
39 	       int kind)
40 {
41 	struct i2c_client *c;
42 	unsigned char buf[1], ad[1] = { 0 };
43 	struct i2c_msg msgs[2] = {
44 		{ addr, 0,        1, ad  },
45 		{ addr, I2C_M_RD, 1, buf }
46 	};
47 
48 	c = kmalloc(sizeof(*c), GFP_KERNEL);
49 	if (!c)
50 		return -ENOMEM;
51 
52 	strcpy(c->name, "PCF8583");
53 	c->id		= pcf8583_driver.id;
54 	c->flags	= 0;
55 	c->addr		= addr;
56 	c->adapter	= adap;
57 	c->driver	= &pcf8583_driver;
58 	c->data		= NULL;
59 
60 	if (i2c_transfer(c->adapter, msgs, 2) == 2)
61 		DAT(c) = buf[0];
62 
63 	return i2c_attach_client(c);
64 }
65 
66 static int
pcf8583_probe(struct i2c_adapter * adap)67 pcf8583_probe(struct i2c_adapter *adap)
68 {
69 	return i2c_probe(adap, &addr_data, pcf8583_attach);
70 }
71 
72 static int
pcf8583_detach(struct i2c_client * client)73 pcf8583_detach(struct i2c_client *client)
74 {
75 	i2c_detach_client(client);
76 	kfree(client);
77 	return 0;
78 }
79 
80 static int
pcf8583_get_datetime(struct i2c_client * client,struct rtc_tm * dt)81 pcf8583_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
82 {
83 	unsigned char buf[8], addr[1] = { 1 };
84 	struct i2c_msg msgs[2] = {
85 		{ client->addr, 0,        1, addr },
86 		{ client->addr, I2C_M_RD, 6, buf  }
87 	};
88 	int ret = -EIO;
89 
90 	memset(buf, 0, sizeof(buf));
91 
92 	ret = i2c_transfer(client->adapter, msgs, 2);
93 	if (ret == 2) {
94 		dt->year_off = buf[4] >> 6;
95 		dt->wday     = buf[5] >> 5;
96 
97 		buf[4] &= 0x3f;
98 		buf[5] &= 0x1f;
99 
100 		dt->cs       = BCD_TO_BIN(buf[0]);
101 		dt->secs     = BCD_TO_BIN(buf[1]);
102 		dt->mins     = BCD_TO_BIN(buf[2]);
103 		dt->hours    = BCD_TO_BIN(buf[3]);
104 		dt->mday     = BCD_TO_BIN(buf[4]);
105 		dt->mon      = BCD_TO_BIN(buf[5]);
106 
107 		ret = 0;
108 	}
109 
110 	return ret;
111 }
112 
113 static int
pcf8583_set_datetime(struct i2c_client * client,struct rtc_tm * dt,int datetoo)114 pcf8583_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
115 {
116 	unsigned char buf[8];
117 	int ret, len = 6;
118 
119 	buf[0] = 0;
120 	buf[1] = DAT(client) | 0x80;
121 	buf[2] = BIN_TO_BCD(dt->cs);
122 	buf[3] = BIN_TO_BCD(dt->secs);
123 	buf[4] = BIN_TO_BCD(dt->mins);
124 	buf[5] = BIN_TO_BCD(dt->hours);
125 
126 	if (datetoo) {
127 		len = 8;
128 		buf[6] = BIN_TO_BCD(dt->mday) | (dt->year_off << 6);
129 		buf[7] = BIN_TO_BCD(dt->mon)  | (dt->wday << 5);
130 	}
131 
132 	ret = i2c_master_send(client, (char *)buf, len);
133 	if (ret == len)
134 		ret = 0;
135 
136 	buf[1] = DAT(client);
137 	i2c_master_send(client, (char *)buf, 2);
138 
139 	return ret;
140 }
141 
142 static int
pcf8583_get_ctrl(struct i2c_client * client,unsigned char * ctrl)143 pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
144 {
145 	*ctrl = DAT(client);
146 	return 0;
147 }
148 
149 static int
pcf8583_set_ctrl(struct i2c_client * client,unsigned char * ctrl)150 pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
151 {
152 	unsigned char buf[2];
153 
154 	buf[0] = 0;
155 	buf[1] = *ctrl;
156 	DAT(client) = *ctrl;
157 
158 	return i2c_master_send(client, (char *)buf, 2);
159 }
160 
161 static int
pcf8583_read_mem(struct i2c_client * client,struct mem * mem)162 pcf8583_read_mem(struct i2c_client *client, struct mem *mem)
163 {
164 	unsigned char addr[1];
165 	struct i2c_msg msgs[2] = {
166 		{ client->addr, 0,        1, addr },
167 		{ client->addr, I2C_M_RD, 0, mem->data }
168 	};
169 
170 	if (mem->loc < 8)
171 		return -EINVAL;
172 
173 	addr[0] = mem->loc;
174 	msgs[1].len = mem->nr;
175 
176 	return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
177 }
178 
179 static int
pcf8583_write_mem(struct i2c_client * client,struct mem * mem)180 pcf8583_write_mem(struct i2c_client *client, struct mem *mem)
181 {
182 	unsigned char addr[1];
183 	struct i2c_msg msgs[2] = {
184 		{ client->addr, 0, 1, addr },
185 		{ client->addr, 0, 0, mem->data }
186 	};
187 
188 	if (mem->loc < 8)
189 		return -EINVAL;
190 
191 	addr[0] = mem->loc;
192 	msgs[1].len = mem->nr;
193 
194 	return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
195 }
196 
197 static int
pcf8583_command(struct i2c_client * client,unsigned int cmd,void * arg)198 pcf8583_command(struct i2c_client *client, unsigned int cmd, void *arg)
199 {
200 	switch (cmd) {
201 	case RTC_GETDATETIME:
202 		return pcf8583_get_datetime(client, arg);
203 
204 	case RTC_SETTIME:
205 		return pcf8583_set_datetime(client, arg, 0);
206 
207 	case RTC_SETDATETIME:
208 		return pcf8583_set_datetime(client, arg, 1);
209 
210 	case RTC_GETCTRL:
211 		return pcf8583_get_ctrl(client, arg);
212 
213 	case RTC_SETCTRL:
214 		return pcf8583_set_ctrl(client, arg);
215 
216 	case MEM_READ:
217 		return pcf8583_read_mem(client, arg);
218 
219 	case MEM_WRITE:
220 		return pcf8583_write_mem(client, arg);
221 
222 	default:
223 		return -EINVAL;
224 	}
225 }
226 
227 static struct i2c_driver pcf8583_driver = {
228 	name:		"PCF8583",
229 	id:		I2C_DRIVERID_PCF8583,
230 	flags:		I2C_DF_NOTIFY,
231 	attach_adapter:	pcf8583_probe,
232 	detach_client:	pcf8583_detach,
233 	command:	pcf8583_command
234 };
235 
pcf8583_init(void)236 static __init int pcf8583_init(void)
237 {
238 	return i2c_add_driver(&pcf8583_driver);
239 }
240 
241 __initcall(pcf8583_init);
242