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 | * |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 6 | * Support for BMA250 (c) Peter Meerwald <pmeerw@pmeerw.net> |
| 7 | * |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 8 | * This file is subject to the terms and conditions of version 2 of |
| 9 | * the GNU General Public License. See the file COPYING in the main |
| 10 | * directory of this archive for more details. |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 11 | * |
| 12 | * SPI is not supported by driver |
| 13 | * BMA180: 7-bit I2C slave address 0x40 or 0x41 |
| 14 | * BMA250: 7-bit I2C slave address 0x18 or 0x19 |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/delay.h> |
Javier Martinez Canillas | 16f1b4f | 2017-03-15 01:44:57 -0300 | [diff] [blame] | 21 | #include <linux/of_device.h> |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/bitops.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/iio/iio.h> |
| 27 | #include <linux/iio/sysfs.h> |
| 28 | #include <linux/iio/buffer.h> |
| 29 | #include <linux/iio/trigger.h> |
| 30 | #include <linux/iio/trigger_consumer.h> |
| 31 | #include <linux/iio/triggered_buffer.h> |
| 32 | |
| 33 | #define BMA180_DRV_NAME "bma180" |
| 34 | #define BMA180_IRQ_NAME "bma180_event" |
| 35 | |
Javier Martinez Canillas | 16f1b4f | 2017-03-15 01:44:57 -0300 | [diff] [blame] | 36 | enum chip_ids { |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 37 | BMA180, |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 38 | BMA250, |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 41 | struct bma180_data; |
| 42 | |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 43 | struct bma180_part_info { |
| 44 | const struct iio_chan_spec *channels; |
Miguel Robles | 7fa6790 | 2017-03-17 00:35:45 +0100 | [diff] [blame] | 45 | unsigned int num_channels; |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 46 | const int *scale_table; |
Miguel Robles | 7fa6790 | 2017-03-17 00:35:45 +0100 | [diff] [blame] | 47 | unsigned int num_scales; |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 48 | const int *bw_table; |
Miguel Robles | 7fa6790 | 2017-03-17 00:35:45 +0100 | [diff] [blame] | 49 | unsigned int num_bw; |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 50 | |
| 51 | u8 int_reset_reg, int_reset_mask; |
| 52 | u8 sleep_reg, sleep_mask; |
| 53 | u8 bw_reg, bw_mask; |
| 54 | u8 scale_reg, scale_mask; |
| 55 | u8 power_reg, power_mask, lowpower_val; |
| 56 | u8 int_enable_reg, int_enable_mask; |
| 57 | u8 softreset_reg; |
| 58 | |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 59 | int (*chip_config)(struct bma180_data *data); |
| 60 | void (*chip_disable)(struct bma180_data *data); |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 63 | /* Register set */ |
| 64 | #define BMA180_CHIP_ID 0x00 /* Need to distinguish BMA180 from other */ |
| 65 | #define BMA180_ACC_X_LSB 0x02 /* First of 6 registers of accel data */ |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 66 | #define BMA180_TEMP 0x08 |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 67 | #define BMA180_CTRL_REG0 0x0d |
| 68 | #define BMA180_RESET 0x10 |
| 69 | #define BMA180_BW_TCS 0x20 |
| 70 | #define BMA180_CTRL_REG3 0x21 |
| 71 | #define BMA180_TCO_Z 0x30 |
| 72 | #define BMA180_OFFSET_LSB1 0x35 |
| 73 | |
| 74 | /* BMA180_CTRL_REG0 bits */ |
| 75 | #define BMA180_DIS_WAKE_UP BIT(0) /* Disable wake up mode */ |
| 76 | #define BMA180_SLEEP BIT(1) /* 1 - chip will sleep */ |
| 77 | #define BMA180_EE_W BIT(4) /* Unlock writing to addr from 0x20 */ |
| 78 | #define BMA180_RESET_INT BIT(6) /* Reset pending interrupts */ |
| 79 | |
| 80 | /* BMA180_CTRL_REG3 bits */ |
| 81 | #define BMA180_NEW_DATA_INT BIT(1) /* Intr every new accel data is ready */ |
| 82 | |
| 83 | /* BMA180_OFFSET_LSB1 skipping mode bit */ |
| 84 | #define BMA180_SMP_SKIP BIT(0) |
| 85 | |
| 86 | /* Bit masks for registers bit fields */ |
Peter Meerwald | cab767d | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 87 | #define BMA180_RANGE 0x0e /* Range of measured accel values */ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 88 | #define BMA180_BW 0xf0 /* Accel bandwidth */ |
| 89 | #define BMA180_MODE_CONFIG 0x03 /* Config operation modes */ |
| 90 | |
| 91 | /* We have to write this value in reset register to do soft reset */ |
| 92 | #define BMA180_RESET_VAL 0xb6 |
| 93 | |
Peter Meerwald | 9a70b14 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 94 | #define BMA180_ID_REG_VAL 0x03 |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 95 | |
| 96 | /* Chip power modes */ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 97 | #define BMA180_LOW_POWER 0x03 |
| 98 | |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 99 | #define BMA250_RANGE_REG 0x0f |
| 100 | #define BMA250_BW_REG 0x10 |
| 101 | #define BMA250_POWER_REG 0x11 |
| 102 | #define BMA250_RESET_REG 0x14 |
| 103 | #define BMA250_INT_ENABLE_REG 0x17 |
| 104 | #define BMA250_INT_MAP_REG 0x1a |
| 105 | #define BMA250_INT_RESET_REG 0x21 |
| 106 | |
| 107 | #define BMA250_RANGE_MASK GENMASK(3, 0) /* Range of accel values */ |
| 108 | #define BMA250_BW_MASK GENMASK(4, 0) /* Accel bandwidth */ |
| 109 | #define BMA250_SUSPEND_MASK BIT(7) /* chip will sleep */ |
| 110 | #define BMA250_LOWPOWER_MASK BIT(6) |
| 111 | #define BMA250_DATA_INTEN_MASK BIT(4) |
| 112 | #define BMA250_INT1_DATA_MASK BIT(0) |
| 113 | #define BMA250_INT_RESET_MASK BIT(7) /* Reset pending interrupts */ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 114 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 115 | struct bma180_data { |
| 116 | struct i2c_client *client; |
| 117 | struct iio_trigger *trig; |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 118 | const struct bma180_part_info *part_info; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 119 | struct mutex mutex; |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 120 | bool sleep_state; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 121 | int scale; |
| 122 | int bw; |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 123 | bool pmode; |
Peter Meerwald | b9a6a237 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 124 | u8 buff[16]; /* 3x 16-bit + 8-bit + padding + timestamp */ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 127 | enum bma180_chan { |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 128 | AXIS_X, |
| 129 | AXIS_Y, |
| 130 | AXIS_Z, |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 131 | TEMP |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
Peter Meerwald | cab767d | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 134 | static int bma180_bw_table[] = { 10, 20, 40, 75, 150, 300 }; /* Hz */ |
| 135 | static int bma180_scale_table[] = { 1275, 1863, 2452, 3727, 4903, 9709, 19417 }; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 136 | |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 137 | static int bma250_bw_table[] = { 8, 16, 31, 63, 125, 250 }; /* Hz */ |
| 138 | static int bma250_scale_table[] = { 0, 0, 0, 38344, 0, 76590, 0, 0, 153180, 0, |
| 139 | 0, 0, 306458 }; |
| 140 | |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 141 | static int bma180_get_data_reg(struct bma180_data *data, enum bma180_chan chan) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 142 | { |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 143 | int ret; |
| 144 | |
| 145 | if (data->sleep_state) |
| 146 | return -EBUSY; |
| 147 | |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 148 | switch (chan) { |
| 149 | case TEMP: |
| 150 | ret = i2c_smbus_read_byte_data(data->client, BMA180_TEMP); |
| 151 | if (ret < 0) |
| 152 | dev_err(&data->client->dev, "failed to read temp register\n"); |
| 153 | break; |
| 154 | default: |
| 155 | ret = i2c_smbus_read_word_data(data->client, |
| 156 | BMA180_ACC_X_LSB + chan * 2); |
| 157 | if (ret < 0) |
| 158 | dev_err(&data->client->dev, |
| 159 | "failed to read accel_%c register\n", |
| 160 | 'x' + chan); |
| 161 | } |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int bma180_set_bits(struct bma180_data *data, u8 reg, u8 mask, u8 val) |
| 167 | { |
| 168 | int ret = i2c_smbus_read_byte_data(data->client, reg); |
| 169 | u8 reg_val = (ret & ~mask) | (val << (ffs(mask) - 1)); |
| 170 | |
| 171 | if (ret < 0) |
| 172 | return ret; |
| 173 | |
| 174 | return i2c_smbus_write_byte_data(data->client, reg, reg_val); |
| 175 | } |
| 176 | |
| 177 | static int bma180_reset_intr(struct bma180_data *data) |
| 178 | { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 179 | int ret = bma180_set_bits(data, data->part_info->int_reset_reg, |
| 180 | data->part_info->int_reset_mask, 1); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 181 | |
| 182 | if (ret) |
| 183 | dev_err(&data->client->dev, "failed to reset interrupt\n"); |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 188 | static int bma180_set_new_data_intr_state(struct bma180_data *data, bool state) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 189 | { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 190 | int ret = bma180_set_bits(data, data->part_info->int_enable_reg, |
| 191 | data->part_info->int_enable_mask, state); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 192 | if (ret) |
| 193 | goto err; |
| 194 | ret = bma180_reset_intr(data); |
| 195 | if (ret) |
| 196 | goto err; |
| 197 | |
| 198 | return 0; |
| 199 | |
| 200 | err: |
| 201 | dev_err(&data->client->dev, |
| 202 | "failed to set new data interrupt state %d\n", state); |
| 203 | return ret; |
| 204 | } |
| 205 | |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 206 | static int bma180_set_sleep_state(struct bma180_data *data, bool state) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 207 | { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 208 | int ret = bma180_set_bits(data, data->part_info->sleep_reg, |
| 209 | data->part_info->sleep_mask, state); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 210 | |
| 211 | if (ret) { |
| 212 | dev_err(&data->client->dev, |
| 213 | "failed to set sleep state %d\n", state); |
| 214 | return ret; |
| 215 | } |
| 216 | data->sleep_state = state; |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 221 | static int bma180_set_ee_writing_state(struct bma180_data *data, bool state) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 222 | { |
| 223 | int ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_EE_W, state); |
| 224 | |
| 225 | if (ret) |
| 226 | dev_err(&data->client->dev, |
| 227 | "failed to set ee writing state %d\n", state); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | static int bma180_set_bw(struct bma180_data *data, int val) |
| 233 | { |
| 234 | int ret, i; |
| 235 | |
| 236 | if (data->sleep_state) |
| 237 | return -EBUSY; |
| 238 | |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 239 | for (i = 0; i < data->part_info->num_bw; ++i) { |
| 240 | if (data->part_info->bw_table[i] == val) { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 241 | ret = bma180_set_bits(data, data->part_info->bw_reg, |
| 242 | data->part_info->bw_mask, i); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 243 | if (ret) { |
| 244 | dev_err(&data->client->dev, |
| 245 | "failed to set bandwidth\n"); |
| 246 | return ret; |
| 247 | } |
| 248 | data->bw = val; |
| 249 | return 0; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | static int bma180_set_scale(struct bma180_data *data, int val) |
| 257 | { |
| 258 | int ret, i; |
| 259 | |
| 260 | if (data->sleep_state) |
| 261 | return -EBUSY; |
| 262 | |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 263 | for (i = 0; i < data->part_info->num_scales; ++i) |
| 264 | if (data->part_info->scale_table[i] == val) { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 265 | ret = bma180_set_bits(data, data->part_info->scale_reg, |
| 266 | data->part_info->scale_mask, i); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 267 | if (ret) { |
| 268 | dev_err(&data->client->dev, |
| 269 | "failed to set scale\n"); |
| 270 | return ret; |
| 271 | } |
| 272 | data->scale = val; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 279 | static int bma180_set_pmode(struct bma180_data *data, bool mode) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 280 | { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 281 | u8 reg_val = mode ? data->part_info->lowpower_val : 0; |
| 282 | int ret = bma180_set_bits(data, data->part_info->power_reg, |
| 283 | data->part_info->power_mask, reg_val); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 284 | |
| 285 | if (ret) { |
| 286 | dev_err(&data->client->dev, "failed to set power mode\n"); |
| 287 | return ret; |
| 288 | } |
| 289 | data->pmode = mode; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int bma180_soft_reset(struct bma180_data *data) |
| 295 | { |
| 296 | int ret = i2c_smbus_write_byte_data(data->client, |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 297 | data->part_info->softreset_reg, BMA180_RESET_VAL); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 298 | |
| 299 | if (ret) |
| 300 | dev_err(&data->client->dev, "failed to reset the chip\n"); |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | static int bma180_chip_init(struct bma180_data *data) |
| 306 | { |
| 307 | /* Try to read chip_id register. It must return 0x03. */ |
| 308 | int ret = i2c_smbus_read_byte_data(data->client, BMA180_CHIP_ID); |
| 309 | |
| 310 | if (ret < 0) |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 311 | return ret; |
| 312 | if (ret != BMA180_ID_REG_VAL) |
| 313 | return -ENODEV; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 314 | |
| 315 | ret = bma180_soft_reset(data); |
| 316 | if (ret) |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 317 | return ret; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 318 | /* |
| 319 | * No serial transaction should occur within minimum 10 us |
| 320 | * after soft_reset command |
| 321 | */ |
| 322 | msleep(20); |
| 323 | |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 324 | ret = bma180_set_new_data_intr_state(data, false); |
| 325 | if (ret) |
| 326 | return ret; |
| 327 | |
| 328 | return bma180_set_pmode(data, false); |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int bma180_chip_config(struct bma180_data *data) |
| 332 | { |
| 333 | int ret = bma180_chip_init(data); |
| 334 | |
| 335 | if (ret) |
| 336 | goto err; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 337 | ret = bma180_set_bits(data, BMA180_CTRL_REG0, BMA180_DIS_WAKE_UP, 1); |
| 338 | if (ret) |
| 339 | goto err; |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 340 | ret = bma180_set_ee_writing_state(data, true); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 341 | if (ret) |
| 342 | goto err; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 343 | ret = bma180_set_bits(data, BMA180_OFFSET_LSB1, BMA180_SMP_SKIP, 1); |
| 344 | if (ret) |
| 345 | goto err; |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 346 | ret = bma180_set_bw(data, 20); /* 20 Hz */ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 347 | if (ret) |
| 348 | goto err; |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 349 | ret = bma180_set_scale(data, 2452); /* 2 G */ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 350 | if (ret) |
| 351 | goto err; |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 352 | |
| 353 | return 0; |
| 354 | |
| 355 | err: |
| 356 | dev_err(&data->client->dev, "failed to config the chip\n"); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static int bma250_chip_config(struct bma180_data *data) |
| 361 | { |
| 362 | int ret = bma180_chip_init(data); |
| 363 | |
| 364 | if (ret) |
| 365 | goto err; |
| 366 | ret = bma180_set_bw(data, 16); /* 16 Hz */ |
| 367 | if (ret) |
| 368 | goto err; |
| 369 | ret = bma180_set_scale(data, 38344); /* 2 G */ |
| 370 | if (ret) |
| 371 | goto err; |
| 372 | ret = bma180_set_bits(data, BMA250_INT_MAP_REG, |
| 373 | BMA250_INT1_DATA_MASK, 1); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 374 | if (ret) |
| 375 | goto err; |
| 376 | |
| 377 | return 0; |
| 378 | |
| 379 | err: |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 380 | dev_err(&data->client->dev, "failed to config the chip\n"); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | static void bma180_chip_disable(struct bma180_data *data) |
| 385 | { |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 386 | if (bma180_set_new_data_intr_state(data, false)) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 387 | goto err; |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 388 | if (bma180_set_ee_writing_state(data, false)) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 389 | goto err; |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 390 | if (bma180_set_sleep_state(data, true)) |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 391 | goto err; |
| 392 | |
| 393 | return; |
| 394 | |
| 395 | err: |
| 396 | dev_err(&data->client->dev, "failed to disable the chip\n"); |
| 397 | } |
| 398 | |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 399 | static void bma250_chip_disable(struct bma180_data *data) |
| 400 | { |
| 401 | if (bma180_set_new_data_intr_state(data, false)) |
| 402 | goto err; |
| 403 | if (bma180_set_sleep_state(data, true)) |
| 404 | goto err; |
| 405 | |
| 406 | return; |
| 407 | |
| 408 | err: |
| 409 | dev_err(&data->client->dev, "failed to disable the chip\n"); |
| 410 | } |
| 411 | |
Miguel Robles | 7fa6790 | 2017-03-17 00:35:45 +0100 | [diff] [blame] | 412 | static ssize_t bma180_show_avail(char *buf, const int *vals, unsigned int n, |
Peter Meerwald | 402a324 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 413 | bool micros) |
| 414 | { |
| 415 | size_t len = 0; |
| 416 | int i; |
| 417 | |
| 418 | for (i = 0; i < n; i++) { |
| 419 | if (!vals[i]) |
| 420 | continue; |
| 421 | len += scnprintf(buf + len, PAGE_SIZE - len, |
| 422 | micros ? "0.%06d " : "%d ", vals[i]); |
| 423 | } |
| 424 | buf[len - 1] = '\n'; |
| 425 | |
| 426 | return len; |
| 427 | } |
| 428 | |
| 429 | static ssize_t bma180_show_filter_freq_avail(struct device *dev, |
| 430 | struct device_attribute *attr, char *buf) |
| 431 | { |
| 432 | struct bma180_data *data = iio_priv(dev_to_iio_dev(dev)); |
| 433 | |
| 434 | return bma180_show_avail(buf, data->part_info->bw_table, |
| 435 | data->part_info->num_bw, false); |
| 436 | } |
| 437 | |
| 438 | static ssize_t bma180_show_scale_avail(struct device *dev, |
| 439 | struct device_attribute *attr, char *buf) |
| 440 | { |
| 441 | struct bma180_data *data = iio_priv(dev_to_iio_dev(dev)); |
| 442 | |
| 443 | return bma180_show_avail(buf, data->part_info->scale_table, |
| 444 | data->part_info->num_scales, true); |
| 445 | } |
| 446 | |
| 447 | static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available, |
| 448 | S_IRUGO, bma180_show_filter_freq_avail, NULL, 0); |
| 449 | |
| 450 | static IIO_DEVICE_ATTR(in_accel_scale_available, |
| 451 | S_IRUGO, bma180_show_scale_avail, NULL, 0); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 452 | |
| 453 | static struct attribute *bma180_attributes[] = { |
Peter Meerwald | 402a324 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 454 | &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available. |
| 455 | dev_attr.attr, |
| 456 | &iio_dev_attr_in_accel_scale_available.dev_attr.attr, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 457 | NULL, |
| 458 | }; |
| 459 | |
| 460 | static const struct attribute_group bma180_attrs_group = { |
| 461 | .attrs = bma180_attributes, |
| 462 | }; |
| 463 | |
| 464 | static int bma180_read_raw(struct iio_dev *indio_dev, |
| 465 | struct iio_chan_spec const *chan, int *val, int *val2, |
| 466 | long mask) |
| 467 | { |
| 468 | struct bma180_data *data = iio_priv(indio_dev); |
| 469 | int ret; |
| 470 | |
| 471 | switch (mask) { |
| 472 | case IIO_CHAN_INFO_RAW: |
Alison Schofield | b75b58b | 2016-07-25 12:11:20 -0700 | [diff] [blame] | 473 | ret = iio_device_claim_direct_mode(indio_dev); |
| 474 | if (ret) |
| 475 | return ret; |
| 476 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 477 | mutex_lock(&data->mutex); |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 478 | ret = bma180_get_data_reg(data, chan->scan_index); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 479 | mutex_unlock(&data->mutex); |
Alison Schofield | b75b58b | 2016-07-25 12:11:20 -0700 | [diff] [blame] | 480 | iio_device_release_direct_mode(indio_dev); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 481 | if (ret < 0) |
| 482 | return ret; |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 483 | *val = sign_extend32(ret >> chan->scan_type.shift, |
| 484 | chan->scan_type.realbits - 1); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 485 | return IIO_VAL_INT; |
| 486 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
| 487 | *val = data->bw; |
| 488 | return IIO_VAL_INT; |
| 489 | case IIO_CHAN_INFO_SCALE: |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 490 | switch (chan->type) { |
| 491 | case IIO_ACCEL: |
| 492 | *val = 0; |
| 493 | *val2 = data->scale; |
| 494 | return IIO_VAL_INT_PLUS_MICRO; |
| 495 | case IIO_TEMP: |
| 496 | *val = 500; |
| 497 | return IIO_VAL_INT; |
| 498 | default: |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | case IIO_CHAN_INFO_OFFSET: |
| 502 | *val = 48; /* 0 LSB @ 24 degree C */ |
| 503 | return IIO_VAL_INT; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 504 | default: |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static int bma180_write_raw(struct iio_dev *indio_dev, |
| 510 | struct iio_chan_spec const *chan, int val, int val2, long mask) |
| 511 | { |
| 512 | struct bma180_data *data = iio_priv(indio_dev); |
| 513 | int ret; |
| 514 | |
| 515 | switch (mask) { |
| 516 | case IIO_CHAN_INFO_SCALE: |
| 517 | if (val) |
| 518 | return -EINVAL; |
| 519 | mutex_lock(&data->mutex); |
| 520 | ret = bma180_set_scale(data, val2); |
| 521 | mutex_unlock(&data->mutex); |
| 522 | return ret; |
| 523 | case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: |
Peter Meerwald | 9b2a4d3 | 2014-07-16 19:32:00 +0100 | [diff] [blame] | 524 | if (val2) |
| 525 | return -EINVAL; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 526 | mutex_lock(&data->mutex); |
| 527 | ret = bma180_set_bw(data, val); |
| 528 | mutex_unlock(&data->mutex); |
| 529 | return ret; |
| 530 | default: |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | } |
| 534 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 535 | static const struct iio_info bma180_info = { |
| 536 | .attrs = &bma180_attrs_group, |
| 537 | .read_raw = bma180_read_raw, |
| 538 | .write_raw = bma180_write_raw, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 539 | }; |
| 540 | |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 541 | static const char * const bma180_power_modes[] = { "low_noise", "low_power" }; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 542 | |
| 543 | static int bma180_get_power_mode(struct iio_dev *indio_dev, |
| 544 | const struct iio_chan_spec *chan) |
| 545 | { |
| 546 | struct bma180_data *data = iio_priv(indio_dev); |
| 547 | |
| 548 | return data->pmode; |
| 549 | } |
| 550 | |
| 551 | static int bma180_set_power_mode(struct iio_dev *indio_dev, |
| 552 | const struct iio_chan_spec *chan, unsigned int mode) |
| 553 | { |
| 554 | struct bma180_data *data = iio_priv(indio_dev); |
| 555 | int ret; |
| 556 | |
| 557 | mutex_lock(&data->mutex); |
| 558 | ret = bma180_set_pmode(data, mode); |
| 559 | mutex_unlock(&data->mutex); |
| 560 | |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | static const struct iio_enum bma180_power_mode_enum = { |
| 565 | .items = bma180_power_modes, |
| 566 | .num_items = ARRAY_SIZE(bma180_power_modes), |
| 567 | .get = bma180_get_power_mode, |
| 568 | .set = bma180_set_power_mode, |
| 569 | }; |
| 570 | |
| 571 | static const struct iio_chan_spec_ext_info bma180_ext_info[] = { |
| 572 | IIO_ENUM("power_mode", true, &bma180_power_mode_enum), |
| 573 | IIO_ENUM_AVAILABLE("power_mode", &bma180_power_mode_enum), |
| 574 | { }, |
| 575 | }; |
| 576 | |
Peter Meerwald | 6377aa4 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 577 | #define BMA180_ACC_CHANNEL(_axis, _bits) { \ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 578 | .type = IIO_ACCEL, \ |
Peter Meerwald | 5585215 | 2014-10-01 21:37:00 +0100 | [diff] [blame] | 579 | .modified = 1, \ |
| 580 | .channel2 = IIO_MOD_##_axis, \ |
Peter Meerwald | 5c1449d | 2014-10-01 21:37:00 +0100 | [diff] [blame] | 581 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 582 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 583 | BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ |
Peter Meerwald | 5585215 | 2014-10-01 21:37:00 +0100 | [diff] [blame] | 584 | .scan_index = AXIS_##_axis, \ |
Jonathan Cameron | 7d7feae | 2013-12-11 18:45:00 +0000 | [diff] [blame] | 585 | .scan_type = { \ |
| 586 | .sign = 's', \ |
Peter Meerwald | 6377aa4 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 587 | .realbits = _bits, \ |
Jonathan Cameron | 7d7feae | 2013-12-11 18:45:00 +0000 | [diff] [blame] | 588 | .storagebits = 16, \ |
Peter Meerwald | 6377aa4 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 589 | .shift = 16 - _bits, \ |
Jonathan Cameron | 7d7feae | 2013-12-11 18:45:00 +0000 | [diff] [blame] | 590 | }, \ |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 591 | .ext_info = bma180_ext_info, \ |
| 592 | } |
| 593 | |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 594 | #define BMA180_TEMP_CHANNEL { \ |
| 595 | .type = IIO_TEMP, \ |
| 596 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 597 | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_OFFSET), \ |
| 598 | .scan_index = TEMP, \ |
| 599 | .scan_type = { \ |
| 600 | .sign = 's', \ |
| 601 | .realbits = 8, \ |
| 602 | .storagebits = 16, \ |
| 603 | }, \ |
| 604 | } |
| 605 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 606 | static const struct iio_chan_spec bma180_channels[] = { |
Peter Meerwald | 6377aa4 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 607 | BMA180_ACC_CHANNEL(X, 14), |
| 608 | BMA180_ACC_CHANNEL(Y, 14), |
| 609 | BMA180_ACC_CHANNEL(Z, 14), |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 610 | BMA180_TEMP_CHANNEL, |
| 611 | IIO_CHAN_SOFT_TIMESTAMP(4), |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 612 | }; |
| 613 | |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 614 | static const struct iio_chan_spec bma250_channels[] = { |
| 615 | BMA180_ACC_CHANNEL(X, 10), |
| 616 | BMA180_ACC_CHANNEL(Y, 10), |
| 617 | BMA180_ACC_CHANNEL(Z, 10), |
| 618 | BMA180_TEMP_CHANNEL, |
| 619 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 620 | }; |
| 621 | |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 622 | static const struct bma180_part_info bma180_part_info[] = { |
| 623 | [BMA180] = { |
| 624 | bma180_channels, ARRAY_SIZE(bma180_channels), |
| 625 | bma180_scale_table, ARRAY_SIZE(bma180_scale_table), |
| 626 | bma180_bw_table, ARRAY_SIZE(bma180_bw_table), |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 627 | BMA180_CTRL_REG0, BMA180_RESET_INT, |
| 628 | BMA180_CTRL_REG0, BMA180_SLEEP, |
| 629 | BMA180_BW_TCS, BMA180_BW, |
| 630 | BMA180_OFFSET_LSB1, BMA180_RANGE, |
| 631 | BMA180_TCO_Z, BMA180_MODE_CONFIG, BMA180_LOW_POWER, |
| 632 | BMA180_CTRL_REG3, BMA180_NEW_DATA_INT, |
| 633 | BMA180_RESET, |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 634 | bma180_chip_config, |
| 635 | bma180_chip_disable, |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 636 | }, |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 637 | [BMA250] = { |
| 638 | bma250_channels, ARRAY_SIZE(bma250_channels), |
| 639 | bma250_scale_table, ARRAY_SIZE(bma250_scale_table), |
| 640 | bma250_bw_table, ARRAY_SIZE(bma250_bw_table), |
| 641 | BMA250_INT_RESET_REG, BMA250_INT_RESET_MASK, |
| 642 | BMA250_POWER_REG, BMA250_SUSPEND_MASK, |
| 643 | BMA250_BW_REG, BMA250_BW_MASK, |
| 644 | BMA250_RANGE_REG, BMA250_RANGE_MASK, |
| 645 | BMA250_POWER_REG, BMA250_LOWPOWER_MASK, 1, |
| 646 | BMA250_INT_ENABLE_REG, BMA250_DATA_INTEN_MASK, |
| 647 | BMA250_RESET_REG, |
| 648 | bma250_chip_config, |
| 649 | bma250_chip_disable, |
| 650 | }, |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 651 | }; |
| 652 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 653 | static irqreturn_t bma180_trigger_handler(int irq, void *p) |
| 654 | { |
| 655 | struct iio_poll_func *pf = p; |
| 656 | struct iio_dev *indio_dev = pf->indio_dev; |
| 657 | struct bma180_data *data = iio_priv(indio_dev); |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 658 | s64 time_ns = iio_get_time_ns(indio_dev); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 659 | int bit, ret, i = 0; |
| 660 | |
| 661 | mutex_lock(&data->mutex); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 662 | |
Octavian Purdila | 70dddee | 2015-03-02 21:03:05 +0200 | [diff] [blame] | 663 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 664 | indio_dev->masklength) { |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 665 | ret = bma180_get_data_reg(data, bit); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 666 | if (ret < 0) { |
| 667 | mutex_unlock(&data->mutex); |
| 668 | goto err; |
| 669 | } |
| 670 | ((s16 *)data->buff)[i++] = ret; |
| 671 | } |
Peter Meerwald | b81fbab | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 672 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 673 | mutex_unlock(&data->mutex); |
| 674 | |
Lars-Peter Clausen | 6bf9d877 | 2013-09-19 13:59:00 +0100 | [diff] [blame] | 675 | iio_push_to_buffers_with_timestamp(indio_dev, data->buff, time_ns); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 676 | err: |
| 677 | iio_trigger_notify_done(indio_dev->trig); |
| 678 | |
| 679 | return IRQ_HANDLED; |
| 680 | } |
| 681 | |
| 682 | static int bma180_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 683 | bool state) |
| 684 | { |
| 685 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 686 | struct bma180_data *data = iio_priv(indio_dev); |
| 687 | |
| 688 | return bma180_set_new_data_intr_state(data, state); |
| 689 | } |
| 690 | |
| 691 | static int bma180_trig_try_reen(struct iio_trigger *trig) |
| 692 | { |
| 693 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 694 | struct bma180_data *data = iio_priv(indio_dev); |
| 695 | |
| 696 | return bma180_reset_intr(data); |
| 697 | } |
| 698 | |
| 699 | static const struct iio_trigger_ops bma180_trigger_ops = { |
| 700 | .set_trigger_state = bma180_data_rdy_trigger_set_state, |
| 701 | .try_reenable = bma180_trig_try_reen, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | static int bma180_probe(struct i2c_client *client, |
| 705 | const struct i2c_device_id *id) |
| 706 | { |
| 707 | struct bma180_data *data; |
| 708 | struct iio_dev *indio_dev; |
Javier Martinez Canillas | 16f1b4f | 2017-03-15 01:44:57 -0300 | [diff] [blame] | 709 | enum chip_ids chip; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 710 | int ret; |
| 711 | |
| 712 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 713 | if (!indio_dev) |
| 714 | return -ENOMEM; |
| 715 | |
| 716 | data = iio_priv(indio_dev); |
| 717 | i2c_set_clientdata(client, indio_dev); |
| 718 | data->client = client; |
Javier Martinez Canillas | 16f1b4f | 2017-03-15 01:44:57 -0300 | [diff] [blame] | 719 | if (client->dev.of_node) |
| 720 | chip = (enum chip_ids)of_device_get_match_data(&client->dev); |
| 721 | else |
| 722 | chip = id->driver_data; |
| 723 | data->part_info = &bma180_part_info[chip]; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 724 | |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 725 | ret = data->part_info->chip_config(data); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 726 | if (ret < 0) |
| 727 | goto err_chip_disable; |
| 728 | |
| 729 | mutex_init(&data->mutex); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 730 | indio_dev->dev.parent = &client->dev; |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 731 | indio_dev->channels = data->part_info->channels; |
| 732 | indio_dev->num_channels = data->part_info->num_channels; |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 733 | indio_dev->name = id->name; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 734 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 735 | indio_dev->info = &bma180_info; |
| 736 | |
Peter Meerwald | 16ed869 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 737 | if (client->irq > 0) { |
| 738 | data->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, |
| 739 | indio_dev->id); |
| 740 | if (!data->trig) { |
| 741 | ret = -ENOMEM; |
| 742 | goto err_chip_disable; |
| 743 | } |
| 744 | |
| 745 | ret = devm_request_irq(&client->dev, client->irq, |
| 746 | iio_trigger_generic_data_rdy_poll, IRQF_TRIGGER_RISING, |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 747 | "bma180_event", data->trig); |
Peter Meerwald | 16ed869 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 748 | if (ret) { |
| 749 | dev_err(&client->dev, "unable to request IRQ\n"); |
| 750 | goto err_trigger_free; |
| 751 | } |
| 752 | |
| 753 | data->trig->dev.parent = &client->dev; |
| 754 | data->trig->ops = &bma180_trigger_ops; |
| 755 | iio_trigger_set_drvdata(data->trig, indio_dev); |
Greg Kroah-Hartman | 8deab47 | 2014-09-22 07:54:10 -0700 | [diff] [blame] | 756 | indio_dev->trig = iio_trigger_get(data->trig); |
Peter Meerwald | 16ed869 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 757 | |
| 758 | ret = iio_trigger_register(data->trig); |
| 759 | if (ret) |
| 760 | goto err_trigger_free; |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 763 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 764 | bma180_trigger_handler, NULL); |
| 765 | if (ret < 0) { |
| 766 | dev_err(&client->dev, "unable to setup iio triggered buffer\n"); |
| 767 | goto err_trigger_unregister; |
| 768 | } |
| 769 | |
| 770 | ret = iio_device_register(indio_dev); |
| 771 | if (ret < 0) { |
| 772 | dev_err(&client->dev, "unable to register iio device\n"); |
| 773 | goto err_buffer_cleanup; |
| 774 | } |
| 775 | |
| 776 | return 0; |
| 777 | |
| 778 | err_buffer_cleanup: |
| 779 | iio_triggered_buffer_cleanup(indio_dev); |
| 780 | err_trigger_unregister: |
Peter Meerwald | 16ed869 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 781 | if (data->trig) |
| 782 | iio_trigger_unregister(data->trig); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 783 | err_trigger_free: |
Peter Meerwald | 16ed869 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 784 | iio_trigger_free(data->trig); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 785 | err_chip_disable: |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 786 | data->part_info->chip_disable(data); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 787 | |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | static int bma180_remove(struct i2c_client *client) |
| 792 | { |
| 793 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 794 | struct bma180_data *data = iio_priv(indio_dev); |
| 795 | |
| 796 | iio_device_unregister(indio_dev); |
| 797 | iio_triggered_buffer_cleanup(indio_dev); |
Peter Meerwald | 16ed869 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 798 | if (data->trig) { |
| 799 | iio_trigger_unregister(data->trig); |
| 800 | iio_trigger_free(data->trig); |
| 801 | } |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 802 | |
| 803 | mutex_lock(&data->mutex); |
Peter Meerwald | c1949ec | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 804 | data->part_info->chip_disable(data); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 805 | mutex_unlock(&data->mutex); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | #ifdef CONFIG_PM_SLEEP |
| 811 | static int bma180_suspend(struct device *dev) |
| 812 | { |
Peter Meerwald | 234efa1 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 813 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 814 | struct bma180_data *data = iio_priv(indio_dev); |
| 815 | int ret; |
| 816 | |
| 817 | mutex_lock(&data->mutex); |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 818 | ret = bma180_set_sleep_state(data, true); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 819 | mutex_unlock(&data->mutex); |
| 820 | |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | static int bma180_resume(struct device *dev) |
| 825 | { |
Peter Meerwald | 234efa1 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 826 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 827 | struct bma180_data *data = iio_priv(indio_dev); |
| 828 | int ret; |
| 829 | |
| 830 | mutex_lock(&data->mutex); |
Peter Meerwald | c7c69e8 | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 831 | ret = bma180_set_sleep_state(data, false); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 832 | mutex_unlock(&data->mutex); |
| 833 | |
| 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | static SIMPLE_DEV_PM_OPS(bma180_pm_ops, bma180_suspend, bma180_resume); |
| 838 | #define BMA180_PM_OPS (&bma180_pm_ops) |
| 839 | #else |
| 840 | #define BMA180_PM_OPS NULL |
| 841 | #endif |
| 842 | |
Arvind Yadav | e05d83b | 2017-08-20 00:17:36 +0530 | [diff] [blame] | 843 | static const struct i2c_device_id bma180_ids[] = { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 844 | { "bma180", BMA180 }, |
| 845 | { "bma250", BMA250 }, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 846 | { } |
| 847 | }; |
| 848 | |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 849 | MODULE_DEVICE_TABLE(i2c, bma180_ids); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 850 | |
Javier Martinez Canillas | 16f1b4f | 2017-03-15 01:44:57 -0300 | [diff] [blame] | 851 | static const struct of_device_id bma180_of_match[] = { |
| 852 | { |
| 853 | .compatible = "bosch,bma180", |
| 854 | .data = (void *)BMA180 |
| 855 | }, |
| 856 | { |
| 857 | .compatible = "bosch,bma250", |
| 858 | .data = (void *)BMA250 |
| 859 | }, |
| 860 | { } |
| 861 | }; |
| 862 | MODULE_DEVICE_TABLE(of, bma180_of_match); |
| 863 | |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 864 | static struct i2c_driver bma180_driver = { |
| 865 | .driver = { |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 866 | .name = "bma180", |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 867 | .pm = BMA180_PM_OPS, |
Javier Martinez Canillas | 16f1b4f | 2017-03-15 01:44:57 -0300 | [diff] [blame] | 868 | .of_match_table = bma180_of_match, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 869 | }, |
| 870 | .probe = bma180_probe, |
| 871 | .remove = bma180_remove, |
Peter Meerwald | 1b9030f | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 872 | .id_table = bma180_ids, |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 873 | }; |
| 874 | |
| 875 | module_i2c_driver(bma180_driver); |
| 876 | |
| 877 | MODULE_AUTHOR("Kravchenko Oleksandr <x0199363@ti.com>"); |
| 878 | MODULE_AUTHOR("Texas Instruments, Inc."); |
Peter Meerwald | 2017cff | 2014-08-19 23:43:00 +0100 | [diff] [blame] | 879 | MODULE_DESCRIPTION("Bosch BMA180/BMA250 triaxial acceleration sensor"); |
Oleksandr Kravchenko | fdadbce | 2013-08-28 12:01:00 +0100 | [diff] [blame] | 880 | MODULE_LICENSE("GPL"); |