Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 1 | /* Date: 2011/3/7 11:00:00 |
| 2 | * Revision: 2.11 |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * This software program is licensed subject to the GNU General Public License |
| 7 | * (GPL).Version 2,June 1991, available at http://www.fsf.org/copyleft/gpl.html |
| 8 | |
| 9 | * (C) Copyright 2011 Bosch Sensortec GmbH |
| 10 | * All Rights Reserved |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | /* file BMA150.c |
| 15 | brief This file contains all function implementations for the BMA150 in linux |
| 16 | |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/input.h> |
| 24 | #include <linux/workqueue.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/bma150.h> |
| 28 | |
| 29 | #define SENSOR_NAME "bma150" |
| 30 | #define GRAVITY_EARTH 9806550 |
| 31 | #define ABSMIN_2G (-GRAVITY_EARTH * 2) |
| 32 | #define ABSMAX_2G (GRAVITY_EARTH * 2) |
| 33 | #define BMA150_MAX_DELAY 200 |
| 34 | #define BMA150_CHIP_ID 2 |
| 35 | #define BMA150_RANGE_SET 0 |
| 36 | #define BMA150_BW_SET 4 |
| 37 | |
| 38 | |
| 39 | |
| 40 | #define BMA150_CHIP_ID_REG 0x00 |
| 41 | #define BMA150_X_AXIS_LSB_REG 0x02 |
| 42 | #define BMA150_X_AXIS_MSB_REG 0x03 |
| 43 | #define BMA150_Y_AXIS_LSB_REG 0x04 |
| 44 | #define BMA150_Y_AXIS_MSB_REG 0x05 |
| 45 | #define BMA150_Z_AXIS_LSB_REG 0x06 |
| 46 | #define BMA150_Z_AXIS_MSB_REG 0x07 |
| 47 | #define BMA150_STATUS_REG 0x09 |
| 48 | #define BMA150_CTRL_REG 0x0a |
| 49 | #define BMA150_CONF1_REG 0x0b |
| 50 | |
| 51 | #define BMA150_CUSTOMER1_REG 0x12 |
| 52 | #define BMA150_CUSTOMER2_REG 0x13 |
| 53 | #define BMA150_RANGE_BWIDTH_REG 0x14 |
| 54 | #define BMA150_CONF2_REG 0x15 |
| 55 | |
| 56 | #define BMA150_OFFS_GAIN_X_REG 0x16 |
| 57 | #define BMA150_OFFS_GAIN_Y_REG 0x17 |
| 58 | #define BMA150_OFFS_GAIN_Z_REG 0x18 |
| 59 | #define BMA150_OFFS_GAIN_T_REG 0x19 |
| 60 | #define BMA150_OFFSET_X_REG 0x1a |
| 61 | #define BMA150_OFFSET_Y_REG 0x1b |
| 62 | #define BMA150_OFFSET_Z_REG 0x1c |
| 63 | #define BMA150_OFFSET_T_REG 0x1d |
| 64 | |
| 65 | #define BMA150_CHIP_ID__POS 0 |
| 66 | #define BMA150_CHIP_ID__MSK 0x07 |
| 67 | #define BMA150_CHIP_ID__LEN 3 |
| 68 | #define BMA150_CHIP_ID__REG BMA150_CHIP_ID_REG |
| 69 | |
| 70 | /* DATA REGISTERS */ |
| 71 | |
| 72 | #define BMA150_NEW_DATA_X__POS 0 |
| 73 | #define BMA150_NEW_DATA_X__LEN 1 |
| 74 | #define BMA150_NEW_DATA_X__MSK 0x01 |
| 75 | #define BMA150_NEW_DATA_X__REG BMA150_X_AXIS_LSB_REG |
| 76 | |
| 77 | #define BMA150_ACC_X_LSB__POS 6 |
| 78 | #define BMA150_ACC_X_LSB__LEN 2 |
| 79 | #define BMA150_ACC_X_LSB__MSK 0xC0 |
| 80 | #define BMA150_ACC_X_LSB__REG BMA150_X_AXIS_LSB_REG |
| 81 | |
| 82 | #define BMA150_ACC_X_MSB__POS 0 |
| 83 | #define BMA150_ACC_X_MSB__LEN 8 |
| 84 | #define BMA150_ACC_X_MSB__MSK 0xFF |
| 85 | #define BMA150_ACC_X_MSB__REG BMA150_X_AXIS_MSB_REG |
| 86 | |
| 87 | #define BMA150_ACC_Y_LSB__POS 6 |
| 88 | #define BMA150_ACC_Y_LSB__LEN 2 |
| 89 | #define BMA150_ACC_Y_LSB__MSK 0xC0 |
| 90 | #define BMA150_ACC_Y_LSB__REG BMA150_Y_AXIS_LSB_REG |
| 91 | |
| 92 | #define BMA150_ACC_Y_MSB__POS 0 |
| 93 | #define BMA150_ACC_Y_MSB__LEN 8 |
| 94 | #define BMA150_ACC_Y_MSB__MSK 0xFF |
| 95 | #define BMA150_ACC_Y_MSB__REG BMA150_Y_AXIS_MSB_REG |
| 96 | |
| 97 | #define BMA150_ACC_Z_LSB__POS 6 |
| 98 | #define BMA150_ACC_Z_LSB__LEN 2 |
| 99 | #define BMA150_ACC_Z_LSB__MSK 0xC0 |
| 100 | #define BMA150_ACC_Z_LSB__REG BMA150_Z_AXIS_LSB_REG |
| 101 | |
| 102 | #define BMA150_ACC_Z_MSB__POS 0 |
| 103 | #define BMA150_ACC_Z_MSB__LEN 8 |
| 104 | #define BMA150_ACC_Z_MSB__MSK 0xFF |
| 105 | #define BMA150_ACC_Z_MSB__REG BMA150_Z_AXIS_MSB_REG |
| 106 | |
| 107 | /* CONTROL BITS */ |
| 108 | |
| 109 | #define BMA150_SLEEP__POS 0 |
| 110 | #define BMA150_SLEEP__LEN 1 |
| 111 | #define BMA150_SLEEP__MSK 0x01 |
| 112 | #define BMA150_SLEEP__REG BMA150_CTRL_REG |
| 113 | |
| 114 | #define BMA150_SOFT_RESET__POS 1 |
| 115 | #define BMA150_SOFT_RESET__LEN 1 |
| 116 | #define BMA150_SOFT_RESET__MSK 0x02 |
| 117 | #define BMA150_SOFT_RESET__REG BMA150_CTRL_REG |
| 118 | |
| 119 | #define BMA150_EE_W__POS 4 |
| 120 | #define BMA150_EE_W__LEN 1 |
| 121 | #define BMA150_EE_W__MSK 0x10 |
| 122 | #define BMA150_EE_W__REG BMA150_CTRL_REG |
| 123 | |
| 124 | #define BMA150_UPDATE_IMAGE__POS 5 |
| 125 | #define BMA150_UPDATE_IMAGE__LEN 1 |
| 126 | #define BMA150_UPDATE_IMAGE__MSK 0x20 |
| 127 | #define BMA150_UPDATE_IMAGE__REG BMA150_CTRL_REG |
| 128 | |
| 129 | #define BMA150_RESET_INT__POS 6 |
| 130 | #define BMA150_RESET_INT__LEN 1 |
| 131 | #define BMA150_RESET_INT__MSK 0x40 |
| 132 | #define BMA150_RESET_INT__REG BMA150_CTRL_REG |
| 133 | |
| 134 | /* BANDWIDTH dependend definitions */ |
| 135 | |
| 136 | #define BMA150_BANDWIDTH__POS 0 |
| 137 | #define BMA150_BANDWIDTH__LEN 3 |
| 138 | #define BMA150_BANDWIDTH__MSK 0x07 |
| 139 | #define BMA150_BANDWIDTH__REG BMA150_RANGE_BWIDTH_REG |
| 140 | |
| 141 | /* RANGE */ |
| 142 | |
| 143 | #define BMA150_RANGE__POS 3 |
| 144 | #define BMA150_RANGE__LEN 2 |
| 145 | #define BMA150_RANGE__MSK 0x18 |
| 146 | #define BMA150_RANGE__REG BMA150_RANGE_BWIDTH_REG |
| 147 | |
| 148 | /* WAKE UP */ |
| 149 | |
| 150 | #define BMA150_WAKE_UP__POS 0 |
| 151 | #define BMA150_WAKE_UP__LEN 1 |
| 152 | #define BMA150_WAKE_UP__MSK 0x01 |
| 153 | #define BMA150_WAKE_UP__REG BMA150_CONF2_REG |
| 154 | |
| 155 | #define BMA150_WAKE_UP_PAUSE__POS 1 |
| 156 | #define BMA150_WAKE_UP_PAUSE__LEN 2 |
| 157 | #define BMA150_WAKE_UP_PAUSE__MSK 0x06 |
| 158 | #define BMA150_WAKE_UP_PAUSE__REG BMA150_CONF2_REG |
| 159 | |
| 160 | #define BMA150_GET_BITSLICE(regvar, bitname)\ |
| 161 | ((regvar & bitname##__MSK) >> bitname##__POS) |
| 162 | |
| 163 | |
| 164 | #define BMA150_SET_BITSLICE(regvar, bitname, val)\ |
| 165 | ((regvar & ~bitname##__MSK) | ((val<<bitname##__POS)&bitname##__MSK)) |
| 166 | |
| 167 | /* range and bandwidth */ |
| 168 | |
| 169 | #define BMA150_RANGE_2G 0 |
| 170 | #define BMA150_RANGE_4G 1 |
| 171 | #define BMA150_RANGE_8G 2 |
| 172 | |
| 173 | #define BMA150_BW_25HZ 0 |
| 174 | #define BMA150_BW_50HZ 1 |
| 175 | #define BMA150_BW_100HZ 2 |
| 176 | #define BMA150_BW_190HZ 3 |
| 177 | #define BMA150_BW_375HZ 4 |
| 178 | #define BMA150_BW_750HZ 5 |
| 179 | #define BMA150_BW_1500HZ 6 |
| 180 | |
| 181 | /* mode settings */ |
| 182 | |
| 183 | #define BMA150_MODE_NORMAL 0 |
| 184 | #define BMA150_MODE_SLEEP 2 |
| 185 | #define BMA150_MODE_WAKE_UP 3 |
| 186 | |
| 187 | struct bma150acc{ |
| 188 | s16 x, |
| 189 | y, |
| 190 | z; |
| 191 | } ; |
| 192 | |
| 193 | struct bma150_data { |
| 194 | struct i2c_client *bma150_client; |
| 195 | struct bma150_platform_data *platform_data; |
| 196 | int IRQ; |
| 197 | atomic_t delay; |
| 198 | unsigned char mode; |
| 199 | struct input_dev *input; |
| 200 | struct bma150acc value; |
| 201 | struct mutex value_mutex; |
| 202 | struct mutex mode_mutex; |
| 203 | struct delayed_work work; |
| 204 | struct work_struct irq_work; |
| 205 | }; |
| 206 | |
| 207 | static int bma150_smbus_read_byte(struct i2c_client *client, |
| 208 | unsigned char reg_addr, unsigned char *data) |
| 209 | { |
| 210 | s32 dummy; |
| 211 | dummy = i2c_smbus_read_byte_data(client, reg_addr); |
| 212 | if (dummy < 0) |
| 213 | return -EPERM; |
| 214 | *data = dummy & 0x000000ff; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int bma150_smbus_write_byte(struct i2c_client *client, |
| 220 | unsigned char reg_addr, unsigned char *data) |
| 221 | { |
| 222 | s32 dummy; |
| 223 | dummy = i2c_smbus_write_byte_data(client, reg_addr, *data); |
| 224 | if (dummy < 0) |
| 225 | return -EPERM; |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int bma150_smbus_read_byte_block(struct i2c_client *client, |
| 230 | unsigned char reg_addr, unsigned char *data, unsigned char len) |
| 231 | { |
| 232 | s32 dummy; |
| 233 | dummy = i2c_smbus_read_i2c_block_data(client, reg_addr, len, data); |
| 234 | if (dummy < 0) |
| 235 | return -EPERM; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int bma150_set_mode(struct i2c_client *client, unsigned char Mode) |
| 240 | { |
| 241 | int comres = 0; |
| 242 | unsigned char data1 = 0, data2 = 0; |
| 243 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 244 | |
| 245 | if (client == NULL) { |
| 246 | comres = -1; |
| 247 | } else{ |
| 248 | if (Mode < 4 && Mode != 1) { |
| 249 | |
| 250 | comres = bma150_smbus_read_byte(client, |
| 251 | BMA150_WAKE_UP__REG, &data1); |
| 252 | data1 = BMA150_SET_BITSLICE(data1, |
| 253 | BMA150_WAKE_UP, Mode); |
| 254 | comres += bma150_smbus_read_byte(client, |
| 255 | BMA150_SLEEP__REG, &data2); |
| 256 | data2 = BMA150_SET_BITSLICE(data2, |
| 257 | BMA150_SLEEP, (Mode>>1)); |
| 258 | comres += bma150_smbus_write_byte(client, |
| 259 | BMA150_WAKE_UP__REG, &data1); |
| 260 | comres += bma150_smbus_write_byte(client, |
| 261 | BMA150_SLEEP__REG, &data2); |
| 262 | mutex_lock(&bma150->mode_mutex); |
| 263 | bma150->mode = (unsigned char) Mode; |
| 264 | mutex_unlock(&bma150->mode_mutex); |
| 265 | |
| 266 | } else{ |
| 267 | comres = -1; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return comres; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static int bma150_set_range(struct i2c_client *client, unsigned char Range) |
| 276 | { |
| 277 | int comres = 0; |
| 278 | unsigned char data = 0; |
| 279 | |
| 280 | if (client == NULL) { |
| 281 | comres = -1; |
| 282 | } else{ |
| 283 | if (Range < 3) { |
| 284 | |
| 285 | comres = bma150_smbus_read_byte(client, |
| 286 | BMA150_RANGE__REG, &data); |
| 287 | data = BMA150_SET_BITSLICE(data, BMA150_RANGE, Range); |
| 288 | comres += bma150_smbus_write_byte(client, |
| 289 | BMA150_RANGE__REG, &data); |
| 290 | |
| 291 | } else{ |
| 292 | comres = -1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return comres; |
| 297 | } |
| 298 | |
| 299 | static int bma150_get_range(struct i2c_client *client, unsigned char *Range) |
| 300 | { |
| 301 | int comres = 0; |
| 302 | unsigned char data; |
| 303 | |
| 304 | if (client == NULL) { |
| 305 | comres = -1; |
| 306 | } else{ |
| 307 | comres = bma150_smbus_read_byte(client, |
| 308 | BMA150_RANGE__REG, &data); |
| 309 | |
| 310 | *Range = BMA150_GET_BITSLICE(data, BMA150_RANGE); |
| 311 | |
| 312 | } |
| 313 | |
| 314 | return comres; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | static int bma150_set_bandwidth(struct i2c_client *client, unsigned char BW) |
| 320 | { |
| 321 | int comres = 0; |
| 322 | unsigned char data = 0; |
| 323 | |
| 324 | if (client == NULL) { |
| 325 | comres = -1; |
| 326 | } else{ |
| 327 | if (BW < 8) { |
| 328 | comres = bma150_smbus_read_byte(client, |
| 329 | BMA150_BANDWIDTH__REG, &data); |
| 330 | data = BMA150_SET_BITSLICE(data, BMA150_BANDWIDTH, BW); |
| 331 | comres += bma150_smbus_write_byte(client, |
| 332 | BMA150_BANDWIDTH__REG, &data); |
| 333 | |
| 334 | } else{ |
| 335 | comres = -1; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return comres; |
| 340 | } |
| 341 | |
| 342 | static int bma150_get_bandwidth(struct i2c_client *client, unsigned char *BW) |
| 343 | { |
| 344 | int comres = 0; |
| 345 | unsigned char data; |
| 346 | |
| 347 | if (client == NULL) { |
| 348 | comres = -1; |
| 349 | } else{ |
| 350 | |
| 351 | |
| 352 | comres = bma150_smbus_read_byte(client, |
| 353 | BMA150_BANDWIDTH__REG, &data); |
| 354 | |
| 355 | *BW = BMA150_GET_BITSLICE(data, BMA150_BANDWIDTH); |
| 356 | |
| 357 | |
| 358 | } |
| 359 | |
| 360 | return comres; |
| 361 | } |
| 362 | |
| 363 | static int bma150_read_accel_xyz(struct i2c_client *client, |
| 364 | struct bma150acc *acc) |
| 365 | { |
| 366 | int comres; |
| 367 | unsigned char data[6]; |
| 368 | if (client == NULL) { |
| 369 | comres = -1; |
| 370 | } else{ |
| 371 | |
| 372 | |
| 373 | comres = bma150_smbus_read_byte_block(client, |
| 374 | BMA150_ACC_X_LSB__REG, &data[0], 6); |
| 375 | |
| 376 | acc->x = BMA150_GET_BITSLICE(data[0], BMA150_ACC_X_LSB) | |
| 377 | (BMA150_GET_BITSLICE(data[1], BMA150_ACC_X_MSB)<< |
| 378 | BMA150_ACC_X_LSB__LEN); |
| 379 | acc->x = acc->x << (sizeof(short)*8-(BMA150_ACC_X_LSB__LEN+ |
| 380 | BMA150_ACC_X_MSB__LEN)); |
| 381 | acc->x = acc->x >> (sizeof(short)*8-(BMA150_ACC_X_LSB__LEN+ |
| 382 | BMA150_ACC_X_MSB__LEN)); |
| 383 | |
| 384 | acc->y = BMA150_GET_BITSLICE(data[2], BMA150_ACC_Y_LSB) | |
| 385 | (BMA150_GET_BITSLICE(data[3], BMA150_ACC_Y_MSB)<< |
| 386 | BMA150_ACC_Y_LSB__LEN); |
| 387 | acc->y = acc->y << (sizeof(short)*8-(BMA150_ACC_Y_LSB__LEN + |
| 388 | BMA150_ACC_Y_MSB__LEN)); |
| 389 | acc->y = acc->y >> (sizeof(short)*8-(BMA150_ACC_Y_LSB__LEN + |
| 390 | BMA150_ACC_Y_MSB__LEN)); |
| 391 | |
| 392 | |
| 393 | acc->z = BMA150_GET_BITSLICE(data[4], BMA150_ACC_Z_LSB); |
| 394 | acc->z |= (BMA150_GET_BITSLICE(data[5], BMA150_ACC_Z_MSB)<< |
| 395 | BMA150_ACC_Z_LSB__LEN); |
| 396 | acc->z = acc->z << (sizeof(short)*8-(BMA150_ACC_Z_LSB__LEN+ |
| 397 | BMA150_ACC_Z_MSB__LEN)); |
| 398 | acc->z = acc->z >> (sizeof(short)*8-(BMA150_ACC_Z_LSB__LEN+ |
| 399 | BMA150_ACC_Z_MSB__LEN)); |
| 400 | |
| 401 | } |
| 402 | |
| 403 | return comres; |
| 404 | } |
| 405 | |
| 406 | static void bma150_work_func(struct work_struct *work) |
| 407 | { |
| 408 | struct bma150_data *bma150 = container_of((struct delayed_work *)work, |
| 409 | struct bma150_data, work); |
| 410 | static struct bma150acc acc; |
| 411 | unsigned long delay = msecs_to_jiffies(atomic_read(&bma150->delay)); |
| 412 | |
| 413 | |
| 414 | |
| 415 | bma150_read_accel_xyz(bma150->bma150_client, &acc); |
| 416 | input_report_abs(bma150->input, ABS_X, acc.x); |
| 417 | input_report_abs(bma150->input, ABS_Y, acc.y); |
| 418 | input_report_abs(bma150->input, ABS_Z, acc.z); |
| 419 | input_sync(bma150->input); |
| 420 | mutex_lock(&bma150->value_mutex); |
| 421 | bma150->value = acc; |
| 422 | mutex_unlock(&bma150->value_mutex); |
| 423 | schedule_delayed_work(&bma150->work, delay); |
| 424 | } |
| 425 | |
| 426 | static ssize_t bma150_mode_show(struct device *dev, |
| 427 | struct device_attribute *attr, char *buf) |
| 428 | { |
| 429 | unsigned char data; |
| 430 | struct i2c_client *client = to_i2c_client(dev); |
| 431 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 432 | |
| 433 | mutex_lock(&bma150->mode_mutex); |
| 434 | data = bma150->mode; |
| 435 | mutex_unlock(&bma150->mode_mutex); |
| 436 | |
| 437 | return sprintf(buf, "%d\n", data); |
| 438 | } |
| 439 | |
| 440 | static ssize_t bma150_mode_store(struct device *dev, |
| 441 | struct device_attribute *attr, |
| 442 | const char *buf, size_t count) |
| 443 | { |
| 444 | unsigned long data; |
| 445 | int error; |
| 446 | struct i2c_client *client = to_i2c_client(dev); |
| 447 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 448 | |
| 449 | error = strict_strtoul(buf, 10, &data); |
| 450 | if (error) |
| 451 | return error; |
| 452 | if (bma150_set_mode(bma150->bma150_client, (unsigned char) data) < 0) |
| 453 | return -EINVAL; |
| 454 | |
| 455 | |
| 456 | return count; |
| 457 | } |
| 458 | static ssize_t bma150_range_show(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
| 460 | { |
| 461 | unsigned char data; |
| 462 | struct i2c_client *client = to_i2c_client(dev); |
| 463 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 464 | |
| 465 | if (bma150_get_range(bma150->bma150_client, &data) < 0) |
| 466 | return sprintf(buf, "Read error\n"); |
| 467 | |
| 468 | return sprintf(buf, "%d\n", data); |
| 469 | } |
| 470 | |
| 471 | static ssize_t bma150_range_store(struct device *dev, |
| 472 | struct device_attribute *attr, |
| 473 | const char *buf, size_t count) |
| 474 | { |
| 475 | unsigned long data; |
| 476 | int error; |
| 477 | struct i2c_client *client = to_i2c_client(dev); |
| 478 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 479 | |
| 480 | error = strict_strtoul(buf, 10, &data); |
| 481 | if (error) |
| 482 | return error; |
| 483 | if (bma150_set_range(bma150->bma150_client, (unsigned char) data) < 0) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | return count; |
| 487 | } |
| 488 | |
| 489 | static ssize_t bma150_bandwidth_show(struct device *dev, |
| 490 | struct device_attribute *attr, char *buf) |
| 491 | { |
| 492 | unsigned char data; |
| 493 | struct i2c_client *client = to_i2c_client(dev); |
| 494 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 495 | |
| 496 | if (bma150_get_bandwidth(bma150->bma150_client, &data) < 0) |
| 497 | return sprintf(buf, "Read error\n"); |
| 498 | |
| 499 | return sprintf(buf, "%d\n", data); |
| 500 | |
| 501 | } |
| 502 | |
| 503 | static ssize_t bma150_bandwidth_store(struct device *dev, |
| 504 | struct device_attribute *attr, |
| 505 | const char *buf, size_t count) |
| 506 | { |
| 507 | unsigned long data; |
| 508 | int error; |
| 509 | struct i2c_client *client = to_i2c_client(dev); |
| 510 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 511 | |
| 512 | error = strict_strtoul(buf, 10, &data); |
| 513 | if (error) |
| 514 | return error; |
| 515 | if (bma150_set_bandwidth(bma150->bma150_client, |
| 516 | (unsigned char) data) < 0) |
| 517 | return -EINVAL; |
| 518 | |
| 519 | return count; |
| 520 | } |
| 521 | |
| 522 | static ssize_t bma150_value_show(struct device *dev, |
| 523 | struct device_attribute *attr, char *buf) |
| 524 | { |
| 525 | struct input_dev *input = to_input_dev(dev); |
| 526 | struct bma150_data *bma150 = input_get_drvdata(input); |
| 527 | struct bma150acc acc_value; |
| 528 | |
| 529 | mutex_lock(&bma150->value_mutex); |
| 530 | acc_value = bma150->value; |
| 531 | mutex_unlock(&bma150->value_mutex); |
| 532 | |
| 533 | return sprintf(buf, "%d %d %d\n", acc_value.x, acc_value.y, |
| 534 | acc_value.z); |
| 535 | } |
| 536 | |
| 537 | |
| 538 | |
| 539 | static ssize_t bma150_delay_show(struct device *dev, |
| 540 | struct device_attribute *attr, char *buf) |
| 541 | { |
| 542 | struct i2c_client *client = to_i2c_client(dev); |
| 543 | struct bma150_data *bma150 = i2c_get_clientdata(client); |
| 544 | |
| 545 | return sprintf(buf, "%d\n", atomic_read(&bma150->delay)); |
| 546 | |
| 547 | } |
| 548 | |
| 549 | static ssize_t bma150_delay_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 (data > BMA150_MAX_DELAY) |
| 562 | data = BMA150_MAX_DELAY; |
| 563 | atomic_set(&bma150->delay, (unsigned int) data); |
| 564 | |
| 565 | return count; |
| 566 | } |
| 567 | |
| 568 | static DEVICE_ATTR(range, S_IRUGO|S_IWUSR|S_IWGRP, |
| 569 | bma150_range_show, bma150_range_store); |
| 570 | static DEVICE_ATTR(bandwidth, S_IRUGO|S_IWUSR|S_IWGRP, |
| 571 | bma150_bandwidth_show, bma150_bandwidth_store); |
| 572 | static DEVICE_ATTR(mode, S_IRUGO|S_IWUSR|S_IWGRP, |
| 573 | bma150_mode_show, bma150_mode_store); |
| 574 | static DEVICE_ATTR(value, S_IRUGO|S_IWUSR|S_IWGRP, |
| 575 | bma150_value_show, NULL); |
| 576 | static DEVICE_ATTR(delay, S_IRUGO|S_IWUSR|S_IWGRP|S_IWOTH, |
| 577 | bma150_delay_show, bma150_delay_store); |
| 578 | |
| 579 | static struct attribute *bma150_attributes[] = { |
| 580 | &dev_attr_range.attr, |
| 581 | &dev_attr_bandwidth.attr, |
| 582 | &dev_attr_mode.attr, |
| 583 | &dev_attr_value.attr, |
| 584 | &dev_attr_delay.attr, |
| 585 | NULL |
| 586 | }; |
| 587 | |
| 588 | static struct attribute_group bma150_attribute_group = { |
| 589 | .attrs = bma150_attributes |
| 590 | }; |
| 591 | |
| 592 | static int bma150_detect(struct i2c_client *client, |
| 593 | struct i2c_board_info *info) |
| 594 | { |
| 595 | struct i2c_adapter *adapter = client->adapter; |
| 596 | |
| 597 | if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) |
| 598 | return -ENODEV; |
| 599 | |
| 600 | strlcpy(info->type, SENSOR_NAME, I2C_NAME_SIZE); |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static int bma150_input_init(struct bma150_data *bma150) |
| 606 | { |
| 607 | struct input_dev *dev; |
| 608 | int err; |
| 609 | |
| 610 | dev = input_allocate_device(); |
| 611 | if (!dev) |
| 612 | return -ENOMEM; |
| 613 | dev->name = SENSOR_NAME; |
| 614 | dev->id.bustype = BUS_I2C; |
| 615 | |
| 616 | input_set_capability(dev, EV_ABS, ABS_MISC); |
| 617 | input_set_abs_params(dev, ABS_X, ABSMIN_2G, ABSMAX_2G, 0, 0); |
| 618 | input_set_abs_params(dev, ABS_Y, ABSMIN_2G, ABSMAX_2G, 0, 0); |
| 619 | input_set_abs_params(dev, ABS_Z, ABSMIN_2G, ABSMAX_2G, 0, 0); |
| 620 | input_set_drvdata(dev, bma150); |
| 621 | |
| 622 | err = input_register_device(dev); |
| 623 | if (err < 0) { |
| 624 | input_free_device(dev); |
| 625 | return err; |
| 626 | } |
| 627 | bma150->input = dev; |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static void bma150_input_delete(struct bma150_data *bma150) |
| 633 | { |
| 634 | struct input_dev *dev = bma150->input; |
| 635 | |
| 636 | input_unregister_device(dev); |
| 637 | input_free_device(dev); |
| 638 | } |
| 639 | |
| 640 | static int bma150_probe(struct i2c_client *client, |
| 641 | const struct i2c_device_id *id) |
| 642 | { |
| 643 | int err = 0; |
| 644 | int tempvalue; |
| 645 | struct bma150_data *data; |
| 646 | |
| 647 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 648 | printk(KERN_INFO "i2c_check_functionality error\n"); |
| 649 | goto exit; |
| 650 | } |
| 651 | data = kzalloc(sizeof(struct bma150_data), GFP_KERNEL); |
| 652 | if (!data) { |
| 653 | err = -ENOMEM; |
| 654 | goto exit; |
| 655 | } |
| 656 | |
| 657 | i2c_set_clientdata(client, data); |
| 658 | data->platform_data = client->dev.platform_data; |
| 659 | |
| 660 | if (data->platform_data->power_on) |
| 661 | data->platform_data->power_on(); |
| 662 | else |
| 663 | printk(KERN_ERR "power_on function not defined!!\n"); |
| 664 | |
| 665 | tempvalue = 0; |
| 666 | tempvalue = i2c_smbus_read_word_data(client, BMA150_CHIP_ID_REG); |
| 667 | |
| 668 | if ((tempvalue&0x00FF) == BMA150_CHIP_ID) { |
| 669 | printk(KERN_INFO "Bosch Sensortec Device detected!\n" \ |
| 670 | "BMA150 registered I2C driver!\n"); |
| 671 | } else{ |
| 672 | printk(KERN_INFO "Bosch Sensortec Device not found" \ |
| 673 | "i2c error %d\n", tempvalue); |
| 674 | err = -1; |
| 675 | goto kfree_exit; |
| 676 | } |
| 677 | i2c_set_clientdata(client, data); |
| 678 | data->bma150_client = client; |
| 679 | mutex_init(&data->value_mutex); |
| 680 | mutex_init(&data->mode_mutex); |
| 681 | bma150_set_bandwidth(client, BMA150_BW_SET); |
| 682 | bma150_set_range(client, BMA150_RANGE_SET); |
| 683 | |
| 684 | |
| 685 | INIT_DELAYED_WORK(&data->work, bma150_work_func); |
| 686 | atomic_set(&data->delay, BMA150_MAX_DELAY); |
| 687 | err = bma150_input_init(data); |
| 688 | if (err < 0) |
| 689 | goto kfree_exit; |
| 690 | |
| 691 | err = sysfs_create_group(&data->input->dev.kobj, |
| 692 | &bma150_attribute_group); |
| 693 | if (err < 0) |
| 694 | goto error_sysfs; |
| 695 | |
| 696 | schedule_delayed_work(&data->work, |
| 697 | msecs_to_jiffies(atomic_read(&data->delay))); |
| 698 | |
| 699 | return 0; |
| 700 | |
| 701 | error_sysfs: |
| 702 | bma150_input_delete(data); |
| 703 | |
| 704 | kfree_exit: |
| 705 | kfree(data); |
| 706 | exit: |
| 707 | return err; |
| 708 | } |
| 709 | |
| 710 | static int bma150_suspend(struct i2c_client *client, pm_message_t mesg) |
| 711 | { |
| 712 | struct bma150_data *data = i2c_get_clientdata(client); |
| 713 | |
| 714 | cancel_delayed_work_sync(&data->work); |
| 715 | |
| 716 | bma150_set_mode(client, BMA150_MODE_SLEEP); |
| 717 | |
| 718 | if ((data->platform_data) && (data->platform_data->power_off)) |
| 719 | data->platform_data->power_off(); |
| 720 | |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | static int bma150_resume(struct i2c_client *client) |
| 725 | { |
| 726 | struct bma150_data *data = i2c_get_clientdata(client); |
| 727 | |
| 728 | if ((data->platform_data) && (data->platform_data->power_on)) |
| 729 | data->platform_data->power_on(); |
| 730 | |
| 731 | bma150_set_mode(client, BMA150_MODE_NORMAL); |
| 732 | |
| 733 | schedule_delayed_work(&data->work, |
| 734 | msecs_to_jiffies(atomic_read(&data->delay))); |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int bma150_remove(struct i2c_client *client) |
| 740 | { |
| 741 | struct bma150_data *data = i2c_get_clientdata(client); |
| 742 | |
| 743 | if (data->platform_data->power_off) |
| 744 | data->platform_data->power_off(); |
| 745 | else |
| 746 | printk(KERN_ERR "power_off function not defined!!\n"); |
| 747 | |
| 748 | sysfs_remove_group(&data->input->dev.kobj, &bma150_attribute_group); |
| 749 | bma150_input_delete(data); |
| 750 | free_irq(data->IRQ, data); |
| 751 | kfree(data); |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static const struct i2c_device_id bma150_id[] = { |
| 757 | { SENSOR_NAME, 0 }, |
| 758 | { } |
| 759 | }; |
| 760 | |
| 761 | MODULE_DEVICE_TABLE(i2c, bma150_id); |
| 762 | |
| 763 | static struct i2c_driver bma150_driver = { |
| 764 | .driver = { |
| 765 | .owner = THIS_MODULE, |
| 766 | .name = SENSOR_NAME, |
| 767 | }, |
| 768 | .class = I2C_CLASS_HWMON, |
| 769 | .id_table = bma150_id, |
| 770 | .probe = bma150_probe, |
| 771 | .remove = bma150_remove, |
| 772 | .detect = bma150_detect, |
| 773 | .suspend = bma150_suspend, |
| 774 | .resume = bma150_resume, |
| 775 | }; |
| 776 | |
| 777 | static int __init BMA150_init(void) |
| 778 | { |
| 779 | return i2c_add_driver(&bma150_driver); |
| 780 | } |
| 781 | |
| 782 | static void __exit BMA150_exit(void) |
| 783 | { |
| 784 | i2c_del_driver(&bma150_driver); |
| 785 | } |
| 786 | |
| 787 | MODULE_DESCRIPTION("BMA150 driver"); |
| 788 | |
| 789 | module_init(BMA150_init); |
| 790 | module_exit(BMA150_exit); |
| 791 | |