summaryrefslogtreecommitdiff
path: root/drivers/i2c/algos/wmt-i2c-algo.c
blob: 3be850d2997ff6c4c108bef55065333c0371bb15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*++
	drivers/i2c/algos/wmt_i2c_algo.c

	Copyright (c) 2008 WonderMedia Technologies, Inc.

	This program is free software: you can redistribute it and/or modify it under the
	terms of the GNU General Public License as published by the Free Software Foundation,
	either version 2 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	PARTICULAR PURPOSE.  See the GNU General Public License for more details.
	You should have received a copy of the GNU General Public License along with
	this program.  If not, see <http://www.gnu.org/licenses/>.

	WonderMedia Technologies, Inc.
	10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C.
--*/

#define WMT_I2C_ALGO_C

#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/i2c.h>
/*
#include <linux/i2c-id.h>
*/

#include <mach/hardware.h>
#include <mach/wmt-i2c-bus.h>

#ifdef __KERNEL__

#ifdef DEBUG
	#define DPRINTK    	printk
#else
	#define DPRINTK(x...)
#endif

#else
#define DPRINTK    printf

#endif

static struct i2c_adapter *wmt_i2c_adap[5];

/*!*************************************************************************
* wmt_i2c_valid_messages()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief verify the input message
*
* \retval  1 if success
*/
static int wmt_i2c_valid_messages(
	struct i2c_msg msgs[], 	/*!<; //[IN] transfer data  */
	int num					/*!<; //[IN] transfer data length */
)
{
	int i;
	if (num < 1 || num > MAX_MESSAGES) {
		DPRINTK(KERN_INFO "Invalid number of messages (max=%d, num=%d)\n", MAX_MESSAGES, num);
		return -EINVAL;
	}

	/* check consistency of our messages */
	for (i = 0; i < num; i++) {
		if (&msgs[i] == NULL) {
			DPRINTK(KERN_INFO "Msgs is NULL\n");
			return -EINVAL;
		} else {
			if (msgs[i].buf == NULL) {
				DPRINTK(KERN_INFO "Length is less than zero\n");
				return -EINVAL;
			}
		}
	}

	return 1;
}
/*!*************************************************************************
* wmt_i2c_do_xfer()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief
*
* \retval  0 if success
*/
static int wmt_i2c_do_xfer(
	struct i2c_adapter *i2c_adap, 	/*!<; //[IN] a pointer point to struct inode */
	struct i2c_msg msgs[], 		/*!<; //[IN] transfer data  */
	int num						/*!<; //[IN] transfer data length */
)
{
	int i;
	struct i2c_algo_wmt_data *adap;
	int ret = 0 ;

	adap = i2c_adap->algo_data;

	/*ret = adap->wait_bus_not_busy();*/
	for (i = 0 ; i < 10; ++i)
		;
	if (ret < 0)
		return ret ;

	ret = adap->send_request(msgs, num, 0, 0, 0);

	return ret;

}

/*!*************************************************************************
* wmt_i2c_xfer()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief Transfer (read/write) data to i2c bus, wmt_i2c_do_xfer will be called to transfer
*
* \retval  0 if success
*/
static int wmt_i2c_xfer(
	struct i2c_adapter *i2c_adap, 	/*!<; //[IN] a pointer point to struct inode */
	struct i2c_msg msgs[], 		/*!<; //[IN] transfer data  */
	int num						/*!<; //[IN] transfer data length */
)
{
	int ret ;
	int i ;

	ret = wmt_i2c_valid_messages(msgs, num);
	if (ret < 0)
		return ret ;

	for (i = i2c_adap->retries ;  i >= 0; i--) {

		ret = wmt_i2c_do_xfer(i2c_adap, msgs, num);
		if (ret > 0)
			return ret ;
		DPRINTK(KERN_INFO"Retrying transmission \n");
		udelay(100);
	}

	DPRINTK(KERN_INFO"Retried %i times\n", i2c_adap->retries);
	return ret;

}
/*!*************************************************************************
* wmt_i2c_functionality()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief
*
* \retval  smbus functionality
*/
static u32 wmt_i2c_functionality(
	struct i2c_adapter *adapter		/*!<; //[IN] a pointer point to struct inode */
)
{
	/* Emulate the SMBUS functions*/
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
}

/*
 * send i2c_msg into fifo of i2c bus
 * msg: transfer data content
 * msg_num:number of transferring msg
 * bus_id :used to indicate which bus do device want to access
 */
int wmt_i2c_transfer(struct i2c_msg* msgs, int msg_num, int bus_id, void (*callback)(void *data), void *data)
{
	struct i2c_algo_wmt_data *adap;
	int ret = 0 ;

	adap = wmt_i2c_adap[bus_id]->algo_data;
	ret = adap->send_request(msgs, msg_num, 1, callback, data);
	return ret;
}

#if 0
/*!*************************************************************************
* wmt_i2c_control()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief To set i2c transfer mode
*
* \retval  0 if success
*/
static int wmt_i2c_control(
	struct i2c_adapter *i2c_adap, 	/*!<; //[IN] a pointer point to struct inode */
	unsigned int cmd, 				/*!<; //[IN] standard or fast mode */
	unsigned long arg				/*!<; //Not in used, but can't delete */
)
{
	int ret ;
	struct i2c_algo_wmt_data *adap = i2c_adap->algo_data;

	ret = 0 ;
	DPRINTK("wmt_i2c_control: cmd = 0x%8.8x \n", cmd);

	switch (cmd) {
	case I2C_SET_STANDARD_MODE:
		adap->set_mode(I2C_STANDARD_MODE);
		break ;
	case I2C_SET_FAST_MODE:
		adap->set_mode(I2C_FAST_MODE);
		break ;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}
#endif


struct i2c_algorithm wmt_i2c_algorithm  = {
	.master_xfer     =         wmt_i2c_xfer,
	.functionality   =         wmt_i2c_functionality,
};
/*!*************************************************************************
* wmt_i2c_add_bus()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief
*
* \retval  NULL
*/
int wmt_i2c_add_bus(struct i2c_adapter *i2c_adap)
{
	printk(KERN_INFO"i2c: adding %s.\n", i2c_adap->name);

	i2c_adap->algo = &wmt_i2c_algorithm;
	wmt_i2c_adap[i2c_adap->nr] = i2c_adap;

	/* register new adapter to i2c module... */
	/*
	i2c_add_adapter(i2c_adap);
	*/
	i2c_add_numbered_adapter(i2c_adap);

	/* adap->reset();*/

	return 0;
}
/*!*************************************************************************
* wmt_i2c_del_bus()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief
*
* \retval  NULL
*/
int wmt_i2c_del_bus(struct i2c_adapter *i2c_adap)
{
	int res;
	res = i2c_del_adapter(i2c_adap);
	if (res < 0)
		return res;

	printk(KERN_INFO "i2c: removing %s.\n", i2c_adap->name);

	return 0;
}
/*!*************************************************************************
* wmt_i2c_algo_init()
*
* Private Function by Paul Kwong, 2007/1/12
*/
/*!
* \brief
*
* \retval  NULL
*/
static int __init wmt_i2c_algo_init(void)
{
	printk(KERN_INFO "i2c: wmt algorithm module loaded.\n");
	return 0;
}

EXPORT_SYMBOL(wmt_i2c_add_bus);
EXPORT_SYMBOL(wmt_i2c_del_bus);

MODULE_AUTHOR("WonderMedia Technologies, Inc.");
MODULE_DESCRIPTION("WMT I2C ALGO Driver");
MODULE_LICENSE("GPL");

module_init(wmt_i2c_algo_init);

#undef WMT_I2C_ALGO_C