Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * bma180.c - IIO driver for Bosch BMA180 triaxial acceleration sensor |
| 3 | * |
| 4 | * Copyright 2013 Oleksandr Kravchenko <x0199363@ti.com> |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of version 2 of |
| 7 | * the GNU General Public License. See the file COPYING in the main |
| 8 | * directory of this archive for more details. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/of.h> |
| 16 | #include <linux/bitops.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/sysfs.h> |
| 21 | #include <linux/iio/buffer.h> |
| 22 | #include <linux/iio/trigger.h> |
| 23 | #include <linux/iio/trigger_consumer.h> |
| 24 | #include <linux/iio/triggered_buffer.h> |
| 25 | |
| 26 | #define BMA180_DRV_NAME "bma180" |
| 27 | #define BMA180_IRQ_NAME "bma180_event" |
| 28 | |
| 29 | /* Register set */ |
| 30 | #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */ |
| 31 | #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */ |
| 32 | #define BMA180_CTRL_REG0 0x0d |
| 33 | #define BMA180_RESET 0x10 |
| 34 | #define BMA180_BW_TCS 0x20 |
| 35 | #define BMA180_CTRL_REG3 0x21 |
| 36 | #define BMA180_TCO_Z 0x30 |
| 37 | #define BMA180_OFFSET_LSB1 0x35 |
| 38 | |
| 39 | /* BMA180_CTRL_REG0 bits */ |
| 40 | #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */ |
| 41 | #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */ |
| 42 | #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */ |
| 43 | #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */ |
| 44 | |
| 45 | /* BMA180_CTRL_REG3 bits */ |
| 46 | #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */ |
| 47 | |
| 48 | /* BMA180_OFFSET_LSB1 skipping mode bit */ |
| 49 | #define BMA180_SMP_SKIP BIT(0) |
| 50 | |
| 51 | /* Bit masks for registers bit fields */ |
| 52 | #define BMA180_RANGE 0x0e /* Range of measured accel values*/ |
| 53 | #define BMA180_BW 0xf0 /* Accel bandwidth */ |
| 54 | #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */ |
| 55 | |
| 56 | /* We have to write this value in reset register to do soft reset */ |
| 57 | #define BMA180_RESET_VAL 0xb6 |
| 58 | |
| 59 | #define BMA_180_ID_REG_VAL 0x03 |
| 60 | |
| 61 | /* Chip power modes */ |
| 62 | #define BMA180_LOW_NOISE 0x00 |
| 63 | #define BMA180_LOW_POWER 0x03 |
| 64 | |
| 65 | #define BMA180_LOW_NOISE_STR "low_noise" |
| 66 | #define BMA180_LOW_POWER_STR "low_power" |
| 67 | |
| 68 | /* Defaults values */ |
| 69 | #define BMA180_DEF_PMODE 0 |
| 70 | #define BMA180_DEF_BW 20 |
| 71 | #define BMA180_DEF_SCALE 250 |
| 72 | |
| 73 | /* Available values for sysfs */ |
| 74 | #define BMA180_FLP_FREQ_AVAILABLE \ |
| 75 | "10 20 40 75 150 300" |
| 76 | #define BMA180_SCALE_AVAILABLE \ |
| 77 | "0.000130 0.000190 0.000250 0.000380 0.000500 0.000990 0.001980" |
| 78 | |
| 79 | struct bma180_data { |
| 80 | struct i2c_client *client; |
| 81 | struct iio_trigger *trig; |
| 82 | struct mutex mutex; |
| 83 | int sleep_state; |
| 84 | int scale; |
| 85 | int bw; |
| 86 | int pmode; |
| 87 | char *buff; |
| 88 | }; |
| 89 | |
| 90 | enum bma180_axis { |
| 91 | AXIS_X, |
| 92 | AXIS_Y, |
| 93 | AXIS_Z, |
| 94 | }; |
| 95 | |
| 96 | static int bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */ |
| 97 | static int scale_table[] = { 130, 190, 250, 380, 500, 990, 1980 }; |
| 98 | |
| 99 | static int bma180_get_acc_reg(struct bma180_data *data, enum bma180_axis axis) |
| 100 | { |
| 101 | u8 reg = BMA180_ACC_X_LSB + axis * 2; |
| 102 | int ret; |
| 103 | |
| 104 | if (data->sleep_state) |
| 105 | return -EBUSY; |
| 106 | |
| 107 | ret = i2c_smbus_read_word_data(data->client, reg); |
| 108 | if (ret < 0) |
| 109 | dev_err(&data->client->dev, |
| 110 | "failed to read accel_%c registers\n", 'x' + axis); |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val) |
| 116 | { |
| 117 | int ret = i2c_smbus_read_byte_data(data->client, reg); |
| 118 | u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1)); |
| 119 | |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | |
| 123 | return i2c_smbus_write_byte_data(data->client, reg, reg_val); |
| 124 | } |
| 125 | |
| 126 | static int bma180_reset_intr(struct bma180_data *data) |
| 127 | { |
| 128 | int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_RESET_INT, 1); |
| 129 | |
| 130 | if (ret) |
| 131 | dev_err(&data->client->dev, "failed to reset interrupt\n"); |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static int bma180_set_new_data_intr_state(struct bma180_data *data, int state) |
| 137 | { |
| 138 | u8 reg_val = state ? BMA180_NEW_DATA_INT : 0x00; |
| 139 | int ret = i2c_smbus_write_byte_data(data->client, BMA180_CTRL_REG3, |
| 140 | reg_val); |
| 141 | |
| 142 | if (ret) |
| 143 | goto err; |
| 144 | ret = bma180_reset_intr(data); |
| 145 | if (ret) |
| 146 | goto err; |
| 147 | |
| 148 | return 0; |
| 149 | |
| 150 | err: |
| 151 | dev_err(&data->client->dev, |
| 152 | "failed to set new data interrupt state %d\n", state); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static int bma180_set_sleep_state(struct bma180_data *data, int state) |
| 157 | { |
| 158 | int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_SLEEP, state); |
| 159 | |
| 160 | if (ret) { |
| 161 | dev_err(&data->client->dev, |
| 162 | "failed to set sleep state %d\n", state); |
| 163 | return ret; |
| 164 | } |
| 165 | data->sleep_state = state; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int bma180_set_ee_writing_state(struct bma180_data *data, int state) |
| 171 | { |
| 172 | int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state); |
| 173 | |
| 174 | if (ret) |
| 175 | dev_err(&data->client->dev, |
| 176 | "failed to set ee writing state %d\n", state); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static int bma180_set_bw(struct bma180_data *data, int val) |
| 182 | { |
| 183 | int ret, i; |
| 184 | |
| 185 | if (data->sleep_state) |
| 186 | return -EBUSY; |
| 187 | |
| 188 | for (i = 0; i < ARRAY_SIZE(bw_table); ++i) { |
| 189 | if (bw_table[i] == val) { |
| 190 | ret = bma180_set_bits(data, |
| 191 | BMA180_BW_TCS, BMA180_BW, i); |
| 192 | if (ret) { |
| 193 | dev_err(&data->client->dev, |
| 194 | "failed to set bandwidth\n"); |
| 195 | return ret; |
| 196 | } |
| 197 | data->bw = val; |
| 198 | return 0; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | static int bma180_set_scale(struct bma180_data *data, int val) |
| 206 | { |
| 207 | int ret, i; |
| 208 | |
| 209 | if (data->sleep_state) |
| 210 | return -EBUSY; |
| 211 | |
| 212 | for (i = 0; i < ARRAY_SIZE(scale_table); ++i) |
| 213 | if (scale_table[i] == val) { |
| 214 | ret = bma180_set_bits(data, |
| 215 | BMA180_OFFSET_LSB1, BMA180_RANGE, i); |
| 216 | if (ret) { |
| 217 | dev_err(&data->client->dev, |
| 218 | "failed to set scale\n"); |
| 219 | return ret; |
| 220 | } |
| 221 | data->scale = val; |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | static int bma180_set_pmode(struct bma180_data *data, int mode) |
| 229 | { |
| 230 | u8 reg_val = mode ? BMA180_LOW_POWER : BMA180_LOW_NOISE; |
| 231 | int ret = bma180_set_bits(data, BMA180_TCO_Z, BMA180_MODE_CONFIG, |
| 232 | reg_val); |
| 233 | |
| 234 | if (ret) { |
| 235 | dev_err(&data->client->dev, "failed to set power mode\n"); |
| 236 | return ret; |
| 237 | } |
| 238 | data->pmode = mode; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int bma180_soft_reset(struct bma180_data *data) |
| 244 | { |
| 245 | int ret = i2c_smbus_write_byte_data(data->client, |
| 246 | BMA180_RESET, BMA180_RESET_VAL); |
| 247 | |
| 248 | if (ret) |
| 249 | dev_err(&data->client->dev, "failed to reset the chip\n"); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static int bma180_chip_init(struct bma180_data *data) |
| 255 | { |
| 256 | /* Try to read chip_id register. It must return 0x03. */ |
| 257 | int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID); |
| 258 | |
| 259 | if (ret < 0) |
| 260 | goto err; |
| 261 | if (ret != BMA_180_ID_REG_VAL) { |
| 262 | ret = -ENODEV; |
| 263 | goto err; |
| 264 | } |
| 265 | |
| 266 | ret = bma180_soft_reset(data); |
| 267 | if (ret) |
| 268 | goto err; |
| 269 | /* |
| 270 | * No serial transaction should occur within minimum 10 us |
| 271 | * after soft_reset command |
| 272 | */ |
| 273 | msleep(20); |
| 274 | |
| 275 | ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1); |
| 276 | if (ret) |
| 277 | goto err; |
| 278 | ret = bma180_set_ee_writing_state(data, 1); |
| 279 | if (ret) |
| 280 | goto err; |
| 281 | ret = bma180_set_new_data_intr_state(data, 0); |
| 282 | if (ret) |
| 283 | goto err; |
| 284 | ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1); |
| 285 | if (ret) |
| 286 | goto err; |
| 287 | ret = bma180_set_pmode(data, BMA180_DEF_PMODE); |
| 288 | if (ret) |
| 289 | goto err; |
| 290 | ret = bma180_set_bw(data, BMA180_DEF_BW); |
| 291 | if (ret) |
| 292 | goto err; |
| 293 | ret = bma180_set_scale(data, BMA180_DEF_SCALE); |
| 294 | if (ret) |
| 295 | goto err; |
| 296 | |
| 297 | return 0; |
| 298 | |
| 299 | err: |
| 300 | dev_err(&data->client->dev, "failed to init the chip\n"); |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | static void bma180_chip_disable(struct bma180_data *data) |
| 305 | { |
| 306 | if (bma180_set_new_data_intr_state(data, 0)) |
| 307 | goto err; |
| 308 | if (bma180_set_ee_writing_state(data, 0)) |
| 309 | goto err; |
| 310 | if (bma180_set_sleep_state(data, 1)) |
| 311 | goto err; |
| 312 | |
| 313 | return; |
| 314 | |
| 315 | err: |
| 316 | dev_err(&data->client->dev, "failed to disable the chip\n"); |
| 317 | } |
| 318 | |
| 319 | static IIO_CONST_ATTR(in_accel_filter_low_pass_3db_frequency_available, |
| 320 | BMA180_FLP_FREQ_AVAILABLE); |
| 321 | static IIO_CONST_ATTR(in_accel_scale_available, BMA180_SCALE_AVAILABLE); |
| 322 | |
| 323 | static struct attribute *bma180_attributes[] = { |
| 324 | &iio_const_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr, |
| 325 | &iio_const_attr_in_accel_scale_available.dev_attr.attr, |
| 326 | NULL, |
| 327 | }; |
| 328 | |
| 329 | static const struct attribute_group bma180_attrs_group = { |
| 330 | .attrs = bma180_attributes, |
| 331 | }; |
| 332 | |
| 333 | static int bma180_read_raw(struct iio_dev *indio_dev, |
| 334 | struct iio_chan_spec const *chan, int *val, int *val2, |
| 335 | long mask) |
| 336 | { |
| 337 | struct bma180_data *data = iio_priv(indio_dev); |
| 338 | int ret; |
| 339 | |
| 340 | switch (mask) { |
| 341 | case IIO_CHAN_INFO_RAW: |
| 342 | mutex_lock(&data->mutex); |
| 343 | if (iio_buffer_enabled(indio_dev)) |
| 344 | ret = -EBUSY; |
| 345 | else |
| 346 | ret = bma180_get_acc_reg(data, chan->scan_index); |
| 347 | mutex_unlock(&data->mutex); |
| 348 | if (ret < 0) |
| 349 | return ret; |
| 350 | *val = (s16)ret >> chan->scan_type.shift; |
| 351 | return IIO_VAL_INT; |
| 352 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 353 | *val = data->bw; |
| 354 | return IIO_VAL_INT; |
| 355 | case IIO_CHAN_INFO_SCALE: |
| 356 | *val = 0; |
| 357 | *val2 = data->scale; |
| 358 | return IIO_VAL_INT_PLUS_MICRO; |
| 359 | default: |
| 360 | return -EINVAL; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | static int bma180_write_raw(struct iio_dev *indio_dev, |
| 365 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
| 366 | { |
| 367 | struct bma180_data *data = iio_priv(indio_dev); |
| 368 | int ret; |
| 369 | |
| 370 | switch (mask) { |
| 371 | case IIO_CHAN_INFO_SCALE: |
| 372 | if (val) |
| 373 | return -EINVAL; |
| 374 | mutex_lock(&data->mutex); |
| 375 | ret = bma180_set_scale(data, val2); |
| 376 | mutex_unlock(&data->mutex); |
| 377 | return ret; |
| 378 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 379 | mutex_lock(&data->mutex); |
| 380 | ret = bma180_set_bw(data, val); |
| 381 | mutex_unlock(&data->mutex); |
| 382 | return ret; |
| 383 | default: |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | static int bma180_update_scan_mode(struct iio_dev *indio_dev, |
| 389 | const unsigned long *scan_mask) |
| 390 | { |
| 391 | struct bma180_data *data = iio_priv(indio_dev); |
| 392 | |
| 393 | if (data->buff) |
| 394 | devm_kfree(&indio_dev->dev, data->buff); |
| 395 | data->buff = devm_kzalloc(&indio_dev->dev, |
| 396 | indio_dev->scan_bytes, GFP_KERNEL); |
| 397 | if (!data->buff) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static const struct iio_info bma180_info = { |
| 404 | .attrs = &bma180_attrs_group, |
| 405 | .read_raw = bma180_read_raw, |
| 406 | .write_raw = bma180_write_raw, |
| 407 | .update_scan_mode = bma180_update_scan_mode, |
| 408 | .driver_module = THIS_MODULE, |
| 409 | }; |
| 410 | |
| 411 | static const char * const bma180_power_modes[] = { |
| 412 | BMA180_LOW_NOISE_STR, |
| 413 | BMA180_LOW_POWER_STR, |
| 414 | }; |
| 415 | |
| 416 | static int bma180_get_power_mode(struct iio_dev *indio_dev, |
| 417 | const struct iio_chan_spec *chan) |
| 418 | { |
| 419 | struct bma180_data *data = iio_priv(indio_dev); |
| 420 | |
| 421 | return data->pmode; |
| 422 | } |
| 423 | |
| 424 | static int bma180_set_power_mode(struct iio_dev *indio_dev, |
| 425 | const struct iio_chan_spec *chan, unsigned int mode) |
| 426 | { |
| 427 | struct bma180_data *data = iio_priv(indio_dev); |
| 428 | int ret; |
| 429 | |
| 430 | mutex_lock(&data->mutex); |
| 431 | ret = bma180_set_pmode(data, mode); |
| 432 | mutex_unlock(&data->mutex); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static const struct iio_enum bma180_power_mode_enum = { |
| 438 | .items = bma180_power_modes, |
| 439 | .num_items = ARRAY_SIZE(bma180_power_modes), |
| 440 | .get = bma180_get_power_mode, |
| 441 | .set = bma180_set_power_mode, |
| 442 | }; |
| 443 | |
| 444 | static const struct iio_chan_spec_ext_info bma180_ext_info[] = { |
| 445 | IIO_ENUM("power_mode", true, &bma180_power_mode_enum), |
| 446 | IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum), |
| 447 | { }, |
| 448 | }; |
| 449 | |
| 450 | #define BMA180_CHANNEL(_index) { \ |
| 451 | .type = IIO_ACCEL, \ |
| 452 | .indexed = 1, \ |
| 453 | .channel = (_index), \ |
| 454 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 455 | BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
| 456 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 457 | .scan_index = (_index), \ |
| 458 | .scan_type = IIO_ST('s', 14, 16, 2), \ |
| 459 | .ext_info = bma180_ext_info, \ |
| 460 | } |
| 461 | |
| 462 | static const struct iio_chan_spec bma180_channels[] = { |
| 463 | BMA180_CHANNEL(AXIS_X), |
| 464 | BMA180_CHANNEL(AXIS_Y), |
| 465 | BMA180_CHANNEL(AXIS_Z), |
| 466 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 467 | }; |
| 468 | |
| 469 | static irqreturn_t bma180_trigger_handler(int irq, void *p) |
| 470 | { |
| 471 | struct iio_poll_func *pf = p; |
| 472 | struct iio_dev *indio_dev = pf->indio_dev; |
| 473 | struct bma180_data *data = iio_priv(indio_dev); |
Lars-Peter Clausen | 6bf9d877 | 2013-09-19 13:59:00 +0100 | [diff] [blame^] | 474 | int64_t time_ns = iio_get_time_ns(); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 475 | int bit, ret, i = 0; |
| 476 | |
| 477 | mutex_lock(&data->mutex); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 478 | |
| 479 | for_each_set_bit(bit, indio_dev->buffer->scan_mask, |
| 480 | indio_dev->masklength) { |
| 481 | ret = bma180_get_acc_reg(data, bit); |
| 482 | if (ret < 0) { |
| 483 | mutex_unlock(&data->mutex); |
| 484 | goto err; |
| 485 | } |
| 486 | ((s16 *)data->buff)[i++] = ret; |
| 487 | } |
| 488 | mutex_unlock(&data->mutex); |
| 489 | |
Lars-Peter Clausen | 6bf9d877 | 2013-09-19 13:59:00 +0100 | [diff] [blame^] | 490 | iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 491 | err: |
| 492 | iio_trigger_notify_done(indio_dev->trig); |
| 493 | |
| 494 | return IRQ_HANDLED; |
| 495 | } |
| 496 | |
| 497 | static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 498 | bool state) |
| 499 | { |
| 500 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 501 | struct bma180_data *data = iio_priv(indio_dev); |
| 502 | |
| 503 | return bma180_set_new_data_intr_state(data, state); |
| 504 | } |
| 505 | |
| 506 | static int bma180_trig_try_reen(struct iio_trigger *trig) |
| 507 | { |
| 508 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 509 | struct bma180_data *data = iio_priv(indio_dev); |
| 510 | |
| 511 | return bma180_reset_intr(data); |
| 512 | } |
| 513 | |
| 514 | static const struct iio_trigger_ops bma180_trigger_ops = { |
| 515 | .set_trigger_state = bma180_data_rdy_trigger_set_state, |
| 516 | .try_reenable = bma180_trig_try_reen, |
| 517 | .owner = THIS_MODULE, |
| 518 | }; |
| 519 | |
| 520 | static int bma180_probe(struct i2c_client *client, |
| 521 | const struct i2c_device_id *id) |
| 522 | { |
| 523 | struct bma180_data *data; |
| 524 | struct iio_dev *indio_dev; |
| 525 | struct iio_trigger *trig; |
| 526 | int ret; |
| 527 | |
| 528 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 529 | if (!indio_dev) |
| 530 | return -ENOMEM; |
| 531 | |
| 532 | data = iio_priv(indio_dev); |
| 533 | i2c_set_clientdata(client, indio_dev); |
| 534 | data->client = client; |
| 535 | |
| 536 | ret = bma180_chip_init(data); |
| 537 | if (ret < 0) |
| 538 | goto err_chip_disable; |
| 539 | |
| 540 | mutex_init(&data->mutex); |
| 541 | |
| 542 | indio_dev->dev.parent = &client->dev; |
| 543 | indio_dev->channels = bma180_channels; |
| 544 | indio_dev->num_channels = ARRAY_SIZE(bma180_channels); |
| 545 | indio_dev->name = BMA180_DRV_NAME; |
| 546 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 547 | indio_dev->info = &bma180_info; |
| 548 | |
| 549 | trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, indio_dev->id); |
| 550 | if (!trig) { |
| 551 | ret = -ENOMEM; |
| 552 | goto err_chip_disable; |
| 553 | } |
| 554 | |
| 555 | ret = devm_request_irq(&client->dev, client->irq, |
| 556 | iio_trigger_generic_data_rdy_poll, |
| 557 | IRQF_TRIGGER_RISING, BMA180_IRQ_NAME, trig); |
| 558 | if (ret) { |
| 559 | dev_err(&client->dev, "unable to request IRQ\n"); |
| 560 | goto err_trigger_free; |
| 561 | } |
| 562 | |
| 563 | trig->dev.parent = &client->dev; |
| 564 | trig->ops = &bma180_trigger_ops; |
| 565 | iio_trigger_set_drvdata(trig, indio_dev); |
| 566 | data->trig = trig; |
| 567 | indio_dev->trig = trig; |
| 568 | |
| 569 | ret = iio_trigger_register(trig); |
| 570 | if (ret) |
| 571 | goto err_trigger_free; |
| 572 | |
| 573 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 574 | bma180_trigger_handler, NULL); |
| 575 | if (ret < 0) { |
| 576 | dev_err(&client->dev, "unable to setup iio triggered buffer\n"); |
| 577 | goto err_trigger_unregister; |
| 578 | } |
| 579 | |
| 580 | ret = iio_device_register(indio_dev); |
| 581 | if (ret < 0) { |
| 582 | dev_err(&client->dev, "unable to register iio device\n"); |
| 583 | goto err_buffer_cleanup; |
| 584 | } |
| 585 | |
| 586 | return 0; |
| 587 | |
| 588 | err_buffer_cleanup: |
| 589 | iio_triggered_buffer_cleanup(indio_dev); |
| 590 | err_trigger_unregister: |
| 591 | iio_trigger_unregister(trig); |
| 592 | err_trigger_free: |
| 593 | iio_trigger_free(trig); |
| 594 | err_chip_disable: |
| 595 | bma180_chip_disable(data); |
| 596 | |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | static int bma180_remove(struct i2c_client *client) |
| 601 | { |
| 602 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 603 | struct bma180_data *data = iio_priv(indio_dev); |
| 604 | |
| 605 | iio_device_unregister(indio_dev); |
| 606 | iio_triggered_buffer_cleanup(indio_dev); |
| 607 | iio_trigger_unregister(data->trig); |
| 608 | iio_trigger_free(data->trig); |
| 609 | |
| 610 | mutex_lock(&data->mutex); |
| 611 | bma180_chip_disable(data); |
| 612 | mutex_unlock(&data->mutex); |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | #ifdef CONFIG_PM_SLEEP |
| 618 | static int bma180_suspend(struct device *dev) |
| 619 | { |
| 620 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 621 | struct bma180_data *data = iio_priv(indio_dev); |
| 622 | int ret; |
| 623 | |
| 624 | mutex_lock(&data->mutex); |
| 625 | ret = bma180_set_sleep_state(data, 1); |
| 626 | mutex_unlock(&data->mutex); |
| 627 | |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static int bma180_resume(struct device *dev) |
| 632 | { |
| 633 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 634 | struct bma180_data *data = iio_priv(indio_dev); |
| 635 | int ret; |
| 636 | |
| 637 | mutex_lock(&data->mutex); |
| 638 | ret = bma180_set_sleep_state(data, 0); |
| 639 | mutex_unlock(&data->mutex); |
| 640 | |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume); |
| 645 | #define BMA180_PM_OPS (&bma180_pm_ops) |
| 646 | #else |
| 647 | #define BMA180_PM_OPS NULL |
| 648 | #endif |
| 649 | |
| 650 | static struct i2c_device_id bma180_id[] = { |
| 651 | { BMA180_DRV_NAME, 0 }, |
| 652 | { } |
| 653 | }; |
| 654 | |
| 655 | MODULE_DEVICE_TABLE(i2c, bma180_id); |
| 656 | |
| 657 | static struct i2c_driver bma180_driver = { |
| 658 | .driver = { |
| 659 | .name = BMA180_DRV_NAME, |
| 660 | .owner = THIS_MODULE, |
| 661 | .pm = BMA180_PM_OPS, |
| 662 | }, |
| 663 | .probe = bma180_probe, |
| 664 | .remove = bma180_remove, |
| 665 | .id_table = bma180_id, |
| 666 | }; |
| 667 | |
| 668 | module_i2c_driver(bma180_driver); |
| 669 | |
| 670 | MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>"); |
| 671 | MODULE_AUTHOR("Texas Instruments, Inc."); |
| 672 | MODULE_DESCRIPTION("Bosch BMA180 triaxial acceleration sensor"); |
| 673 | MODULE_LICENSE("GPL"); |