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