1 /* drivers/i2c/chips/bma023.c - bma023 compass driver
3 * Copyright (C) 2007-2008 HTC Corporation.
4 * Author: Hou-Kun Chen <houkun.chen@gmail.com>
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.
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.
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>
39 #define DBG(x...) printk(x)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
150 /* BANDWIDTH dependend definitions */
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
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
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
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
176 #define BMA150_GET_BITSLICE(regvar, bitname)\
177 ((regvar & bitname##__MSK) >> bitname##__POS)
180 #define BMA150_SET_BITSLICE(regvar, bitname, val)\
181 ((regvar & ~bitname##__MSK) | ((val<<bitname##__POS)&bitname##__MSK))
183 /* range and bandwidth */
185 #define BMA150_RANGE_2G 0
186 #define BMA150_RANGE_4G 1
187 #define BMA150_RANGE_8G 2
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
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
204 #define BMA150_MODE_NORMAL 0
205 #define BMA150_MODE_SLEEP 2
206 #define BMA150_MODE_WAKE_UP 3
219 struct i2c_client *bma150_client;
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;
232 #define RBUFF_SIZE 12 /* Rx buffer size */
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])
242 /* IOCTLs for APPs */
243 #define BMA_IOCTL_APP_SET_RATE _IOW(BMAIO, 0x10, char)
245 static long bma023_ioctl( struct file *file, unsigned int cmd, unsigned long arg);
247 static void bma150_early_suspend(struct early_suspend *h);
248 static void bma150_late_resume(struct early_suspend *h);
250 static int bma150_smbus_read_byte(struct i2c_client *client,
251 unsigned char reg_addr, unsigned char *data)
254 dummy = i2c_smbus_read_byte_data(client, reg_addr);
257 *data = dummy & 0x000000ff;
262 static int bma150_smbus_write_byte(struct i2c_client *client,
263 unsigned char reg_addr, unsigned char *data)
266 dummy = i2c_smbus_write_byte_data(client, reg_addr, *data);
272 static int bma150_smbus_read_byte_block(struct i2c_client *client,
273 unsigned char reg_addr, unsigned char *data, unsigned char len)
276 dummy = i2c_smbus_read_i2c_block_data(client, reg_addr, len, data);
282 static int bma150_set_mode(struct i2c_client *client, unsigned char Mode)
285 unsigned char data1, data2;
286 struct bma150_data *bma150 = i2c_get_clientdata(client);
288 if (client == NULL) {
291 if (Mode < 4 && Mode != 1) {
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);
318 static int bma150_set_range(struct i2c_client *client, unsigned char Range)
323 if (client == NULL) {
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);
342 static int bma150_get_range(struct i2c_client *client, unsigned char *Range)
347 if (client == NULL) {
350 comres = bma150_smbus_read_byte(client,
351 BMA150_RANGE__REG, &data);
353 *Range = BMA150_GET_BITSLICE(data, BMA150_RANGE);
362 static int bma150_set_bandwidth(struct i2c_client *client, unsigned char BW)
367 if (client == NULL) {
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);
385 static int bma150_get_bandwidth(struct i2c_client *client, unsigned char *BW)
390 if (client == NULL) {
395 comres = bma150_smbus_read_byte(client,
396 BMA150_BANDWIDTH__REG, &data);
398 *BW = BMA150_GET_BITSLICE(data, BMA150_BANDWIDTH);
406 static int bma150_read_accel_xyz(struct i2c_client *client,
407 struct bma150acc *acc)
410 unsigned char data[6];
411 if (client == NULL) {
416 comres = bma150_smbus_read_byte_block(client,
417 BMA150_ACC_X_LSB__REG, &data[0], 6);
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;
425 acc->x = ~( ((~acc->x & (0x7fff>>(16-BMA150_PRECISION)) ) + 1)
426 * BMA150_GRAVITY_STEP) + 1;
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));
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;
440 acc->y = ~( ((~acc->y & (0x7fff>>(16-BMA150_PRECISION)) ) + 1)
441 * BMA150_GRAVITY_STEP) + 1;
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));
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;
456 acc->z = ~( ((~acc->z & (0x7fff>>(16-BMA150_PRECISION)) ) + 1)
457 * BMA150_GRAVITY_STEP) + 1;
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));
470 static void bma150_work_func(struct work_struct *work)
472 struct bma150_data *bma150 = container_of((struct delayed_work *)work,
473 struct bma150_data, work);
474 static struct bma150acc acc;
476 unsigned long delay = msecs_to_jiffies(atomic_read(&bma150->delay));
477 struct bma023_platform_data *pdata = pdata = (bma150->bma150_client)->dev.platform_data;
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;
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);
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);
504 static ssize_t bma150_mode_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
508 struct i2c_client *client = to_i2c_client(dev);
509 struct bma150_data *bma150 = i2c_get_clientdata(client);
511 mutex_lock(&bma150->mode_mutex);
513 mutex_unlock(&bma150->mode_mutex);
515 return sprintf(buf, "%d\n", data);
518 static ssize_t bma150_mode_store(struct device *dev,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
524 struct i2c_client *client = to_i2c_client(dev);
525 struct bma150_data *bma150 = i2c_get_clientdata(client);
527 error = strict_strtoul(buf, 10, &data);
530 if (bma150_set_mode(bma150->bma150_client, (unsigned char) data) < 0)
536 static ssize_t bma150_range_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
540 struct i2c_client *client = to_i2c_client(dev);
541 struct bma150_data *bma150 = i2c_get_clientdata(client);
543 if (bma150_get_range(bma150->bma150_client, &data) < 0)
544 return sprintf(buf, "Read error\n");
546 return sprintf(buf, "%d\n", data);
549 static ssize_t bma150_range_store(struct device *dev,
550 struct device_attribute *attr,
551 const char *buf, size_t count)
555 struct i2c_client *client = to_i2c_client(dev);
556 struct bma150_data *bma150 = i2c_get_clientdata(client);
558 error = strict_strtoul(buf, 10, &data);
561 if (bma150_set_range(bma150->bma150_client, (unsigned char) data) < 0)
567 static ssize_t bma150_bandwidth_show(struct device *dev,
568 struct device_attribute *attr, char *buf)
571 struct i2c_client *client = to_i2c_client(dev);
572 struct bma150_data *bma150 = i2c_get_clientdata(client);
574 if (bma150_get_bandwidth(bma150->bma150_client, &data) < 0)
575 return sprintf(buf, "Read error\n");
577 return sprintf(buf, "%d\n", data);
581 static ssize_t bma150_bandwidth_store(struct device *dev,
582 struct device_attribute *attr,
583 const char *buf, size_t count)
587 struct i2c_client *client = to_i2c_client(dev);
588 struct bma150_data *bma150 = i2c_get_clientdata(client);
590 error = strict_strtoul(buf, 10, &data);
593 if (bma150_set_bandwidth(bma150->bma150_client,
594 (unsigned char) data) < 0)
600 static ssize_t bma150_value_show(struct device *dev,
601 struct device_attribute *attr, char *buf)
603 struct input_dev *input = to_input_dev(dev);
604 struct bma150_data *bma150 = input_get_drvdata(input);
605 struct bma150acc acc_value;
607 mutex_lock(&bma150->value_mutex);
608 acc_value = bma150->value;
609 mutex_unlock(&bma150->value_mutex);
611 return sprintf(buf, "%ll %ll %ll\n", acc_value.x, acc_value.y,
617 static ssize_t bma150_delay_show(struct device *dev,
618 struct device_attribute *attr, char *buf)
620 struct i2c_client *client = to_i2c_client(dev);
621 struct bma150_data *bma150 = i2c_get_clientdata(client);
623 return sprintf(buf, "%d\n", atomic_read(&bma150->delay));
627 static ssize_t bma150_delay_store(struct device *dev,
628 struct device_attribute *attr,
629 const char *buf, size_t count)
633 struct i2c_client *client = to_i2c_client(dev);
634 struct bma150_data *bma150 = i2c_get_clientdata(client);
636 error = strict_strtoul(buf, 10, &data);
639 if (data > BMA150_MAX_DELAY)
640 data = BMA150_MAX_DELAY;
641 atomic_set(&bma150->delay, (unsigned int) data);
646 static ssize_t bma150_enable_show(struct device *dev,
647 struct device_attribute *attr, char *buf)
649 struct i2c_client *client = to_i2c_client(dev);
650 struct bma150_data *bma150 = i2c_get_clientdata(client);
652 return sprintf(buf, "%d\n", atomic_read(&bma150->enable));
656 static void bma150_set_enable(struct device *dev, int enable)
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);
662 mutex_lock(&bma150->enable_mutex);
664 if (pre_enable ==0) {
665 bma150_set_mode(bma150->bma150_client,
667 schedule_delayed_work(&bma150->work,
668 msecs_to_jiffies(atomic_read(&bma150->delay)));
669 atomic_set(&bma150->enable, 1);
673 if (pre_enable ==1) {
674 bma150_set_mode(bma150->bma150_client,
676 cancel_delayed_work_sync(&bma150->work);
677 atomic_set(&bma150->enable, 0);
680 mutex_unlock(&bma150->enable_mutex);
684 static ssize_t bma150_enable_store(struct device *dev,
685 struct device_attribute *attr,
686 const char *buf, size_t count)
691 error = strict_strtoul(buf, 10, &data);
694 if ((data == 0)||(data==1)) {
695 bma150_set_enable(dev,data);
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);
714 static struct attribute *bma150_attributes[] = {
715 &dev_attr_range.attr,
716 &dev_attr_bandwidth.attr,
718 &dev_attr_value.attr,
719 &dev_attr_delay.attr,
720 &dev_attr_enable.attr,
724 static struct attribute_group bma150_attribute_group = {
725 .attrs = bma150_attributes
728 static int bma150_input_init(struct bma150_data *bma150)
730 struct input_dev *dev;
733 dev = input_allocate_device();
736 dev->name = "gsensor";//SENSOR_NAME;
737 dev->id.bustype = BUS_I2C;
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);
745 err = input_register_device(dev);
747 input_free_device(dev);
755 static void bma150_input_delete(struct bma150_data *bma150)
757 struct input_dev *dev = bma150->input;
759 input_unregister_device(dev);
760 input_free_device(dev);
764 static int bma023_open(struct inode *inode, struct file *file)
766 printk("%s\n",__FUNCTION__);
767 return 0;//nonseekable_open(inode, file);
770 static int bma023_release(struct inode *inode, struct file *file)
774 static struct file_operations bma023_fops = {
775 .owner = THIS_MODULE,
777 .release = bma023_release,
778 .unlocked_ioctl = bma023_ioctl,
780 static struct miscdevice bma023_device = {
781 .minor = MISC_DYNAMIC_MINOR,
782 .name = "mma8452_daemon",//"mma8452_daemon",
783 .fops = &bma023_fops,
787 static bma023_enable(struct i2c_client *client, int enable)
790 struct bma150_data *bma150 = i2c_get_clientdata(client);
791 int pre_enable = atomic_read(&bma150->enable);
793 mutex_lock(&bma150->enable_mutex);
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);
807 bma150_set_mode(client,BMA150_MODE_SLEEP);
808 cancel_delayed_work_sync(&bma150->work);
809 atomic_set(&bma150->enable, 0);
812 mutex_unlock(&bma150->enable_mutex);
817 static long bma023_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
819 void __user *argp = (void __user *)arg;
821 struct i2c_client *client = container_of(bma023_device.parent, struct i2c_client, dev);
822 struct bma150_data *bma150 = i2c_get_clientdata(client);;
824 case BMA_IOCTL_START:
825 bma023_enable(client, 1);
826 DBG("%s:%d,cmd=BMA_IOCTL_START\n",__FUNCTION__,__LINE__);
829 case BMA_IOCTL_CLOSE:
830 bma023_enable(client, 0);
831 DBG("%s:%d,cmd=BMA_IOCTL_CLOSE\n",__FUNCTION__,__LINE__);
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__);
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);
850 if ( copy_to_user(argp, &sense_data, sizeof(sense_data) ) ) {
851 printk("failed to copy sense data to user space.");
855 DBG("%s:%d,cmd=BMA_IOCTL_GETDATA\n",__FUNCTION__,__LINE__);
858 printk("%s:%d,error,cmd=%x\n",__FUNCTION__,__LINE__,cmd);
864 static int bma150_probe(struct i2c_client *client,
865 const struct i2c_device_id *id)
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");
875 data = kzalloc(sizeof(struct bma150_data), GFP_KERNEL);
882 tempvalue = i2c_smbus_read_word_data(client, BMA150_CHIP_ID_REG);
884 if ((tempvalue&0x00FF) == BMA150_CHIP_ID) {
885 printk(KERN_INFO "Bosch Sensortec Device detected!\n" \
886 "BMA150 registered I2C driver!\n");
888 printk(KERN_INFO "Bosch Sensortec Device not found" \
889 "i2c error %d \n", tempvalue);
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);
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);
909 err = sysfs_create_group(&data->input->dev.kobj,
910 &bma150_attribute_group);
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);
923 // bma150_input_delete(data);
932 static int bma150_remove(struct i2c_client *client)
934 struct bma150_data *data = i2c_get_clientdata(client);
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);
948 static void bma150_early_suspend(struct early_suspend *h)
950 struct bma150_data *data =
951 container_of(h, struct bma150_data, early_suspend);
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);
958 mutex_unlock(&data->enable_mutex);
962 static void bma150_late_resume(struct early_suspend *h)
964 struct bma150_data *data =
965 container_of(h, struct bma150_data, early_suspend);
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)));
973 mutex_unlock(&data->enable_mutex);
976 static const struct i2c_device_id bma150_id[] = {
981 MODULE_DEVICE_TABLE(i2c, bma150_id);
983 static struct i2c_driver bma150_driver = {
985 .owner = THIS_MODULE,
988 .id_table = bma150_id,
989 .probe = bma150_probe,
990 .remove = bma150_remove,
994 static int __init BMA150_init(void)
996 return i2c_add_driver(&bma150_driver);
999 static void __exit BMA150_exit(void)
1001 i2c_del_driver(&bma150_driver);
1004 MODULE_AUTHOR("Lan Bin Yuan <lby@rock-chips.com>");
1005 MODULE_DESCRIPTION("BMA150 driver");
1006 MODULE_LICENSE("GPL");
1008 module_init(BMA150_init);
1009 module_exit(BMA150_exit);