Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 1 | /** |
| 2 | * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor |
| 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 STK3310/STK3311. 7-bit I2C address: 0x48. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/acpi.h> |
| 14 | #include <linux/i2c.h> |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/regmap.h> |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 19 | #include <linux/iio/events.h> |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 20 | #include <linux/iio/iio.h> |
| 21 | #include <linux/iio/sysfs.h> |
| 22 | |
| 23 | #define STK3310_REG_STATE 0x00 |
| 24 | #define STK3310_REG_PSCTRL 0x01 |
| 25 | #define STK3310_REG_ALSCTRL 0x02 |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 26 | #define STK3310_REG_INT 0x04 |
| 27 | #define STK3310_REG_THDH_PS 0x06 |
| 28 | #define STK3310_REG_THDL_PS 0x08 |
| 29 | #define STK3310_REG_FLAG 0x10 |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 30 | #define STK3310_REG_PS_DATA_MSB 0x11 |
| 31 | #define STK3310_REG_PS_DATA_LSB 0x12 |
| 32 | #define STK3310_REG_ALS_DATA_MSB 0x13 |
| 33 | #define STK3310_REG_ALS_DATA_LSB 0x14 |
| 34 | #define STK3310_REG_ID 0x3E |
| 35 | #define STK3310_MAX_REG 0x80 |
| 36 | |
Hartmut Knaack | 952c3aa | 2015-07-09 23:51:32 +0200 | [diff] [blame] | 37 | #define STK3310_STATE_EN_PS BIT(0) |
| 38 | #define STK3310_STATE_EN_ALS BIT(1) |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 39 | #define STK3310_STATE_STANDBY 0x00 |
| 40 | |
| 41 | #define STK3310_CHIP_ID_VAL 0x13 |
| 42 | #define STK3311_CHIP_ID_VAL 0x1D |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 43 | #define STK3310_PSINT_EN 0x01 |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 44 | #define STK3310_PS_MAX_VAL 0xFFFF |
| 45 | |
| 46 | #define STK3310_DRIVER_NAME "stk3310" |
| 47 | #define STK3310_REGMAP_NAME "stk3310_regmap" |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 48 | #define STK3310_EVENT "stk3310_event" |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 49 | |
| 50 | #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1" |
| 51 | |
| 52 | #define STK3310_IT_AVAILABLE \ |
| 53 | "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \ |
| 54 | "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \ |
| 55 | "3.031040 6.062080" |
| 56 | |
| 57 | #define STK3310_REGFIELD(name) \ |
| 58 | do { \ |
| 59 | data->reg_##name = \ |
| 60 | devm_regmap_field_alloc(&client->dev, regmap, \ |
| 61 | stk3310_reg_field_##name); \ |
| 62 | if (IS_ERR(data->reg_##name)) { \ |
| 63 | dev_err(&client->dev, "reg field alloc failed.\n"); \ |
| 64 | return PTR_ERR(data->reg_##name); \ |
| 65 | } \ |
| 66 | } while (0) |
| 67 | |
| 68 | static const struct reg_field stk3310_reg_field_state = |
| 69 | REG_FIELD(STK3310_REG_STATE, 0, 2); |
| 70 | static const struct reg_field stk3310_reg_field_als_gain = |
| 71 | REG_FIELD(STK3310_REG_ALSCTRL, 4, 5); |
| 72 | static const struct reg_field stk3310_reg_field_ps_gain = |
| 73 | REG_FIELD(STK3310_REG_PSCTRL, 4, 5); |
| 74 | static const struct reg_field stk3310_reg_field_als_it = |
| 75 | REG_FIELD(STK3310_REG_ALSCTRL, 0, 3); |
| 76 | static const struct reg_field stk3310_reg_field_ps_it = |
| 77 | REG_FIELD(STK3310_REG_PSCTRL, 0, 3); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 78 | static const struct reg_field stk3310_reg_field_int_ps = |
| 79 | REG_FIELD(STK3310_REG_INT, 0, 2); |
| 80 | static const struct reg_field stk3310_reg_field_flag_psint = |
| 81 | REG_FIELD(STK3310_REG_FLAG, 4, 4); |
| 82 | static const struct reg_field stk3310_reg_field_flag_nf = |
| 83 | REG_FIELD(STK3310_REG_FLAG, 0, 0); |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 84 | |
| 85 | /* Estimate maximum proximity values with regard to measurement scale. */ |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 86 | static const int stk3310_ps_max[4] = { |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 87 | STK3310_PS_MAX_VAL / 640, |
| 88 | STK3310_PS_MAX_VAL / 160, |
| 89 | STK3310_PS_MAX_VAL / 40, |
| 90 | STK3310_PS_MAX_VAL / 10 |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static const int stk3310_scale_table[][2] = { |
| 94 | {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000} |
| 95 | }; |
| 96 | |
| 97 | /* Integration time in seconds, microseconds */ |
| 98 | static const int stk3310_it_table[][2] = { |
| 99 | {0, 185}, {0, 370}, {0, 741}, {0, 1480}, |
| 100 | {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680}, |
| 101 | {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880}, |
| 102 | {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080}, |
| 103 | }; |
| 104 | |
| 105 | struct stk3310_data { |
| 106 | struct i2c_client *client; |
| 107 | struct mutex lock; |
| 108 | bool als_enabled; |
| 109 | bool ps_enabled; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 110 | u64 timestamp; |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 111 | struct regmap *regmap; |
| 112 | struct regmap_field *reg_state; |
| 113 | struct regmap_field *reg_als_gain; |
| 114 | struct regmap_field *reg_ps_gain; |
| 115 | struct regmap_field *reg_als_it; |
| 116 | struct regmap_field *reg_ps_it; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 117 | struct regmap_field *reg_int_ps; |
| 118 | struct regmap_field *reg_flag_psint; |
| 119 | struct regmap_field *reg_flag_nf; |
| 120 | }; |
| 121 | |
| 122 | static const struct iio_event_spec stk3310_events[] = { |
| 123 | /* Proximity event */ |
| 124 | { |
| 125 | .type = IIO_EV_TYPE_THRESH, |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 126 | .dir = IIO_EV_DIR_RISING, |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 127 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 128 | BIT(IIO_EV_INFO_ENABLE), |
| 129 | }, |
| 130 | /* Out-of-proximity event */ |
| 131 | { |
| 132 | .type = IIO_EV_TYPE_THRESH, |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 133 | .dir = IIO_EV_DIR_FALLING, |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 134 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 135 | BIT(IIO_EV_INFO_ENABLE), |
| 136 | }, |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | static const struct iio_chan_spec stk3310_channels[] = { |
| 140 | { |
| 141 | .type = IIO_LIGHT, |
| 142 | .info_mask_separate = |
| 143 | BIT(IIO_CHAN_INFO_RAW) | |
| 144 | BIT(IIO_CHAN_INFO_SCALE) | |
| 145 | BIT(IIO_CHAN_INFO_INT_TIME), |
| 146 | }, |
| 147 | { |
| 148 | .type = IIO_PROXIMITY, |
| 149 | .info_mask_separate = |
| 150 | BIT(IIO_CHAN_INFO_RAW) | |
| 151 | BIT(IIO_CHAN_INFO_SCALE) | |
| 152 | BIT(IIO_CHAN_INFO_INT_TIME), |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 153 | .event_spec = stk3310_events, |
| 154 | .num_event_specs = ARRAY_SIZE(stk3310_events), |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 155 | } |
| 156 | }; |
| 157 | |
| 158 | static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE); |
| 159 | |
| 160 | static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE); |
| 161 | |
| 162 | static IIO_CONST_ATTR(in_illuminance_integration_time_available, |
| 163 | STK3310_IT_AVAILABLE); |
| 164 | |
| 165 | static IIO_CONST_ATTR(in_proximity_integration_time_available, |
| 166 | STK3310_IT_AVAILABLE); |
| 167 | |
| 168 | static struct attribute *stk3310_attributes[] = { |
| 169 | &iio_const_attr_in_illuminance_scale_available.dev_attr.attr, |
| 170 | &iio_const_attr_in_proximity_scale_available.dev_attr.attr, |
| 171 | &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr, |
| 172 | &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr, |
| 173 | NULL, |
| 174 | }; |
| 175 | |
| 176 | static const struct attribute_group stk3310_attribute_group = { |
| 177 | .attrs = stk3310_attributes |
| 178 | }; |
| 179 | |
| 180 | static int stk3310_get_index(const int table[][2], int table_size, |
| 181 | int val, int val2) |
| 182 | { |
| 183 | int i; |
| 184 | |
| 185 | for (i = 0; i < table_size; i++) { |
| 186 | if (val == table[i][0] && val2 == table[i][1]) |
| 187 | return i; |
| 188 | } |
| 189 | |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 193 | static int stk3310_read_event(struct iio_dev *indio_dev, |
| 194 | const struct iio_chan_spec *chan, |
| 195 | enum iio_event_type type, |
| 196 | enum iio_event_direction dir, |
| 197 | enum iio_event_info info, |
| 198 | int *val, int *val2) |
| 199 | { |
| 200 | u8 reg; |
Hartmut Knaack | 423ad0c | 2015-07-09 23:51:30 +0200 | [diff] [blame] | 201 | __be16 buf; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 202 | int ret; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 203 | struct stk3310_data *data = iio_priv(indio_dev); |
| 204 | |
| 205 | if (info != IIO_EV_INFO_VALUE) |
| 206 | return -EINVAL; |
| 207 | |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 208 | /* Only proximity interrupts are implemented at the moment. */ |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 209 | if (dir == IIO_EV_DIR_RISING) |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 210 | reg = STK3310_REG_THDH_PS; |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 211 | else if (dir == IIO_EV_DIR_FALLING) |
| 212 | reg = STK3310_REG_THDL_PS; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 213 | else |
| 214 | return -EINVAL; |
| 215 | |
| 216 | mutex_lock(&data->lock); |
| 217 | ret = regmap_bulk_read(data->regmap, reg, &buf, 2); |
| 218 | mutex_unlock(&data->lock); |
| 219 | if (ret < 0) { |
| 220 | dev_err(&data->client->dev, "register read failed\n"); |
| 221 | return ret; |
| 222 | } |
Hartmut Knaack | 423ad0c | 2015-07-09 23:51:30 +0200 | [diff] [blame] | 223 | *val = be16_to_cpu(buf); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 224 | |
| 225 | return IIO_VAL_INT; |
| 226 | } |
| 227 | |
| 228 | static int stk3310_write_event(struct iio_dev *indio_dev, |
| 229 | const struct iio_chan_spec *chan, |
| 230 | enum iio_event_type type, |
| 231 | enum iio_event_direction dir, |
| 232 | enum iio_event_info info, |
| 233 | int val, int val2) |
| 234 | { |
| 235 | u8 reg; |
Hartmut Knaack | 423ad0c | 2015-07-09 23:51:30 +0200 | [diff] [blame] | 236 | __be16 buf; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 237 | int ret; |
| 238 | unsigned int index; |
| 239 | struct stk3310_data *data = iio_priv(indio_dev); |
| 240 | struct i2c_client *client = data->client; |
| 241 | |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 242 | ret = regmap_field_read(data->reg_ps_gain, &index); |
| 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | |
| 246 | if (val < 0 || val > stk3310_ps_max[index]) |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | |
| 249 | if (dir == IIO_EV_DIR_RISING) |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 250 | reg = STK3310_REG_THDH_PS; |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 251 | else if (dir == IIO_EV_DIR_FALLING) |
| 252 | reg = STK3310_REG_THDL_PS; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 253 | else |
| 254 | return -EINVAL; |
| 255 | |
Hartmut Knaack | 423ad0c | 2015-07-09 23:51:30 +0200 | [diff] [blame] | 256 | buf = cpu_to_be16(val); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 257 | ret = regmap_bulk_write(data->regmap, reg, &buf, 2); |
| 258 | if (ret < 0) |
| 259 | dev_err(&client->dev, "failed to set PS threshold!\n"); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int stk3310_read_event_config(struct iio_dev *indio_dev, |
| 265 | const struct iio_chan_spec *chan, |
| 266 | enum iio_event_type type, |
| 267 | enum iio_event_direction dir) |
| 268 | { |
| 269 | unsigned int event_val; |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 270 | int ret; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 271 | struct stk3310_data *data = iio_priv(indio_dev); |
| 272 | |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 273 | ret = regmap_field_read(data->reg_int_ps, &event_val); |
| 274 | if (ret < 0) |
| 275 | return ret; |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 276 | |
| 277 | return event_val; |
| 278 | } |
| 279 | |
| 280 | static int stk3310_write_event_config(struct iio_dev *indio_dev, |
| 281 | const struct iio_chan_spec *chan, |
| 282 | enum iio_event_type type, |
| 283 | enum iio_event_direction dir, |
| 284 | int state) |
| 285 | { |
| 286 | int ret; |
| 287 | struct stk3310_data *data = iio_priv(indio_dev); |
| 288 | struct i2c_client *client = data->client; |
| 289 | |
| 290 | if (state < 0 || state > 7) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | /* Set INT_PS value */ |
| 294 | mutex_lock(&data->lock); |
| 295 | ret = regmap_field_write(data->reg_int_ps, state); |
| 296 | if (ret < 0) |
| 297 | dev_err(&client->dev, "failed to set interrupt mode\n"); |
| 298 | mutex_unlock(&data->lock); |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 303 | static int stk3310_read_raw(struct iio_dev *indio_dev, |
| 304 | struct iio_chan_spec const *chan, |
| 305 | int *val, int *val2, long mask) |
| 306 | { |
| 307 | u8 reg; |
Hartmut Knaack | 423ad0c | 2015-07-09 23:51:30 +0200 | [diff] [blame] | 308 | __be16 buf; |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 309 | int ret; |
| 310 | unsigned int index; |
| 311 | struct stk3310_data *data = iio_priv(indio_dev); |
| 312 | struct i2c_client *client = data->client; |
| 313 | |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 314 | if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY) |
| 315 | return -EINVAL; |
| 316 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 317 | switch (mask) { |
| 318 | case IIO_CHAN_INFO_RAW: |
| 319 | if (chan->type == IIO_LIGHT) |
| 320 | reg = STK3310_REG_ALS_DATA_MSB; |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 321 | else |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 322 | reg = STK3310_REG_PS_DATA_MSB; |
| 323 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 324 | mutex_lock(&data->lock); |
| 325 | ret = regmap_bulk_read(data->regmap, reg, &buf, 2); |
| 326 | if (ret < 0) { |
| 327 | dev_err(&client->dev, "register read failed\n"); |
| 328 | mutex_unlock(&data->lock); |
| 329 | return ret; |
| 330 | } |
Hartmut Knaack | 423ad0c | 2015-07-09 23:51:30 +0200 | [diff] [blame] | 331 | *val = be16_to_cpu(buf); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 332 | mutex_unlock(&data->lock); |
| 333 | return IIO_VAL_INT; |
| 334 | case IIO_CHAN_INFO_INT_TIME: |
| 335 | if (chan->type == IIO_LIGHT) |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 336 | ret = regmap_field_read(data->reg_als_it, &index); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 337 | else |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 338 | ret = regmap_field_read(data->reg_ps_it, &index); |
| 339 | if (ret < 0) |
| 340 | return ret; |
| 341 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 342 | *val = stk3310_it_table[index][0]; |
| 343 | *val2 = stk3310_it_table[index][1]; |
| 344 | return IIO_VAL_INT_PLUS_MICRO; |
| 345 | case IIO_CHAN_INFO_SCALE: |
| 346 | if (chan->type == IIO_LIGHT) |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 347 | ret = regmap_field_read(data->reg_als_gain, &index); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 348 | else |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 349 | ret = regmap_field_read(data->reg_ps_gain, &index); |
| 350 | if (ret < 0) |
| 351 | return ret; |
| 352 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 353 | *val = stk3310_scale_table[index][0]; |
| 354 | *val2 = stk3310_scale_table[index][1]; |
| 355 | return IIO_VAL_INT_PLUS_MICRO; |
| 356 | } |
| 357 | |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | |
| 361 | static int stk3310_write_raw(struct iio_dev *indio_dev, |
| 362 | struct iio_chan_spec const *chan, |
| 363 | int val, int val2, long mask) |
| 364 | { |
| 365 | int ret; |
Dan Carpenter | ed6e75c | 2015-05-27 11:24:25 +0300 | [diff] [blame] | 366 | int index; |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 367 | struct stk3310_data *data = iio_priv(indio_dev); |
| 368 | |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 369 | if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY) |
| 370 | return -EINVAL; |
| 371 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 372 | switch (mask) { |
| 373 | case IIO_CHAN_INFO_INT_TIME: |
| 374 | index = stk3310_get_index(stk3310_it_table, |
| 375 | ARRAY_SIZE(stk3310_it_table), |
| 376 | val, val2); |
| 377 | if (index < 0) |
| 378 | return -EINVAL; |
| 379 | mutex_lock(&data->lock); |
| 380 | if (chan->type == IIO_LIGHT) |
| 381 | ret = regmap_field_write(data->reg_als_it, index); |
| 382 | else |
| 383 | ret = regmap_field_write(data->reg_ps_it, index); |
| 384 | if (ret < 0) |
| 385 | dev_err(&data->client->dev, |
Hartmut Knaack | 5b958f1 | 2015-07-09 23:51:33 +0200 | [diff] [blame] | 386 | "sensor configuration failed\n"); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 387 | mutex_unlock(&data->lock); |
| 388 | return ret; |
| 389 | |
| 390 | case IIO_CHAN_INFO_SCALE: |
| 391 | index = stk3310_get_index(stk3310_scale_table, |
| 392 | ARRAY_SIZE(stk3310_scale_table), |
| 393 | val, val2); |
| 394 | if (index < 0) |
| 395 | return -EINVAL; |
| 396 | mutex_lock(&data->lock); |
| 397 | if (chan->type == IIO_LIGHT) |
| 398 | ret = regmap_field_write(data->reg_als_gain, index); |
| 399 | else |
| 400 | ret = regmap_field_write(data->reg_ps_gain, index); |
| 401 | if (ret < 0) |
| 402 | dev_err(&data->client->dev, |
Hartmut Knaack | 5b958f1 | 2015-07-09 23:51:33 +0200 | [diff] [blame] | 403 | "sensor configuration failed\n"); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 404 | mutex_unlock(&data->lock); |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | static const struct iio_info stk3310_info = { |
| 412 | .driver_module = THIS_MODULE, |
| 413 | .read_raw = stk3310_read_raw, |
| 414 | .write_raw = stk3310_write_raw, |
| 415 | .attrs = &stk3310_attribute_group, |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 416 | .read_event_value = stk3310_read_event, |
| 417 | .write_event_value = stk3310_write_event, |
| 418 | .read_event_config = stk3310_read_event_config, |
| 419 | .write_event_config = stk3310_write_event_config, |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | static int stk3310_set_state(struct stk3310_data *data, u8 state) |
| 423 | { |
| 424 | int ret; |
| 425 | struct i2c_client *client = data->client; |
| 426 | |
| 427 | /* 3-bit state; 0b100 is not supported. */ |
| 428 | if (state > 7 || state == 4) |
| 429 | return -EINVAL; |
| 430 | |
| 431 | mutex_lock(&data->lock); |
| 432 | ret = regmap_field_write(data->reg_state, state); |
| 433 | if (ret < 0) { |
| 434 | dev_err(&client->dev, "failed to change sensor state\n"); |
| 435 | } else if (state != STK3310_STATE_STANDBY) { |
| 436 | /* Don't reset the 'enabled' flags if we're going in standby */ |
Hartmut Knaack | 952c3aa | 2015-07-09 23:51:32 +0200 | [diff] [blame] | 437 | data->ps_enabled = !!(state & STK3310_STATE_EN_PS); |
| 438 | data->als_enabled = !!(state & STK3310_STATE_EN_ALS); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 439 | } |
| 440 | mutex_unlock(&data->lock); |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
| 445 | static int stk3310_init(struct iio_dev *indio_dev) |
| 446 | { |
| 447 | int ret; |
| 448 | int chipid; |
| 449 | u8 state; |
| 450 | struct stk3310_data *data = iio_priv(indio_dev); |
| 451 | struct i2c_client *client = data->client; |
| 452 | |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 453 | ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid); |
| 454 | if (ret < 0) |
| 455 | return ret; |
| 456 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 457 | if (chipid != STK3310_CHIP_ID_VAL && |
| 458 | chipid != STK3311_CHIP_ID_VAL) { |
| 459 | dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid); |
| 460 | return -ENODEV; |
| 461 | } |
| 462 | |
| 463 | state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS; |
| 464 | ret = stk3310_set_state(data, state); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 465 | if (ret < 0) { |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 466 | dev_err(&client->dev, "failed to enable sensor"); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | /* Enable PS interrupts */ |
| 471 | ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN); |
| 472 | if (ret < 0) |
| 473 | dev_err(&client->dev, "failed to enable interrupts!\n"); |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 478 | static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg) |
| 479 | { |
| 480 | switch (reg) { |
| 481 | case STK3310_REG_ALS_DATA_MSB: |
| 482 | case STK3310_REG_ALS_DATA_LSB: |
| 483 | case STK3310_REG_PS_DATA_LSB: |
| 484 | case STK3310_REG_PS_DATA_MSB: |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 485 | case STK3310_REG_FLAG: |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 486 | return true; |
| 487 | default: |
| 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | static struct regmap_config stk3310_regmap_config = { |
| 493 | .name = STK3310_REGMAP_NAME, |
| 494 | .reg_bits = 8, |
| 495 | .val_bits = 8, |
| 496 | .max_register = STK3310_MAX_REG, |
| 497 | .cache_type = REGCACHE_RBTREE, |
| 498 | .volatile_reg = stk3310_is_volatile_reg, |
| 499 | }; |
| 500 | |
| 501 | static int stk3310_regmap_init(struct stk3310_data *data) |
| 502 | { |
| 503 | struct regmap *regmap; |
| 504 | struct i2c_client *client; |
| 505 | |
| 506 | client = data->client; |
| 507 | regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config); |
| 508 | if (IS_ERR(regmap)) { |
| 509 | dev_err(&client->dev, "regmap initialization failed.\n"); |
| 510 | return PTR_ERR(regmap); |
| 511 | } |
| 512 | data->regmap = regmap; |
| 513 | |
| 514 | STK3310_REGFIELD(state); |
| 515 | STK3310_REGFIELD(als_gain); |
| 516 | STK3310_REGFIELD(ps_gain); |
| 517 | STK3310_REGFIELD(als_it); |
| 518 | STK3310_REGFIELD(ps_it); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 519 | STK3310_REGFIELD(int_ps); |
| 520 | STK3310_REGFIELD(flag_psint); |
| 521 | STK3310_REGFIELD(flag_nf); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 526 | static irqreturn_t stk3310_irq_handler(int irq, void *private) |
| 527 | { |
| 528 | struct iio_dev *indio_dev = private; |
| 529 | struct stk3310_data *data = iio_priv(indio_dev); |
| 530 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 531 | data->timestamp = iio_get_time_ns(indio_dev); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 532 | |
| 533 | return IRQ_WAKE_THREAD; |
| 534 | } |
| 535 | |
| 536 | static irqreturn_t stk3310_irq_event_handler(int irq, void *private) |
| 537 | { |
| 538 | int ret; |
| 539 | unsigned int dir; |
| 540 | u64 event; |
| 541 | |
| 542 | struct iio_dev *indio_dev = private; |
| 543 | struct stk3310_data *data = iio_priv(indio_dev); |
| 544 | |
| 545 | /* Read FLAG_NF to figure out what threshold has been met. */ |
| 546 | mutex_lock(&data->lock); |
| 547 | ret = regmap_field_read(data->reg_flag_nf, &dir); |
| 548 | if (ret < 0) { |
| 549 | dev_err(&data->client->dev, "register read failed\n"); |
| 550 | mutex_unlock(&data->lock); |
| 551 | return ret; |
| 552 | } |
| 553 | event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1, |
| 554 | IIO_EV_TYPE_THRESH, |
Tiberiu Breana | 0f16fc8 | 2015-07-01 15:32:00 +0300 | [diff] [blame] | 555 | (dir ? IIO_EV_DIR_FALLING : |
| 556 | IIO_EV_DIR_RISING)); |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 557 | iio_push_event(indio_dev, event, data->timestamp); |
| 558 | |
| 559 | /* Reset the interrupt flag */ |
| 560 | ret = regmap_field_write(data->reg_flag_psint, 0); |
| 561 | if (ret < 0) |
| 562 | dev_err(&data->client->dev, "failed to reset interrupts\n"); |
| 563 | mutex_unlock(&data->lock); |
| 564 | |
| 565 | return IRQ_HANDLED; |
| 566 | } |
| 567 | |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 568 | static int stk3310_probe(struct i2c_client *client, |
| 569 | const struct i2c_device_id *id) |
| 570 | { |
| 571 | int ret; |
| 572 | struct iio_dev *indio_dev; |
| 573 | struct stk3310_data *data; |
| 574 | |
| 575 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 576 | if (!indio_dev) { |
| 577 | dev_err(&client->dev, "iio allocation failed!\n"); |
| 578 | return -ENOMEM; |
| 579 | } |
| 580 | |
| 581 | data = iio_priv(indio_dev); |
| 582 | data->client = client; |
| 583 | i2c_set_clientdata(client, indio_dev); |
| 584 | mutex_init(&data->lock); |
| 585 | |
| 586 | ret = stk3310_regmap_init(data); |
| 587 | if (ret < 0) |
| 588 | return ret; |
| 589 | |
| 590 | indio_dev->dev.parent = &client->dev; |
| 591 | indio_dev->info = &stk3310_info; |
| 592 | indio_dev->name = STK3310_DRIVER_NAME; |
| 593 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 594 | indio_dev->channels = stk3310_channels; |
| 595 | indio_dev->num_channels = ARRAY_SIZE(stk3310_channels); |
| 596 | |
| 597 | ret = stk3310_init(indio_dev); |
| 598 | if (ret < 0) |
| 599 | return ret; |
| 600 | |
Octavian Purdila | 6839c1b | 2015-09-23 12:02:00 +0300 | [diff] [blame] | 601 | if (client->irq > 0) { |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 602 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 603 | stk3310_irq_handler, |
| 604 | stk3310_irq_event_handler, |
| 605 | IRQF_TRIGGER_FALLING | |
| 606 | IRQF_ONESHOT, |
| 607 | STK3310_EVENT, indio_dev); |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 608 | if (ret < 0) { |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 609 | dev_err(&client->dev, "request irq %d failed\n", |
Hartmut Knaack | 5b958f1 | 2015-07-09 23:51:33 +0200 | [diff] [blame] | 610 | client->irq); |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 611 | goto err_standby; |
| 612 | } |
Tiberiu Breana | 3dd477a | 2015-04-27 18:34:01 +0300 | [diff] [blame] | 613 | } |
| 614 | |
Hartmut Knaack | 037e966 | 2015-07-09 23:51:29 +0200 | [diff] [blame] | 615 | ret = iio_device_register(indio_dev); |
| 616 | if (ret < 0) { |
| 617 | dev_err(&client->dev, "device_register failed\n"); |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 618 | goto err_standby; |
Hartmut Knaack | 037e966 | 2015-07-09 23:51:29 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Hartmut Knaack | 7c7a9eeaa | 2015-07-09 23:51:31 +0200 | [diff] [blame] | 621 | return 0; |
| 622 | |
| 623 | err_standby: |
| 624 | stk3310_set_state(data, STK3310_STATE_STANDBY); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 625 | return ret; |
| 626 | } |
| 627 | |
| 628 | static int stk3310_remove(struct i2c_client *client) |
| 629 | { |
| 630 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 631 | |
| 632 | iio_device_unregister(indio_dev); |
| 633 | return stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY); |
| 634 | } |
| 635 | |
| 636 | #ifdef CONFIG_PM_SLEEP |
| 637 | static int stk3310_suspend(struct device *dev) |
| 638 | { |
| 639 | struct stk3310_data *data; |
| 640 | |
| 641 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 642 | |
| 643 | return stk3310_set_state(data, STK3310_STATE_STANDBY); |
| 644 | } |
| 645 | |
| 646 | static int stk3310_resume(struct device *dev) |
| 647 | { |
Hartmut Knaack | 952c3aa | 2015-07-09 23:51:32 +0200 | [diff] [blame] | 648 | u8 state = 0; |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 649 | struct stk3310_data *data; |
| 650 | |
| 651 | data = iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 652 | if (data->ps_enabled) |
| 653 | state |= STK3310_STATE_EN_PS; |
| 654 | if (data->als_enabled) |
| 655 | state |= STK3310_STATE_EN_ALS; |
| 656 | |
| 657 | return stk3310_set_state(data, state); |
| 658 | } |
| 659 | |
| 660 | static SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend, stk3310_resume); |
| 661 | |
| 662 | #define STK3310_PM_OPS (&stk3310_pm_ops) |
| 663 | #else |
| 664 | #define STK3310_PM_OPS NULL |
| 665 | #endif |
| 666 | |
| 667 | static const struct i2c_device_id stk3310_i2c_id[] = { |
| 668 | {"STK3310", 0}, |
| 669 | {"STK3311", 0}, |
| 670 | {} |
| 671 | }; |
Javier Martinez Canillas | 58e446f | 2015-07-30 18:18:28 +0200 | [diff] [blame] | 672 | MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id); |
Tiberiu Breana | be9e622 | 2015-04-27 18:34:00 +0300 | [diff] [blame] | 673 | |
| 674 | static const struct acpi_device_id stk3310_acpi_id[] = { |
| 675 | {"STK3310", 0}, |
| 676 | {"STK3311", 0}, |
| 677 | {} |
| 678 | }; |
| 679 | |
| 680 | MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id); |
| 681 | |
| 682 | static struct i2c_driver stk3310_driver = { |
| 683 | .driver = { |
| 684 | .name = "stk3310", |
| 685 | .pm = STK3310_PM_OPS, |
| 686 | .acpi_match_table = ACPI_PTR(stk3310_acpi_id), |
| 687 | }, |
| 688 | .probe = stk3310_probe, |
| 689 | .remove = stk3310_remove, |
| 690 | .id_table = stk3310_i2c_id, |
| 691 | }; |
| 692 | |
| 693 | module_i2c_driver(stk3310_driver); |
| 694 | |
| 695 | MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); |
| 696 | MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver"); |
| 697 | MODULE_LICENSE("GPL v2"); |