summaryrefslogtreecommitdiff
path: root/drivers/hwmon/mxc_mma7450.c
blob: fa1dd41267eb5cc85445c913d7b4e55eb8a31cf5 (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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*
 * linux/drivers/hwmon/mma7450.c
 *
 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*include file*/
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/input-polldev.h>
#include <linux/hwmon.h>
#include <linux/regulator/consumer.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/fsl_devices.h>

/*macro define*/
#define MMA7450_I2C_ADDR	0x1D
#define DEVICE_NAME		"mma7450"
#define POLL_INTERVAL		100
#define DEBUG

#define INPUT_FUZZ	4
#define INPUT_FLAT	4

enum {
	REG_XOUTL = 0x00,
	REG_XOUTH,
	REG_YOUTL,
	REG_YOUTH,
	REG_ZOUTL,
	REG_ZOUTH,
	REG_XOUT8,
	REG_YOUT8,
	REG_ZOUT8,
	REG_STATUS,
	REG_DETSRC,
	REG_TOUT,
	REG_RESERVED_0,
	REG_I2CAD,
	REG_USRINF,
	REG_WHOAMI,
	REG_XOFFL,
	REG_XOFFH,
	REG_YOFFL,
	REG_YOFFH,
	REG_ZOFFL,
	REG_ZOFFH,
	REG_MCTL,
	REG_INTRST,
	REG_CTL1,
	REG_CTL2,
	REG_LDTH,
	REG_PDTH,
	REG_PD,
	REG_LT,
	REG_TW,
	REG_REVERVED_1,
};

enum {
	MOD_STANDBY = 0,
	MOD_MEASURE,
	MOD_LEVEL_D,
	MOD_PULSE_D,
};

enum {
	INT_1L_2P = 0,
	INT_1P_2L,
	INT_1SP_2P,
};

struct mma7450_status {
	u8 mod;
	u8 ctl1;
	u8 ctl2;
};

/*forward declear*/
static ssize_t mma7450_show(struct device *dev,
			    struct device_attribute *attr, char *buf);
static ssize_t mma7450_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count);
static int mma7450_probe(struct i2c_client *client,
			 const struct i2c_device_id *id);
static int mma7450_remove(struct i2c_client *client);
static int mma7450_suspend(struct i2c_client *client, pm_message_t state);
static int mma7450_resume(struct i2c_client *client);
static void mma_bh_handler(struct work_struct *work);

/*definition*/
static struct regulator *reg_dvdd_io;
static struct regulator *reg_avdd;
static struct i2c_client *mma7450_client;
static struct device *hwmon_dev;
static struct input_polled_dev *mma7450_idev;
static struct mxc_mma7450_platform_data *plat_data;
static u8 mma7450_mode;
static struct device_attribute mma7450_dev_attr = {
	.attr = {
		 .name = "mma7450_ctl",
		 .mode = S_IRUSR | S_IWUSR,
		 },
	.show = mma7450_show,
	.store = mma7450_store,
};

static const struct i2c_device_id mma7450_id[] = {
	{"mma7450", 0},
	{},
};

MODULE_DEVICE_TABLE(i2c, mma7450_id);

static struct i2c_driver i2c_mma7450_driver = {
	.driver = {
		   .name = "mma7450",
		   },
	.probe = mma7450_probe,
	.remove = mma7450_remove,
	.suspend = mma7450_suspend,
	.resume = mma7450_resume,
	.id_table = mma7450_id,
};

static struct mma7450_status mma_status = {
	.mod = 0,
	.ctl1 = 0,
	.ctl2 = 0,
};

DECLARE_WORK(mma_work, mma_bh_handler);

#ifdef DEBUG
enum {
	MMA_REG_R = 0,
	MMA_REG_W,
	MMA_SET_MOD,
	MMA_SET_L_THR,
	MMA_SET_P_THR,
	MMA_SET_INTP,
	MMA_SET_INTB,
	MMA_SET_G,
	MMA_I2C_EABLE,
	MMA_OFF_X,
	MMA_OFF_Y,
	MMA_OFF_Z,
	MMA_SELF_TEST,
	MMA_SET_LDPL,
	MMA_SET_PDPL,
	MMA_SET_PDV,
	MMA_SET_LTV,
	MMA_SET_TW,
	MMA_CMD_MAX
};

static char *command[MMA_CMD_MAX] = {
	[MMA_REG_R] = "readreg",
	[MMA_REG_W] = "writereg",
	[MMA_SET_MOD] = "setmod",
	[MMA_SET_L_THR] = "setlt",
	[MMA_SET_P_THR] = "setpt",
	[MMA_SET_INTP] = "setintp",
	[MMA_SET_INTB] = "setintb",
	[MMA_SET_G] = "setg",
	[MMA_I2C_EABLE] = "setie",
	[MMA_OFF_X] = "setxo",
	[MMA_OFF_Y] = "setyo",
	[MMA_OFF_Z] = "setzo",
	[MMA_SELF_TEST] = "selft",
	[MMA_SET_LDPL] = "setldp",
	[MMA_SET_PDPL] = "setpdp",
	[MMA_SET_PDV] = "setpdv",
	[MMA_SET_LTV] = "setltv",
	[MMA_SET_TW] = "settw",
};

static void set_mod(u8 mode)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_MCTL);
	/* shall I test the ret value? */
	ret = (ret & ~0x3) | (mode & 0x3);
	mma_status.mod = ret;
	i2c_smbus_write_byte_data(mma7450_client, REG_MCTL, ret);
}

static void set_level_thr(u8 lth)
{
	i2c_smbus_write_byte_data(mma7450_client, REG_LDTH, lth);
}

static void set_pulse_thr(u8 pth)
{
	i2c_smbus_write_byte_data(mma7450_client, REG_PDTH, pth);
}

static void set_int_pin(u8 pin)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_CTL1);
	ret = (ret & ~0x1) | (pin & 0x1);
	mma_status.ctl1 = ret;
	i2c_smbus_write_byte_data(mma7450_client, REG_CTL1, ret);
}

static void set_int_bit(u8 bit)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_CTL1);
	ret = (ret & ~0x6) | ((bit << 1) & 0x6);
	mma_status.ctl1 = ret;
	i2c_smbus_write_byte_data(mma7450_client, REG_CTL1, ret);
}

static void set_g_level(u8 gl)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_MCTL);
	ret = (ret & ~0xC) | ((gl << 2) & 0xC);
	i2c_smbus_write_byte_data(mma7450_client, REG_MCTL, ret);
}

static void set_i2c_enable(u8 i2c_e)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_I2CAD);
	ret = (ret & ~0x80) | ((i2c_e << 7) & 0x80);
	i2c_smbus_write_byte_data(mma7450_client, REG_I2CAD, ret);
}

static void set_x_offset(u16 xo)
{
	u8 data;

	data = (xo & 0xFF);
	i2c_smbus_write_byte_data(mma7450_client, REG_XOFFL, data);
	data = (xo & 0xFF00) >> 8;
	i2c_smbus_write_byte_data(mma7450_client, REG_XOFFH, data);
}

static void set_y_offset(u16 yo)
{
	u8 data;

	data = (yo & 0xFF);
	i2c_smbus_write_byte_data(mma7450_client, REG_YOFFL, data);
	data = (yo & 0xFF00) >> 8;
	i2c_smbus_write_byte_data(mma7450_client, REG_YOFFH, data);
}

static void set_z_offset(u16 zo)
{
	u8 data;

	data = (zo & 0xFF);
	i2c_smbus_write_byte_data(mma7450_client, REG_ZOFFL, data);
	data = (zo & 0xFF00) >> 8;
	i2c_smbus_write_byte_data(mma7450_client, REG_ZOFFH, data);
}

static void selftest(u8 st)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_MCTL);
	ret = (ret & ~0x10) | ((st << 4) & 0x10);
	i2c_smbus_write_byte_data(mma7450_client, REG_MCTL, ret);
}

static void set_level_det_p(u8 ldp)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_CTL2);
	ret = (ret & ~0x1) | ((ldp << 0) & 0x1);
	mma_status.ctl2 = ret;
	i2c_smbus_write_byte_data(mma7450_client, REG_CTL2, ret);
}

static void set_pulse_det_p(u8 pdp)
{
	int ret;

	ret = i2c_smbus_read_byte_data(mma7450_client, REG_CTL2);
	ret = (ret & ~0x2) | ((pdp << 1) & 0x2);
	mma_status.ctl2 = ret;
	i2c_smbus_write_byte_data(mma7450_client, REG_CTL2, ret);
}

static void set_pulse_duration(u8 pd)
{
	i2c_smbus_write_byte_data(mma7450_client, REG_PD, pd);
}

static void set_latency_time(u8 lt)
{
	i2c_smbus_write_byte_data(mma7450_client, REG_LT, lt);
}

static void set_time_window(u8 tw)
{
	i2c_smbus_write_byte_data(mma7450_client, REG_TW, tw);
}

static void parse_arg(const char *arg, int *reg, int *value)
{
	const char *p;

	for (p = arg;; p++) {
		if (*p == ' ' || *p == '\0')
			break;
	}

	p++;

	*reg = simple_strtoul(arg, NULL, 16);
	*value = simple_strtoul(p, NULL, 16);
}

static void cmd_read_reg(const char *arg)
{
	int reg, value, ret;

	parse_arg(arg, &reg, &value);
	ret = i2c_smbus_read_byte_data(mma7450_client, reg);
	dev_info(&mma7450_client->dev, "read reg0x%x = %x\n", reg, ret);
}

static void cmd_write_reg(const char *arg)
{
	int reg, value, ret;

	parse_arg(arg, &reg, &value);
	ret = i2c_smbus_write_byte_data(mma7450_client, reg, value);
	dev_info(&mma7450_client->dev, "write reg result %s\n",
		 ret ? "failed" : "success");
}

static int exec_command(const char *buf, size_t count)
{
	const char *p, *s;
	const char *arg;
	int i, value = 0;

	for (p = buf;; p++) {
		if (*p == ' ' || *p == '\0' || p - buf >= count)
			break;
	}
	arg = p + 1;

	for (i = MMA_REG_R; i < MMA_CMD_MAX; i++) {
		s = command[i];
		if (s && !strncmp(buf, s, p - buf)) {
			dev_info(&mma7450_client->dev, "command %s\n", s);
			goto mma_exec_command;
		}
	}

	dev_err(&mma7450_client->dev, "command is not found\n");
	return -1;

      mma_exec_command:
	if (i != MMA_REG_R && i != MMA_REG_W)
		value = simple_strtoul(arg, NULL, 16);

	switch (i) {
	case MMA_REG_R:
		cmd_read_reg(arg);
		break;
	case MMA_REG_W:
		cmd_write_reg(arg);
		break;
	case MMA_SET_MOD:
		set_mod(value);
		break;
	case MMA_SET_L_THR:
		set_level_thr(value);
		break;
	case MMA_SET_P_THR:
		set_pulse_thr(value);
		break;
	case MMA_SET_INTP:
		set_int_pin(value);
		break;
	case MMA_SET_INTB:
		set_int_bit(value);
		break;
	case MMA_SET_G:
		set_g_level(value);
		break;
	case MMA_I2C_EABLE:
		set_i2c_enable(value);
		break;
	case MMA_OFF_X:
		set_x_offset(value);
		break;
	case MMA_OFF_Y:
		set_y_offset(value);
		break;
	case MMA_OFF_Z:
		set_z_offset(value);
		break;
	case MMA_SELF_TEST:
		selftest(value);
		break;
	case MMA_SET_LDPL:
		set_level_det_p(value);
		break;
	case MMA_SET_PDPL:
		set_pulse_det_p(value);
		break;
	case MMA_SET_PDV:
		set_pulse_duration(value);
		break;
	case MMA_SET_LTV:
		set_latency_time(value);
		break;
	case MMA_SET_TW:
		set_time_window(value);
		break;
	default:
		dev_err(&mma7450_client->dev, "command is not found\n");
		break;
	}

	return 0;
}

static ssize_t mma7450_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	int ret, reg;

	for (reg = REG_XOUTL; reg < REG_REVERVED_1; reg++) {
		ret = i2c_smbus_read_byte_data(mma7450_client, reg);
		dev_info(&mma7450_client->dev, "reg0x%02x:\t%03d\t0x%02x\n",
			 reg, (s8) ret, ret);
	}

	return 0;
}

static ssize_t mma7450_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	exec_command(buf, count);

	return count;
}

#else

static ssize_t mma7450_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	return 0;
}

static ssize_t mma7450_store(struct device *dev,
			     struct device_attribute *attr, const char *buf,
			     size_t count)
{
	return count;
}

#endif

static void report_abs(void)
{
	u8 status, mod = mma_status.mod;
	s16 x, y, z;

	status = i2c_smbus_read_byte_data(mma7450_client, REG_STATUS);
	if (!(status & 0x01)) {	/* data ready in measurement mode? */
		return;
	}
	if ((mod & 0x0c) == 0) {	/* 8g range */
		x = 0xFF & i2c_smbus_read_byte_data(mma7450_client, REG_XOUTL);
		x |= 0xFF00 &
		    (i2c_smbus_read_byte_data(mma7450_client, REG_XOUTH) << 8);
		y = 0xFF & i2c_smbus_read_byte_data(mma7450_client, REG_YOUTL);
		y |= 0xFF00 &
		    (i2c_smbus_read_byte_data(mma7450_client, REG_YOUTH) << 8);
		z = 0xFF & i2c_smbus_read_byte_data(mma7450_client, REG_ZOUTL);
		z |= 0xFF00 &
		    (i2c_smbus_read_byte_data(mma7450_client, REG_ZOUTH) << 8);
	} else {		/* 2g/4g range */
		x = 0xFF & i2c_smbus_read_byte_data(mma7450_client, REG_XOUT8);
		y = 0xFF & i2c_smbus_read_byte_data(mma7450_client, REG_YOUT8);
		z = 0xFF & i2c_smbus_read_byte_data(mma7450_client, REG_ZOUT8);
	}

	status = i2c_smbus_read_byte_data(mma7450_client, REG_STATUS);
	if (status & 0x02) {	/* data is overwrite */
		return;
	}

	/* convert signed 10bits to signed 16bits */
	x = (short)(x << 6) >> 6;
	y = (short)(y << 6) >> 6;
	z = (short)(z << 6) >> 6;

	input_report_abs(mma7450_idev->input, ABS_X, x);
	input_report_abs(mma7450_idev->input, ABS_Y, y);
	input_report_abs(mma7450_idev->input, ABS_Z, z);
	input_sync(mma7450_idev->input);
}

static void mma_bh_handler(struct work_struct *work)
{
}

static void mma7450_dev_poll(struct input_polled_dev *dev)
{
	report_abs();
}

static irqreturn_t mma7450_interrupt(int irq, void *dev_id)
{
	struct input_dev *input_dev = dev_id;
	u8 int_bit, int_pin;

	int_bit = mma_status.ctl1 & 0x6;
	int_pin = mma_status.ctl1 & 0x1;

	switch (mma_status.mod & 0x03) {
	case 1:
		/*only int1 report data ready int */
		if (plat_data->int1 != irq)
			goto error_bad_int;
		schedule_work(&mma_work);
		break;
	case 2:
		/* for level and pulse detection mode,
		 * choice tasklet to handle interrupt quickly.
		 * Currently, leave it doing nothing*/
		if (plat_data->int1 == irq) {
			if ((int_bit == 0) && (int_pin != 0))
				goto error_bad_int;
			if ((int_bit == 0x2) && (int_pin != 0x1))
				goto error_bad_int;
			if (int_bit == 0x4)
				goto error_bad_int;
		}
		if (plat_data->int2 == irq) {
			if ((int_bit == 0) && (int_pin != 0x1))
				goto error_bad_int;
			if ((int_bit == 0x2) && (int_pin != 0))
				goto error_bad_int;
			if (int_bit == 0x4)
				goto error_bad_int;
		}

		dev_info(&input_dev->dev, "motion detected in level mod\n");

		break;
	case 3:
		if (plat_data->int1 == irq) {
			if ((int_bit == 0) && (int_pin != 0x1))
				goto error_bad_int;
			if ((int_bit == 0x2) && (int_pin != 0))
				goto error_bad_int;
			if ((int_bit == 0x4) && (int_pin != 0x1))
				goto error_bad_int;
		}
		if (plat_data->int2 == irq) {
			if ((int_bit == 0) && (int_pin != 0))
				goto error_bad_int;
			if ((int_bit == 0x2) && (int_pin != 0x1))
				goto error_bad_int;
			if ((int_bit == 0x4) && (int_pin != 0))
				goto error_bad_int;
		}

		if (mma_status.ctl2 & 0x02)
			dev_info(&input_dev->dev,
				 "freefall detected in pulse mod\n");
		else
			dev_info(&input_dev->dev,
				 "motion detected in pulse mod\n");

		break;
	case 0:
	default:
		break;
	}
      error_bad_int:
	return IRQ_RETVAL(1);
}

static int mma7450_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	int ret;
	struct input_dev *idev;

	plat_data =
	    (struct mxc_mma7450_platform_data *)client->dev.platform_data;
	if (plat_data == NULL) {
		dev_err(&client->dev, "lack of platform data!\n");
		return -ENODEV;
	}

	/*enable power supply */
	/*when to power on/off the power is to be considered later */
	/*shall I check the return value */
	reg_dvdd_io = regulator_get(&client->dev, plat_data->reg_dvdd_io);
	if (reg_dvdd_io != ERR_PTR(-ENOENT))
		regulator_enable(reg_dvdd_io);
	else
		return -EINVAL;

	reg_avdd = regulator_get(&client->dev, plat_data->reg_avdd);
	if (reg_avdd != ERR_PTR(-ENOENT))
		regulator_enable(reg_avdd);
	else {
		regulator_put(reg_dvdd_io);
		return -EINVAL;
	}

	/*bind the right device to the driver */
	ret = i2c_smbus_read_byte_data(client, REG_I2CAD);
	if (MMA7450_I2C_ADDR != (0x7F & ret)) {	/*compare the address value */
		dev_err(&client->dev,
			"read chip ID 0x%x is not equal to 0x%x!\n", ret,
			MMA7450_I2C_ADDR);
		goto error_disable_power;
	}
	mma7450_client = client;

	/*interrupt register */
	/*when to register interrupt is to be considered later */

	/*create device file in sysfs as user interface */
	ret = device_create_file(&client->dev, &mma7450_dev_attr);
	if (ret) {
		dev_err(&client->dev, "create device file failed!\n");
		goto error_disable_power;
	}

	/*register to hwmon device */
	hwmon_dev = hwmon_device_register(&client->dev);
	if (IS_ERR(hwmon_dev)) {
		dev_err(&client->dev, "hwmon register failed!\n");
		ret = PTR_ERR(hwmon_dev);
		goto error_rm_dev_file;
	}

	/*input poll device register */
	mma7450_idev = input_allocate_polled_device();
	if (!mma7450_idev) {
		dev_err(&client->dev, "alloc poll device failed!\n");
		ret = -ENOMEM;
		goto error_rm_hwmon_dev;
	}
	mma7450_idev->poll = mma7450_dev_poll;
	mma7450_idev->poll_interval = POLL_INTERVAL;
	idev = mma7450_idev->input;
	idev->name = DEVICE_NAME;
	idev->id.bustype = BUS_I2C;
	idev->dev.parent = &client->dev;
	idev->evbit[0] = BIT_MASK(EV_ABS);

	input_set_abs_params(idev, ABS_X, -512, 512, INPUT_FUZZ, INPUT_FLAT);
	input_set_abs_params(idev, ABS_Y, -512, 512, INPUT_FUZZ, INPUT_FLAT);
	input_set_abs_params(idev, ABS_Z, -512, 512, INPUT_FUZZ, INPUT_FLAT);
	ret = input_register_polled_device(mma7450_idev);
	if (ret) {
		dev_err(&client->dev, "register poll device failed!\n");
		goto error_free_poll_dev;
	}

	/* configure gpio as input for interrupt monitor */
	plat_data->gpio_pin_get();

	set_irq_type(plat_data->int1, IRQF_TRIGGER_RISING);
	/* register interrupt handle */
	ret = request_irq(plat_data->int1, mma7450_interrupt,
			  IRQF_TRIGGER_RISING, DEVICE_NAME, idev);

	if (ret) {
		dev_err(&client->dev, "request_irq(%d) returned error %d\n",
			plat_data->int1, ret);
		goto error_rm_poll_dev;
	}

	set_irq_type(plat_data->int2, IRQF_TRIGGER_RISING);
	ret = request_irq(plat_data->int2, mma7450_interrupt,
			  IRQF_TRIGGER_RISING, DEVICE_NAME, idev);
	if (ret) {
		dev_err(&client->dev, "request_irq(%d) returned error %d\n",
			plat_data->int2, ret);
		goto error_free_irq1;
	}

	dev_info(&client->dev, "mma7450 device is probed successfully.\n");

	set_mod(1);
	return 0;		/*what value shall be return */

	/*error handle */
      error_free_irq1:
	free_irq(plat_data->int1, 0);
      error_rm_poll_dev:
	input_unregister_polled_device(mma7450_idev);
      error_free_poll_dev:
	input_free_polled_device(mma7450_idev);
      error_rm_hwmon_dev:
	hwmon_device_unregister(hwmon_dev);
      error_rm_dev_file:
	device_remove_file(&client->dev, &mma7450_dev_attr);
      error_disable_power:
	regulator_disable(reg_dvdd_io);	/*shall I check the return value */
	regulator_disable(reg_avdd);
	regulator_put(reg_dvdd_io);
	regulator_put(reg_avdd);

	return ret;
}

static int mma7450_remove(struct i2c_client *client)
{
	free_irq(plat_data->int2, mma7450_idev->input);
	free_irq(plat_data->int1, mma7450_idev->input);
	plat_data->gpio_pin_put();
	input_unregister_polled_device(mma7450_idev);
	input_free_polled_device(mma7450_idev);
	hwmon_device_unregister(hwmon_dev);
	device_remove_file(&client->dev, &mma7450_dev_attr);
	regulator_disable(reg_dvdd_io);	/*shall I check the return value */
	regulator_disable(reg_avdd);
	regulator_put(reg_dvdd_io);
	regulator_put(reg_avdd);
	return 0;
}

static int mma7450_suspend(struct i2c_client *client, pm_message_t state)
{
	mma7450_mode = i2c_smbus_read_byte_data(mma7450_client, REG_MCTL);
	i2c_smbus_write_byte_data(mma7450_client, REG_MCTL,
				  mma7450_mode & ~0x3);
	return 0;
}

static int mma7450_resume(struct i2c_client *client)
{
	i2c_smbus_write_byte_data(mma7450_client, REG_MCTL, mma7450_mode);
	return 0;
}

static int __init init_mma7450(void)
{
	/*register driver */
	printk(KERN_INFO "add mma i2c driver\n");
	return i2c_add_driver(&i2c_mma7450_driver);
}

static void __exit exit_mma7450(void)
{
	printk(KERN_INFO "del mma i2c driver.\n");
	return i2c_del_driver(&i2c_mma7450_driver);
}

module_init(init_mma7450);
module_exit(exit_mma7450);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("MMA7450 sensor driver");
MODULE_LICENSE("GPL");