Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 1 | /** |
| 2 | * Sensortek STK8312 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 | * IIO driver for STK8312; 7-bit I2C address: 0x3D. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/acpi.h> |
| 14 | #include <linux/i2c.h> |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/delay.h> |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 19 | #include <linux/iio/buffer.h> |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 20 | #include <linux/iio/iio.h> |
| 21 | #include <linux/iio/sysfs.h> |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 22 | #include <linux/iio/trigger.h> |
| 23 | #include <linux/iio/triggered_buffer.h> |
| 24 | #include <linux/iio/trigger_consumer.h> |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 25 | |
| 26 | #define STK8312_REG_XOUT 0x00 |
| 27 | #define STK8312_REG_YOUT 0x01 |
| 28 | #define STK8312_REG_ZOUT 0x02 |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 29 | #define STK8312_REG_INTSU 0x06 |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 30 | #define STK8312_REG_MODE 0x07 |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 31 | #define STK8312_REG_SR 0x08 |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 32 | #define STK8312_REG_STH 0x13 |
| 33 | #define STK8312_REG_RESET 0x20 |
| 34 | #define STK8312_REG_AFECTRL 0x24 |
| 35 | #define STK8312_REG_OTPADDR 0x3D |
| 36 | #define STK8312_REG_OTPDATA 0x3E |
| 37 | #define STK8312_REG_OTPCTRL 0x3F |
| 38 | |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 39 | #define STK8312_MODE_ACTIVE BIT(0) |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 40 | #define STK8312_MODE_STANDBY 0x00 |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 41 | #define STK8312_MODE_INT_AH_PP 0xC0 /* active-high, push-pull */ |
| 42 | #define STK8312_DREADY_BIT BIT(4) |
| 43 | #define STK8312_RNG_6G 1 |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 44 | #define STK8312_RNG_SHIFT 6 |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 45 | #define STK8312_RNG_MASK GENMASK(7, 6) |
| 46 | #define STK8312_SR_MASK GENMASK(2, 0) |
| 47 | #define STK8312_SR_400HZ_IDX 0 |
| 48 | #define STK8312_ALL_CHANNEL_MASK GENMASK(2, 0) |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 49 | #define STK8312_ALL_CHANNEL_SIZE 3 |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 50 | |
| 51 | #define STK8312_DRIVER_NAME "stk8312" |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 52 | #define STK8312_IRQ_NAME "stk8312_event" |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * The accelerometer has two measurement ranges: |
| 56 | * |
| 57 | * -6g - +6g (8-bit, signed) |
| 58 | * -16g - +16g (8-bit, signed) |
| 59 | * |
| 60 | * scale1 = (6 + 6) * 9.81 / (2^8 - 1) = 0.4616 |
| 61 | * scale2 = (16 + 16) * 9.81 / (2^8 - 1) = 1.2311 |
| 62 | */ |
| 63 | #define STK8312_SCALE_AVAIL "0.4616 1.2311" |
| 64 | |
| 65 | static const int stk8312_scale_table[][2] = { |
| 66 | {0, 461600}, {1, 231100} |
| 67 | }; |
| 68 | |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 69 | static const struct { |
Hartmut Knaack | 7d73e02 | 2015-07-29 23:39:38 +0200 | [diff] [blame] | 70 | int val; |
| 71 | int val2; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 72 | } stk8312_samp_freq_table[] = { |
| 73 | {400, 0}, {200, 0}, {100, 0}, {50, 0}, {25, 0}, |
| 74 | {12, 500000}, {6, 250000}, {3, 125000} |
| 75 | }; |
| 76 | |
| 77 | #define STK8312_ACCEL_CHANNEL(index, reg, axis) { \ |
| 78 | .type = IIO_ACCEL, \ |
| 79 | .address = reg, \ |
| 80 | .modified = 1, \ |
| 81 | .channel2 = IIO_MOD_##axis, \ |
| 82 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 83 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 84 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 85 | .scan_index = index, \ |
| 86 | .scan_type = { \ |
| 87 | .sign = 's', \ |
| 88 | .realbits = 8, \ |
| 89 | .storagebits = 8, \ |
| 90 | .endianness = IIO_CPU, \ |
| 91 | }, \ |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static const struct iio_chan_spec stk8312_channels[] = { |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 95 | STK8312_ACCEL_CHANNEL(0, STK8312_REG_XOUT, X), |
| 96 | STK8312_ACCEL_CHANNEL(1, STK8312_REG_YOUT, Y), |
| 97 | STK8312_ACCEL_CHANNEL(2, STK8312_REG_ZOUT, Z), |
| 98 | IIO_CHAN_SOFT_TIMESTAMP(3), |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct stk8312_data { |
| 102 | struct i2c_client *client; |
| 103 | struct mutex lock; |
Hartmut Knaack | 7d73e02 | 2015-07-29 23:39:38 +0200 | [diff] [blame] | 104 | u8 range; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 105 | u8 sample_rate_idx; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 106 | u8 mode; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 107 | struct iio_trigger *dready_trig; |
| 108 | bool dready_trigger_on; |
| 109 | s8 buffer[16]; /* 3x8-bit channels + 5x8 padding + 64-bit timestamp */ |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | static IIO_CONST_ATTR(in_accel_scale_available, STK8312_SCALE_AVAIL); |
| 113 | |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 114 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("3.125 6.25 12.5 25 50 100 200 400"); |
| 115 | |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 116 | static struct attribute *stk8312_attributes[] = { |
| 117 | &iio_const_attr_in_accel_scale_available.dev_attr.attr, |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 118 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 119 | NULL, |
| 120 | }; |
| 121 | |
| 122 | static const struct attribute_group stk8312_attribute_group = { |
| 123 | .attrs = stk8312_attributes |
| 124 | }; |
| 125 | |
| 126 | static int stk8312_otp_init(struct stk8312_data *data) |
| 127 | { |
| 128 | int ret; |
| 129 | int count = 10; |
| 130 | struct i2c_client *client = data->client; |
| 131 | |
| 132 | ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPADDR, 0x70); |
| 133 | if (ret < 0) |
| 134 | goto exit_err; |
| 135 | ret = i2c_smbus_write_byte_data(client, STK8312_REG_OTPCTRL, 0x02); |
| 136 | if (ret < 0) |
| 137 | goto exit_err; |
| 138 | |
| 139 | do { |
| 140 | usleep_range(1000, 5000); |
| 141 | ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPCTRL); |
| 142 | if (ret < 0) |
| 143 | goto exit_err; |
| 144 | count--; |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 145 | } while (!(ret & BIT(7)) && count > 0); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 146 | |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 147 | if (count == 0) { |
| 148 | ret = -ETIMEDOUT; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 149 | goto exit_err; |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 150 | } |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 151 | |
| 152 | ret = i2c_smbus_read_byte_data(client, STK8312_REG_OTPDATA); |
Hartmut Knaack | b41e63c | 2015-07-28 00:49:22 +0200 | [diff] [blame] | 153 | if (ret == 0) |
| 154 | ret = -EINVAL; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 155 | if (ret < 0) |
| 156 | goto exit_err; |
| 157 | |
Hartmut Knaack | dca5d28 | 2015-07-29 23:39:39 +0200 | [diff] [blame] | 158 | ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_AFECTRL, ret); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 159 | if (ret < 0) |
| 160 | goto exit_err; |
| 161 | msleep(150); |
| 162 | |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 163 | return 0; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 164 | |
| 165 | exit_err: |
| 166 | dev_err(&client->dev, "failed to initialize sensor\n"); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int stk8312_set_mode(struct stk8312_data *data, u8 mode) |
| 171 | { |
| 172 | int ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 173 | struct i2c_client *client = data->client; |
| 174 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 175 | if (mode == data->mode) |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 176 | return 0; |
| 177 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 178 | ret = i2c_smbus_write_byte_data(client, STK8312_REG_MODE, mode); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 179 | if (ret < 0) { |
| 180 | dev_err(&client->dev, "failed to change sensor mode\n"); |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | data->mode = mode; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 185 | if (mode & STK8312_MODE_ACTIVE) { |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 186 | /* Need to run OTP sequence before entering active mode */ |
| 187 | usleep_range(1000, 5000); |
| 188 | ret = stk8312_otp_init(data); |
| 189 | } |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 194 | static int stk8312_set_interrupts(struct stk8312_data *data, u8 int_mask) |
| 195 | { |
| 196 | int ret; |
| 197 | u8 mode; |
| 198 | struct i2c_client *client = data->client; |
| 199 | |
| 200 | mode = data->mode; |
| 201 | /* We need to go in standby mode to modify registers */ |
| 202 | ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); |
| 203 | if (ret < 0) |
| 204 | return ret; |
| 205 | |
| 206 | ret = i2c_smbus_write_byte_data(client, STK8312_REG_INTSU, int_mask); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 207 | if (ret < 0) { |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 208 | dev_err(&client->dev, "failed to set interrupts\n"); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 209 | stk8312_set_mode(data, mode); |
| 210 | return ret; |
| 211 | } |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 212 | |
| 213 | return stk8312_set_mode(data, mode); |
| 214 | } |
| 215 | |
| 216 | static int stk8312_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 217 | bool state) |
| 218 | { |
| 219 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 220 | struct stk8312_data *data = iio_priv(indio_dev); |
| 221 | int ret; |
| 222 | |
| 223 | if (state) |
| 224 | ret = stk8312_set_interrupts(data, STK8312_DREADY_BIT); |
| 225 | else |
| 226 | ret = stk8312_set_interrupts(data, 0x00); |
| 227 | |
| 228 | if (ret < 0) { |
| 229 | dev_err(&data->client->dev, "failed to set trigger state\n"); |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | data->dready_trigger_on = state; |
| 234 | |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 235 | return 0; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static const struct iio_trigger_ops stk8312_trigger_ops = { |
| 239 | .set_trigger_state = stk8312_data_rdy_trigger_set_state, |
| 240 | .owner = THIS_MODULE, |
| 241 | }; |
| 242 | |
Hartmut Knaack | 7d73e02 | 2015-07-29 23:39:38 +0200 | [diff] [blame] | 243 | static int stk8312_set_sample_rate(struct stk8312_data *data, u8 rate) |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 244 | { |
| 245 | int ret; |
| 246 | u8 masked_reg; |
| 247 | u8 mode; |
| 248 | struct i2c_client *client = data->client; |
| 249 | |
| 250 | if (rate == data->sample_rate_idx) |
| 251 | return 0; |
| 252 | |
| 253 | mode = data->mode; |
| 254 | /* We need to go in standby mode to modify registers */ |
| 255 | ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); |
| 256 | if (ret < 0) |
| 257 | return ret; |
| 258 | |
| 259 | ret = i2c_smbus_read_byte_data(client, STK8312_REG_SR); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 260 | if (ret < 0) |
| 261 | goto err_activate; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 262 | |
| 263 | masked_reg = (ret & (~STK8312_SR_MASK)) | rate; |
| 264 | |
| 265 | ret = i2c_smbus_write_byte_data(client, STK8312_REG_SR, masked_reg); |
| 266 | if (ret < 0) |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 267 | goto err_activate; |
| 268 | |
| 269 | data->sample_rate_idx = rate; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 270 | |
| 271 | return stk8312_set_mode(data, mode); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 272 | |
| 273 | err_activate: |
| 274 | dev_err(&client->dev, "failed to set sampling rate\n"); |
| 275 | stk8312_set_mode(data, mode); |
| 276 | |
| 277 | return ret; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 278 | } |
| 279 | |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 280 | static int stk8312_set_range(struct stk8312_data *data, u8 range) |
| 281 | { |
| 282 | int ret; |
| 283 | u8 masked_reg; |
| 284 | u8 mode; |
| 285 | struct i2c_client *client = data->client; |
| 286 | |
| 287 | if (range != 1 && range != 2) |
| 288 | return -EINVAL; |
| 289 | else if (range == data->range) |
| 290 | return 0; |
| 291 | |
| 292 | mode = data->mode; |
| 293 | /* We need to go in standby mode to modify registers */ |
| 294 | ret = stk8312_set_mode(data, STK8312_MODE_STANDBY); |
| 295 | if (ret < 0) |
| 296 | return ret; |
| 297 | |
| 298 | ret = i2c_smbus_read_byte_data(client, STK8312_REG_STH); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 299 | if (ret < 0) |
| 300 | goto err_activate; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 301 | |
| 302 | masked_reg = ret & (~STK8312_RNG_MASK); |
| 303 | masked_reg |= range << STK8312_RNG_SHIFT; |
| 304 | |
| 305 | ret = i2c_smbus_write_byte_data(client, STK8312_REG_STH, masked_reg); |
| 306 | if (ret < 0) |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 307 | goto err_activate; |
| 308 | |
| 309 | data->range = range; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 310 | |
| 311 | return stk8312_set_mode(data, mode); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 312 | |
| 313 | err_activate: |
| 314 | dev_err(&client->dev, "failed to change sensor range\n"); |
| 315 | stk8312_set_mode(data, mode); |
| 316 | |
| 317 | return ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int stk8312_read_accel(struct stk8312_data *data, u8 address) |
| 321 | { |
| 322 | int ret; |
| 323 | struct i2c_client *client = data->client; |
| 324 | |
| 325 | if (address > 2) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | ret = i2c_smbus_read_byte_data(client, address); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 329 | if (ret < 0) |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 330 | dev_err(&client->dev, "register read failed\n"); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 331 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 332 | return ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static int stk8312_read_raw(struct iio_dev *indio_dev, |
| 336 | struct iio_chan_spec const *chan, |
| 337 | int *val, int *val2, long mask) |
| 338 | { |
| 339 | struct stk8312_data *data = iio_priv(indio_dev); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 340 | int ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 341 | |
| 342 | switch (mask) { |
| 343 | case IIO_CHAN_INFO_RAW: |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 344 | if (iio_buffer_enabled(indio_dev)) |
| 345 | return -EBUSY; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 346 | mutex_lock(&data->lock); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 347 | ret = stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE); |
| 348 | if (ret < 0) { |
| 349 | mutex_unlock(&data->lock); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 350 | return ret; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 351 | } |
| 352 | ret = stk8312_read_accel(data, chan->address); |
| 353 | if (ret < 0) { |
| 354 | stk8312_set_mode(data, |
| 355 | data->mode & (~STK8312_MODE_ACTIVE)); |
| 356 | mutex_unlock(&data->lock); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 357 | return ret; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 358 | } |
| 359 | *val = sign_extend32(ret, 7); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 360 | ret = stk8312_set_mode(data, |
| 361 | data->mode & (~STK8312_MODE_ACTIVE)); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 362 | mutex_unlock(&data->lock); |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 363 | if (ret < 0) |
| 364 | return ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 365 | return IIO_VAL_INT; |
| 366 | case IIO_CHAN_INFO_SCALE: |
| 367 | *val = stk8312_scale_table[data->range - 1][0]; |
| 368 | *val2 = stk8312_scale_table[data->range - 1][1]; |
| 369 | return IIO_VAL_INT_PLUS_MICRO; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 370 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 371 | *val = stk8312_samp_freq_table[data->sample_rate_idx].val; |
| 372 | *val2 = stk8312_samp_freq_table[data->sample_rate_idx].val2; |
| 373 | return IIO_VAL_INT_PLUS_MICRO; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | static int stk8312_write_raw(struct iio_dev *indio_dev, |
| 380 | struct iio_chan_spec const *chan, |
| 381 | int val, int val2, long mask) |
| 382 | { |
| 383 | int i; |
| 384 | int index = -1; |
| 385 | int ret; |
| 386 | struct stk8312_data *data = iio_priv(indio_dev); |
| 387 | |
| 388 | switch (mask) { |
| 389 | case IIO_CHAN_INFO_SCALE: |
| 390 | for (i = 0; i < ARRAY_SIZE(stk8312_scale_table); i++) |
| 391 | if (val == stk8312_scale_table[i][0] && |
| 392 | val2 == stk8312_scale_table[i][1]) { |
| 393 | index = i + 1; |
| 394 | break; |
| 395 | } |
| 396 | if (index < 0) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | mutex_lock(&data->lock); |
| 400 | ret = stk8312_set_range(data, index); |
| 401 | mutex_unlock(&data->lock); |
| 402 | |
| 403 | return ret; |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 404 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 405 | for (i = 0; i < ARRAY_SIZE(stk8312_samp_freq_table); i++) |
| 406 | if (val == stk8312_samp_freq_table[i].val && |
| 407 | val2 == stk8312_samp_freq_table[i].val2) { |
| 408 | index = i; |
| 409 | break; |
| 410 | } |
| 411 | if (index < 0) |
| 412 | return -EINVAL; |
| 413 | mutex_lock(&data->lock); |
| 414 | ret = stk8312_set_sample_rate(data, index); |
| 415 | mutex_unlock(&data->lock); |
| 416 | |
| 417 | return ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | static const struct iio_info stk8312_info = { |
| 424 | .driver_module = THIS_MODULE, |
| 425 | .read_raw = stk8312_read_raw, |
| 426 | .write_raw = stk8312_write_raw, |
| 427 | .attrs = &stk8312_attribute_group, |
| 428 | }; |
| 429 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 430 | static irqreturn_t stk8312_trigger_handler(int irq, void *p) |
| 431 | { |
| 432 | struct iio_poll_func *pf = p; |
| 433 | struct iio_dev *indio_dev = pf->indio_dev; |
| 434 | struct stk8312_data *data = iio_priv(indio_dev); |
| 435 | int bit, ret, i = 0; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 436 | |
| 437 | mutex_lock(&data->lock); |
| 438 | /* |
| 439 | * Do a bulk read if all channels are requested, |
| 440 | * from 0x00 (XOUT) to 0x02 (ZOUT) |
| 441 | */ |
| 442 | if (*(indio_dev->active_scan_mask) == STK8312_ALL_CHANNEL_MASK) { |
| 443 | ret = i2c_smbus_read_i2c_block_data(data->client, |
| 444 | STK8312_REG_XOUT, |
| 445 | STK8312_ALL_CHANNEL_SIZE, |
Hartmut Knaack | 7a9fbd2 | 2015-07-29 23:39:40 +0200 | [diff] [blame] | 446 | data->buffer); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 447 | if (ret < STK8312_ALL_CHANNEL_SIZE) { |
| 448 | dev_err(&data->client->dev, "register read failed\n"); |
| 449 | mutex_unlock(&data->lock); |
| 450 | goto err; |
| 451 | } |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 452 | } else { |
| 453 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
Hartmut Knaack | dca5d28 | 2015-07-29 23:39:39 +0200 | [diff] [blame] | 454 | indio_dev->masklength) { |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 455 | ret = stk8312_read_accel(data, bit); |
| 456 | if (ret < 0) { |
| 457 | mutex_unlock(&data->lock); |
| 458 | goto err; |
| 459 | } |
| 460 | data->buffer[i++] = ret; |
| 461 | } |
| 462 | } |
| 463 | mutex_unlock(&data->lock); |
| 464 | |
| 465 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 466 | pf->timestamp); |
| 467 | err: |
| 468 | iio_trigger_notify_done(indio_dev->trig); |
| 469 | |
| 470 | return IRQ_HANDLED; |
| 471 | } |
| 472 | |
| 473 | static irqreturn_t stk8312_data_rdy_trig_poll(int irq, void *private) |
| 474 | { |
| 475 | struct iio_dev *indio_dev = private; |
| 476 | struct stk8312_data *data = iio_priv(indio_dev); |
| 477 | |
| 478 | if (data->dready_trigger_on) |
| 479 | iio_trigger_poll(data->dready_trig); |
| 480 | |
| 481 | return IRQ_HANDLED; |
| 482 | } |
| 483 | |
| 484 | static int stk8312_buffer_preenable(struct iio_dev *indio_dev) |
| 485 | { |
| 486 | struct stk8312_data *data = iio_priv(indio_dev); |
| 487 | |
| 488 | return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE); |
| 489 | } |
| 490 | |
| 491 | static int stk8312_buffer_postdisable(struct iio_dev *indio_dev) |
| 492 | { |
| 493 | struct stk8312_data *data = iio_priv(indio_dev); |
| 494 | |
| 495 | return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE)); |
| 496 | } |
| 497 | |
| 498 | static const struct iio_buffer_setup_ops stk8312_buffer_setup_ops = { |
| 499 | .preenable = stk8312_buffer_preenable, |
| 500 | .postenable = iio_triggered_buffer_postenable, |
| 501 | .predisable = iio_triggered_buffer_predisable, |
| 502 | .postdisable = stk8312_buffer_postdisable, |
| 503 | }; |
| 504 | |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 505 | static int stk8312_probe(struct i2c_client *client, |
| 506 | const struct i2c_device_id *id) |
| 507 | { |
| 508 | int ret; |
| 509 | struct iio_dev *indio_dev; |
| 510 | struct stk8312_data *data; |
| 511 | |
| 512 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 513 | if (!indio_dev) { |
| 514 | dev_err(&client->dev, "iio allocation failed!\n"); |
| 515 | return -ENOMEM; |
| 516 | } |
| 517 | |
| 518 | data = iio_priv(indio_dev); |
| 519 | data->client = client; |
| 520 | i2c_set_clientdata(client, indio_dev); |
| 521 | mutex_init(&data->lock); |
| 522 | |
| 523 | indio_dev->dev.parent = &client->dev; |
| 524 | indio_dev->info = &stk8312_info; |
| 525 | indio_dev->name = STK8312_DRIVER_NAME; |
| 526 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 527 | indio_dev->channels = stk8312_channels; |
| 528 | indio_dev->num_channels = ARRAY_SIZE(stk8312_channels); |
| 529 | |
| 530 | /* A software reset is recommended at power-on */ |
| 531 | ret = i2c_smbus_write_byte_data(data->client, STK8312_REG_RESET, 0x00); |
| 532 | if (ret < 0) { |
| 533 | dev_err(&client->dev, "failed to reset sensor\n"); |
| 534 | return ret; |
| 535 | } |
Tiberiu Breana | 5e913d2 | 2015-06-24 17:01:49 +0300 | [diff] [blame] | 536 | data->sample_rate_idx = STK8312_SR_400HZ_IDX; |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 537 | ret = stk8312_set_range(data, STK8312_RNG_6G); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 538 | if (ret < 0) |
| 539 | return ret; |
| 540 | |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 541 | ret = stk8312_set_mode(data, |
| 542 | STK8312_MODE_INT_AH_PP | STK8312_MODE_ACTIVE); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 543 | if (ret < 0) |
| 544 | return ret; |
| 545 | |
Octavian Purdila | 6839c1b | 2015-09-23 12:02:00 +0300 | [diff] [blame] | 546 | if (client->irq > 0) { |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 547 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 548 | stk8312_data_rdy_trig_poll, |
| 549 | NULL, |
| 550 | IRQF_TRIGGER_RISING | |
| 551 | IRQF_ONESHOT, |
| 552 | STK8312_IRQ_NAME, |
| 553 | indio_dev); |
| 554 | if (ret < 0) { |
| 555 | dev_err(&client->dev, "request irq %d failed\n", |
| 556 | client->irq); |
| 557 | goto err_power_off; |
| 558 | } |
| 559 | |
| 560 | data->dready_trig = devm_iio_trigger_alloc(&client->dev, |
| 561 | "%s-dev%d", |
| 562 | indio_dev->name, |
| 563 | indio_dev->id); |
| 564 | if (!data->dready_trig) { |
| 565 | ret = -ENOMEM; |
| 566 | goto err_power_off; |
| 567 | } |
| 568 | |
| 569 | data->dready_trig->dev.parent = &client->dev; |
| 570 | data->dready_trig->ops = &stk8312_trigger_ops; |
| 571 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); |
| 572 | ret = iio_trigger_register(data->dready_trig); |
| 573 | if (ret) { |
| 574 | dev_err(&client->dev, "iio trigger register failed\n"); |
| 575 | goto err_power_off; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | ret = iio_triggered_buffer_setup(indio_dev, |
| 580 | iio_pollfunc_store_time, |
| 581 | stk8312_trigger_handler, |
| 582 | &stk8312_buffer_setup_ops); |
| 583 | if (ret < 0) { |
| 584 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); |
| 585 | goto err_trigger_unregister; |
| 586 | } |
| 587 | |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 588 | ret = iio_device_register(indio_dev); |
| 589 | if (ret < 0) { |
| 590 | dev_err(&client->dev, "device_register failed\n"); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 591 | goto err_buffer_cleanup; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 592 | } |
| 593 | |
Hartmut Knaack | 18422bb | 2015-07-29 23:39:36 +0200 | [diff] [blame] | 594 | return 0; |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 595 | |
| 596 | err_buffer_cleanup: |
| 597 | iio_triggered_buffer_cleanup(indio_dev); |
| 598 | err_trigger_unregister: |
| 599 | if (data->dready_trig) |
| 600 | iio_trigger_unregister(data->dready_trig); |
| 601 | err_power_off: |
| 602 | stk8312_set_mode(data, STK8312_MODE_STANDBY); |
| 603 | return ret; |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int stk8312_remove(struct i2c_client *client) |
| 607 | { |
| 608 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 609 | struct stk8312_data *data = iio_priv(indio_dev); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 610 | |
| 611 | iio_device_unregister(indio_dev); |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 612 | iio_triggered_buffer_cleanup(indio_dev); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 613 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 614 | if (data->dready_trig) |
| 615 | iio_trigger_unregister(data->dready_trig); |
| 616 | |
| 617 | return stk8312_set_mode(data, STK8312_MODE_STANDBY); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | #ifdef CONFIG_PM_SLEEP |
| 621 | static int stk8312_suspend(struct device *dev) |
| 622 | { |
| 623 | struct stk8312_data *data; |
| 624 | |
| 625 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 626 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 627 | return stk8312_set_mode(data, data->mode & (~STK8312_MODE_ACTIVE)); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int stk8312_resume(struct device *dev) |
| 631 | { |
| 632 | struct stk8312_data *data; |
| 633 | |
| 634 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 635 | |
Tiberiu Breana | 95c12bb | 2015-06-24 17:01:48 +0300 | [diff] [blame] | 636 | return stk8312_set_mode(data, data->mode | STK8312_MODE_ACTIVE); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | static SIMPLE_DEV_PM_OPS(stk8312_pm_ops, stk8312_suspend, stk8312_resume); |
| 640 | |
| 641 | #define STK8312_PM_OPS (&stk8312_pm_ops) |
| 642 | #else |
| 643 | #define STK8312_PM_OPS NULL |
| 644 | #endif |
| 645 | |
| 646 | static const struct i2c_device_id stk8312_i2c_id[] = { |
| 647 | {"STK8312", 0}, |
| 648 | {} |
| 649 | }; |
Javier Martinez Canillas | 58e446f | 2015-07-30 18:18:28 +0200 | [diff] [blame] | 650 | MODULE_DEVICE_TABLE(i2c, stk8312_i2c_id); |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 651 | |
| 652 | static const struct acpi_device_id stk8312_acpi_id[] = { |
| 653 | {"STK8312", 0}, |
| 654 | {} |
| 655 | }; |
| 656 | |
| 657 | MODULE_DEVICE_TABLE(acpi, stk8312_acpi_id); |
| 658 | |
| 659 | static struct i2c_driver stk8312_driver = { |
| 660 | .driver = { |
Hartmut Knaack | 03ecd50 | 2015-07-29 23:39:37 +0200 | [diff] [blame] | 661 | .name = STK8312_DRIVER_NAME, |
Tiberiu Breana | 90bad33 | 2015-05-12 18:48:42 +0300 | [diff] [blame] | 662 | .pm = STK8312_PM_OPS, |
| 663 | .acpi_match_table = ACPI_PTR(stk8312_acpi_id), |
| 664 | }, |
| 665 | .probe = stk8312_probe, |
| 666 | .remove = stk8312_remove, |
| 667 | .id_table = stk8312_i2c_id, |
| 668 | }; |
| 669 | |
| 670 | module_i2c_driver(stk8312_driver); |
| 671 | |
| 672 | MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); |
| 673 | MODULE_DESCRIPTION("STK8312 3-Axis Accelerometer driver"); |
| 674 | MODULE_LICENSE("GPL v2"); |