| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* VCNL4035 Ambient Light and Proximity Sensor - 7-bit I2C slave address 0x60 |
| 4 |
* |
| 5 |
* Copyright (c) 2018, DENX Software Engineering GmbH |
| 6 |
* Author: Parthiban Nallathambi <[email protected]> |
| 7 |
* |
| 8 |
* TODO: Proximity |
| 9 |
*/ |
| 10 |
#include <linux/bitops.h> |
| 11 |
#include <linux/bitfield.h> |
| 12 |
#include <linux/i2c.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/pm_runtime.h> |
| 15 |
#include <linux/regmap.h> |
| 16 |
|
| 17 |
#include <linux/iio/buffer.h> |
| 18 |
#include <linux/iio/events.h> |
| 19 |
#include <linux/iio/iio.h> |
| 20 |
#include <linux/iio/sysfs.h> |
| 21 |
#include <linux/iio/trigger.h> |
| 22 |
#include <linux/iio/trigger_consumer.h> |
| 23 |
#include <linux/iio/triggered_buffer.h> |
| 24 |
|
| 25 |
#define VCNL4035_DRV_NAME "vcnl4035" |
| 26 |
|
| 27 |
/* Device registers */ |
| 28 |
#define VCNL4035_ALS_CONF 0x00 |
| 29 |
#define VCNL4035_ALS_THDH 0x01 |
| 30 |
#define VCNL4035_ALS_THDL 0x02 |
| 31 |
#define VCNL4035_ALS_DATA 0x0B |
| 32 |
#define VCNL4035_WHITE_DATA 0x0C |
| 33 |
#define VCNL4035_INT_FLAG 0x0D |
| 34 |
#define VCNL4035_DEV_ID 0x0E |
| 35 |
|
| 36 |
/* Register masks */ |
| 37 |
#define VCNL4035_MODE_ALS_MASK BIT(0) |
| 38 |
#define VCNL4035_MODE_ALS_WHITE_CHAN BIT(8) |
| 39 |
#define VCNL4035_MODE_ALS_INT_MASK BIT(1) |
| 40 |
#define VCNL4035_ALS_IT_MASK GENMASK(7, 5) |
| 41 |
#define VCNL4035_ALS_PERS_MASK GENMASK(3, 2) |
| 42 |
#define VCNL4035_INT_ALS_IF_H_MASK BIT(12) |
| 43 |
#define VCNL4035_INT_ALS_IF_L_MASK BIT(13) |
| 44 |
#define VCNL4035_DEV_ID_MASK GENMASK(7, 0) |
| 45 |
|
| 46 |
/* Default values */ |
| 47 |
#define VCNL4035_MODE_ALS_ENABLE BIT(0) |
| 48 |
#define VCNL4035_MODE_ALS_DISABLE 0x00 |
| 49 |
#define VCNL4035_MODE_ALS_INT_ENABLE BIT(1) |
| 50 |
#define VCNL4035_MODE_ALS_INT_DISABLE 0 |
| 51 |
#define VCNL4035_DEV_ID_VAL 0x80 |
| 52 |
#define VCNL4035_ALS_IT_DEFAULT 0x01 |
| 53 |
#define VCNL4035_ALS_PERS_DEFAULT 0x00 |
| 54 |
#define VCNL4035_ALS_THDH_DEFAULT 5000 |
| 55 |
#define VCNL4035_ALS_THDL_DEFAULT 100 |
| 56 |
#define VCNL4035_SLEEP_DELAY_MS 2000 |
| 57 |
|
| 58 |
struct vcnl4035_data { |
| 59 |
struct i2c_client *client; |
| 60 |
struct regmap *regmap; |
| 61 |
unsigned int als_it_val; |
| 62 |
unsigned int als_persistence; |
| 63 |
unsigned int als_thresh_low; |
| 64 |
unsigned int als_thresh_high; |
| 65 |
struct iio_trigger *drdy_trigger0; |
| 66 |
}; |
| 67 |
|
| 68 |
static inline bool vcnl4035_is_triggered(struct vcnl4035_data *data) |
| 69 |
{ |
| 70 |
int ret; |
| 71 |
int reg; |
| 72 |
|
| 73 |
ret = regmap_read(data->regmap, VCNL4035_INT_FLAG, ®); |
| 74 |
if (ret < 0) |
| 75 |
return false; |
| 76 |
|
| 77 |
return !!(reg & |
| 78 |
(VCNL4035_INT_ALS_IF_H_MASK | VCNL4035_INT_ALS_IF_L_MASK)); |
| 79 |
} |
| 80 |
|
| 81 |
static irqreturn_t vcnl4035_drdy_irq_thread(int irq, void *private) |
| 82 |
{ |
| 83 |
struct iio_dev *indio_dev = private; |
| 84 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 85 |
|
| 86 |
if (vcnl4035_is_triggered(data)) { |
| 87 |
iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_LIGHT, |
| 88 |
0, |
| 89 |
IIO_EV_TYPE_THRESH, |
| 90 |
IIO_EV_DIR_EITHER), |
| 91 |
iio_get_time_ns(indio_dev)); |
| 92 |
iio_trigger_poll_nested(data->drdy_trigger0); |
| 93 |
return IRQ_HANDLED; |
| 94 |
} |
| 95 |
|
| 96 |
return IRQ_NONE; |
| 97 |
} |
| 98 |
|
| 99 |
/* Triggered buffer */ |
| 100 |
static irqreturn_t vcnl4035_trigger_consumer_handler(int irq, void *p) |
| 101 |
{ |
| 102 |
struct iio_poll_func *pf = p; |
| 103 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 104 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 105 |
/* Ensure naturally aligned timestamp */ |
| 106 |
struct { |
| 107 |
u16 als_data; |
| 108 |
aligned_s64 timestamp; |
| 109 |
} buffer = { }; |
| 110 |
unsigned int val; |
| 111 |
int ret; |
| 112 |
|
| 113 |
ret = regmap_read(data->regmap, VCNL4035_ALS_DATA, &val); |
| 114 |
if (ret < 0) { |
| 115 |
dev_err(&data->client->dev, |
| 116 |
"Trigger consumer can't read from sensor.\n"); |
| 117 |
goto fail_read; |
| 118 |
} |
| 119 |
|
| 120 |
buffer.als_data = val; |
| 121 |
iio_push_to_buffers_with_timestamp(indio_dev, &buffer, |
| 122 |
iio_get_time_ns(indio_dev)); |
| 123 |
|
| 124 |
fail_read: |
| 125 |
iio_trigger_notify_done(indio_dev->trig); |
| 126 |
|
| 127 |
return IRQ_HANDLED; |
| 128 |
} |
| 129 |
|
| 130 |
static int vcnl4035_als_drdy_set_state(struct iio_trigger *trigger, |
| 131 |
bool enable_drdy) |
| 132 |
{ |
| 133 |
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger); |
| 134 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 135 |
int val = enable_drdy ? VCNL4035_MODE_ALS_INT_ENABLE : |
| 136 |
VCNL4035_MODE_ALS_INT_DISABLE; |
| 137 |
|
| 138 |
return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 139 |
VCNL4035_MODE_ALS_INT_MASK, |
| 140 |
val); |
| 141 |
} |
| 142 |
|
| 143 |
static const struct iio_trigger_ops vcnl4035_trigger_ops = { |
| 144 |
.validate_device = iio_trigger_validate_own_device, |
| 145 |
.set_trigger_state = vcnl4035_als_drdy_set_state, |
| 146 |
}; |
| 147 |
|
| 148 |
static int vcnl4035_set_pm_runtime_state(struct vcnl4035_data *data, bool on) |
| 149 |
{ |
| 150 |
struct device *dev = &data->client->dev; |
| 151 |
|
| 152 |
if (on) |
| 153 |
return pm_runtime_resume_and_get(dev); |
| 154 |
|
| 155 |
return pm_runtime_put_autosuspend(dev); |
| 156 |
} |
| 157 |
|
| 158 |
static int vcnl4035_read_info_raw(struct iio_dev *indio_dev, |
| 159 |
struct iio_chan_spec const *chan, int *val) |
| 160 |
{ |
| 161 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 162 |
int ret; |
| 163 |
int raw_data; |
| 164 |
unsigned int reg; |
| 165 |
|
| 166 |
if (!iio_device_claim_direct(indio_dev)) |
| 167 |
return -EBUSY; |
| 168 |
|
| 169 |
if (chan->channel) |
| 170 |
reg = VCNL4035_ALS_DATA; |
| 171 |
else |
| 172 |
reg = VCNL4035_WHITE_DATA; |
| 173 |
ret = regmap_read(data->regmap, reg, &raw_data); |
| 174 |
iio_device_release_direct(indio_dev); |
| 175 |
if (ret) |
| 176 |
return ret; |
| 177 |
|
| 178 |
*val = raw_data; |
| 179 |
|
| 180 |
return IIO_VAL_INT; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Device IT INT Time (ms) Scale (lux/step) |
| 185 |
* 000 50 0.064 |
| 186 |
* 001 100 0.032 |
| 187 |
* 010 200 0.016 |
| 188 |
* 100 400 0.008 |
| 189 |
* 101 - 111 800 0.004 |
| 190 |
* Values are proportional, so ALS INT is selected for input due to |
| 191 |
* simplicity reason. Integration time value and scaling is |
| 192 |
* calculated based on device INT value |
| 193 |
* |
| 194 |
* Raw value needs to be scaled using ALS steps |
| 195 |
*/ |
| 196 |
static int vcnl4035_read_raw(struct iio_dev *indio_dev, |
| 197 |
struct iio_chan_spec const *chan, int *val, |
| 198 |
int *val2, long mask) |
| 199 |
{ |
| 200 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 201 |
int ret; |
| 202 |
|
| 203 |
switch (mask) { |
| 204 |
case IIO_CHAN_INFO_RAW: |
| 205 |
ret = vcnl4035_set_pm_runtime_state(data, true); |
| 206 |
if (ret < 0) |
| 207 |
return ret; |
| 208 |
ret = vcnl4035_read_info_raw(indio_dev, chan, val); |
| 209 |
vcnl4035_set_pm_runtime_state(data, false); |
| 210 |
return ret; |
| 211 |
case IIO_CHAN_INFO_INT_TIME: |
| 212 |
*val = 50; |
| 213 |
if (data->als_it_val) |
| 214 |
*val = data->als_it_val * 100; |
| 215 |
return IIO_VAL_INT; |
| 216 |
case IIO_CHAN_INFO_SCALE: |
| 217 |
*val = 64; |
| 218 |
if (!data->als_it_val) |
| 219 |
*val2 = 1000; |
| 220 |
else |
| 221 |
*val2 = data->als_it_val * 2 * 1000; |
| 222 |
return IIO_VAL_FRACTIONAL; |
| 223 |
default: |
| 224 |
return -EINVAL; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static int vcnl4035_write_raw(struct iio_dev *indio_dev, |
| 229 |
struct iio_chan_spec const *chan, |
| 230 |
int val, int val2, long mask) |
| 231 |
{ |
| 232 |
int ret; |
| 233 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 234 |
|
| 235 |
switch (mask) { |
| 236 |
case IIO_CHAN_INFO_INT_TIME: |
| 237 |
if (val <= 0 || val > 800) |
| 238 |
return -EINVAL; |
| 239 |
|
| 240 |
ret = vcnl4035_set_pm_runtime_state(data, true); |
| 241 |
if (ret < 0) |
| 242 |
return ret; |
| 243 |
|
| 244 |
ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 245 |
VCNL4035_ALS_IT_MASK, |
| 246 |
val / 100); |
| 247 |
if (!ret) |
| 248 |
data->als_it_val = val / 100; |
| 249 |
|
| 250 |
vcnl4035_set_pm_runtime_state(data, false); |
| 251 |
return ret; |
| 252 |
default: |
| 253 |
return -EINVAL; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
/* No direct ABI for persistence and threshold, so eventing */ |
| 258 |
static int vcnl4035_read_thresh(struct iio_dev *indio_dev, |
| 259 |
const struct iio_chan_spec *chan, enum iio_event_type type, |
| 260 |
enum iio_event_direction dir, enum iio_event_info info, |
| 261 |
int *val, int *val2) |
| 262 |
{ |
| 263 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 264 |
|
| 265 |
switch (info) { |
| 266 |
case IIO_EV_INFO_VALUE: |
| 267 |
switch (dir) { |
| 268 |
case IIO_EV_DIR_RISING: |
| 269 |
*val = data->als_thresh_high; |
| 270 |
return IIO_VAL_INT; |
| 271 |
case IIO_EV_DIR_FALLING: |
| 272 |
*val = data->als_thresh_low; |
| 273 |
return IIO_VAL_INT; |
| 274 |
default: |
| 275 |
return -EINVAL; |
| 276 |
} |
| 277 |
break; |
| 278 |
case IIO_EV_INFO_PERIOD: |
| 279 |
*val = data->als_persistence; |
| 280 |
return IIO_VAL_INT; |
| 281 |
default: |
| 282 |
return -EINVAL; |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
static int vcnl4035_write_thresh(struct iio_dev *indio_dev, |
| 288 |
const struct iio_chan_spec *chan, enum iio_event_type type, |
| 289 |
enum iio_event_direction dir, enum iio_event_info info, int val, |
| 290 |
int val2) |
| 291 |
{ |
| 292 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 293 |
int ret; |
| 294 |
|
| 295 |
switch (info) { |
| 296 |
case IIO_EV_INFO_VALUE: |
| 297 |
/* 16 bit threshold range 0 - 65535 */ |
| 298 |
if (val < 0 || val > 65535) |
| 299 |
return -EINVAL; |
| 300 |
if (dir == IIO_EV_DIR_RISING) { |
| 301 |
if (val < data->als_thresh_low) |
| 302 |
return -EINVAL; |
| 303 |
ret = regmap_write(data->regmap, VCNL4035_ALS_THDH, |
| 304 |
val); |
| 305 |
if (ret) |
| 306 |
return ret; |
| 307 |
data->als_thresh_high = val; |
| 308 |
} else { |
| 309 |
if (val > data->als_thresh_high) |
| 310 |
return -EINVAL; |
| 311 |
ret = regmap_write(data->regmap, VCNL4035_ALS_THDL, |
| 312 |
val); |
| 313 |
if (ret) |
| 314 |
return ret; |
| 315 |
data->als_thresh_low = val; |
| 316 |
} |
| 317 |
return ret; |
| 318 |
case IIO_EV_INFO_PERIOD: |
| 319 |
/* allow only 1 2 4 8 as persistence value */ |
| 320 |
if (val < 0 || val > 8 || hweight8(val) != 1) |
| 321 |
return -EINVAL; |
| 322 |
ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 323 |
VCNL4035_ALS_PERS_MASK, val); |
| 324 |
if (!ret) |
| 325 |
data->als_persistence = val; |
| 326 |
return ret; |
| 327 |
default: |
| 328 |
return -EINVAL; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
static IIO_CONST_ATTR_INT_TIME_AVAIL("50 100 200 400 800"); |
| 333 |
|
| 334 |
static struct attribute *vcnl4035_attributes[] = { |
| 335 |
&iio_const_attr_integration_time_available.dev_attr.attr, |
| 336 |
NULL, |
| 337 |
}; |
| 338 |
|
| 339 |
static const struct attribute_group vcnl4035_attribute_group = { |
| 340 |
.attrs = vcnl4035_attributes, |
| 341 |
}; |
| 342 |
|
| 343 |
static const struct iio_info vcnl4035_info = { |
| 344 |
.read_raw = vcnl4035_read_raw, |
| 345 |
.write_raw = vcnl4035_write_raw, |
| 346 |
.read_event_value = vcnl4035_read_thresh, |
| 347 |
.write_event_value = vcnl4035_write_thresh, |
| 348 |
.attrs = &vcnl4035_attribute_group, |
| 349 |
}; |
| 350 |
|
| 351 |
static const struct iio_event_spec vcnl4035_event_spec[] = { |
| 352 |
{ |
| 353 |
.type = IIO_EV_TYPE_THRESH, |
| 354 |
.dir = IIO_EV_DIR_RISING, |
| 355 |
.mask_separate = BIT(IIO_EV_INFO_VALUE), |
| 356 |
}, { |
| 357 |
.type = IIO_EV_TYPE_THRESH, |
| 358 |
.dir = IIO_EV_DIR_FALLING, |
| 359 |
.mask_separate = BIT(IIO_EV_INFO_VALUE), |
| 360 |
}, { |
| 361 |
.type = IIO_EV_TYPE_THRESH, |
| 362 |
.dir = IIO_EV_DIR_EITHER, |
| 363 |
.mask_separate = BIT(IIO_EV_INFO_PERIOD), |
| 364 |
}, |
| 365 |
}; |
| 366 |
|
| 367 |
enum vcnl4035_scan_index_order { |
| 368 |
VCNL4035_CHAN_INDEX_LIGHT, |
| 369 |
VCNL4035_CHAN_INDEX_WHITE_LED, |
| 370 |
}; |
| 371 |
|
| 372 |
static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { |
| 373 |
.validate_scan_mask = &iio_validate_scan_mask_onehot, |
| 374 |
}; |
| 375 |
|
| 376 |
static const struct iio_chan_spec vcnl4035_channels[] = { |
| 377 |
{ |
| 378 |
.type = IIO_LIGHT, |
| 379 |
.channel = 0, |
| 380 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 381 |
BIT(IIO_CHAN_INFO_INT_TIME) | |
| 382 |
BIT(IIO_CHAN_INFO_SCALE), |
| 383 |
.event_spec = vcnl4035_event_spec, |
| 384 |
.num_event_specs = ARRAY_SIZE(vcnl4035_event_spec), |
| 385 |
.scan_index = VCNL4035_CHAN_INDEX_LIGHT, |
| 386 |
.scan_type = { |
| 387 |
.sign = 'u', |
| 388 |
.realbits = 16, |
| 389 |
.storagebits = 16, |
| 390 |
.endianness = IIO_CPU, |
| 391 |
}, |
| 392 |
}, |
| 393 |
{ |
| 394 |
.type = IIO_INTENSITY, |
| 395 |
.channel = 1, |
| 396 |
.modified = 1, |
| 397 |
.channel2 = IIO_MOD_LIGHT_BOTH, |
| 398 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 399 |
.scan_index = VCNL4035_CHAN_INDEX_WHITE_LED, |
| 400 |
.scan_type = { |
| 401 |
.sign = 'u', |
| 402 |
.realbits = 16, |
| 403 |
.storagebits = 16, |
| 404 |
.endianness = IIO_CPU, |
| 405 |
}, |
| 406 |
}, |
| 407 |
}; |
| 408 |
|
| 409 |
static int vcnl4035_set_als_power_state(struct vcnl4035_data *data, u8 status) |
| 410 |
{ |
| 411 |
return regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 412 |
VCNL4035_MODE_ALS_MASK, |
| 413 |
status); |
| 414 |
} |
| 415 |
|
| 416 |
static int vcnl4035_init(struct vcnl4035_data *data) |
| 417 |
{ |
| 418 |
int ret; |
| 419 |
int id; |
| 420 |
|
| 421 |
ret = regmap_read(data->regmap, VCNL4035_DEV_ID, &id); |
| 422 |
if (ret < 0) { |
| 423 |
dev_err(&data->client->dev, "Failed to read DEV_ID register\n"); |
| 424 |
return ret; |
| 425 |
} |
| 426 |
|
| 427 |
id = FIELD_GET(VCNL4035_DEV_ID_MASK, id); |
| 428 |
if (id != VCNL4035_DEV_ID_VAL) { |
| 429 |
dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n", |
| 430 |
id, VCNL4035_DEV_ID_VAL); |
| 431 |
return -ENODEV; |
| 432 |
} |
| 433 |
|
| 434 |
ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE); |
| 435 |
if (ret < 0) |
| 436 |
return ret; |
| 437 |
|
| 438 |
/* ALS white channel enable */ |
| 439 |
ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 440 |
VCNL4035_MODE_ALS_WHITE_CHAN, |
| 441 |
1); |
| 442 |
if (ret) { |
| 443 |
dev_err(&data->client->dev, "set white channel enable %d\n", |
| 444 |
ret); |
| 445 |
return ret; |
| 446 |
} |
| 447 |
|
| 448 |
/* set default integration time - 100 ms for ALS */ |
| 449 |
ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 450 |
VCNL4035_ALS_IT_MASK, |
| 451 |
VCNL4035_ALS_IT_DEFAULT); |
| 452 |
if (ret) { |
| 453 |
dev_err(&data->client->dev, "set default ALS IT returned %d\n", |
| 454 |
ret); |
| 455 |
return ret; |
| 456 |
} |
| 457 |
data->als_it_val = VCNL4035_ALS_IT_DEFAULT; |
| 458 |
|
| 459 |
/* set default persistence time - 1 for ALS */ |
| 460 |
ret = regmap_update_bits(data->regmap, VCNL4035_ALS_CONF, |
| 461 |
VCNL4035_ALS_PERS_MASK, |
| 462 |
VCNL4035_ALS_PERS_DEFAULT); |
| 463 |
if (ret) { |
| 464 |
dev_err(&data->client->dev, "set default PERS returned %d\n", |
| 465 |
ret); |
| 466 |
return ret; |
| 467 |
} |
| 468 |
data->als_persistence = VCNL4035_ALS_PERS_DEFAULT; |
| 469 |
|
| 470 |
/* set default HIGH threshold for ALS */ |
| 471 |
ret = regmap_write(data->regmap, VCNL4035_ALS_THDH, |
| 472 |
VCNL4035_ALS_THDH_DEFAULT); |
| 473 |
if (ret) { |
| 474 |
dev_err(&data->client->dev, "set default THDH returned %d\n", |
| 475 |
ret); |
| 476 |
return ret; |
| 477 |
} |
| 478 |
data->als_thresh_high = VCNL4035_ALS_THDH_DEFAULT; |
| 479 |
|
| 480 |
/* set default LOW threshold for ALS */ |
| 481 |
ret = regmap_write(data->regmap, VCNL4035_ALS_THDL, |
| 482 |
VCNL4035_ALS_THDL_DEFAULT); |
| 483 |
if (ret) { |
| 484 |
dev_err(&data->client->dev, "set default THDL returned %d\n", |
| 485 |
ret); |
| 486 |
return ret; |
| 487 |
} |
| 488 |
data->als_thresh_low = VCNL4035_ALS_THDL_DEFAULT; |
| 489 |
|
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
static bool vcnl4035_is_volatile_reg(struct device *dev, unsigned int reg) |
| 494 |
{ |
| 495 |
switch (reg) { |
| 496 |
case VCNL4035_ALS_CONF: |
| 497 |
case VCNL4035_DEV_ID: |
| 498 |
return false; |
| 499 |
default: |
| 500 |
return true; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
static const struct regmap_config vcnl4035_regmap_config = { |
| 505 |
.name = "vcnl4035_regmap", |
| 506 |
.reg_bits = 8, |
| 507 |
.val_bits = 16, |
| 508 |
.max_register = VCNL4035_DEV_ID, |
| 509 |
.cache_type = REGCACHE_RBTREE, |
| 510 |
.volatile_reg = vcnl4035_is_volatile_reg, |
| 511 |
.val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 512 |
}; |
| 513 |
|
| 514 |
static int vcnl4035_probe_trigger(struct iio_dev *indio_dev) |
| 515 |
{ |
| 516 |
int ret; |
| 517 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 518 |
|
| 519 |
data->drdy_trigger0 = devm_iio_trigger_alloc( |
| 520 |
indio_dev->dev.parent, |
| 521 |
"%s-dev%d", indio_dev->name, iio_device_id(indio_dev)); |
| 522 |
if (!data->drdy_trigger0) |
| 523 |
return -ENOMEM; |
| 524 |
|
| 525 |
data->drdy_trigger0->ops = &vcnl4035_trigger_ops; |
| 526 |
iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev); |
| 527 |
ret = devm_iio_trigger_register(indio_dev->dev.parent, |
| 528 |
data->drdy_trigger0); |
| 529 |
if (ret) { |
| 530 |
dev_err(&data->client->dev, "iio trigger register failed\n"); |
| 531 |
return ret; |
| 532 |
} |
| 533 |
|
| 534 |
/* Trigger setup */ |
| 535 |
ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent, indio_dev, |
| 536 |
NULL, vcnl4035_trigger_consumer_handler, |
| 537 |
&iio_triggered_buffer_setup_ops); |
| 538 |
if (ret < 0) { |
| 539 |
dev_err(&data->client->dev, "iio triggered buffer setup failed\n"); |
| 540 |
return ret; |
| 541 |
} |
| 542 |
|
| 543 |
/* IRQ to trigger mapping */ |
| 544 |
ret = devm_request_threaded_irq(&data->client->dev, data->client->irq, |
| 545 |
NULL, vcnl4035_drdy_irq_thread, |
| 546 |
IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 547 |
"vcnl4035_event", indio_dev); |
| 548 |
if (ret < 0) |
| 549 |
dev_err(&data->client->dev, "request irq %d for trigger0 failed\n", |
| 550 |
data->client->irq); |
| 551 |
return ret; |
| 552 |
} |
| 553 |
|
| 554 |
static int vcnl4035_probe(struct i2c_client *client) |
| 555 |
{ |
| 556 |
struct vcnl4035_data *data; |
| 557 |
struct iio_dev *indio_dev; |
| 558 |
struct regmap *regmap; |
| 559 |
int ret; |
| 560 |
|
| 561 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 562 |
if (!indio_dev) |
| 563 |
return -ENOMEM; |
| 564 |
|
| 565 |
regmap = devm_regmap_init_i2c(client, &vcnl4035_regmap_config); |
| 566 |
if (IS_ERR(regmap)) { |
| 567 |
dev_err(&client->dev, "regmap_init failed!\n"); |
| 568 |
return PTR_ERR(regmap); |
| 569 |
} |
| 570 |
|
| 571 |
data = iio_priv(indio_dev); |
| 572 |
i2c_set_clientdata(client, indio_dev); |
| 573 |
data->client = client; |
| 574 |
data->regmap = regmap; |
| 575 |
|
| 576 |
indio_dev->info = &vcnl4035_info; |
| 577 |
indio_dev->name = VCNL4035_DRV_NAME; |
| 578 |
indio_dev->channels = vcnl4035_channels; |
| 579 |
indio_dev->num_channels = ARRAY_SIZE(vcnl4035_channels); |
| 580 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 581 |
|
| 582 |
ret = vcnl4035_init(data); |
| 583 |
if (ret < 0) { |
| 584 |
dev_err(&client->dev, "vcnl4035 chip init failed\n"); |
| 585 |
return ret; |
| 586 |
} |
| 587 |
|
| 588 |
if (client->irq > 0) { |
| 589 |
ret = vcnl4035_probe_trigger(indio_dev); |
| 590 |
if (ret < 0) { |
| 591 |
dev_err(&client->dev, "vcnl4035 unable init trigger\n"); |
| 592 |
goto fail_poweroff; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
ret = pm_runtime_set_active(&client->dev); |
| 597 |
if (ret < 0) |
| 598 |
goto fail_poweroff; |
| 599 |
|
| 600 |
ret = iio_device_register(indio_dev); |
| 601 |
if (ret < 0) |
| 602 |
goto fail_poweroff; |
| 603 |
|
| 604 |
pm_runtime_enable(&client->dev); |
| 605 |
pm_runtime_set_autosuspend_delay(&client->dev, VCNL4035_SLEEP_DELAY_MS); |
| 606 |
pm_runtime_use_autosuspend(&client->dev); |
| 607 |
|
| 608 |
return 0; |
| 609 |
|
| 610 |
fail_poweroff: |
| 611 |
vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE); |
| 612 |
return ret; |
| 613 |
} |
| 614 |
|
| 615 |
static void vcnl4035_remove(struct i2c_client *client) |
| 616 |
{ |
| 617 |
struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 618 |
int ret; |
| 619 |
|
| 620 |
pm_runtime_dont_use_autosuspend(&client->dev); |
| 621 |
pm_runtime_disable(&client->dev); |
| 622 |
iio_device_unregister(indio_dev); |
| 623 |
pm_runtime_set_suspended(&client->dev); |
| 624 |
|
| 625 |
ret = vcnl4035_set_als_power_state(iio_priv(indio_dev), |
| 626 |
VCNL4035_MODE_ALS_DISABLE); |
| 627 |
if (ret) |
| 628 |
dev_warn(&client->dev, "Failed to put device into standby (%pe)\n", |
| 629 |
ERR_PTR(ret)); |
| 630 |
} |
| 631 |
|
| 632 |
static int vcnl4035_runtime_suspend(struct device *dev) |
| 633 |
{ |
| 634 |
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 635 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 636 |
int ret; |
| 637 |
|
| 638 |
ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_DISABLE); |
| 639 |
regcache_mark_dirty(data->regmap); |
| 640 |
|
| 641 |
return ret; |
| 642 |
} |
| 643 |
|
| 644 |
static int vcnl4035_runtime_resume(struct device *dev) |
| 645 |
{ |
| 646 |
struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 647 |
struct vcnl4035_data *data = iio_priv(indio_dev); |
| 648 |
int ret; |
| 649 |
|
| 650 |
regcache_sync(data->regmap); |
| 651 |
ret = vcnl4035_set_als_power_state(data, VCNL4035_MODE_ALS_ENABLE); |
| 652 |
if (ret < 0) |
| 653 |
return ret; |
| 654 |
|
| 655 |
/* wait for 1 ALS integration cycle */ |
| 656 |
msleep(data->als_it_val * 100); |
| 657 |
|
| 658 |
return 0; |
| 659 |
} |
| 660 |
|
| 661 |
static DEFINE_RUNTIME_DEV_PM_OPS(vcnl4035_pm_ops, vcnl4035_runtime_suspend, |
| 662 |
vcnl4035_runtime_resume, NULL); |
| 663 |
|
| 664 |
static const struct i2c_device_id vcnl4035_id[] = { |
| 665 |
{ "vcnl4035" }, |
| 666 |
{ } |
| 667 |
}; |
| 668 |
MODULE_DEVICE_TABLE(i2c, vcnl4035_id); |
| 669 |
|
| 670 |
static const struct of_device_id vcnl4035_of_match[] = { |
| 671 |
{ .compatible = "vishay,vcnl4035", }, |
| 672 |
{ } |
| 673 |
}; |
| 674 |
MODULE_DEVICE_TABLE(of, vcnl4035_of_match); |
| 675 |
|
| 676 |
static struct i2c_driver vcnl4035_driver = { |
| 677 |
.driver = { |
| 678 |
.name = VCNL4035_DRV_NAME, |
| 679 |
.pm = pm_ptr(&vcnl4035_pm_ops), |
| 680 |
.of_match_table = vcnl4035_of_match, |
| 681 |
}, |
| 682 |
.probe = vcnl4035_probe, |
| 683 |
.remove = vcnl4035_remove, |
| 684 |
.id_table = vcnl4035_id, |
| 685 |
}; |
| 686 |
|
| 687 |
module_i2c_driver(vcnl4035_driver); |
| 688 |
|
| 689 |
MODULE_AUTHOR("Parthiban Nallathambi <[email protected]>"); |
| 690 |
MODULE_DESCRIPTION("VCNL4035 Ambient Light Sensor driver"); |
| 691 |
MODULE_LICENSE("GPL v2"); |