aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/ak8973b.c
blob: 32de51bfb870bac99435fc867cc891584895fbdf (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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*

 * drivers/i2c/chips/akm8973.c - akm8973 compass driver

 *

 * Copyright (C) 2008-2009 HTC Corporation.

 * Author: viral wang <viralwang@gmail.com>

 *

 * This software is licensed under the terms of the GNU General Public

 * License version 2, as published by the Free Software Foundation, and

 * may be copied, distributed, and modified under those terms.

 *

 * 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.

 *

 */


/*

 * Revised by AKM 2010/03/25

 *

 */

#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/mutex.h>
#include <linux/gpio.h>
#include <linux/earlysuspend.h>
#include <mach/hardware.h>
#include <plat/gpio-cfg.h>
#include <mach/regs-gpio.h>
#include <asm/uaccess.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/freezer.h>
#include <mach/gpio-p1.h>

#include "ak8973b.h"

#define DEBUG 0

#define E_COMPASS_ADDRESS	0x1c	/* CAD0 : 0, CAD1 : 0 */
#define I2C_DF_NOTIFY       0x01
#define IRQ_COMPASS_INT IRQ_EINT(2) /* EINT(2) */

static DEFINE_MUTEX(ak8973b_mutex);


static struct i2c_client *this_client;

struct ak8973b_data {
	struct i2c_client		*client;
	struct early_suspend	early_suspend;
};

static DECLARE_WAIT_QUEUE_HEAD(open_wq);
static char ak_e2prom_data[3];

// this proc file system's path is "/proc/driver/ak8973"
// usage :	(at the path) type "cat bma020" , it will show short information for current accelation
// 			use it for simple working test only
#define AK8973_PROC_FS

#ifdef AK8973_PROC_FS
#include <linux/proc_fs.h>
#define DRIVER_PROC_ENTRY		"driver/ak8973"

static int AKECS_SetMeasure (void);
static int AKECS_GetData (short *rbuf);

static int ak_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
{
	char *p = page;
	int len;

	short mag_sensor[4] ={0, 0, 0, 0};
	s3c_gpio_cfgpin(GPIO_MSENSE_IRQ, GPIO_INPUT);
	s3c_gpio_setpull(GPIO_MSENSE_IRQ, S3C_GPIO_PULL_NONE);
	AKECS_SetMeasure();
	while(!gpio_get_value(GPIO_MSENSE_IRQ));
	AKECS_GetData(mag_sensor);

	p += sprintf(p,"[AK8973]\nX: %d, Y: %d, Z: %d\n", mag_sensor[1], mag_sensor[2], mag_sensor[3] );
	len = (p - page) - off;
	if (len < 0) {
		len = 0;
	}
	printk("ak_proc_read: success full\n");
	*eof = (len <= count) ? 1 : 0;
	*start = page + off;

	return len;
}
#endif	//BMA020_PROC_FS

static int AKI2C_RxData(char *rxData, int length)
{
	struct i2c_msg msgs[] = {
		{
		 .addr = this_client->addr,
		 .flags = 0,
		 .len = 1,
		 .buf = rxData,
		 },
		{
		 .addr = this_client->addr,
		 .flags = I2C_M_RD,
		 .len = length,
		 .buf = rxData,
		 },
	};

	if (i2c_transfer(this_client->adapter, msgs, 2) < 0) {
		printk(KERN_ERR "AKI2C_RxData: transfer error \n");
		return -EIO;
	} else
		return 0;
}

static int AKI2C_TxData(char *txData, int length)
{
	struct i2c_msg msg[] = {
		{
		 .addr = this_client->addr,
		 .flags = 0,
		 .len = length,
		 .buf = txData,
		 },
	};

	if (i2c_transfer(this_client->adapter, msg, 1) < 0) {
		printk(KERN_ERR "AKI2C_TxData: transfer error addr \n");
		return -EIO;
	} else
		return 0;
}

static int AKECS_Init(void)
{
	return 0;
}

/*----------------------------------------------------------------------------*/
//Description : Resetting AKECS
//WHO : AKEK GWLEE
//DATE : 2008 12 08
/*----------------------------------------------------------------------------*/
static void AKECS_Reset (void)
{

	gpio_set_value(GPIO_MSENSE_nRST, GPIO_LEVEL_LOW);
	udelay(120);
	gpio_set_value(GPIO_MSENSE_nRST, GPIO_LEVEL_HIGH);
	gprintk("AKECS RESET COMPLETE\n");
}

/*----------------------------------------------------------------------------*/
//Description : Setting measurement to AKECS
//WHO : AKEK GWLEE
//DATE : 2008 12 08
/*----------------------------------------------------------------------------*/
static int AKECS_SetMeasure (void)
{
	char buffer[2];

	gprintk("MEASURE MODE\n");
	/* Set measure mode */
	buffer[0] = AKECS_REG_MS1;
	buffer[1] = AKECS_MODE_MEASURE;

	/* Set data */
	return AKI2C_TxData(buffer, 2);
}

/*----------------------------------------------------------------------------*/
//Description : Setting EEPROM to AKECS
//WHO : AKEK GWLEE
//DATE : 2008 12 08
/*----------------------------------------------------------------------------*/
static int AKECS_SetE2PRead ( void )
{
	char buffer[2];

	/* Set measure mode */
	buffer[0] = AKECS_REG_MS1;
	buffer[1] = AKECS_MODE_E2P_READ;
	/* Set data */
	return AKI2C_TxData(buffer, 2);
}


/*----------------------------------------------------------------------------*/
//Description : Power Down to AKECS
//WHO : AKEK GWLEE
//DATE : 2008 12 08
/*----------------------------------------------------------------------------*/
static int AKECS_PowerDown (void)
{
	char buffer[2];
	int ret;

	/* Set powerdown mode */
	buffer[0] = AKECS_REG_MS1;
	buffer[1] = AKECS_MODE_POWERDOWN;
	/* Set data */
	ret = AKI2C_TxData(buffer, 2);
	if (ret < 0)
		return ret;

	/* Dummy read for clearing INT pin */
	buffer[0] = AKECS_REG_TMPS;
	/* Read data */
	ret = AKI2C_RxData(buffer, 1);
	if (ret < 0)
		return ret;

 return ret;
}

/*----------------------------------------------------------------------------*/
//Description : Get EEPROM Data to AKECS
//WHO : AKEK GWLEE
//DATE : 2008 12 08
/*----------------------------------------------------------------------------*/
static int AKECS_GetEEPROMData (void)
{
	int ret;
	char buffer[RBUFF_SIZE + 1];

	ret = AKECS_SetE2PRead();
	if (ret < 0) return ret;

	memset(buffer, 0, RBUFF_SIZE + 1);
	buffer[0] = AKECS_EEP_EHXGA;
	ret =  AKI2C_RxData(buffer, 3);

	if (ret < 0) return ret;

	ak_e2prom_data[0]= buffer[0];
	ak_e2prom_data[1]= buffer[1];
	ak_e2prom_data[2]= buffer[2];
	gprintk("AKE2PROM_Data -->%d , %d, %d ----\n", ak_e2prom_data[0],ak_e2prom_data[1],ak_e2prom_data[2]);

	return ret;
}

static int AKECS_SetMode(char mode)
{
	int ret = 0;

	switch (mode) {
		case AKECS_MODE_MEASURE:
			ret = AKECS_SetMeasure();
			break;
		case AKECS_MODE_E2P_READ:
			ret = AKECS_SetE2PRead();
			break;
		case AKECS_MODE_POWERDOWN:
			ret = AKECS_PowerDown();
			break;
		default:
			return -EINVAL;
	}

	/* wait at least 300us after changing mode */
	msleep(1);
	return ret;
}

static int AKECS_TransRBuff(char *rbuf, int size)
{
	if(size < RBUFF_SIZE + 1)
		return -EINVAL;

	// read C0 - C4
	rbuf[0] = AKECS_REG_ST;
	return AKI2C_RxData(rbuf, RBUFF_SIZE + 1);

}

/*----------------------------------------------------------------------------*/
//Description : Get EEPROM Data to AKECS
//WHO : AKEK GWLEE
//DATE : 2008 12 08
/*----------------------------------------------------------------------------*/
static int AKECS_GetData (short *rbuf)
{
	int ret;
	char buffer[RBUFF_SIZE + 1];

	memset(buffer, 0, RBUFF_SIZE + 1);
	buffer[0] = AKECS_REG_TMPS;

	ret =  AKI2C_RxData(buffer, 4);  //temp, x, y, z
	if(ret<0) {
		printk(KERN_ERR "AKECS_GetData  failed--------------\n");
		return ret;
		}
	else	{

			rbuf[0] = 35 + (120 -buffer[0])*10/16;
			rbuf[1] = buffer[1];
			rbuf[2] = buffer[2];
			rbuf[3] = buffer[3];
			printk("AKECS_Get_MAG: temp = %d, x = %d, y = %d, z = %d\n", rbuf[0], rbuf[1], rbuf[2], rbuf[3]);
		}
  return ret;
}

static int akmd_open(struct inode *inode, struct file *file)
{
	gprintk(KERN_INFO "[AK8973B] %s\n", __FUNCTION__);
	return nonseekable_open(inode, file);
}

static int akmd_release(struct inode *inode, struct file *file)
{
	gprintk(KERN_INFO "[AK8973B] %s\n", __FUNCTION__);
	return 0;
}

static int akmd_ioctl(struct file *file, unsigned int cmd,
		unsigned long arg)
{
	int err = 0;
	int i;
	void __user *argp = (void __user *)arg;

	char msg[RBUFF_SIZE + 1], rwbuf[16];//, numfrq[2];
	int ret = -1;
	short mode; /* step_count,*/

	mutex_lock(&ak8973b_mutex);
	/* check cmd */
	if(_IOC_TYPE(cmd) != AKMIO)
	{
#if DEBUG
		printk("[AK8973] cmd magic type error\n");
#endif
		mutex_unlock(&ak8973b_mutex);
		return -ENOTTY;
	}
	if(_IOC_DIR(cmd) & _IOC_READ)
		err = !access_ok(VERIFY_WRITE,(void __user*)arg, _IOC_SIZE(cmd));
	else if(_IOC_DIR(cmd) & _IOC_WRITE)
		err = !access_ok(VERIFY_READ, (void __user*)arg, _IOC_SIZE(cmd));
	if(err)
	{
#if DEBUG
		printk("[AK8973] cmd access_ok error\n");
#endif
		mutex_unlock(&ak8973b_mutex);
		return -EFAULT;
	}

	switch (cmd) {
		case ECS_IOCTL_READ:
		case ECS_IOCTL_WRITE:
			if (copy_from_user(&rwbuf, argp, sizeof(rwbuf)))
				err = -EFAULT;
			break;
		case ECS_IOCTL_SET_MODE:
			if (copy_from_user(&mode, argp, sizeof(mode)))
				err = -EFAULT;
			break;
		default:
			break;
	}
	switch (cmd) {
		case ECS_IOCTL_RESET:
			gprintk("[AK8973B] ECS_IOCTL_RESET %x\n", cmd);
			AKECS_Reset();
			break;
		case ECS_IOCTL_READ:
			gprintk("[AK8973B] ECS_IOCTL_READ %x\n", cmd);
			gprintk(" len %02x:", rwbuf[0]);
			gprintk(" addr %02x:", rwbuf[1]);
			gprintk("\n");
			if (rwbuf[0] < 1)
				err = -EINVAL;
			ret = AKI2C_RxData(&rwbuf[1], rwbuf[0]);
			//for(i=0; i<rwbuf[0]; i++){
			//	printk(" %02x", rwbuf[i+1]);
			//}
			gprintk(" ret = %d\n", ret);
			if (ret < 0)
				err = ret;
			break;
		case ECS_IOCTL_WRITE:
			gprintk("[AK8973B] ECS_IOCTL_WRITE %x\n", cmd);
			gprintk(" len %02x:", rwbuf[0]);
			for(i=0; i<rwbuf[0]; i++){
				gprintk(" %02x", rwbuf[i+1]);
			}
			gprintk("\n");
			if (rwbuf[0] < 2)
				err = -EINVAL;
			ret = AKI2C_TxData(&rwbuf[1], rwbuf[0]);
			gprintk(" ret = %d\n", ret);
			if (ret < 0)
				err = ret;
			break;
		case ECS_IOCTL_SET_MODE:
			gprintk("[AK8973B] ECS_IOCTL_SET_MODE %x mode=%x\n", cmd, mode);
			ret = AKECS_SetMode((char)mode);
			gprintk(" ret = %d\n", ret);
			if (ret < 0)
				err = ret;
			break;
		case ECS_IOCTL_GETDATA:
			gprintk("[AK8973B] ECS_IOCTL_GETDATA %x\n", cmd);
			ret = AKECS_TransRBuff(msg, RBUFF_SIZE+1);
			gprintk(" ret = %d\n", ret);
			if (ret < 0)
				err = ret;
			for(i=0; i<ret; i++){
				gprintk(" %02x", msg[i]);
			}
			gprintk("\n");
			break;
		default:
			gprintk("Unknown cmd %x\n", cmd);
			err = -ENOTTY;
	}

	switch (cmd) {
		case ECS_IOCTL_READ:
			if (copy_to_user(argp, &rwbuf, sizeof(rwbuf)))
				err = -EFAULT;
			break;
		case ECS_IOCTL_GETDATA:
			if (copy_to_user(argp, &msg, sizeof(msg)))
				err = -EFAULT;
			break;
		default:
			break;
	}
	mutex_unlock(&ak8973b_mutex);
	return err;
}

static void ak8973b_init_hw(void)
{
#if 0
	s3c_gpio_cfgpin(GPIO_MSENSE_INT, S3C_GPIO_SFN(GPIO_MSENSE_INT_AF));
	s3c_gpio_setpull(GPIO_MSENSE_INT, S3C_GPIO_PULL_NONE);
	set_irq_type(IRQ_COMPASS_INT, IRQ_TYPE_EDGE_RISING);
#endif

	if(gpio_is_valid(GPIO_MSENSE_nRST)){
		if(gpio_request(GPIO_MSENSE_nRST, "GPB"))
		{
			printk(KERN_ERR "Failed to request GPIO_MSENSE_nRST!\n");
		}
		gpio_direction_output(GPIO_MSENSE_nRST, GPIO_LEVEL_LOW);
	}

//	s3c_gpio_setpull(GPIO_MSENSE_nRST, S3C_GPIO_PULL_NONE);
/*VNVS: 14-MAY'10 Pulling the RST Pin HIGH instead of keeping in floating state(Pulling NONE)*/
	s3c_gpio_setpull(GPIO_MSENSE_nRST, S3C_GPIO_PULL_NONE);

	gprintk("gpio setting complete!\n");
}

static struct file_operations akmd_fops = {
	.owner = THIS_MODULE,
	.open = akmd_open,
	.release = akmd_release,
	.unlocked_ioctl = akmd_ioctl,
};

static struct miscdevice akmd_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "akm8973",
	.fops = &akmd_fops,
};

static int ak8973_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	int err = 0;
	struct ak8973b_data *akm;
	struct device *dev = &client->dev;

	gprintk("start\n");
	printk("[%s] ak8973 started...\n",__func__);

	akm = kzalloc(sizeof(struct ak8973b_data), GFP_KERNEL);
	if (!akm) {
		err = -ENOMEM;
		goto exit_alloc_data_failed;
	}

    this_client = client;
    i2c_set_clientdata(client, akm);

	if(this_client == NULL)
	{
		gprintk("i2c_client is NULL\n");
		return -ENODEV;
	}

	err = misc_register(&akmd_device);
	if (err) {
		printk(KERN_ERR "akm8973_probe: akmd_device register failed\n");
		goto exit_misc_device_register_failed;
	}

#ifdef AK8973_PROC_FS
	create_proc_read_entry(DRIVER_PROC_ENTRY, 0, 0, ak_proc_read, NULL);
#endif	//BMA020_PROC_FS


	AKECS_GetEEPROMData();

	return 0;

exit_misc_device_register_failed:
exit_input_dev_alloc_failed:
	kfree(akm);
exit_alloc_data_failed:
	return err;
}

static int __exit ak8973_remove(struct i2c_client *client)
{
	// int err;
	struct ak8973b_data *akm = i2c_get_clientdata(client);

	printk("[%s] ak8973 removed...\n",__func__);

	misc_deregister(&akmd_device);

	kfree(akm);
	kfree(client);
	akm = NULL;
	this_client = NULL;

	gpio_free(GPIO_MSENSE_nRST);

	gprintk("end\n");
	return 0;
}

static int ak8973_suspend( struct platform_device* pdev, pm_message_t state )
{
	printk("############## %s \n",__func__);
	printk("[%s] Set Mode AKECS_MODE_POWERDOWN\n", __func__);
	AKECS_SetMode(AKECS_MODE_POWERDOWN);
	return 0;
}


static int ak8973_resume( struct platform_device* pdev )
{
	printk("@@@@ %s \n",__func__);

	printk("[%s] Set Mode AKECS_MODE_MEASURE\n", __func__);
	AKECS_SetMode(AKECS_MODE_MEASURE);

	wake_up(&open_wq);
	return 0;
}

static const struct i2c_device_id ak8973_id[] = {
	{"ak8973", 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, ak8973_id);

static struct i2c_driver ak8973b_i2c_driver = {
	.driver = {
		   .name = "ak8973",
		   },
	.probe = ak8973_probe,
	.remove = __exit_p(ak8973_remove),
	.suspend = ak8973_suspend,
	.resume = ak8973_resume,
	.id_table = ak8973_id,
};

static int __init ak8973b_init(void)
{
	gprintk("__start\n");
	ak8973b_init_hw();
	AKECS_Reset(); /*module reset */
	udelay(200);

	return i2c_add_driver(&ak8973b_i2c_driver);
}

static void __exit ak8973b_exit(void)
{
	gprintk("__exit\n");
	i2c_del_driver(&ak8973b_i2c_driver);
}

module_init(ak8973b_init);
module_exit(ak8973b_exit);

MODULE_AUTHOR("Asahi Kasei Microdevices");
MODULE_DESCRIPTION("AK8973B compass driver");
MODULE_LICENSE("GPL");