Thomas Gleixner | 36edc93 | 2019-05-29 16:57:44 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 2 | /** |
| 3 | * Sensortek STK8BA50 3-Axis Accelerometer |
| 4 | * |
| 5 | * Copyright (c) 2015, Intel Corporation. |
| 6 | * |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 7 | * STK8BA50 7-bit I2C address: 0x18. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/acpi.h> |
| 11 | #include <linux/i2c.h> |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 15 | #include <linux/iio/buffer.h> |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 16 | #include <linux/iio/iio.h> |
| 17 | #include <linux/iio/sysfs.h> |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 18 | #include <linux/iio/trigger.h> |
| 19 | #include <linux/iio/triggered_buffer.h> |
| 20 | #include <linux/iio/trigger_consumer.h> |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 21 | |
| 22 | #define STK8BA50_REG_XOUT 0x02 |
| 23 | #define STK8BA50_REG_YOUT 0x04 |
| 24 | #define STK8BA50_REG_ZOUT 0x06 |
| 25 | #define STK8BA50_REG_RANGE 0x0F |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 26 | #define STK8BA50_REG_BWSEL 0x10 |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 27 | #define STK8BA50_REG_POWMODE 0x11 |
| 28 | #define STK8BA50_REG_SWRST 0x14 |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 29 | #define STK8BA50_REG_INTEN2 0x17 |
| 30 | #define STK8BA50_REG_INTMAP2 0x1A |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 31 | |
| 32 | #define STK8BA50_MODE_NORMAL 0 |
| 33 | #define STK8BA50_MODE_SUSPEND 1 |
| 34 | #define STK8BA50_MODE_POWERBIT BIT(7) |
| 35 | #define STK8BA50_DATA_SHIFT 6 |
| 36 | #define STK8BA50_RESET_CMD 0xB6 |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 37 | #define STK8BA50_SR_1792HZ_IDX 7 |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 38 | #define STK8BA50_DREADY_INT_MASK 0x10 |
| 39 | #define STK8BA50_DREADY_INT_MAP 0x81 |
| 40 | #define STK8BA50_ALL_CHANNEL_MASK 7 |
| 41 | #define STK8BA50_ALL_CHANNEL_SIZE 6 |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 42 | |
| 43 | #define STK8BA50_DRIVER_NAME "stk8ba50" |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 44 | #define STK8BA50_IRQ_NAME "stk8ba50_event" |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 45 | |
| 46 | #define STK8BA50_SCALE_AVAIL "0.0384 0.0767 0.1534 0.3069" |
| 47 | |
| 48 | /* |
| 49 | * The accelerometer has four measurement ranges: |
| 50 | * +/-2g; +/-4g; +/-8g; +/-16g |
| 51 | * |
| 52 | * Acceleration values are 10-bit, 2's complement. |
| 53 | * Scales are calculated as following: |
| 54 | * |
| 55 | * scale1 = (2 + 2) * 9.81 / (2^10 - 1) = 0.0384 |
| 56 | * scale2 = (4 + 4) * 9.81 / (2^10 - 1) = 0.0767 |
| 57 | * etc. |
| 58 | * |
| 59 | * Scales are stored in this format: |
| 60 | * { <register value>, <scale value> } |
| 61 | * |
| 62 | * Locally, the range is stored as a table index. |
| 63 | */ |
Tiberiu Breana | 003f488 | 2015-06-10 18:07:29 +0300 | [diff] [blame] | 64 | static const struct { |
| 65 | u8 reg_val; |
| 66 | u32 scale_val; |
| 67 | } stk8ba50_scale_table[] = { |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 68 | {3, 38400}, {5, 76700}, {8, 153400}, {12, 306900} |
| 69 | }; |
| 70 | |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 71 | /* Sample rates are stored as { <register value>, <Hz value> } */ |
| 72 | static const struct { |
| 73 | u8 reg_val; |
| 74 | u16 samp_freq; |
| 75 | } stk8ba50_samp_freq_table[] = { |
| 76 | {0x08, 14}, {0x09, 25}, {0x0A, 56}, {0x0B, 112}, |
| 77 | {0x0C, 224}, {0x0D, 448}, {0x0E, 896}, {0x0F, 1792} |
| 78 | }; |
| 79 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 80 | /* Used to map scan mask bits to their corresponding channel register. */ |
| 81 | static const int stk8ba50_channel_table[] = { |
| 82 | STK8BA50_REG_XOUT, |
| 83 | STK8BA50_REG_YOUT, |
| 84 | STK8BA50_REG_ZOUT |
| 85 | }; |
| 86 | |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 87 | struct stk8ba50_data { |
| 88 | struct i2c_client *client; |
| 89 | struct mutex lock; |
| 90 | int range; |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 91 | u8 sample_rate_idx; |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 92 | struct iio_trigger *dready_trig; |
| 93 | bool dready_trigger_on; |
| 94 | /* |
| 95 | * 3 x 16-bit channels (10-bit data, 6-bit padding) + |
| 96 | * 1 x 16 padding + |
| 97 | * 4 x 16 64-bit timestamp |
| 98 | */ |
| 99 | s16 buffer[8]; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 100 | }; |
| 101 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 102 | #define STK8BA50_ACCEL_CHANNEL(index, reg, axis) { \ |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 103 | .type = IIO_ACCEL, \ |
| 104 | .address = reg, \ |
| 105 | .modified = 1, \ |
| 106 | .channel2 = IIO_MOD_##axis, \ |
| 107 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 108 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
| 109 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 110 | .scan_index = index, \ |
| 111 | .scan_type = { \ |
| 112 | .sign = 's', \ |
| 113 | .realbits = 10, \ |
| 114 | .storagebits = 16, \ |
| 115 | .shift = STK8BA50_DATA_SHIFT, \ |
| 116 | .endianness = IIO_CPU, \ |
| 117 | }, \ |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static const struct iio_chan_spec stk8ba50_channels[] = { |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 121 | STK8BA50_ACCEL_CHANNEL(0, STK8BA50_REG_XOUT, X), |
| 122 | STK8BA50_ACCEL_CHANNEL(1, STK8BA50_REG_YOUT, Y), |
| 123 | STK8BA50_ACCEL_CHANNEL(2, STK8BA50_REG_ZOUT, Z), |
| 124 | IIO_CHAN_SOFT_TIMESTAMP(3), |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static IIO_CONST_ATTR(in_accel_scale_available, STK8BA50_SCALE_AVAIL); |
| 128 | |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 129 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("14 25 56 112 224 448 896 1792"); |
| 130 | |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 131 | static struct attribute *stk8ba50_attributes[] = { |
| 132 | &iio_const_attr_in_accel_scale_available.dev_attr.attr, |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 133 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 134 | NULL, |
| 135 | }; |
| 136 | |
| 137 | static const struct attribute_group stk8ba50_attribute_group = { |
| 138 | .attrs = stk8ba50_attributes |
| 139 | }; |
| 140 | |
| 141 | static int stk8ba50_read_accel(struct stk8ba50_data *data, u8 reg) |
| 142 | { |
| 143 | int ret; |
| 144 | struct i2c_client *client = data->client; |
| 145 | |
| 146 | ret = i2c_smbus_read_word_data(client, reg); |
| 147 | if (ret < 0) { |
| 148 | dev_err(&client->dev, "register read failed\n"); |
| 149 | return ret; |
| 150 | } |
| 151 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static int stk8ba50_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 156 | bool state) |
| 157 | { |
| 158 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 159 | struct stk8ba50_data *data = iio_priv(indio_dev); |
| 160 | int ret; |
| 161 | |
| 162 | if (state) |
| 163 | ret = i2c_smbus_write_byte_data(data->client, |
| 164 | STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK); |
| 165 | else |
| 166 | ret = i2c_smbus_write_byte_data(data->client, |
| 167 | STK8BA50_REG_INTEN2, 0x00); |
| 168 | |
| 169 | if (ret < 0) |
| 170 | dev_err(&data->client->dev, "failed to set trigger state\n"); |
| 171 | else |
| 172 | data->dready_trigger_on = state; |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static const struct iio_trigger_ops stk8ba50_trigger_ops = { |
| 178 | .set_trigger_state = stk8ba50_data_rdy_trigger_set_state, |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | static int stk8ba50_set_power(struct stk8ba50_data *data, bool mode) |
| 182 | { |
| 183 | int ret; |
| 184 | u8 masked_reg; |
| 185 | struct i2c_client *client = data->client; |
| 186 | |
| 187 | ret = i2c_smbus_read_byte_data(client, STK8BA50_REG_POWMODE); |
| 188 | if (ret < 0) |
| 189 | goto exit_err; |
| 190 | |
| 191 | if (mode) |
| 192 | masked_reg = ret | STK8BA50_MODE_POWERBIT; |
| 193 | else |
| 194 | masked_reg = ret & (~STK8BA50_MODE_POWERBIT); |
| 195 | |
| 196 | ret = i2c_smbus_write_byte_data(client, STK8BA50_REG_POWMODE, |
| 197 | masked_reg); |
| 198 | if (ret < 0) |
| 199 | goto exit_err; |
| 200 | |
| 201 | return ret; |
| 202 | |
| 203 | exit_err: |
| 204 | dev_err(&client->dev, "failed to change sensor mode\n"); |
| 205 | return ret; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static int stk8ba50_read_raw(struct iio_dev *indio_dev, |
| 209 | struct iio_chan_spec const *chan, |
| 210 | int *val, int *val2, long mask) |
| 211 | { |
| 212 | struct stk8ba50_data *data = iio_priv(indio_dev); |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 213 | int ret; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 214 | |
| 215 | switch (mask) { |
| 216 | case IIO_CHAN_INFO_RAW: |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 217 | if (iio_buffer_enabled(indio_dev)) |
| 218 | return -EBUSY; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 219 | mutex_lock(&data->lock); |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 220 | ret = stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); |
| 221 | if (ret < 0) { |
| 222 | mutex_unlock(&data->lock); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | ret = stk8ba50_read_accel(data, chan->address); |
| 226 | if (ret < 0) { |
| 227 | stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); |
| 228 | mutex_unlock(&data->lock); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | *val = sign_extend32(ret >> STK8BA50_DATA_SHIFT, 9); |
| 232 | stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 233 | mutex_unlock(&data->lock); |
| 234 | return IIO_VAL_INT; |
| 235 | case IIO_CHAN_INFO_SCALE: |
| 236 | *val = 0; |
Tiberiu Breana | 003f488 | 2015-06-10 18:07:29 +0300 | [diff] [blame] | 237 | *val2 = stk8ba50_scale_table[data->range].scale_val; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 238 | return IIO_VAL_INT_PLUS_MICRO; |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 239 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 240 | *val = stk8ba50_samp_freq_table |
| 241 | [data->sample_rate_idx].samp_freq; |
| 242 | *val2 = 0; |
| 243 | return IIO_VAL_INT; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | static int stk8ba50_write_raw(struct iio_dev *indio_dev, |
| 250 | struct iio_chan_spec const *chan, |
| 251 | int val, int val2, long mask) |
| 252 | { |
| 253 | int ret; |
| 254 | int i; |
| 255 | int index = -1; |
| 256 | struct stk8ba50_data *data = iio_priv(indio_dev); |
| 257 | |
| 258 | switch (mask) { |
| 259 | case IIO_CHAN_INFO_SCALE: |
| 260 | if (val != 0) |
| 261 | return -EINVAL; |
| 262 | |
| 263 | for (i = 0; i < ARRAY_SIZE(stk8ba50_scale_table); i++) |
Tiberiu Breana | 003f488 | 2015-06-10 18:07:29 +0300 | [diff] [blame] | 264 | if (val2 == stk8ba50_scale_table[i].scale_val) { |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 265 | index = i; |
| 266 | break; |
| 267 | } |
| 268 | if (index < 0) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | ret = i2c_smbus_write_byte_data(data->client, |
| 272 | STK8BA50_REG_RANGE, |
Tiberiu Breana | 003f488 | 2015-06-10 18:07:29 +0300 | [diff] [blame] | 273 | stk8ba50_scale_table[index].reg_val); |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 274 | if (ret < 0) |
| 275 | dev_err(&data->client->dev, |
| 276 | "failed to set measurement range\n"); |
| 277 | else |
| 278 | data->range = index; |
| 279 | |
| 280 | return ret; |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 281 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 282 | for (i = 0; i < ARRAY_SIZE(stk8ba50_samp_freq_table); i++) |
| 283 | if (val == stk8ba50_samp_freq_table[i].samp_freq) { |
| 284 | index = i; |
| 285 | break; |
| 286 | } |
| 287 | if (index < 0) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | ret = i2c_smbus_write_byte_data(data->client, |
| 291 | STK8BA50_REG_BWSEL, |
| 292 | stk8ba50_samp_freq_table[index].reg_val); |
| 293 | if (ret < 0) |
| 294 | dev_err(&data->client->dev, |
| 295 | "failed to set sampling rate\n"); |
| 296 | else |
| 297 | data->sample_rate_idx = index; |
| 298 | |
| 299 | return ret; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | static const struct iio_info stk8ba50_info = { |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 306 | .read_raw = stk8ba50_read_raw, |
| 307 | .write_raw = stk8ba50_write_raw, |
| 308 | .attrs = &stk8ba50_attribute_group, |
| 309 | }; |
| 310 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 311 | static irqreturn_t stk8ba50_trigger_handler(int irq, void *p) |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 312 | { |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 313 | struct iio_poll_func *pf = p; |
| 314 | struct iio_dev *indio_dev = pf->indio_dev; |
| 315 | struct stk8ba50_data *data = iio_priv(indio_dev); |
| 316 | int bit, ret, i = 0; |
| 317 | |
| 318 | mutex_lock(&data->lock); |
| 319 | /* |
| 320 | * Do a bulk read if all channels are requested, |
| 321 | * from 0x02 (XOUT1) to 0x07 (ZOUT2) |
| 322 | */ |
| 323 | if (*(indio_dev->active_scan_mask) == STK8BA50_ALL_CHANNEL_MASK) { |
| 324 | ret = i2c_smbus_read_i2c_block_data(data->client, |
| 325 | STK8BA50_REG_XOUT, |
| 326 | STK8BA50_ALL_CHANNEL_SIZE, |
| 327 | (u8 *)data->buffer); |
| 328 | if (ret < STK8BA50_ALL_CHANNEL_SIZE) { |
| 329 | dev_err(&data->client->dev, "register read failed\n"); |
| 330 | goto err; |
| 331 | } |
| 332 | } else { |
| 333 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 334 | indio_dev->masklength) { |
| 335 | ret = stk8ba50_read_accel(data, |
| 336 | stk8ba50_channel_table[bit]); |
| 337 | if (ret < 0) |
| 338 | goto err; |
| 339 | |
| 340 | data->buffer[i++] = ret; |
| 341 | } |
| 342 | } |
| 343 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 344 | pf->timestamp); |
| 345 | err: |
| 346 | mutex_unlock(&data->lock); |
| 347 | iio_trigger_notify_done(indio_dev->trig); |
| 348 | |
| 349 | return IRQ_HANDLED; |
| 350 | } |
| 351 | |
| 352 | static irqreturn_t stk8ba50_data_rdy_trig_poll(int irq, void *private) |
| 353 | { |
| 354 | struct iio_dev *indio_dev = private; |
| 355 | struct stk8ba50_data *data = iio_priv(indio_dev); |
| 356 | |
| 357 | if (data->dready_trigger_on) |
| 358 | iio_trigger_poll(data->dready_trig); |
| 359 | |
| 360 | return IRQ_HANDLED; |
| 361 | } |
| 362 | |
| 363 | static int stk8ba50_buffer_preenable(struct iio_dev *indio_dev) |
| 364 | { |
| 365 | struct stk8ba50_data *data = iio_priv(indio_dev); |
| 366 | |
| 367 | return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); |
| 368 | } |
| 369 | |
| 370 | static int stk8ba50_buffer_postdisable(struct iio_dev *indio_dev) |
| 371 | { |
| 372 | struct stk8ba50_data *data = iio_priv(indio_dev); |
| 373 | |
| 374 | return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); |
| 375 | } |
| 376 | |
| 377 | static const struct iio_buffer_setup_ops stk8ba50_buffer_setup_ops = { |
| 378 | .preenable = stk8ba50_buffer_preenable, |
| 379 | .postenable = iio_triggered_buffer_postenable, |
| 380 | .predisable = iio_triggered_buffer_predisable, |
| 381 | .postdisable = stk8ba50_buffer_postdisable, |
| 382 | }; |
| 383 | |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 384 | static int stk8ba50_probe(struct i2c_client *client, |
| 385 | const struct i2c_device_id *id) |
| 386 | { |
| 387 | int ret; |
| 388 | struct iio_dev *indio_dev; |
| 389 | struct stk8ba50_data *data; |
| 390 | |
| 391 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 392 | if (!indio_dev) { |
| 393 | dev_err(&client->dev, "iio allocation failed!\n"); |
| 394 | return -ENOMEM; |
| 395 | } |
| 396 | |
| 397 | data = iio_priv(indio_dev); |
| 398 | data->client = client; |
| 399 | i2c_set_clientdata(client, indio_dev); |
| 400 | mutex_init(&data->lock); |
| 401 | |
| 402 | indio_dev->dev.parent = &client->dev; |
| 403 | indio_dev->info = &stk8ba50_info; |
| 404 | indio_dev->name = STK8BA50_DRIVER_NAME; |
| 405 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 406 | indio_dev->channels = stk8ba50_channels; |
| 407 | indio_dev->num_channels = ARRAY_SIZE(stk8ba50_channels); |
| 408 | |
| 409 | /* Reset all registers on startup */ |
| 410 | ret = i2c_smbus_write_byte_data(client, |
| 411 | STK8BA50_REG_SWRST, STK8BA50_RESET_CMD); |
| 412 | if (ret < 0) { |
| 413 | dev_err(&client->dev, "failed to reset sensor\n"); |
Tiberiu Breana | db42a9b | 2015-06-19 17:56:37 +0300 | [diff] [blame] | 414 | goto err_power_off; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* The default range is +/-2g */ |
| 418 | data->range = 0; |
| 419 | |
Tiberiu Breana | eb2c9ce | 2015-06-10 18:07:30 +0300 | [diff] [blame] | 420 | /* The default sampling rate is 1792 Hz (maximum) */ |
| 421 | data->sample_rate_idx = STK8BA50_SR_1792HZ_IDX; |
| 422 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 423 | /* Set up interrupts */ |
| 424 | ret = i2c_smbus_write_byte_data(client, |
| 425 | STK8BA50_REG_INTEN2, STK8BA50_DREADY_INT_MASK); |
| 426 | if (ret < 0) { |
| 427 | dev_err(&client->dev, "failed to set up interrupts\n"); |
| 428 | goto err_power_off; |
| 429 | } |
| 430 | ret = i2c_smbus_write_byte_data(client, |
| 431 | STK8BA50_REG_INTMAP2, STK8BA50_DREADY_INT_MAP); |
| 432 | if (ret < 0) { |
| 433 | dev_err(&client->dev, "failed to set up interrupts\n"); |
| 434 | goto err_power_off; |
| 435 | } |
| 436 | |
Octavian Purdila | 6839c1b | 2015-09-23 12:02:00 +0300 | [diff] [blame] | 437 | if (client->irq > 0) { |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 438 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 439 | stk8ba50_data_rdy_trig_poll, |
| 440 | NULL, |
| 441 | IRQF_TRIGGER_RISING | |
| 442 | IRQF_ONESHOT, |
| 443 | STK8BA50_IRQ_NAME, |
| 444 | indio_dev); |
| 445 | if (ret < 0) { |
| 446 | dev_err(&client->dev, "request irq %d failed\n", |
| 447 | client->irq); |
| 448 | goto err_power_off; |
| 449 | } |
| 450 | |
| 451 | data->dready_trig = devm_iio_trigger_alloc(&client->dev, |
| 452 | "%s-dev%d", |
| 453 | indio_dev->name, |
| 454 | indio_dev->id); |
| 455 | if (!data->dready_trig) { |
| 456 | ret = -ENOMEM; |
| 457 | goto err_power_off; |
| 458 | } |
| 459 | |
| 460 | data->dready_trig->dev.parent = &client->dev; |
| 461 | data->dready_trig->ops = &stk8ba50_trigger_ops; |
| 462 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); |
| 463 | ret = iio_trigger_register(data->dready_trig); |
| 464 | if (ret) { |
| 465 | dev_err(&client->dev, "iio trigger register failed\n"); |
| 466 | goto err_power_off; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | ret = iio_triggered_buffer_setup(indio_dev, |
| 471 | iio_pollfunc_store_time, |
| 472 | stk8ba50_trigger_handler, |
| 473 | &stk8ba50_buffer_setup_ops); |
| 474 | if (ret < 0) { |
| 475 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
| 476 | goto err_trigger_unregister; |
| 477 | } |
| 478 | |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 479 | ret = iio_device_register(indio_dev); |
| 480 | if (ret < 0) { |
| 481 | dev_err(&client->dev, "device_register failed\n"); |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 482 | goto err_buffer_cleanup; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | return ret; |
Tiberiu Breana | db42a9b | 2015-06-19 17:56:37 +0300 | [diff] [blame] | 486 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 487 | err_buffer_cleanup: |
| 488 | iio_triggered_buffer_cleanup(indio_dev); |
| 489 | err_trigger_unregister: |
| 490 | if (data->dready_trig) |
| 491 | iio_trigger_unregister(data->dready_trig); |
Tiberiu Breana | db42a9b | 2015-06-19 17:56:37 +0300 | [diff] [blame] | 492 | err_power_off: |
| 493 | stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); |
| 494 | return ret; |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int stk8ba50_remove(struct i2c_client *client) |
| 498 | { |
| 499 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 500 | struct stk8ba50_data *data = iio_priv(indio_dev); |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 501 | |
| 502 | iio_device_unregister(indio_dev); |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 503 | iio_triggered_buffer_cleanup(indio_dev); |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 504 | |
Tiberiu Breana | db6a19b | 2015-06-24 16:54:46 +0300 | [diff] [blame] | 505 | if (data->dready_trig) |
| 506 | iio_trigger_unregister(data->dready_trig); |
| 507 | |
| 508 | return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | #ifdef CONFIG_PM_SLEEP |
| 512 | static int stk8ba50_suspend(struct device *dev) |
| 513 | { |
| 514 | struct stk8ba50_data *data; |
| 515 | |
| 516 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 517 | |
| 518 | return stk8ba50_set_power(data, STK8BA50_MODE_SUSPEND); |
| 519 | } |
| 520 | |
| 521 | static int stk8ba50_resume(struct device *dev) |
| 522 | { |
| 523 | struct stk8ba50_data *data; |
| 524 | |
| 525 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 526 | |
| 527 | return stk8ba50_set_power(data, STK8BA50_MODE_NORMAL); |
| 528 | } |
| 529 | |
| 530 | static SIMPLE_DEV_PM_OPS(stk8ba50_pm_ops, stk8ba50_suspend, stk8ba50_resume); |
| 531 | |
| 532 | #define STK8BA50_PM_OPS (&stk8ba50_pm_ops) |
| 533 | #else |
| 534 | #define STK8BA50_PM_OPS NULL |
| 535 | #endif |
| 536 | |
| 537 | static const struct i2c_device_id stk8ba50_i2c_id[] = { |
| 538 | {"stk8ba50", 0}, |
| 539 | {} |
| 540 | }; |
Javier Martinez Canillas | 58e446f | 2015-07-30 18:18:28 +0200 | [diff] [blame] | 541 | MODULE_DEVICE_TABLE(i2c, stk8ba50_i2c_id); |
Tiberiu Breana | 884ca45 | 2015-05-18 14:49:50 +0300 | [diff] [blame] | 542 | |
| 543 | static const struct acpi_device_id stk8ba50_acpi_id[] = { |
| 544 | {"STK8BA50", 0}, |
| 545 | {} |
| 546 | }; |
| 547 | |
| 548 | MODULE_DEVICE_TABLE(acpi, stk8ba50_acpi_id); |
| 549 | |
| 550 | static struct i2c_driver stk8ba50_driver = { |
| 551 | .driver = { |
| 552 | .name = "stk8ba50", |
| 553 | .pm = STK8BA50_PM_OPS, |
| 554 | .acpi_match_table = ACPI_PTR(stk8ba50_acpi_id), |
| 555 | }, |
| 556 | .probe = stk8ba50_probe, |
| 557 | .remove = stk8ba50_remove, |
| 558 | .id_table = stk8ba50_i2c_id, |
| 559 | }; |
| 560 | |
| 561 | module_i2c_driver(stk8ba50_driver); |
| 562 | |
| 563 | MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); |
| 564 | MODULE_DESCRIPTION("STK8BA50 3-Axis Accelerometer driver"); |
| 565 | MODULE_LICENSE("GPL v2"); |