summaryrefslogtreecommitdiff
path: root/drivers/autotst/autotstdrv.c
blob: be14b956299789db52c6c1251fee6d47afb38905 (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
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
436
437
438
439
440
// ****************************************** //
// access IIC, GPIO, etc. from user space by anli.wei
// 2012-11-25
// 2013-01-22 add key info anli.wei
// ****************************************** //

#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/module.h>
#include <linux/types.h>
#include <soc/sprd/pinmap.h>

#include <asm/io.h>
#include <asm/uaccess.h>
#include <soc/sprd/board.h>
#include <soc/sprd/gpio.h>
//
#include "autotstdrv.h"
#include "dispc.h"
//
#define DBG_ENABLE_FUN
#define DBG_ENABLE_INFO

#ifdef DBG_ENABLE_FUN
#define FUN_ENTER               printk(KERN_INFO "autotst-> %s ++.\n", __FUNCTION__)
#define FUN_LEAVE               printk(KERN_INFO "autotst-> %s --.\n", __FUNCTION__)
#else
#define FUN_ENTER               do {} while(0)
#define FUN_LEAVE               do {} while(0)
#endif // DBG_ENABLE_FUN

#ifdef DBG_ENABLE_INFO
#define DBG_INFO(fmt, arg...)   printk(KERN_INFO "autotst-> " fmt, ##arg)
#else
#define DBG_INFO(fmt, arg...)   do {} while(0)
#endif // DBG_ENABLE_INFO

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#define SPRD_MAX_PIN                200
#define SPRD_GPIO_PIN_INVALID_VALUE 0xFFFFFFFF

//static uint32_t  sGpioPins[SPRD_MAX_PIN];

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
extern int     lcm_init( void );
extern int32_t lcm_send_data (uint32_t data);

//------------------------------------------------------------------------------
#define CDEV_NAME   "autotst"

static dev_t         s_devt;
static struct cdev   s_cdev;
static struct class *s_class;

//------------------------------------------------------------------------------

static int autotst_open (struct inode *inode, struct file *filp)
{
	FUN_ENTER;
	FUN_LEAVE;
    return 0;
}

static int autotst_release (struct inode *inode, struct file *filp)
{
	FUN_ENTER;
	FUN_LEAVE;
    return 0;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

static int i2c_read( struct autotst_i2c_info_t * info )
{
    struct i2c_msg xfer[2];
    u8  reg[2], reg_len = 0;
    u8  addr = (info->addr >> 1);
    int ret;

    struct i2c_adapter * adpt = i2c_get_adapter(info->bus);
    if( NULL == adpt ) {
        printk(KERN_ERR "get adapter(%d) fail!\n", info->bus);
        return -ENXIO;
    }

    DBG_INFO("i2c read: bus = %d, addr = %X, reg = %X\n", info->bus, addr, info->reg);

    if( 16 == info->regBits ) {
        reg[reg_len++] = (u8)(info->reg >> 8);
    }
    reg[reg_len++] = (u8)(info->reg);

	// Write address
	xfer[0].addr  = addr;
	xfer[0].flags = 0;
	xfer[0].len   = reg_len;
	xfer[0].buf   = reg;

	/* Read data */
	xfer[1].addr  = addr;
	xfer[1].flags = I2C_M_RD;
	xfer[1].len   = info->data_len;
	xfer[1].buf   = info->data;

	ret = i2c_transfer(adpt, xfer, 2);
	if( ret < 0 ) {
		printk(KERN_ERR "i2c_transfer() returned %d\n", ret);

        i2c_put_adapter(adpt);
		return ret;
	}

    DBG_INFO("i2c read done: bus = %d, addr = 0x%04X, reg = 0x%04X\n", 
        info->bus, info->addr, info->reg);

    i2c_put_adapter(adpt);
    return 0;
}

//------------------------------------------------------------------------------
static int i2c_write( struct autotst_i2c_info_t * info )
{
    u8  buf[64];
    u8  num = 0;
    int ret, i;

    struct i2c_msg xfer;

    struct i2c_adapter * adpt = i2c_get_adapter(info->bus);
    if( NULL == adpt ) {
        printk(KERN_ERR "get adapter(%d) fail!\n", info->bus);
        return -ENXIO;
    }

    if( 16 == info->regBits ) {
        buf[num++] = (u8)(info->reg >> 8);
    }

    buf[num++] = (u8)(info->reg);

    for( i = 0; i < info->data_len; ++i )
    {
        buf[num++] = info->data[i];
    }

    xfer.addr  = info->addr;
    xfer.flags = 0;
    xfer.len   = num;
    xfer.buf   = buf;

    ret = i2c_transfer(adpt, &xfer, 1);
	if( ret < 0 ) {
		printk(KERN_ERR "i2c_transfer() returned %d\n", ret);

        i2c_put_adapter(adpt);
		return ret;
	}

    DBG_INFO("i2c write done: bus = %d, addr = 0x%04X, reg = 0x%04X\n",
        info->bus, info->addr, info->reg);

    i2c_put_adapter(adpt);
	return 0;
}

//------------------------------------------------------------------------------
static int i2c_ioctl( unsigned int cmd, unsigned long arg )
{
    int ret = 0;
    struct autotst_i2c_info_t iit;

    if( copy_from_user(&iit, (const void __user *)arg, sizeof(struct autotst_i2c_info_t)) ) {
        printk(KERN_ERR "copy_from_user fail: arg = %lu\n", arg);
        return -EFAULT;
    }

    switch( cmd ) {
    case AUTOTST_IOCTL_I2C_READ:
        ret = i2c_read( &iit );
        if( ret == 0 ) {
            if( copy_to_user((void __user *)arg, &iit, sizeof(struct autotst_i2c_info_t)) ) {
                printk(KERN_ERR "copy_to_user fail: arg = %lu\n", arg);
                ret = -EFAULT;
            }
        }
        break;
    case AUTOTST_IOCTL_I2C_WRITE:
        ret = i2c_write( &iit );
        break;
    default:
        ret = -EINVAL;
        break;
    }
    return ret;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

static int gpio_ioctl( unsigned int cmd, unsigned long arg )
{
    int ret = 0;
    int pull = 0;
    struct autotst_gpio_info_t git;

    if( copy_from_user(&git, (const void __user *)arg, sizeof(struct autotst_gpio_info_t)) ) {
        printk(KERN_ERR "copy_from_user fail: arg = %lu\n", arg);
        return -EFAULT;
    }

    switch( cmd ) {
    case AUTOTST_IOCTL_GPIO_INIT:
    {
        char name[32];
        snprintf(name, 32, "at_gio_%d", git.gpio);
/*      // not support, already config on platform
        if( git.pup_enb ) {
        }
        if( git.pdwn_enb ) {
        }
*/
        pull = ( (git.pdwn_enb << 6) & (BIT_PIN_WPD) ) | ((git.pup_enb << 7) & (BIT_PIN_WPU)); 
        autotst_pin_ctrl(DISPC_PIN_FUNC3, git.gpio, pull);
        gpio_request(git.gpio, name);
        if( AUTOTST_GPIO_DIR_IN == git.dir ) {
            gpio_direction_input(git.gpio);
        } else {
            gpio_direction_output(git.gpio, git.val);
        }
    }
        break;
    case AUTOTST_IOCTL_GPIO_GET: {
		int gv = gpio_get_value(git.gpio);

        git.val = (gv > 0) ? 1 : 0;
        if( copy_to_user((void __user *)arg, &git, sizeof(struct autotst_gpio_info_t)) ) {
            printk(KERN_ERR "copy_to_user fail: arg = %lu\n", arg);
            ret = -EFAULT;
        }
	}
        break;
    case AUTOTST_IOCTL_GPIO_SET:
        gpio_set_value(git.gpio, git.val);
        break;
    case AUTOTST_IOCTL_GPIO_CLOSE:
	 autotst_pin_ctrl(DISPC_PIN_FUNC0, git.gpio, pull);
	 break;
    default:
        ret = -EINVAL;
        break;
    }
    return ret;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

static int key_ioctl( unsigned int cmd, unsigned long arg )
{
	int ret = 0;
	struct autotst_key_info_t kit;

    if( copy_from_user(&kit, (const void __user *)arg, sizeof(struct autotst_key_info_t)) ) {
        printk(KERN_ERR "copy_from_user fail: arg = %lu\n", arg);
        return -EFAULT;
    }

	if( KEY_POWER == kit.val ) {
		kit.row = AUTOTST_KEY_INVALID_ROW;
		kit.col = AUTOTST_KEY_INVALID_COL;
		kit.gio = EIC_KEY_POWER;
	} else {
		unsigned char rc = (kit.val & 0xF7);

		kit.row = ((rc >> 4) & 0x0F);
		kit.col = ((rc >> 0) & 0x0F);
		kit.gio = 0;
	}

	if( 0 == ret ) {
		if( copy_to_user((void __user *)arg, &kit, sizeof(struct autotst_key_info_t)) ) {
            printk(KERN_ERR "copy_to_user fail: arg = %lu\n", arg);
            ret = -EFAULT;
        }
	}

	return ret;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

static long autotst_ioctl( struct file *filp, unsigned int cmd, unsigned long arg )
{
    long ret = -1;

	FUN_ENTER;

	DBG_INFO("cmd = 0x%X\n", cmd);
    switch( cmd ) {
    case AUTOTST_IOCTL_I2C_READ:
    case AUTOTST_IOCTL_I2C_WRITE:
        ret = i2c_ioctl(cmd, arg);
        break;
    case AUTOTST_IOCTL_GPIO_INIT:
    case AUTOTST_IOCTL_GPIO_GET:
    case AUTOTST_IOCTL_GPIO_SET:
    case AUTOTST_IOCTL_GPIO_CLOSE:
        ret = gpio_ioctl(cmd, arg);
        break;
    case AUTOTST_IOCTL_LCD_DATA:
    {
        int i;
        DBG_INFO("lcd data = 0x%X\n", (uint32_t)arg);
#if 0
        lcm_init();
        for( i = 0; i < 32; ++i ) {
            lcm_send_data((uint32_t)arg);
        }
#else
        autotst_dispc_init(DISPLAY_TYPE_MCU);
        for( i = 0; i < 32; ++i ) {
            autotst_dispc_mcu_send_data((uint32_t)arg);
        }
        autotst_dispc_uninit(DISPLAY_TYPE_MCU);
#endif
        ret = 0;
    }
        break;
#if 1
        case AUTOTST_IOCTL_LCD_MIPI_ON:
        {
	         int i;
	         DBG_INFO("lcd mipi on\n");
                autotst_dispc_init(DISPLAY_TYPE_MIPI);
                autotst_dispc_refresh();
		   ret = 0;
        }
        break;
        case AUTOTST_IOCTL_LCD_MIPI_OFF:
        {
	         int i;
                DBG_INFO("lcd mipi off\n");
                autotst_dispc_uninit(DISPLAY_TYPE_MIPI);
		   ret = 0;
        }
        break;
#endif
	case AUTOTST_IOCTL_GET_KEYINFO:
		ret = key_ioctl(cmd, arg);
		break;
    default:
        ret = -EINVAL;
	 DBG_INFO("lcd default \n");
        break;
    }

	FUN_LEAVE;
    return ret;
}

static struct file_operations autotst_fops =
{
    .owner          = THIS_MODULE,
    .unlocked_ioctl = autotst_ioctl,
    .open           = autotst_open,
    .release        = autotst_release,
};

//==============================================================================

static int __init autotst_init(void)
{
    int ret;

    FUN_ENTER;

	// auto select a major
    ret = alloc_chrdev_region(&s_devt, 0, 1, CDEV_NAME);
	if( ret < 0 ) {
        printk(KERN_ERR "ERROR: alloc_chrdev_region %d\n", ret);
        return ret;
    }

	cdev_init(&s_cdev, &autotst_fops);
	ret = cdev_add(&s_cdev, s_devt, 1);
    if( ret < 0 )
	{
		unregister_chrdev_region(s_devt, 1);
        printk(KERN_ERR "ERROR: cdev_add %d\n", ret);
        return ret;
	}

    s_class = class_create(THIS_MODULE, CDEV_NAME);
    device_create(s_class, NULL, MKDEV(MAJOR(s_devt), MINOR(s_devt)), NULL, CDEV_NAME);

//	autotst_dispc_pin_ctrl(DISPC_PIN_FUNC3);

    //for( i = 0; i < SPRD_MAX_PIN; ++i ) {
    //    sGpioPins[i] = SPRD_GPIO_PIN_INVALID_VALUE;
    //}
    //sGpioPins[59] = MFP_CFG_X(SIMCLK3,	AF3,	DS1,	F_PULL_NONE,	S_PULL_NONE,	IO_OE);

    FUN_LEAVE;
    return 0;
}

static void __exit autotst_exit(void)
{
	FUN_ENTER;

    device_destroy(s_class, MKDEV(MAJOR(s_devt), MINOR(s_devt)));
    class_destroy(s_class);

    cdev_del(&s_cdev);
    unregister_chrdev_region(s_devt, 1);

//	autotst_dispc_pin_ctrl(DISPC_PIN_FUNC0);

    FUN_LEAVE;
}

module_init(autotst_init);
module_exit(autotst_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("anli.wei");
MODULE_VERSION("0.0.1");