rk fb: extend output mutex lock in win config
[firefly-linux-kernel-4.4.55.git] / drivers / input / gsensor / bma023.c
1 /* drivers/i2c/chips/bma023.c - bma023 compass driver
2  *
3  * Copyright (C) 2007-2008 HTC Corporation.
4  * Author: Hou-Kun Chen <houkun.chen@gmail.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/i2c.h>
20 #include <linux/slab.h>
21 #include <linux/irq.h>
22 #include <linux/miscdevice.h>
23 #include <linux/gpio.h>
24 #include <asm/uaccess.h>
25 #include <asm/atomic.h>
26 #include <linux/delay.h>
27 #include <linux/input.h>
28 #include <linux/workqueue.h>
29 #include <linux/mutex.h>
30 #include <linux/freezer.h>
31 #include <mach/gpio.h>
32 #include <mach/board.h>
33 #ifdef CONFIG_HAS_EARLYSUSPEND
34 #include <linux/earlysuspend.h>
35 #endif
36
37
38 #if 0
39 #define DBG(x...) printk(x)
40 #else
41 #define DBG(x...)
42 #endif
43
44
45 #define SENSOR_NAME                     "bma150"
46 #define GRAVITY_EARTH                   9806550
47 #define ABSMIN_2G                       (-GRAVITY_EARTH * 2)
48 #define ABSMAX_2G                       (GRAVITY_EARTH * 2)
49 #define BMA150_MAX_DELAY                200
50 #define BMA150_CHIP_ID                  2
51 #define BMA150_RANGE_SET                0
52 #define BMA150_BW_SET                   4
53
54
55
56 #define BMA150_CHIP_ID_REG                      0x00
57 #define BMA150_X_AXIS_LSB_REG           0x02
58 #define BMA150_X_AXIS_MSB_REG           0x03
59 #define BMA150_Y_AXIS_LSB_REG           0x04
60 #define BMA150_Y_AXIS_MSB_REG           0x05
61 #define BMA150_Z_AXIS_LSB_REG           0x06
62 #define BMA150_Z_AXIS_MSB_REG           0x07
63 #define BMA150_STATUS_REG       0x09
64 #define BMA150_CTRL_REG         0x0a
65 #define BMA150_CONF1_REG        0x0b
66
67 #define BMA150_CUSTOMER1_REG            0x12
68 #define BMA150_CUSTOMER2_REG            0x13
69 #define BMA150_RANGE_BWIDTH_REG 0x14
70 #define BMA150_CONF2_REG        0x15
71
72 #define BMA150_OFFS_GAIN_X_REG          0x16
73 #define BMA150_OFFS_GAIN_Y_REG          0x17
74 #define BMA150_OFFS_GAIN_Z_REG          0x18
75 #define BMA150_OFFS_GAIN_T_REG          0x19
76 #define BMA150_OFFSET_X_REG             0x1a
77 #define BMA150_OFFSET_Y_REG             0x1b
78 #define BMA150_OFFSET_Z_REG             0x1c
79 #define BMA150_OFFSET_T_REG             0x1d
80
81 #define BMA150_CHIP_ID__POS             0
82 #define BMA150_CHIP_ID__MSK             0x07
83 #define BMA150_CHIP_ID__LEN             3
84 #define BMA150_CHIP_ID__REG             BMA150_CHIP_ID_REG
85
86 /* DATA REGISTERS */
87
88 #define BMA150_NEW_DATA_X__POS          0
89 #define BMA150_NEW_DATA_X__LEN          1
90 #define BMA150_NEW_DATA_X__MSK          0x01
91 #define BMA150_NEW_DATA_X__REG          BMA150_X_AXIS_LSB_REG
92
93 #define BMA150_ACC_X_LSB__POS           6
94 #define BMA150_ACC_X_LSB__LEN           2
95 #define BMA150_ACC_X_LSB__MSK           0xC0
96 #define BMA150_ACC_X_LSB__REG           BMA150_X_AXIS_LSB_REG
97
98 #define BMA150_ACC_X_MSB__POS           0
99 #define BMA150_ACC_X_MSB__LEN           8
100 #define BMA150_ACC_X_MSB__MSK           0xFF
101 #define BMA150_ACC_X_MSB__REG           BMA150_X_AXIS_MSB_REG
102
103 #define BMA150_ACC_Y_LSB__POS           6
104 #define BMA150_ACC_Y_LSB__LEN           2
105 #define BMA150_ACC_Y_LSB__MSK           0xC0
106 #define BMA150_ACC_Y_LSB__REG           BMA150_Y_AXIS_LSB_REG
107
108 #define BMA150_ACC_Y_MSB__POS           0
109 #define BMA150_ACC_Y_MSB__LEN           8
110 #define BMA150_ACC_Y_MSB__MSK           0xFF
111 #define BMA150_ACC_Y_MSB__REG           BMA150_Y_AXIS_MSB_REG
112
113 #define BMA150_ACC_Z_LSB__POS           6
114 #define BMA150_ACC_Z_LSB__LEN           2
115 #define BMA150_ACC_Z_LSB__MSK           0xC0
116 #define BMA150_ACC_Z_LSB__REG           BMA150_Z_AXIS_LSB_REG
117
118 #define BMA150_ACC_Z_MSB__POS           0
119 #define BMA150_ACC_Z_MSB__LEN           8
120 #define BMA150_ACC_Z_MSB__MSK           0xFF
121 #define BMA150_ACC_Z_MSB__REG           BMA150_Z_AXIS_MSB_REG
122
123 /* CONTROL BITS */
124
125 #define BMA150_SLEEP__POS                       0
126 #define BMA150_SLEEP__LEN                       1
127 #define BMA150_SLEEP__MSK                       0x01
128 #define BMA150_SLEEP__REG                       BMA150_CTRL_REG
129
130 #define BMA150_SOFT_RESET__POS          1
131 #define BMA150_SOFT_RESET__LEN          1
132 #define BMA150_SOFT_RESET__MSK          0x02
133 #define BMA150_SOFT_RESET__REG          BMA150_CTRL_REG
134
135 #define BMA150_EE_W__POS                        4
136 #define BMA150_EE_W__LEN                        1
137 #define BMA150_EE_W__MSK                        0x10
138 #define BMA150_EE_W__REG                        BMA150_CTRL_REG
139
140 #define BMA150_UPDATE_IMAGE__POS        5
141 #define BMA150_UPDATE_IMAGE__LEN        1
142 #define BMA150_UPDATE_IMAGE__MSK        0x20
143 #define BMA150_UPDATE_IMAGE__REG        BMA150_CTRL_REG
144
145 #define BMA150_RESET_INT__POS           6
146 #define BMA150_RESET_INT__LEN           1
147 #define BMA150_RESET_INT__MSK           0x40
148 #define BMA150_RESET_INT__REG           BMA150_CTRL_REG
149
150 /* BANDWIDTH dependend definitions */
151
152 #define BMA150_BANDWIDTH__POS                           0
153 #define BMA150_BANDWIDTH__LEN                           3
154 #define BMA150_BANDWIDTH__MSK                           0x07
155 #define BMA150_BANDWIDTH__REG                           BMA150_RANGE_BWIDTH_REG
156
157 /* RANGE */
158
159 #define BMA150_RANGE__POS                               3
160 #define BMA150_RANGE__LEN                               2
161 #define BMA150_RANGE__MSK                               0x18
162 #define BMA150_RANGE__REG                               BMA150_RANGE_BWIDTH_REG
163
164 /* WAKE UP */
165
166 #define BMA150_WAKE_UP__POS                     0
167 #define BMA150_WAKE_UP__LEN                     1
168 #define BMA150_WAKE_UP__MSK                     0x01
169 #define BMA150_WAKE_UP__REG                     BMA150_CONF2_REG
170
171 #define BMA150_WAKE_UP_PAUSE__POS               1
172 #define BMA150_WAKE_UP_PAUSE__LEN               2
173 #define BMA150_WAKE_UP_PAUSE__MSK               0x06
174 #define BMA150_WAKE_UP_PAUSE__REG               BMA150_CONF2_REG
175
176 #define BMA150_GET_BITSLICE(regvar, bitname)\
177         ((regvar & bitname##__MSK) >> bitname##__POS)
178
179
180 #define BMA150_SET_BITSLICE(regvar, bitname, val)\
181         ((regvar & ~bitname##__MSK) | ((val<<bitname##__POS)&bitname##__MSK))
182
183 /* range and bandwidth */
184
185 #define BMA150_RANGE_2G                 0
186 #define BMA150_RANGE_4G                 1
187 #define BMA150_RANGE_8G                 2
188
189 #define BMA150_RANGE                    2000000
190 #define BMA150_PRECISION                10
191 #define BMA150_BOUNDARY                 (0x1 << (BMA150_PRECISION - 1))
192 #define BMA150_GRAVITY_STEP             BMA150_RANGE / BMA150_BOUNDARY
193
194 #define BMA150_BW_25HZ          0
195 #define BMA150_BW_50HZ          1
196 #define BMA150_BW_100HZ         2
197 #define BMA150_BW_190HZ         3
198 #define BMA150_BW_375HZ         4
199 #define BMA150_BW_750HZ         5
200 #define BMA150_BW_1500HZ        6
201
202 /* mode settings */
203
204 #define BMA150_MODE_NORMAL      0
205 #define BMA150_MODE_SLEEP       2
206 #define BMA150_MODE_WAKE_UP     3
207
208 struct bma150acc{
209         s64     x,
210                 y,
211                 z;
212 } ;
213 static struct  {
214         int x;
215         int y;
216         int z;
217 }sense_data;
218 struct bma150_data {
219         struct i2c_client *bma150_client;
220         atomic_t delay;
221         atomic_t enable;
222         unsigned char mode;
223         struct input_dev *input;
224         struct bma150acc value;
225         struct mutex value_mutex;
226         struct mutex enable_mutex;
227         struct mutex mode_mutex;
228         struct delayed_work work;
229         struct work_struct irq_work;
230         struct early_suspend early_suspend;
231 };
232 #define RBUFF_SIZE              12      /* Rx buffer size */
233 #define BMAIO                           0xA1
234
235 /* IOCTLs for MMA8452 library */
236 #define BMA_IOCTL_INIT                  _IO(BMAIO, 0x01)
237 #define BMA_IOCTL_RESET                   _IO(BMAIO, 0x04)
238 #define BMA_IOCTL_CLOSE                    _IO(BMAIO, 0x02)
239 #define BMA_IOCTL_START                      _IO(BMAIO, 0x03)
240 #define BMA_IOCTL_GETDATA               _IOR(BMAIO, 0x08, char[RBUFF_SIZE+1])
241
242 /* IOCTLs for APPs */
243 #define BMA_IOCTL_APP_SET_RATE          _IOW(BMAIO, 0x10, char)
244
245 static long bma023_ioctl( struct file *file, unsigned int cmd, unsigned long arg);
246
247 static void bma150_early_suspend(struct early_suspend *h);
248 static void bma150_late_resume(struct early_suspend *h);
249
250 static int bma150_smbus_read_byte(struct i2c_client *client,
251                 unsigned char reg_addr, unsigned char *data)
252 {
253         s32 dummy;
254         dummy = i2c_smbus_read_byte_data(client, reg_addr);
255         if (dummy < 0)
256                 return -1;
257         *data = dummy & 0x000000ff;
258
259         return 0;
260 }
261
262 static int bma150_smbus_write_byte(struct i2c_client *client,
263                 unsigned char reg_addr, unsigned char *data)
264 {
265         s32 dummy;
266         dummy = i2c_smbus_write_byte_data(client, reg_addr, *data);
267         if (dummy < 0)
268                 return -1;
269         return 0;
270 }
271
272 static int bma150_smbus_read_byte_block(struct i2c_client *client,
273                 unsigned char reg_addr, unsigned char *data, unsigned char len)
274 {
275         s32 dummy;
276         dummy = i2c_smbus_read_i2c_block_data(client, reg_addr, len, data);
277         if (dummy < 0)
278                 return -1;
279         return 0;
280 }
281
282 static int bma150_set_mode(struct i2c_client *client, unsigned char Mode)
283 {
284         int comres = 0;
285         unsigned char data1, data2;
286         struct bma150_data *bma150 = i2c_get_clientdata(client);
287
288         if (client == NULL) {
289                 comres = -1;
290         } else{
291                 if (Mode < 4 && Mode != 1) {
292
293                         comres = bma150_smbus_read_byte(client,
294                                                 BMA150_WAKE_UP__REG, &data1);
295                         data1 = BMA150_SET_BITSLICE(data1,
296                                                 BMA150_WAKE_UP, Mode);
297                         comres += bma150_smbus_read_byte(client,
298                                                 BMA150_SLEEP__REG, &data2);
299                         data2 = BMA150_SET_BITSLICE(data2,
300                                                 BMA150_SLEEP, (Mode>>1));
301                         comres += bma150_smbus_write_byte(client,
302                                                 BMA150_WAKE_UP__REG, &data1);
303                         comres += bma150_smbus_write_byte(client,
304                                                 BMA150_SLEEP__REG, &data2);
305                         mutex_lock(&bma150->mode_mutex);
306                         bma150->mode = (unsigned char) Mode;
307                         mutex_unlock(&bma150->mode_mutex);
308
309                 } else{
310                         comres = -1;
311                 }
312         }
313
314         return comres;
315 }
316
317
318 static int bma150_set_range(struct i2c_client *client, unsigned char Range)
319 {
320         int comres = 0;
321         unsigned char data;
322
323         if (client == NULL) {
324                 comres = -1;
325         } else{
326                 if (Range < 3) {
327
328                         comres = bma150_smbus_read_byte(client,
329                                                 BMA150_RANGE__REG, &data);
330                         data = BMA150_SET_BITSLICE(data, BMA150_RANGE, Range);
331                         comres += bma150_smbus_write_byte(client,
332                                                 BMA150_RANGE__REG, &data);
333
334                 } else{
335                         comres = -1;
336                 }
337         }
338
339         return comres;
340 }
341
342 static int bma150_get_range(struct i2c_client *client, unsigned char *Range)
343 {
344         int comres = 0;
345         unsigned char data;
346
347         if (client == NULL) {
348                 comres = -1;
349         } else{
350                 comres = bma150_smbus_read_byte(client,
351                                                 BMA150_RANGE__REG, &data);
352
353                 *Range = BMA150_GET_BITSLICE(data, BMA150_RANGE);
354
355         }
356
357         return comres;
358 }
359
360
361
362 static int bma150_set_bandwidth(struct i2c_client *client, unsigned char BW)
363 {
364         int comres = 0;
365         unsigned char data;
366
367         if (client == NULL) {
368                 comres = -1;
369         } else{
370                 if (BW < 8) {
371                         comres = bma150_smbus_read_byte(client,
372                                                 BMA150_BANDWIDTH__REG, &data);
373                         data = BMA150_SET_BITSLICE(data, BMA150_BANDWIDTH, BW);
374                         comres += bma150_smbus_write_byte(client,
375                                                 BMA150_BANDWIDTH__REG, &data);
376
377                 } else{
378                         comres = -1;
379                 }
380         }
381
382         return comres;
383 }
384
385 static int bma150_get_bandwidth(struct i2c_client *client, unsigned char *BW)
386 {
387         int comres = 0;
388         unsigned char data;
389
390         if (client == NULL) {
391                 comres = -1;
392         } else{
393
394
395                 comres = bma150_smbus_read_byte(client,
396                                                 BMA150_BANDWIDTH__REG, &data);
397
398                 *BW = BMA150_GET_BITSLICE(data, BMA150_BANDWIDTH);
399
400
401         }
402
403         return comres;
404 }
405
406 static int bma150_read_accel_xyz(struct i2c_client *client,
407                 struct bma150acc *acc)
408 {
409         int comres;
410         unsigned char data[6];
411         if (client == NULL) {
412                 comres = -1;
413         } else{
414
415
416                 comres = bma150_smbus_read_byte_block(client,
417                                         BMA150_ACC_X_LSB__REG, &data[0], 6);
418
419                 acc->x = BMA150_GET_BITSLICE(data[0], BMA150_ACC_X_LSB) |
420                         (BMA150_GET_BITSLICE(data[1], BMA150_ACC_X_MSB)<<
421                                                         BMA150_ACC_X_LSB__LEN); 
422                 if (acc->x < BMA150_BOUNDARY)
423                         acc->x = acc->x * BMA150_GRAVITY_STEP;
424                 else
425                         acc->x = ~( ((~acc->x & (0x7fff>>(16-BMA150_PRECISION)) ) + 1) 
426                                                 * BMA150_GRAVITY_STEP) + 1;
427 #if 0
428                 acc->x = acc->x << (sizeof(short)*8-(BMA150_ACC_X_LSB__LEN+
429                                                         BMA150_ACC_X_MSB__LEN));
430                 acc->x = acc->x >> (sizeof(short)*8-(BMA150_ACC_X_LSB__LEN+
431                                                         BMA150_ACC_X_MSB__LEN));
432 #endif
433
434                 acc->y = BMA150_GET_BITSLICE(data[2], BMA150_ACC_Y_LSB) |
435                         (BMA150_GET_BITSLICE(data[3], BMA150_ACC_Y_MSB)<<
436                                                         BMA150_ACC_Y_LSB__LEN);
437                 if (acc->y < BMA150_BOUNDARY)
438                         acc->y = acc->y * BMA150_GRAVITY_STEP;
439                 else
440                         acc->y = ~( ((~acc->y & (0x7fff>>(16-BMA150_PRECISION)) ) + 1) 
441                                                 * BMA150_GRAVITY_STEP) + 1;
442 #if 0
443                 acc->y = acc->y << (sizeof(short)*8-(BMA150_ACC_Y_LSB__LEN +
444                                                         BMA150_ACC_Y_MSB__LEN));
445                 acc->y = acc->y >> (sizeof(short)*8-(BMA150_ACC_Y_LSB__LEN +
446                                                         BMA150_ACC_Y_MSB__LEN));
447 #endif
448
449
450                 acc->z = BMA150_GET_BITSLICE(data[4], BMA150_ACC_Z_LSB);
451                 acc->z |= (BMA150_GET_BITSLICE(data[5], BMA150_ACC_Z_MSB)<<
452                                                         BMA150_ACC_Z_LSB__LEN);
453                 if (acc->z < BMA150_BOUNDARY)
454                         acc->z = acc->z * BMA150_GRAVITY_STEP;
455                 else
456                         acc->z = ~( ((~acc->z & (0x7fff>>(16-BMA150_PRECISION)) ) + 1) 
457                                                 * BMA150_GRAVITY_STEP) + 1;
458 #if 0
459                 acc->z = acc->z << (sizeof(short)*8-(BMA150_ACC_Z_LSB__LEN+
460                                                         BMA150_ACC_Z_MSB__LEN));
461                 acc->z = acc->z >> (sizeof(short)*8-(BMA150_ACC_Z_LSB__LEN+
462                                                         BMA150_ACC_Z_MSB__LEN));
463 #endif
464
465         }
466
467         return comres;
468 }
469
470 static void bma150_work_func(struct work_struct *work)
471 {
472         struct bma150_data *bma150 = container_of((struct delayed_work *)work,
473                         struct bma150_data, work);
474         static struct bma150acc acc;
475         s32     x,y,z;
476         unsigned long delay = msecs_to_jiffies(atomic_read(&bma150->delay));
477         struct bma023_platform_data *pdata = pdata = (bma150->bma150_client)->dev.platform_data;
478
479         bma150_read_accel_xyz(bma150->bma150_client, &acc);
480         if (pdata->swap_xyz) {
481                 x = (pdata->orientation[0])*acc.x + (pdata->orientation[1])*acc.y + (pdata->orientation[2])*acc.z;
482                 y = (pdata->orientation[3])*acc.x + (pdata->orientation[4])*acc.y + (pdata->orientation[5])*acc.z;
483                 z = (pdata->orientation[6])*acc.x + (pdata->orientation[7])*acc.y + (pdata->orientation[8])*acc.z;
484         }
485         else {
486                 x = acc.x;
487                 y = acc.y;
488                 z = acc.z;
489         }
490         input_report_abs(bma150->input, ABS_X, x);
491         input_report_abs(bma150->input, ABS_Y, y);
492         input_report_abs(bma150->input, ABS_Z, z);
493         input_sync(bma150->input);
494         mutex_lock(&bma150->value_mutex);
495         bma150->value.x = x;
496         bma150->value.y = y;
497         bma150->value.z = z;
498         mutex_unlock(&bma150->value_mutex);
499         DBG("bma150_work_func   acc.x=%d,acc.y=%d,acc.z=%d\n",acc.x,acc.y,acc.z);
500         schedule_delayed_work(&bma150->work, delay);
501
502 }
503 #if 0
504 static ssize_t bma150_mode_show(struct device *dev,
505                 struct device_attribute *attr, char *buf)
506 {
507         unsigned char data;
508         struct i2c_client *client = to_i2c_client(dev);
509         struct bma150_data *bma150 = i2c_get_clientdata(client);
510
511         mutex_lock(&bma150->mode_mutex);
512         data = bma150->mode;
513         mutex_unlock(&bma150->mode_mutex);
514
515         return sprintf(buf, "%d\n", data);
516 }
517
518 static ssize_t bma150_mode_store(struct device *dev,
519                 struct device_attribute *attr,
520                 const char *buf, size_t count)
521 {
522         unsigned long data;
523         int error;
524         struct i2c_client *client = to_i2c_client(dev);
525         struct bma150_data *bma150 = i2c_get_clientdata(client);
526
527         error = strict_strtoul(buf, 10, &data);
528         if (error)
529                 return error;
530         if (bma150_set_mode(bma150->bma150_client, (unsigned char) data) < 0)
531                 return -EINVAL;
532
533
534         return count;
535 }
536 static ssize_t bma150_range_show(struct device *dev,
537                 struct device_attribute *attr, char *buf)
538 {
539         unsigned char data;
540         struct i2c_client *client = to_i2c_client(dev);
541         struct bma150_data *bma150 = i2c_get_clientdata(client);
542
543         if (bma150_get_range(bma150->bma150_client, &data) < 0)
544                 return sprintf(buf, "Read error\n");
545
546         return sprintf(buf, "%d\n", data);
547 }
548
549 static ssize_t bma150_range_store(struct device *dev,
550                 struct device_attribute *attr,
551                 const char *buf, size_t count)
552 {
553         unsigned long data;
554         int error;
555         struct i2c_client *client = to_i2c_client(dev);
556         struct bma150_data *bma150 = i2c_get_clientdata(client);
557
558         error = strict_strtoul(buf, 10, &data);
559         if (error)
560                 return error;
561         if (bma150_set_range(bma150->bma150_client, (unsigned char) data) < 0)
562                 return -EINVAL;
563
564         return count;
565 }
566
567 static ssize_t bma150_bandwidth_show(struct device *dev,
568                 struct device_attribute *attr, char *buf)
569 {
570         unsigned char data;
571         struct i2c_client *client = to_i2c_client(dev);
572         struct bma150_data *bma150 = i2c_get_clientdata(client);
573
574         if (bma150_get_bandwidth(bma150->bma150_client, &data) < 0)
575                 return sprintf(buf, "Read error\n");
576
577         return sprintf(buf, "%d\n", data);
578
579 }
580
581 static ssize_t bma150_bandwidth_store(struct device *dev,
582                 struct device_attribute *attr,
583                 const char *buf, size_t count)
584 {
585         unsigned long data;
586         int error;
587         struct i2c_client *client = to_i2c_client(dev);
588         struct bma150_data *bma150 = i2c_get_clientdata(client);
589
590         error = strict_strtoul(buf, 10, &data);
591         if (error)
592                 return error;
593         if (bma150_set_bandwidth(bma150->bma150_client,
594                                 (unsigned char) data) < 0)
595                 return -EINVAL;
596
597         return count;
598 }
599
600 static ssize_t bma150_value_show(struct device *dev,
601                 struct device_attribute *attr, char *buf)
602 {
603         struct input_dev *input = to_input_dev(dev);
604         struct bma150_data *bma150 = input_get_drvdata(input);
605         struct bma150acc acc_value;
606
607         mutex_lock(&bma150->value_mutex);
608         acc_value = bma150->value;
609         mutex_unlock(&bma150->value_mutex);
610
611         return sprintf(buf, "%ll %ll %ll\n", acc_value.x, acc_value.y,
612                         acc_value.z);
613 }
614
615
616
617 static ssize_t bma150_delay_show(struct device *dev,
618                 struct device_attribute *attr, char *buf)
619 {
620         struct i2c_client *client = to_i2c_client(dev);
621         struct bma150_data *bma150 = i2c_get_clientdata(client);
622
623         return sprintf(buf, "%d\n", atomic_read(&bma150->delay));
624
625 }
626
627 static ssize_t bma150_delay_store(struct device *dev,
628                 struct device_attribute *attr,
629                 const char *buf, size_t count)
630 {
631         unsigned long data;
632         int error;
633         struct i2c_client *client = to_i2c_client(dev);
634         struct bma150_data *bma150 = i2c_get_clientdata(client);
635
636         error = strict_strtoul(buf, 10, &data);
637         if (error)
638                 return error;
639         if (data > BMA150_MAX_DELAY)
640                 data = BMA150_MAX_DELAY;
641         atomic_set(&bma150->delay, (unsigned int) data);
642
643         return count;
644 }
645
646 static ssize_t bma150_enable_show(struct device *dev,
647                 struct device_attribute *attr, char *buf)
648 {
649         struct i2c_client *client = to_i2c_client(dev);
650         struct bma150_data *bma150 = i2c_get_clientdata(client);
651
652         return sprintf(buf, "%d\n", atomic_read(&bma150->enable));
653
654 }
655
656 static void bma150_set_enable(struct device *dev, int enable)
657 {
658         struct i2c_client *client = to_i2c_client(dev);
659         struct bma150_data *bma150 = i2c_get_clientdata(client);
660         int pre_enable = atomic_read(&bma150->enable);
661
662         mutex_lock(&bma150->enable_mutex);
663         if (enable) {
664                 if (pre_enable ==0) {
665                         bma150_set_mode(bma150->bma150_client,
666                                                         BMA150_MODE_NORMAL);
667                         schedule_delayed_work(&bma150->work,
668                                 msecs_to_jiffies(atomic_read(&bma150->delay)));
669                         atomic_set(&bma150->enable, 1);
670                 }
671
672         } else {
673                 if (pre_enable ==1) {
674                         bma150_set_mode(bma150->bma150_client,
675                                                         BMA150_MODE_SLEEP);
676                         cancel_delayed_work_sync(&bma150->work);
677                         atomic_set(&bma150->enable, 0);
678                 }
679         }
680         mutex_unlock(&bma150->enable_mutex);
681
682 }
683
684 static ssize_t bma150_enable_store(struct device *dev,
685                 struct device_attribute *attr,
686                 const char *buf, size_t count)
687 {
688         unsigned long data;
689         int error;
690
691         error = strict_strtoul(buf, 10, &data);
692         if (error)
693                 return error;
694         if ((data == 0)||(data==1)) {
695                 bma150_set_enable(dev,data);
696         }
697
698         return count;
699 }
700
701 static DEVICE_ATTR(range, S_IRUGO|S_IWUSR|S_IWGRP|S_IWOTH,
702                 bma150_range_show, bma150_range_store);
703 static DEVICE_ATTR(bandwidth, S_IRUGO|S_IWUSR|S_IWGRP|S_IWOTH,
704                 bma150_bandwidth_show, bma150_bandwidth_store);
705 static DEVICE_ATTR(mode, S_IRUGO|S_IWUSR|S_IWGRP|S_IWOTH,
706                 bma150_mode_show, bma150_mode_store);
707 static DEVICE_ATTR(value, S_IRUGO|S_IWUSR|S_IWGRP,
708                 bma150_value_show, NULL);
709 static DEVICE_ATTR(delay, S_IRUGO|S_IWUSR|S_IWGRP|S_IWOTH,
710                 bma150_delay_show, bma150_delay_store);
711 static DEVICE_ATTR(enable, S_IRUGO|S_IWUSR|S_IWGRP|S_IWOTH,
712                 bma150_enable_show, bma150_enable_store);
713
714 static struct attribute *bma150_attributes[] = {
715         &dev_attr_range.attr,
716         &dev_attr_bandwidth.attr,
717         &dev_attr_mode.attr,
718         &dev_attr_value.attr,
719         &dev_attr_delay.attr,
720         &dev_attr_enable.attr,
721         NULL
722 };
723
724 static struct attribute_group bma150_attribute_group = {
725         .attrs = bma150_attributes
726 };
727 #endif
728 static int bma150_input_init(struct bma150_data *bma150)
729 {
730         struct input_dev *dev;
731         int err;
732
733         dev = input_allocate_device();
734         if (!dev)
735                 return -ENOMEM;
736         dev->name = "gsensor";//SENSOR_NAME;
737         dev->id.bustype = BUS_I2C;
738
739         input_set_capability(dev, EV_ABS, ABS_MISC);
740         input_set_abs_params(dev, ABS_X, ABSMIN_2G, ABSMAX_2G, 0, 0);
741         input_set_abs_params(dev, ABS_Y, ABSMIN_2G, ABSMAX_2G, 0, 0);
742         input_set_abs_params(dev, ABS_Z, ABSMIN_2G, ABSMAX_2G, 0, 0);
743         input_set_drvdata(dev, bma150);
744
745         err = input_register_device(dev);
746         if (err < 0) {
747                 input_free_device(dev);
748                 return err;
749         }
750         bma150->input = dev;
751
752         return 0;
753 }
754
755 static void bma150_input_delete(struct bma150_data *bma150)
756 {
757         struct input_dev *dev = bma150->input;
758
759         input_unregister_device(dev);
760         input_free_device(dev);
761 }
762
763
764 static int bma023_open(struct inode *inode, struct file *file)
765 {
766         printk("%s\n",__FUNCTION__);
767         return 0;//nonseekable_open(inode, file);
768 }
769
770 static int bma023_release(struct inode *inode, struct file *file)
771 {
772         return 0;
773 }
774 static struct file_operations bma023_fops = {
775         .owner = THIS_MODULE,
776         .open = bma023_open,
777         .release = bma023_release,
778         .unlocked_ioctl = bma023_ioctl,
779 };
780 static struct miscdevice bma023_device = {
781         .minor = MISC_DYNAMIC_MINOR,
782         .name = "mma8452_daemon",//"mma8452_daemon",
783         .fops = &bma023_fops,
784 };
785
786
787 static bma023_enable(struct i2c_client *client, int enable)
788 {
789         
790         struct bma150_data *bma150 = i2c_get_clientdata(client);
791         int pre_enable = atomic_read(&bma150->enable);
792         
793         mutex_lock(&bma150->enable_mutex);
794         
795         if(enable)
796         {
797                 if(pre_enable==0)
798                 {
799                         bma150_set_mode(client,BMA150_MODE_NORMAL);
800                         schedule_delayed_work(&bma150->work,
801                                 msecs_to_jiffies(atomic_read(&bma150->delay)));
802                         atomic_set(&bma150->enable, 1);
803                 }
804         }
805         else    
806         {
807                 bma150_set_mode(client,BMA150_MODE_SLEEP);
808                 cancel_delayed_work_sync(&bma150->work);
809                 atomic_set(&bma150->enable, 0);
810         }
811         
812         mutex_unlock(&bma150->enable_mutex);
813
814 }
815
816
817 static long bma023_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
818 {
819         void __user *argp = (void __user *)arg;
820
821         struct i2c_client *client = container_of(bma023_device.parent, struct i2c_client, dev);
822         struct bma150_data *bma150 = i2c_get_clientdata(client);;
823         switch (cmd) {
824         case BMA_IOCTL_START:
825                 bma023_enable(client, 1);
826                 DBG("%s:%d,cmd=BMA_IOCTL_START\n",__FUNCTION__,__LINE__);
827                 break;
828
829         case BMA_IOCTL_CLOSE:
830                 bma023_enable(client, 0);               
831                 DBG("%s:%d,cmd=BMA_IOCTL_CLOSE\n",__FUNCTION__,__LINE__);
832                 break;
833
834         case BMA_IOCTL_APP_SET_RATE:    
835                 atomic_set(&bma150->delay, 20);//20ms   
836                 DBG("%s:%d,cmd=BMA_IOCTL_APP_SET_RATE\n",__FUNCTION__,__LINE__);
837                 break;
838                 
839         case BMA_IOCTL_GETDATA:
840                 mutex_lock(&bma150->value_mutex);
841                 if(abs(sense_data.x-bma150->value.x)>40000)//·À¶¶¶¯
842                         sense_data.x=bma150->value.x;
843                 if(abs(sense_data.y-(bma150->value.y))>40000)//·À¶¶¶¯
844                         sense_data.y=bma150->value.y;
845                 if(abs(sense_data.z-(bma150->value.z))>40000)//·À¶¶¶¯
846                         sense_data.z=bma150->value.z;
847                //bma150->value = acc;
848                 mutex_unlock(&bma150->value_mutex);
849
850                 if ( copy_to_user(argp, &sense_data, sizeof(sense_data) ) ) {
851             printk("failed to copy sense data to user space.");
852                         return -EFAULT;
853         }
854
855                 DBG("%s:%d,cmd=BMA_IOCTL_GETDATA\n",__FUNCTION__,__LINE__);
856                         break;
857         default:
858                 printk("%s:%d,error,cmd=%x\n",__FUNCTION__,__LINE__,cmd);
859                 break;
860                 }
861         return 0;
862 }
863
864 static int bma150_probe(struct i2c_client *client,
865                 const struct i2c_device_id *id)
866 {
867         int err = 0;
868         int tempvalue;
869         struct bma150_data *data;
870         printk(KERN_INFO "bma150_probe  \n");
871         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
872                 printk(KERN_INFO "i2c_check_functionality error\n");
873                 goto exit;
874         }
875         data = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
876         if (!data) {
877                 err = -ENOMEM;
878                 goto exit;
879         }
880
881         tempvalue = 0;
882         tempvalue = i2c_smbus_read_word_data(client, BMA150_CHIP_ID_REG);
883
884         if ((tempvalue&0x00FF) == BMA150_CHIP_ID) {
885                 printk(KERN_INFO "Bosch Sensortec Device detected!\n" \
886                                 "BMA150 registered I2C driver!\n");
887         } else{
888                 printk(KERN_INFO "Bosch Sensortec Device not found" \
889                                 "i2c error %d \n", tempvalue);
890                 err = -1;
891                 goto kfree_exit;
892         }
893         i2c_set_clientdata(client, data);
894         data->bma150_client = client;
895         mutex_init(&data->value_mutex);
896         mutex_init(&data->mode_mutex);
897         mutex_init(&data->enable_mutex);
898         bma150_set_bandwidth(client, BMA150_BW_SET);
899         bma150_set_range(client, BMA150_RANGE_SET);
900
901
902         INIT_DELAYED_WORK(&data->work, bma150_work_func);
903         atomic_set(&data->delay, BMA150_MAX_DELAY);
904         atomic_set(&data->enable, 0);
905         err = bma150_input_init(data);
906         if (err < 0)
907                 goto kfree_exit;
908 #if 0
909         err = sysfs_create_group(&data->input->dev.kobj,
910                         &bma150_attribute_group);
911         if (err < 0)
912                 goto error_sysfs;
913 #endif
914         data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
915         data->early_suspend.suspend = bma150_early_suspend;
916         data->early_suspend.resume = bma150_late_resume;
917         register_early_suspend(&data->early_suspend);
918         bma023_device.parent = &client->dev;
919         misc_register(&bma023_device);
920         return 0;
921
922 //error_sysfs:
923 //      bma150_input_delete(data);
924
925 kfree_exit:
926         kfree(data);
927 exit:
928         return err;
929 }
930
931
932 static int bma150_remove(struct i2c_client *client)
933 {
934         struct bma150_data *data = i2c_get_clientdata(client);
935
936         bma023_enable(&client->dev, 0);
937         unregister_early_suspend(&data->early_suspend);
938         //sysfs_remove_group(&data->input->dev.kobj, &bma150_attribute_group);
939         bma150_input_delete(data);
940         kfree(data);
941
942         return 0;
943 }
944
945
946
947
948 static void bma150_early_suspend(struct early_suspend *h)
949 {
950         struct bma150_data *data =
951                 container_of(h, struct bma150_data, early_suspend);
952
953         mutex_lock(&data->enable_mutex);
954         if (atomic_read(&data->enable)==1) {
955                 bma150_set_mode(data->bma150_client, BMA150_MODE_SLEEP);
956                 cancel_delayed_work_sync(&data->work);
957         }
958         mutex_unlock(&data->enable_mutex);
959 }
960
961
962 static void bma150_late_resume(struct early_suspend *h)
963 {
964         struct bma150_data *data =
965                 container_of(h, struct bma150_data, early_suspend);
966
967         mutex_lock(&data->enable_mutex);
968         if (atomic_read(&data->enable)==1) {
969                 bma150_set_mode(data->bma150_client, BMA150_MODE_NORMAL);
970                 schedule_delayed_work(&data->work,
971                         msecs_to_jiffies(atomic_read(&data->delay)));
972         }
973         mutex_unlock(&data->enable_mutex);
974 }
975
976 static const struct i2c_device_id bma150_id[] = {
977         { SENSOR_NAME, 0 },
978         { }
979 };
980
981 MODULE_DEVICE_TABLE(i2c, bma150_id);
982
983 static struct i2c_driver bma150_driver = {
984         .driver = {
985                 .owner  = THIS_MODULE,
986                 .name   = SENSOR_NAME,
987         },
988         .id_table       = bma150_id,
989         .probe          = bma150_probe,
990         .remove         = bma150_remove,
991
992 };
993
994 static int __init BMA150_init(void)
995 {
996         return i2c_add_driver(&bma150_driver);
997 }
998
999 static void __exit BMA150_exit(void)
1000 {
1001         i2c_del_driver(&bma150_driver);
1002 }
1003
1004 MODULE_AUTHOR("Lan Bin Yuan <lby@rock-chips.com>");
1005 MODULE_DESCRIPTION("BMA150 driver");
1006 MODULE_LICENSE("GPL");
1007
1008 module_init(BMA150_init);
1009 module_exit(BMA150_exit);