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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <mach/wmt_env.h>
#include "nmidrv_kernel.h"
#include "nmidrv_custom_wmt.h"
extern int nmi_cmos_init(void);
extern void nmi_cmos_exit(void);
struct nmi_5625_dev {
struct i2c_client *i2c_client_atv;
struct mutex mu;
struct class *tv_class;
dev_t devn;
struct cdev cdv;
};
static int already_init = 0;
static struct nmi_5625_dev nd;
static u8 i2cBuf[32];
static bool g_bIsAtvStart = false;
int nmi_running(void)
{
return g_bIsAtvStart;
}
EXPORT_SYMBOL(nmi_running);
/**************************************************************
file operation:
**************************************************************/
static int nmi5625_open(struct inode *inode, struct file *file)
{
int ret = 0;
func_enter();
if (!already_init) {
ret = -ENODEV;
goto _fail_;
}
/***************************************
initialize 2.8V 1.2V RESET GPIO mode for reference
****************************************/
//PWR Enable
NMI_SET_GPIO_MODE_ENABLE(NMI_POWER_VDDIO_PIN);
NMI_SET_GPIO_DIR(NMI_POWER_VDDIO_PIN, 1);
NMI_SET_GPIO_PULL_DISABLE(NMI_POWER_VDDIO_PIN);
NMI_SET_GPIO_LEVEL(NMI_POWER_VDDIO_PIN, 0);
//LDO_Enable
NMI_SET_GPIO_MODE_ENABLE(NMI_POWER_VCORE_PIN);
NMI_SET_GPIO_DIR(NMI_POWER_VCORE_PIN, 1);
NMI_SET_GPIO_PULL_DISABLE(NMI_POWER_VCORE_PIN);
NMI_SET_GPIO_LEVEL(NMI_POWER_VCORE_PIN, 0);
//Reset
NMI_SET_GPIO_MODE_ENABLE(NMI_RESET_PIN);
NMI_SET_GPIO_DIR(NMI_RESET_PIN, 1);
NMI_SET_GPIO_PULL_DISABLE(NMI_RESET_PIN);
NMI_SET_GPIO_LEVEL(NMI_RESET_PIN, 0);
#ifndef NMI_HW_I2C
nmi_i2c_init();
#endif
file->private_data = (void *)&nd;
_fail_:
func_exit();
return ret;
}
static int nmi5625_release(struct inode * inode, struct file * file)
{
int ret = 0;
//struct nmi_5625_dev *d = file->private_data;
/**
nothing to do
**/
func_enter();
#ifndef NMI_HW_I2C
nmi_i2c_deinit();
#endif
func_exit();
return ret;
}
static long nmi5625_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct nmi_5625_dev *d = file->private_data;
int ret = 0;
//func_enter();
switch ((cmd&0xffff0000)) {
case NMI_POWER_VDDIO_CTL:
dPrint(N_TRACE,"NM5625_PWR_2P8_CTL, power %s\n", (arg==1)?"on":"off");
if (arg == 1){ /* on */
NMI_SET_GPIO_LEVEL(NMI_POWER_VDDIO_PIN, 1);
}
else{
NMI_SET_GPIO_LEVEL(NMI_POWER_VDDIO_PIN, 0);
}
break;
case NMI_POWER_VCORE_CTL:
dPrint(N_TRACE,"NM5625_PWR_1P2_CTL, power %s\n", (arg==1)?"on":"off");
if (arg == 1){ /* on */
NMI_SET_GPIO_LEVEL(NMI_POWER_VCORE_PIN, 1);
g_bIsAtvStart = 1;
}
else{
NMI_SET_GPIO_LEVEL(NMI_POWER_VCORE_PIN, 0);
g_bIsAtvStart = 0;
}
break;
case NMI_FM_POWER_VCORE_CTL:
dPrint(N_TRACE,"NMI_FM_POWER_VCORE_CTL, power %s\n", (arg==1)?"on":"off");
if (arg == 1){ /* on */
NMI_SET_GPIO_LEVEL(NMI_POWER_VCORE_PIN, 1);
//g_bIsAtvStart = 1;
}
else{
NMI_SET_GPIO_LEVEL(NMI_POWER_VCORE_PIN, 0);
//g_bIsAtvStart = 0;
}
break;
case NMI_RESET_CTL:
dPrint(N_TRACE,"NM5625_ATV_RESET_CTL, reset %s\n", (arg==1)?"high":"low");
if (arg == 1) {
NMI_SET_GPIO_LEVEL(NMI_RESET_PIN, 1);
}
else {
NMI_SET_GPIO_LEVEL(NMI_RESET_PIN, 0);
}
break;
case NMI_I2C_READ:
{
u8 *kbuf = &i2cBuf[0];
int size = cmd&0xffff; /* Note: I used the lower 16 bits for size */
int len = size;
dPrint(N_TRACE,"NM5625_ATV_I2C_READ\n");
mutex_lock(&d->mu);
#ifdef NMI_HW_I2C
while(len) {
int sz;
if (len > NMI_I2C_RW_LENGTH)
sz = NMI_I2C_RW_LENGTH;
else
sz = len;
ret = i2c_master_recv(d->i2c_client_atv, kbuf, sz);
if (ret < 0) {
dPrint(N_ERR, "nmi: failed i2c read...(%d)\n", ret);
//kfree(kbuf);
mutex_unlock(&d->mu);
goto _fail_;
}
kbuf += NMI_I2C_RW_LENGTH;
len -= sz;
}
#else
ret = nmi_i2c_read(0x60,kbuf,size);
#endif
dPrint(N_TRACE,"kernel:nmi_i2c_read buf is (%p), size is (%d)\n",kbuf,size);
if (copy_to_user((void *)arg, i2cBuf, size) ) {
dPrint(N_ERR, "nmi: failed copy to user...\n");
ret = -EFAULT;
//kfree(kbuf);
mutex_unlock(&d->mu);
goto _fail_;
}
//kfree(kbuf);
mutex_unlock(&d->mu);
}
break;
case NMI_I2C_WRITE:
{
u8 *kbuf = &i2cBuf[0];
int size = cmd&0xffff; /* Note: I used the lower 16 bits for size */
int len = size;
dPrint(N_TRACE,"NM5625_ATV_I2C_WRITE\n");
if (copy_from_user((void *)kbuf, (void *)arg, size)) {
dPrint(N_ERR, "nmi: failed copy from user...\n");
ret = -EFAULT;
goto _fail_;
}
mutex_lock(&d->mu);
#ifdef NMI_HW_I2C
while (len) {
int sz;
if (len > NMI_I2C_RW_LENGTH)
sz = NMI_I2C_RW_LENGTH;
else
sz = len;
ret = i2c_master_send(d->i2c_client_atv, kbuf, sz);
if (ret < 0) {
dPrint(N_ERR, "nmi: failed i2c write...(%d)\n", ret);
//kfree(kbuf);
mutex_unlock(&d->mu);
goto _fail_;
}
kbuf += NMI_I2C_RW_LENGTH;
len -= sz;
}
#else
ret = nmi_i2c_write(0x60,kbuf,size);
#endif
dPrint(N_TRACE,"kernel:nmi_i2c_write buf is (%p), size is (%d)\n",kbuf,size);
mutex_unlock(&d->mu);
}
break;
default:
break;
}
_fail_:
//func_exit();
dPrint(N_TRACE, "nmi_ioctl return value...(%d)\n", ret);
return ret;
}
static const struct file_operations nmi5625_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = nmi5625_ioctl,
.open = nmi5625_open,
.release = nmi5625_release,
};
/**************************************************************
i2c:
**************************************************************/
static int nmi5625_remove(struct i2c_client *client)
{
return 0;
}
static int nmi5625_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
{
strcpy(info->type, "nmiatv");
return 0;
}
static int nmi5625_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int ret = 0;
struct device *dev;
func_enter();
if (!already_init) {
memset(&nd, 0, sizeof(struct nmi_5625_dev));
/**
initialize mutex
**/
mutex_init(&nd.mu);
/**
register our driver
**/
if ((ret = alloc_chrdev_region (&nd.devn, 0, 1, "nmi")) < 0) {
dPrint(N_ERR, "nmi: failed unable to get major...%d\n", ret);
goto _fail_;
}
dPrint(N_INFO, "nmi:dynamic major(%d),minor(%d)\n", MAJOR(nd.devn), MINOR(nd.devn));
cdev_init(&nd.cdv, &nmi5625_fops);
nd.cdv.owner = THIS_MODULE;
ret = cdev_add(&nd.cdv, nd.devn, 1);
if (ret) {
dPrint(N_ERR, "nmi: failed to add device...%d\n", ret);
goto _fail_;
}
nd.tv_class = class_create(THIS_MODULE, "atv");
if (IS_ERR(nd.tv_class)) {
dPrint(N_ERR, "nmi: failed to create the atv class\n");
}
dev = device_create(nd.tv_class, NULL, nd.devn, NULL, "nmi");
if (IS_ERR(dev)) {
dPrint(N_ERR, "nmi: failed to create device\n");
}
/*User interface end */
already_init = 1;
}
nd.i2c_client_atv = client;
_fail_:
func_exit();
return ret;
}
static const struct i2c_device_id nmi5625_id[] = {
{"nmiatv", 0},
{},
};
//static unsigned short force[] = {2, 0xc0, I2C_CLIENT_END, I2C_CLIENT_END};
//static const unsigned short * const forces[] = { force, NULL };
//static struct i2c_client_address_data addr_data = { .forces = forces,};
static struct i2c_driver nmi5625_i2c_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "nmiatv",
},
.probe = nmi5625_probe,
.detect = nmi5625_detect,
.remove = nmi5625_remove,
.id_table = nmi5625_id,
// .address_data =&addr_data,
};
static struct i2c_board_info nmi5625_i2c_dev = {
I2C_BOARD_INFO("nmiatv", 0x60),
};
/**************************************************************
Module:
**************************************************************/
static int parse_nmi_param(void)
{
char env[] = "wmt.nmi.param";
char buf[64];
size_t l = sizeof(buf);
int nr = -EINVAL;
if (wmt_getsyspara(env, buf, &l) == 0) {
sscanf(buf, "i2c-%d", &nr);
pr_info("nmi5625 i2c adapter %d\n", nr);
}
return nr;
}
static __init int nmi5625_init(void)
{
int ret;
struct i2c_adapter *adapter;
struct i2c_client *client;
int nr;
func_enter();
nr = parse_nmi_param();
if (nr < 0)
return nr;
adapter = i2c_get_adapter(nr);
client = i2c_new_device(adapter, &nmi5625_i2c_dev);
i2c_put_adapter(adapter);
if (!client) {
printk("nmi i2c_new_device failed\n");
return -EINVAL;
}
ret = i2c_add_driver(&nmi5625_i2c_driver);
if (ret < 0) {
dPrint(N_ERR, "nmi: failed register i2c driver...(%d)\n", ret);
}
func_exit();
return ret;
}
static __exit void nmi5625_clean(void)
{
func_enter();
i2c_del_driver(&nmi5625_i2c_driver);
if (already_init) {
device_destroy(nd.tv_class, nd.devn);
class_destroy(nd.tv_class);
cdev_del(&nd.cdv);
unregister_chrdev_region(nd.devn, 1);
already_init = 0;
i2c_unregister_device(nd.i2c_client_atv);
nd.i2c_client_atv = NULL;
}
func_exit();
}
module_init(nmi5625_init);
module_exit(nmi5625_clean);
MODULE_AUTHOR("nmi");
MODULE_DESCRIPTION("nmi TV 5625 driver");
MODULE_LICENSE("GPL");
|