| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* si1145.c - Support for Silabs SI1132 and SI1141/2/3/5/6/7 combined ambient |
| 4 |
* light, UV index and proximity sensors |
| 5 |
* |
| 6 |
* Copyright 2014-16 Peter Meerwald-Stadler <[email protected]> |
| 7 |
* Copyright 2016 Crestez Dan Leonard <[email protected]> |
| 8 |
* |
| 9 |
* SI1132 (7-bit I2C slave address 0x60) |
| 10 |
* SI1141/2/3 (7-bit I2C slave address 0x5a) |
| 11 |
* SI1145/6/6 (7-bit I2C slave address 0x60) |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/i2c.h> |
| 16 |
#include <linux/err.h> |
| 17 |
#include <linux/slab.h> |
| 18 |
#include <linux/delay.h> |
| 19 |
#include <linux/irq.h> |
| 20 |
|
| 21 |
#include <linux/iio/iio.h> |
| 22 |
#include <linux/iio/sysfs.h> |
| 23 |
#include <linux/iio/trigger.h> |
| 24 |
#include <linux/iio/trigger_consumer.h> |
| 25 |
#include <linux/iio/triggered_buffer.h> |
| 26 |
#include <linux/iio/buffer.h> |
| 27 |
#include <linux/util_macros.h> |
| 28 |
|
| 29 |
#define SI1145_REG_PART_ID 0x00 |
| 30 |
#define SI1145_REG_REV_ID 0x01 |
| 31 |
#define SI1145_REG_SEQ_ID 0x02 |
| 32 |
#define SI1145_REG_INT_CFG 0x03 |
| 33 |
#define SI1145_REG_IRQ_ENABLE 0x04 |
| 34 |
#define SI1145_REG_IRQ_MODE 0x05 |
| 35 |
#define SI1145_REG_HW_KEY 0x07 |
| 36 |
#define SI1145_REG_MEAS_RATE 0x08 |
| 37 |
#define SI1145_REG_PS_LED21 0x0f |
| 38 |
#define SI1145_REG_PS_LED3 0x10 |
| 39 |
#define SI1145_REG_UCOEF1 0x13 |
| 40 |
#define SI1145_REG_UCOEF2 0x14 |
| 41 |
#define SI1145_REG_UCOEF3 0x15 |
| 42 |
#define SI1145_REG_UCOEF4 0x16 |
| 43 |
#define SI1145_REG_PARAM_WR 0x17 |
| 44 |
#define SI1145_REG_COMMAND 0x18 |
| 45 |
#define SI1145_REG_RESPONSE 0x20 |
| 46 |
#define SI1145_REG_IRQ_STATUS 0x21 |
| 47 |
#define SI1145_REG_ALSVIS_DATA 0x22 |
| 48 |
#define SI1145_REG_ALSIR_DATA 0x24 |
| 49 |
#define SI1145_REG_PS1_DATA 0x26 |
| 50 |
#define SI1145_REG_PS2_DATA 0x28 |
| 51 |
#define SI1145_REG_PS3_DATA 0x2a |
| 52 |
#define SI1145_REG_AUX_DATA 0x2c |
| 53 |
#define SI1145_REG_PARAM_RD 0x2e |
| 54 |
#define SI1145_REG_CHIP_STAT 0x30 |
| 55 |
|
| 56 |
#define SI1145_UCOEF1_DEFAULT 0x7b |
| 57 |
#define SI1145_UCOEF2_DEFAULT 0x6b |
| 58 |
#define SI1145_UCOEF3_DEFAULT 0x01 |
| 59 |
#define SI1145_UCOEF4_DEFAULT 0x00 |
| 60 |
|
| 61 |
/* Helper to figure out PS_LED register / shift per channel */ |
| 62 |
#define SI1145_PS_LED_REG(ch) \ |
| 63 |
(((ch) == 2) ? SI1145_REG_PS_LED3 : SI1145_REG_PS_LED21) |
| 64 |
#define SI1145_PS_LED_SHIFT(ch) \ |
| 65 |
(((ch) == 1) ? 4 : 0) |
| 66 |
|
| 67 |
/* Parameter offsets */ |
| 68 |
#define SI1145_PARAM_CHLIST 0x01 |
| 69 |
#define SI1145_PARAM_PSLED12_SELECT 0x02 |
| 70 |
#define SI1145_PARAM_PSLED3_SELECT 0x03 |
| 71 |
#define SI1145_PARAM_PS_ENCODING 0x05 |
| 72 |
#define SI1145_PARAM_ALS_ENCODING 0x06 |
| 73 |
#define SI1145_PARAM_PS1_ADC_MUX 0x07 |
| 74 |
#define SI1145_PARAM_PS2_ADC_MUX 0x08 |
| 75 |
#define SI1145_PARAM_PS3_ADC_MUX 0x09 |
| 76 |
#define SI1145_PARAM_PS_ADC_COUNTER 0x0a |
| 77 |
#define SI1145_PARAM_PS_ADC_GAIN 0x0b |
| 78 |
#define SI1145_PARAM_PS_ADC_MISC 0x0c |
| 79 |
#define SI1145_PARAM_ALS_ADC_MUX 0x0d |
| 80 |
#define SI1145_PARAM_ALSIR_ADC_MUX 0x0e |
| 81 |
#define SI1145_PARAM_AUX_ADC_MUX 0x0f |
| 82 |
#define SI1145_PARAM_ALSVIS_ADC_COUNTER 0x10 |
| 83 |
#define SI1145_PARAM_ALSVIS_ADC_GAIN 0x11 |
| 84 |
#define SI1145_PARAM_ALSVIS_ADC_MISC 0x12 |
| 85 |
#define SI1145_PARAM_LED_RECOVERY 0x1c |
| 86 |
#define SI1145_PARAM_ALSIR_ADC_COUNTER 0x1d |
| 87 |
#define SI1145_PARAM_ALSIR_ADC_GAIN 0x1e |
| 88 |
#define SI1145_PARAM_ALSIR_ADC_MISC 0x1f |
| 89 |
#define SI1145_PARAM_ADC_OFFSET 0x1a |
| 90 |
|
| 91 |
/* Channel enable masks for CHLIST parameter */ |
| 92 |
#define SI1145_CHLIST_EN_PS1 BIT(0) |
| 93 |
#define SI1145_CHLIST_EN_PS2 BIT(1) |
| 94 |
#define SI1145_CHLIST_EN_PS3 BIT(2) |
| 95 |
#define SI1145_CHLIST_EN_ALSVIS BIT(4) |
| 96 |
#define SI1145_CHLIST_EN_ALSIR BIT(5) |
| 97 |
#define SI1145_CHLIST_EN_AUX BIT(6) |
| 98 |
#define SI1145_CHLIST_EN_UV BIT(7) |
| 99 |
|
| 100 |
/* Proximity measurement mode for ADC_MISC parameter */ |
| 101 |
#define SI1145_PS_ADC_MODE_NORMAL BIT(2) |
| 102 |
/* Signal range mask for ADC_MISC parameter */ |
| 103 |
#define SI1145_ADC_MISC_RANGE BIT(5) |
| 104 |
|
| 105 |
/* Commands for REG_COMMAND */ |
| 106 |
#define SI1145_CMD_NOP 0x00 |
| 107 |
#define SI1145_CMD_RESET 0x01 |
| 108 |
#define SI1145_CMD_PS_FORCE 0x05 |
| 109 |
#define SI1145_CMD_ALS_FORCE 0x06 |
| 110 |
#define SI1145_CMD_PSALS_FORCE 0x07 |
| 111 |
#define SI1145_CMD_PS_PAUSE 0x09 |
| 112 |
#define SI1145_CMD_ALS_PAUSE 0x0a |
| 113 |
#define SI1145_CMD_PSALS_PAUSE 0x0b |
| 114 |
#define SI1145_CMD_PS_AUTO 0x0d |
| 115 |
#define SI1145_CMD_ALS_AUTO 0x0e |
| 116 |
#define SI1145_CMD_PSALS_AUTO 0x0f |
| 117 |
#define SI1145_CMD_PARAM_QUERY 0x80 |
| 118 |
#define SI1145_CMD_PARAM_SET 0xa0 |
| 119 |
|
| 120 |
#define SI1145_RSP_INVALID_SETTING 0x80 |
| 121 |
#define SI1145_RSP_COUNTER_MASK 0x0F |
| 122 |
|
| 123 |
/* Minimum sleep after each command to ensure it's received */ |
| 124 |
#define SI1145_COMMAND_MINSLEEP_MS 5 |
| 125 |
/* Return -ETIMEDOUT after this long */ |
| 126 |
#define SI1145_COMMAND_TIMEOUT_MS 25 |
| 127 |
|
| 128 |
/* Interrupt configuration masks for INT_CFG register */ |
| 129 |
#define SI1145_INT_CFG_OE BIT(0) /* enable interrupt */ |
| 130 |
#define SI1145_INT_CFG_MODE BIT(1) /* auto reset interrupt pin */ |
| 131 |
|
| 132 |
/* Interrupt enable masks for IRQ_ENABLE register */ |
| 133 |
#define SI1145_MASK_ALL_IE (BIT(4) | BIT(3) | BIT(2) | BIT(0)) |
| 134 |
|
| 135 |
#define SI1145_MUX_TEMP 0x65 |
| 136 |
#define SI1145_MUX_VDD 0x75 |
| 137 |
|
| 138 |
/* Proximity LED current; see Table 2 in datasheet */ |
| 139 |
#define SI1145_LED_CURRENT_45mA 0x04 |
| 140 |
|
| 141 |
enum { |
| 142 |
SI1132, |
| 143 |
SI1141, |
| 144 |
SI1142, |
| 145 |
SI1143, |
| 146 |
SI1145, |
| 147 |
SI1146, |
| 148 |
SI1147, |
| 149 |
}; |
| 150 |
|
| 151 |
struct si1145_part_info { |
| 152 |
u8 part; |
| 153 |
const struct iio_info *iio_info; |
| 154 |
const struct iio_chan_spec *channels; |
| 155 |
unsigned int num_channels; |
| 156 |
unsigned int num_leds; |
| 157 |
bool uncompressed_meas_rate; |
| 158 |
}; |
| 159 |
|
| 160 |
/** |
| 161 |
* struct si1145_data - si1145 chip state data |
| 162 |
* @client: I2C client |
| 163 |
* @lock: mutex to protect shared state. |
| 164 |
* @cmdlock: Low-level mutex to protect command execution only |
| 165 |
* @rsp_seq: Next expected response number or -1 if counter reset required |
| 166 |
* @scan_mask: Saved scan mask to avoid duplicate set_chlist |
| 167 |
* @autonomous: If automatic measurements are active (for buffer support) |
| 168 |
* @part_info: Part information |
| 169 |
* @trig: Pointer to iio trigger |
| 170 |
* @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode |
| 171 |
* @buffer: Used to pack data read from sensor. |
| 172 |
*/ |
| 173 |
struct si1145_data { |
| 174 |
struct i2c_client *client; |
| 175 |
struct mutex lock; |
| 176 |
struct mutex cmdlock; |
| 177 |
int rsp_seq; |
| 178 |
const struct si1145_part_info *part_info; |
| 179 |
unsigned long scan_mask; |
| 180 |
bool autonomous; |
| 181 |
struct iio_trigger *trig; |
| 182 |
int meas_rate; |
| 183 |
/* |
| 184 |
* Ensure timestamp will be naturally aligned if present. |
| 185 |
* Maximum buffer size (may be only partly used if not all |
| 186 |
* channels are enabled): |
| 187 |
* 6*2 bytes channels data + 4 bytes alignment + |
| 188 |
* 8 bytes timestamp |
| 189 |
*/ |
| 190 |
u8 buffer[24] __aligned(8); |
| 191 |
}; |
| 192 |
|
| 193 |
/* |
| 194 |
* __si1145_command_reset() - Send CMD_NOP and wait for response 0 |
| 195 |
* |
| 196 |
* Does not modify data->rsp_seq |
| 197 |
* |
| 198 |
* Return: 0 on success and -errno on error. |
| 199 |
*/ |
| 200 |
static int __si1145_command_reset(struct si1145_data *data) |
| 201 |
{ |
| 202 |
struct device *dev = &data->client->dev; |
| 203 |
unsigned long stop_jiffies; |
| 204 |
int ret; |
| 205 |
|
| 206 |
ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, |
| 207 |
SI1145_CMD_NOP); |
| 208 |
if (ret < 0) |
| 209 |
return ret; |
| 210 |
msleep(SI1145_COMMAND_MINSLEEP_MS); |
| 211 |
|
| 212 |
stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000; |
| 213 |
while (true) { |
| 214 |
ret = i2c_smbus_read_byte_data(data->client, |
| 215 |
SI1145_REG_RESPONSE); |
| 216 |
if (ret <= 0) |
| 217 |
return ret; |
| 218 |
if (time_after(jiffies, stop_jiffies)) { |
| 219 |
dev_warn(dev, "timeout on reset\n"); |
| 220 |
return -ETIMEDOUT; |
| 221 |
} |
| 222 |
msleep(SI1145_COMMAND_MINSLEEP_MS); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* si1145_command() - Execute a command and poll the response register |
| 228 |
* |
| 229 |
* All conversion overflows are reported as -EOVERFLOW |
| 230 |
* INVALID_SETTING is reported as -EINVAL |
| 231 |
* Timeouts are reported as -ETIMEDOUT |
| 232 |
* |
| 233 |
* Return: 0 on success or -errno on failure |
| 234 |
*/ |
| 235 |
static int si1145_command(struct si1145_data *data, u8 cmd) |
| 236 |
{ |
| 237 |
struct device *dev = &data->client->dev; |
| 238 |
unsigned long stop_jiffies; |
| 239 |
int ret; |
| 240 |
|
| 241 |
mutex_lock(&data->cmdlock); |
| 242 |
|
| 243 |
if (data->rsp_seq < 0) { |
| 244 |
ret = __si1145_command_reset(data); |
| 245 |
if (ret < 0) { |
| 246 |
dev_err(dev, "failed to reset command counter, ret=%d\n", |
| 247 |
ret); |
| 248 |
goto out; |
| 249 |
} |
| 250 |
data->rsp_seq = 0; |
| 251 |
} |
| 252 |
|
| 253 |
ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd); |
| 254 |
if (ret) { |
| 255 |
dev_warn(dev, "failed to write command, ret=%d\n", ret); |
| 256 |
goto out; |
| 257 |
} |
| 258 |
/* Sleep a little to ensure the command is received */ |
| 259 |
msleep(SI1145_COMMAND_MINSLEEP_MS); |
| 260 |
|
| 261 |
stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000; |
| 262 |
while (true) { |
| 263 |
ret = i2c_smbus_read_byte_data(data->client, |
| 264 |
SI1145_REG_RESPONSE); |
| 265 |
if (ret < 0) { |
| 266 |
dev_warn(dev, "failed to read response, ret=%d\n", ret); |
| 267 |
break; |
| 268 |
} |
| 269 |
|
| 270 |
if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) { |
| 271 |
if (ret == data->rsp_seq) { |
| 272 |
if (time_after(jiffies, stop_jiffies)) { |
| 273 |
dev_warn(dev, "timeout on command 0x%02x\n", |
| 274 |
cmd); |
| 275 |
ret = -ETIMEDOUT; |
| 276 |
break; |
| 277 |
} |
| 278 |
msleep(SI1145_COMMAND_MINSLEEP_MS); |
| 279 |
continue; |
| 280 |
} |
| 281 |
if (ret == ((data->rsp_seq + 1) & |
| 282 |
SI1145_RSP_COUNTER_MASK)) { |
| 283 |
data->rsp_seq = ret; |
| 284 |
ret = 0; |
| 285 |
break; |
| 286 |
} |
| 287 |
dev_warn(dev, "unexpected response counter %d instead of %d\n", |
| 288 |
ret, (data->rsp_seq + 1) & |
| 289 |
SI1145_RSP_COUNTER_MASK); |
| 290 |
ret = -EIO; |
| 291 |
} else { |
| 292 |
if (ret == SI1145_RSP_INVALID_SETTING) { |
| 293 |
dev_warn(dev, "INVALID_SETTING error on command 0x%02x\n", |
| 294 |
cmd); |
| 295 |
ret = -EINVAL; |
| 296 |
} else { |
| 297 |
/* All overflows are treated identically */ |
| 298 |
dev_dbg(dev, "overflow, ret=%d, cmd=0x%02x\n", |
| 299 |
ret, cmd); |
| 300 |
ret = -EOVERFLOW; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
/* Force a counter reset next time */ |
| 305 |
data->rsp_seq = -1; |
| 306 |
break; |
| 307 |
} |
| 308 |
|
| 309 |
out: |
| 310 |
mutex_unlock(&data->cmdlock); |
| 311 |
|
| 312 |
return ret; |
| 313 |
} |
| 314 |
|
| 315 |
static int si1145_param_update(struct si1145_data *data, u8 op, u8 param, |
| 316 |
u8 value) |
| 317 |
{ |
| 318 |
int ret; |
| 319 |
|
| 320 |
ret = i2c_smbus_write_byte_data(data->client, |
| 321 |
SI1145_REG_PARAM_WR, value); |
| 322 |
if (ret < 0) |
| 323 |
return ret; |
| 324 |
|
| 325 |
return si1145_command(data, op | (param & 0x1F)); |
| 326 |
} |
| 327 |
|
| 328 |
static int si1145_param_set(struct si1145_data *data, u8 param, u8 value) |
| 329 |
{ |
| 330 |
return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value); |
| 331 |
} |
| 332 |
|
| 333 |
/* Set param. Returns negative errno or current value */ |
| 334 |
static int si1145_param_query(struct si1145_data *data, u8 param) |
| 335 |
{ |
| 336 |
int ret; |
| 337 |
|
| 338 |
ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F)); |
| 339 |
if (ret < 0) |
| 340 |
return ret; |
| 341 |
|
| 342 |
return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD); |
| 343 |
} |
| 344 |
|
| 345 |
/* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */ |
| 346 |
static u16 si1145_uncompress(u8 x) |
| 347 |
{ |
| 348 |
u16 result = 0; |
| 349 |
u8 exponent = 0; |
| 350 |
|
| 351 |
if (x < 8) |
| 352 |
return 0; |
| 353 |
|
| 354 |
exponent = (x & 0xf0) >> 4; |
| 355 |
result = 0x10 | (x & 0x0f); |
| 356 |
|
| 357 |
if (exponent >= 4) |
| 358 |
return result << (exponent - 4); |
| 359 |
return result >> (4 - exponent); |
| 360 |
} |
| 361 |
|
| 362 |
/* Compress 16 bit value to 8 bit, see Silabs AN498 */ |
| 363 |
static u8 si1145_compress(u16 x) |
| 364 |
{ |
| 365 |
u32 exponent = 0; |
| 366 |
u32 significand = 0; |
| 367 |
u32 tmp = x; |
| 368 |
|
| 369 |
if (x == 0x0000) |
| 370 |
return 0x00; |
| 371 |
if (x == 0x0001) |
| 372 |
return 0x08; |
| 373 |
|
| 374 |
while (1) { |
| 375 |
tmp >>= 1; |
| 376 |
exponent += 1; |
| 377 |
if (tmp == 1) |
| 378 |
break; |
| 379 |
} |
| 380 |
|
| 381 |
if (exponent < 5) { |
| 382 |
significand = x << (4 - exponent); |
| 383 |
return (exponent << 4) | (significand & 0xF); |
| 384 |
} |
| 385 |
|
| 386 |
significand = x >> (exponent - 5); |
| 387 |
if (significand & 1) { |
| 388 |
significand += 2; |
| 389 |
if (significand & 0x0040) { |
| 390 |
exponent += 1; |
| 391 |
significand >>= 1; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
return (exponent << 4) | ((significand >> 1) & 0xF); |
| 396 |
} |
| 397 |
|
| 398 |
/* Write meas_rate in hardware */ |
| 399 |
static int si1145_set_meas_rate(struct si1145_data *data, int interval) |
| 400 |
{ |
| 401 |
if (data->part_info->uncompressed_meas_rate) |
| 402 |
return i2c_smbus_write_word_data(data->client, |
| 403 |
SI1145_REG_MEAS_RATE, interval); |
| 404 |
else |
| 405 |
return i2c_smbus_write_byte_data(data->client, |
| 406 |
SI1145_REG_MEAS_RATE, interval); |
| 407 |
} |
| 408 |
|
| 409 |
static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2) |
| 410 |
{ |
| 411 |
*val = 32000; |
| 412 |
if (data->part_info->uncompressed_meas_rate) |
| 413 |
*val2 = data->meas_rate; |
| 414 |
else |
| 415 |
*val2 = si1145_uncompress(data->meas_rate); |
| 416 |
return IIO_VAL_FRACTIONAL; |
| 417 |
} |
| 418 |
|
| 419 |
/* Set the samp freq in driver private data */ |
| 420 |
static int si1145_store_samp_freq(struct si1145_data *data, int val) |
| 421 |
{ |
| 422 |
int ret = 0; |
| 423 |
int meas_rate; |
| 424 |
|
| 425 |
if (val <= 0 || val > 32000) |
| 426 |
return -ERANGE; |
| 427 |
meas_rate = 32000 / val; |
| 428 |
|
| 429 |
mutex_lock(&data->lock); |
| 430 |
if (data->autonomous) { |
| 431 |
ret = si1145_set_meas_rate(data, meas_rate); |
| 432 |
if (ret) |
| 433 |
goto out; |
| 434 |
} |
| 435 |
if (data->part_info->uncompressed_meas_rate) |
| 436 |
data->meas_rate = meas_rate; |
| 437 |
else |
| 438 |
data->meas_rate = si1145_compress(meas_rate); |
| 439 |
|
| 440 |
out: |
| 441 |
mutex_unlock(&data->lock); |
| 442 |
|
| 443 |
return ret; |
| 444 |
} |
| 445 |
|
| 446 |
static irqreturn_t si1145_trigger_handler(int irq, void *private) |
| 447 |
{ |
| 448 |
struct iio_poll_func *pf = private; |
| 449 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 450 |
struct si1145_data *data = iio_priv(indio_dev); |
| 451 |
int i, j = 0; |
| 452 |
int ret; |
| 453 |
u8 irq_status = 0; |
| 454 |
|
| 455 |
if (!data->autonomous) { |
| 456 |
ret = si1145_command(data, SI1145_CMD_PSALS_FORCE); |
| 457 |
if (ret < 0 && ret != -EOVERFLOW) |
| 458 |
goto done; |
| 459 |
} else { |
| 460 |
irq_status = ret = i2c_smbus_read_byte_data(data->client, |
| 461 |
SI1145_REG_IRQ_STATUS); |
| 462 |
if (ret < 0) |
| 463 |
goto done; |
| 464 |
if (!(irq_status & SI1145_MASK_ALL_IE)) |
| 465 |
goto done; |
| 466 |
} |
| 467 |
|
| 468 |
iio_for_each_active_channel(indio_dev, i) { |
| 469 |
int run = 1; |
| 470 |
|
| 471 |
while (i + run < iio_get_masklength(indio_dev)) { |
| 472 |
if (!test_bit(i + run, indio_dev->active_scan_mask)) |
| 473 |
break; |
| 474 |
if (indio_dev->channels[i + run].address != |
| 475 |
indio_dev->channels[i].address + 2 * run) |
| 476 |
break; |
| 477 |
run++; |
| 478 |
} |
| 479 |
|
| 480 |
ret = i2c_smbus_read_i2c_block_data_or_emulated( |
| 481 |
data->client, indio_dev->channels[i].address, |
| 482 |
sizeof(u16) * run, &data->buffer[j]); |
| 483 |
if (ret < 0) |
| 484 |
goto done; |
| 485 |
j += run * sizeof(u16); |
| 486 |
i += run - 1; |
| 487 |
} |
| 488 |
|
| 489 |
if (data->autonomous) { |
| 490 |
ret = i2c_smbus_write_byte_data(data->client, |
| 491 |
SI1145_REG_IRQ_STATUS, |
| 492 |
irq_status & SI1145_MASK_ALL_IE); |
| 493 |
if (ret < 0) |
| 494 |
goto done; |
| 495 |
} |
| 496 |
|
| 497 |
iio_push_to_buffers_with_ts(indio_dev, data->buffer, |
| 498 |
sizeof(data->buffer), |
| 499 |
iio_get_time_ns(indio_dev)); |
| 500 |
|
| 501 |
done: |
| 502 |
iio_trigger_notify_done(indio_dev->trig); |
| 503 |
return IRQ_HANDLED; |
| 504 |
} |
| 505 |
|
| 506 |
static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask) |
| 507 |
{ |
| 508 |
struct si1145_data *data = iio_priv(indio_dev); |
| 509 |
u8 reg = 0, mux; |
| 510 |
int ret; |
| 511 |
int i; |
| 512 |
|
| 513 |
/* channel list already set, no need to reprogram */ |
| 514 |
if (data->scan_mask == scan_mask) |
| 515 |
return 0; |
| 516 |
|
| 517 |
for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) { |
| 518 |
switch (indio_dev->channels[i].address) { |
| 519 |
case SI1145_REG_ALSVIS_DATA: |
| 520 |
reg |= SI1145_CHLIST_EN_ALSVIS; |
| 521 |
break; |
| 522 |
case SI1145_REG_ALSIR_DATA: |
| 523 |
reg |= SI1145_CHLIST_EN_ALSIR; |
| 524 |
break; |
| 525 |
case SI1145_REG_PS1_DATA: |
| 526 |
reg |= SI1145_CHLIST_EN_PS1; |
| 527 |
break; |
| 528 |
case SI1145_REG_PS2_DATA: |
| 529 |
reg |= SI1145_CHLIST_EN_PS2; |
| 530 |
break; |
| 531 |
case SI1145_REG_PS3_DATA: |
| 532 |
reg |= SI1145_CHLIST_EN_PS3; |
| 533 |
break; |
| 534 |
case SI1145_REG_AUX_DATA: |
| 535 |
switch (indio_dev->channels[i].type) { |
| 536 |
case IIO_UVINDEX: |
| 537 |
reg |= SI1145_CHLIST_EN_UV; |
| 538 |
break; |
| 539 |
default: |
| 540 |
reg |= SI1145_CHLIST_EN_AUX; |
| 541 |
if (indio_dev->channels[i].type == IIO_TEMP) |
| 542 |
mux = SI1145_MUX_TEMP; |
| 543 |
else |
| 544 |
mux = SI1145_MUX_VDD; |
| 545 |
ret = si1145_param_set(data, |
| 546 |
SI1145_PARAM_AUX_ADC_MUX, mux); |
| 547 |
if (ret < 0) |
| 548 |
return ret; |
| 549 |
|
| 550 |
break; |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
data->scan_mask = scan_mask; |
| 556 |
ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg); |
| 557 |
|
| 558 |
return ret < 0 ? ret : 0; |
| 559 |
} |
| 560 |
|
| 561 |
static int si1145_measure(struct iio_dev *indio_dev, |
| 562 |
struct iio_chan_spec const *chan) |
| 563 |
{ |
| 564 |
struct si1145_data *data = iio_priv(indio_dev); |
| 565 |
u8 cmd; |
| 566 |
int ret; |
| 567 |
|
| 568 |
ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index)); |
| 569 |
if (ret < 0) |
| 570 |
return ret; |
| 571 |
|
| 572 |
cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE : |
| 573 |
SI1145_CMD_ALS_FORCE; |
| 574 |
ret = si1145_command(data, cmd); |
| 575 |
if (ret < 0 && ret != -EOVERFLOW) |
| 576 |
return ret; |
| 577 |
|
| 578 |
return i2c_smbus_read_word_data(data->client, chan->address); |
| 579 |
} |
| 580 |
|
| 581 |
/* |
| 582 |
* Conversion between iio scale and ADC_GAIN values |
| 583 |
* These could be further adjusted but proximity/intensity are dimensionless |
| 584 |
*/ |
| 585 |
static const int si1145_proximity_scale_available[] = { |
| 586 |
128, 64, 32, 16, 8, 4}; |
| 587 |
static const int si1145_intensity_scale_available[] = { |
| 588 |
128, 64, 32, 16, 8, 4, 2, 1}; |
| 589 |
static IIO_CONST_ATTR(in_proximity_scale_available, |
| 590 |
"128 64 32 16 8 4"); |
| 591 |
static IIO_CONST_ATTR(in_intensity_scale_available, |
| 592 |
"128 64 32 16 8 4 2 1"); |
| 593 |
static IIO_CONST_ATTR(in_intensity_ir_scale_available, |
| 594 |
"128 64 32 16 8 4 2 1"); |
| 595 |
|
| 596 |
static int si1145_scale_from_adcgain(int regval) |
| 597 |
{ |
| 598 |
return 128 >> regval; |
| 599 |
} |
| 600 |
|
| 601 |
static int si1145_proximity_adcgain_from_scale(int val, int val2) |
| 602 |
{ |
| 603 |
val = find_closest_descending(val, si1145_proximity_scale_available, |
| 604 |
ARRAY_SIZE(si1145_proximity_scale_available)); |
| 605 |
if (val < 0 || val > 5 || val2 != 0) |
| 606 |
return -EINVAL; |
| 607 |
|
| 608 |
return val; |
| 609 |
} |
| 610 |
|
| 611 |
static int si1145_intensity_adcgain_from_scale(int val, int val2) |
| 612 |
{ |
| 613 |
val = find_closest_descending(val, si1145_intensity_scale_available, |
| 614 |
ARRAY_SIZE(si1145_intensity_scale_available)); |
| 615 |
if (val < 0 || val > 7 || val2 != 0) |
| 616 |
return -EINVAL; |
| 617 |
|
| 618 |
return val; |
| 619 |
} |
| 620 |
|
| 621 |
static int si1145_read_raw(struct iio_dev *indio_dev, |
| 622 |
struct iio_chan_spec const *chan, |
| 623 |
int *val, int *val2, long mask) |
| 624 |
{ |
| 625 |
struct si1145_data *data = iio_priv(indio_dev); |
| 626 |
int ret; |
| 627 |
u8 reg; |
| 628 |
|
| 629 |
switch (mask) { |
| 630 |
case IIO_CHAN_INFO_RAW: |
| 631 |
switch (chan->type) { |
| 632 |
case IIO_INTENSITY: |
| 633 |
case IIO_PROXIMITY: |
| 634 |
case IIO_VOLTAGE: |
| 635 |
case IIO_TEMP: |
| 636 |
case IIO_UVINDEX: |
| 637 |
if (!iio_device_claim_direct(indio_dev)) |
| 638 |
return -EBUSY; |
| 639 |
ret = si1145_measure(indio_dev, chan); |
| 640 |
iio_device_release_direct(indio_dev); |
| 641 |
|
| 642 |
if (ret < 0) |
| 643 |
return ret; |
| 644 |
|
| 645 |
*val = ret; |
| 646 |
|
| 647 |
return IIO_VAL_INT; |
| 648 |
case IIO_CURRENT: |
| 649 |
ret = i2c_smbus_read_byte_data(data->client, |
| 650 |
SI1145_PS_LED_REG(chan->channel)); |
| 651 |
if (ret < 0) |
| 652 |
return ret; |
| 653 |
|
| 654 |
*val = (ret >> SI1145_PS_LED_SHIFT(chan->channel)) |
| 655 |
& 0x0f; |
| 656 |
|
| 657 |
return IIO_VAL_INT; |
| 658 |
default: |
| 659 |
return -EINVAL; |
| 660 |
} |
| 661 |
case IIO_CHAN_INFO_SCALE: |
| 662 |
switch (chan->type) { |
| 663 |
case IIO_PROXIMITY: |
| 664 |
reg = SI1145_PARAM_PS_ADC_GAIN; |
| 665 |
break; |
| 666 |
case IIO_INTENSITY: |
| 667 |
if (chan->channel2 == IIO_MOD_LIGHT_IR) |
| 668 |
reg = SI1145_PARAM_ALSIR_ADC_GAIN; |
| 669 |
else |
| 670 |
reg = SI1145_PARAM_ALSVIS_ADC_GAIN; |
| 671 |
break; |
| 672 |
case IIO_TEMP: |
| 673 |
*val = 28; |
| 674 |
*val2 = 571429; |
| 675 |
return IIO_VAL_INT_PLUS_MICRO; |
| 676 |
case IIO_UVINDEX: |
| 677 |
*val = 0; |
| 678 |
*val2 = 10000; |
| 679 |
return IIO_VAL_INT_PLUS_MICRO; |
| 680 |
default: |
| 681 |
return -EINVAL; |
| 682 |
} |
| 683 |
|
| 684 |
ret = si1145_param_query(data, reg); |
| 685 |
if (ret < 0) |
| 686 |
return ret; |
| 687 |
|
| 688 |
*val = si1145_scale_from_adcgain(ret & 0x07); |
| 689 |
|
| 690 |
return IIO_VAL_INT; |
| 691 |
case IIO_CHAN_INFO_OFFSET: |
| 692 |
switch (chan->type) { |
| 693 |
case IIO_TEMP: |
| 694 |
/* |
| 695 |
* -ADC offset - ADC counts @ 25°C - |
| 696 |
* 35 * ADC counts / °C |
| 697 |
*/ |
| 698 |
*val = -256 - 11136 + 25 * 35; |
| 699 |
return IIO_VAL_INT; |
| 700 |
default: |
| 701 |
/* |
| 702 |
* All ADC measurements have are by default offset |
| 703 |
* by -256 |
| 704 |
* See AN498 5.6.3 |
| 705 |
*/ |
| 706 |
ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET); |
| 707 |
if (ret < 0) |
| 708 |
return ret; |
| 709 |
*val = -si1145_uncompress(ret); |
| 710 |
return IIO_VAL_INT; |
| 711 |
} |
| 712 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 713 |
return si1145_read_samp_freq(data, val, val2); |
| 714 |
default: |
| 715 |
return -EINVAL; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
static int si1145_write_raw(struct iio_dev *indio_dev, |
| 720 |
struct iio_chan_spec const *chan, |
| 721 |
int val, int val2, long mask) |
| 722 |
{ |
| 723 |
struct si1145_data *data = iio_priv(indio_dev); |
| 724 |
u8 reg1, reg2, shift; |
| 725 |
int ret; |
| 726 |
|
| 727 |
switch (mask) { |
| 728 |
case IIO_CHAN_INFO_SCALE: |
| 729 |
switch (chan->type) { |
| 730 |
case IIO_PROXIMITY: |
| 731 |
val = si1145_proximity_adcgain_from_scale(val, val2); |
| 732 |
if (val < 0) |
| 733 |
return val; |
| 734 |
reg1 = SI1145_PARAM_PS_ADC_GAIN; |
| 735 |
reg2 = SI1145_PARAM_PS_ADC_COUNTER; |
| 736 |
break; |
| 737 |
case IIO_INTENSITY: |
| 738 |
val = si1145_intensity_adcgain_from_scale(val, val2); |
| 739 |
if (val < 0) |
| 740 |
return val; |
| 741 |
if (chan->channel2 == IIO_MOD_LIGHT_IR) { |
| 742 |
reg1 = SI1145_PARAM_ALSIR_ADC_GAIN; |
| 743 |
reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER; |
| 744 |
} else { |
| 745 |
reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN; |
| 746 |
reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER; |
| 747 |
} |
| 748 |
break; |
| 749 |
default: |
| 750 |
return -EINVAL; |
| 751 |
} |
| 752 |
|
| 753 |
if (!iio_device_claim_direct(indio_dev)) |
| 754 |
return -EBUSY; |
| 755 |
|
| 756 |
ret = si1145_param_set(data, reg1, val); |
| 757 |
if (ret < 0) { |
| 758 |
iio_device_release_direct(indio_dev); |
| 759 |
return ret; |
| 760 |
} |
| 761 |
/* Set recovery period to one's complement of gain */ |
| 762 |
ret = si1145_param_set(data, reg2, (~val & 0x07) << 4); |
| 763 |
iio_device_release_direct(indio_dev); |
| 764 |
return ret; |
| 765 |
case IIO_CHAN_INFO_RAW: |
| 766 |
if (chan->type != IIO_CURRENT) |
| 767 |
return -EINVAL; |
| 768 |
|
| 769 |
if (val < 0 || val > 15 || val2 != 0) |
| 770 |
return -EINVAL; |
| 771 |
|
| 772 |
reg1 = SI1145_PS_LED_REG(chan->channel); |
| 773 |
shift = SI1145_PS_LED_SHIFT(chan->channel); |
| 774 |
|
| 775 |
if (!iio_device_claim_direct(indio_dev)) |
| 776 |
return -EBUSY; |
| 777 |
|
| 778 |
ret = i2c_smbus_read_byte_data(data->client, reg1); |
| 779 |
if (ret < 0) { |
| 780 |
iio_device_release_direct(indio_dev); |
| 781 |
return ret; |
| 782 |
} |
| 783 |
ret = i2c_smbus_write_byte_data(data->client, reg1, |
| 784 |
(ret & ~(0x0f << shift)) | |
| 785 |
((val & 0x0f) << shift)); |
| 786 |
iio_device_release_direct(indio_dev); |
| 787 |
return ret; |
| 788 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 789 |
return si1145_store_samp_freq(data, val); |
| 790 |
default: |
| 791 |
return -EINVAL; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
#define SI1145_ST { \ |
| 796 |
.sign = 'u', \ |
| 797 |
.realbits = 16, \ |
| 798 |
.storagebits = 16, \ |
| 799 |
.endianness = IIO_LE, \ |
| 800 |
} |
| 801 |
|
| 802 |
#define SI1145_INTENSITY_CHANNEL(_si) { \ |
| 803 |
.type = IIO_INTENSITY, \ |
| 804 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 805 |
BIT(IIO_CHAN_INFO_OFFSET) | \ |
| 806 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 807 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 808 |
.scan_type = SI1145_ST, \ |
| 809 |
.scan_index = _si, \ |
| 810 |
.address = SI1145_REG_ALSVIS_DATA, \ |
| 811 |
} |
| 812 |
|
| 813 |
#define SI1145_INTENSITY_IR_CHANNEL(_si) { \ |
| 814 |
.type = IIO_INTENSITY, \ |
| 815 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 816 |
BIT(IIO_CHAN_INFO_OFFSET) | \ |
| 817 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 818 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 819 |
.modified = 1, \ |
| 820 |
.channel2 = IIO_MOD_LIGHT_IR, \ |
| 821 |
.scan_type = SI1145_ST, \ |
| 822 |
.scan_index = _si, \ |
| 823 |
.address = SI1145_REG_ALSIR_DATA, \ |
| 824 |
} |
| 825 |
|
| 826 |
#define SI1145_TEMP_CHANNEL(_si) { \ |
| 827 |
.type = IIO_TEMP, \ |
| 828 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 829 |
BIT(IIO_CHAN_INFO_OFFSET) | \ |
| 830 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 831 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 832 |
.scan_type = SI1145_ST, \ |
| 833 |
.scan_index = _si, \ |
| 834 |
.address = SI1145_REG_AUX_DATA, \ |
| 835 |
} |
| 836 |
|
| 837 |
#define SI1145_UV_CHANNEL(_si) { \ |
| 838 |
.type = IIO_UVINDEX, \ |
| 839 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
| 840 |
BIT(IIO_CHAN_INFO_SCALE), \ |
| 841 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 842 |
.scan_type = SI1145_ST, \ |
| 843 |
.scan_index = _si, \ |
| 844 |
.address = SI1145_REG_AUX_DATA, \ |
| 845 |
} |
| 846 |
|
| 847 |
#define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \ |
| 848 |
.type = IIO_PROXIMITY, \ |
| 849 |
.indexed = 1, \ |
| 850 |
.channel = _ch, \ |
| 851 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 852 |
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ |
| 853 |
BIT(IIO_CHAN_INFO_OFFSET), \ |
| 854 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 855 |
.scan_type = SI1145_ST, \ |
| 856 |
.scan_index = _si, \ |
| 857 |
.address = SI1145_REG_PS1_DATA + _ch * 2, \ |
| 858 |
} |
| 859 |
|
| 860 |
#define SI1145_VOLTAGE_CHANNEL(_si) { \ |
| 861 |
.type = IIO_VOLTAGE, \ |
| 862 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 863 |
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 864 |
.scan_type = SI1145_ST, \ |
| 865 |
.scan_index = _si, \ |
| 866 |
.address = SI1145_REG_AUX_DATA, \ |
| 867 |
} |
| 868 |
|
| 869 |
#define SI1145_CURRENT_CHANNEL(_ch) { \ |
| 870 |
.type = IIO_CURRENT, \ |
| 871 |
.indexed = 1, \ |
| 872 |
.channel = _ch, \ |
| 873 |
.output = 1, \ |
| 874 |
.scan_index = -1, \ |
| 875 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 876 |
} |
| 877 |
|
| 878 |
static const struct iio_chan_spec si1132_channels[] = { |
| 879 |
SI1145_INTENSITY_CHANNEL(0), |
| 880 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 881 |
SI1145_TEMP_CHANNEL(2), |
| 882 |
SI1145_VOLTAGE_CHANNEL(3), |
| 883 |
SI1145_UV_CHANNEL(4), |
| 884 |
IIO_CHAN_SOFT_TIMESTAMP(6), |
| 885 |
}; |
| 886 |
|
| 887 |
static const struct iio_chan_spec si1141_channels[] = { |
| 888 |
SI1145_INTENSITY_CHANNEL(0), |
| 889 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 890 |
SI1145_PROXIMITY_CHANNEL(2, 0), |
| 891 |
SI1145_TEMP_CHANNEL(3), |
| 892 |
SI1145_VOLTAGE_CHANNEL(4), |
| 893 |
IIO_CHAN_SOFT_TIMESTAMP(5), |
| 894 |
SI1145_CURRENT_CHANNEL(0), |
| 895 |
}; |
| 896 |
|
| 897 |
static const struct iio_chan_spec si1142_channels[] = { |
| 898 |
SI1145_INTENSITY_CHANNEL(0), |
| 899 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 900 |
SI1145_PROXIMITY_CHANNEL(2, 0), |
| 901 |
SI1145_PROXIMITY_CHANNEL(3, 1), |
| 902 |
SI1145_TEMP_CHANNEL(4), |
| 903 |
SI1145_VOLTAGE_CHANNEL(5), |
| 904 |
IIO_CHAN_SOFT_TIMESTAMP(6), |
| 905 |
SI1145_CURRENT_CHANNEL(0), |
| 906 |
SI1145_CURRENT_CHANNEL(1), |
| 907 |
}; |
| 908 |
|
| 909 |
static const struct iio_chan_spec si1143_channels[] = { |
| 910 |
SI1145_INTENSITY_CHANNEL(0), |
| 911 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 912 |
SI1145_PROXIMITY_CHANNEL(2, 0), |
| 913 |
SI1145_PROXIMITY_CHANNEL(3, 1), |
| 914 |
SI1145_PROXIMITY_CHANNEL(4, 2), |
| 915 |
SI1145_TEMP_CHANNEL(5), |
| 916 |
SI1145_VOLTAGE_CHANNEL(6), |
| 917 |
IIO_CHAN_SOFT_TIMESTAMP(7), |
| 918 |
SI1145_CURRENT_CHANNEL(0), |
| 919 |
SI1145_CURRENT_CHANNEL(1), |
| 920 |
SI1145_CURRENT_CHANNEL(2), |
| 921 |
}; |
| 922 |
|
| 923 |
static const struct iio_chan_spec si1145_channels[] = { |
| 924 |
SI1145_INTENSITY_CHANNEL(0), |
| 925 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 926 |
SI1145_PROXIMITY_CHANNEL(2, 0), |
| 927 |
SI1145_TEMP_CHANNEL(3), |
| 928 |
SI1145_VOLTAGE_CHANNEL(4), |
| 929 |
SI1145_UV_CHANNEL(5), |
| 930 |
IIO_CHAN_SOFT_TIMESTAMP(6), |
| 931 |
SI1145_CURRENT_CHANNEL(0), |
| 932 |
}; |
| 933 |
|
| 934 |
static const struct iio_chan_spec si1146_channels[] = { |
| 935 |
SI1145_INTENSITY_CHANNEL(0), |
| 936 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 937 |
SI1145_TEMP_CHANNEL(2), |
| 938 |
SI1145_VOLTAGE_CHANNEL(3), |
| 939 |
SI1145_UV_CHANNEL(4), |
| 940 |
SI1145_PROXIMITY_CHANNEL(5, 0), |
| 941 |
SI1145_PROXIMITY_CHANNEL(6, 1), |
| 942 |
IIO_CHAN_SOFT_TIMESTAMP(7), |
| 943 |
SI1145_CURRENT_CHANNEL(0), |
| 944 |
SI1145_CURRENT_CHANNEL(1), |
| 945 |
}; |
| 946 |
|
| 947 |
static const struct iio_chan_spec si1147_channels[] = { |
| 948 |
SI1145_INTENSITY_CHANNEL(0), |
| 949 |
SI1145_INTENSITY_IR_CHANNEL(1), |
| 950 |
SI1145_PROXIMITY_CHANNEL(2, 0), |
| 951 |
SI1145_PROXIMITY_CHANNEL(3, 1), |
| 952 |
SI1145_PROXIMITY_CHANNEL(4, 2), |
| 953 |
SI1145_TEMP_CHANNEL(5), |
| 954 |
SI1145_VOLTAGE_CHANNEL(6), |
| 955 |
SI1145_UV_CHANNEL(7), |
| 956 |
IIO_CHAN_SOFT_TIMESTAMP(8), |
| 957 |
SI1145_CURRENT_CHANNEL(0), |
| 958 |
SI1145_CURRENT_CHANNEL(1), |
| 959 |
SI1145_CURRENT_CHANNEL(2), |
| 960 |
}; |
| 961 |
|
| 962 |
static struct attribute *si1132_attributes[] = { |
| 963 |
&iio_const_attr_in_intensity_scale_available.dev_attr.attr, |
| 964 |
&iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr, |
| 965 |
NULL, |
| 966 |
}; |
| 967 |
|
| 968 |
static struct attribute *si114x_attributes[] = { |
| 969 |
&iio_const_attr_in_intensity_scale_available.dev_attr.attr, |
| 970 |
&iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr, |
| 971 |
&iio_const_attr_in_proximity_scale_available.dev_attr.attr, |
| 972 |
NULL, |
| 973 |
}; |
| 974 |
|
| 975 |
static const struct attribute_group si1132_attribute_group = { |
| 976 |
.attrs = si1132_attributes, |
| 977 |
}; |
| 978 |
|
| 979 |
static const struct attribute_group si114x_attribute_group = { |
| 980 |
.attrs = si114x_attributes, |
| 981 |
}; |
| 982 |
|
| 983 |
|
| 984 |
static const struct iio_info si1132_info = { |
| 985 |
.read_raw = si1145_read_raw, |
| 986 |
.write_raw = si1145_write_raw, |
| 987 |
.attrs = &si1132_attribute_group, |
| 988 |
}; |
| 989 |
|
| 990 |
static const struct iio_info si114x_info = { |
| 991 |
.read_raw = si1145_read_raw, |
| 992 |
.write_raw = si1145_write_raw, |
| 993 |
.attrs = &si114x_attribute_group, |
| 994 |
}; |
| 995 |
|
| 996 |
#define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \ |
| 997 |
{id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate} |
| 998 |
|
| 999 |
static const struct si1145_part_info si1145_part_info[] = { |
| 1000 |
[SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true), |
| 1001 |
[SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false), |
| 1002 |
[SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false), |
| 1003 |
[SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false), |
| 1004 |
[SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true), |
| 1005 |
[SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true), |
| 1006 |
[SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true), |
| 1007 |
}; |
| 1008 |
|
| 1009 |
static int si1145_initialize(struct si1145_data *data) |
| 1010 |
{ |
| 1011 |
struct i2c_client *client = data->client; |
| 1012 |
int ret; |
| 1013 |
|
| 1014 |
ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND, |
| 1015 |
SI1145_CMD_RESET); |
| 1016 |
if (ret < 0) |
| 1017 |
return ret; |
| 1018 |
msleep(SI1145_COMMAND_TIMEOUT_MS); |
| 1019 |
|
| 1020 |
/* Hardware key, magic value */ |
| 1021 |
ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17); |
| 1022 |
if (ret < 0) |
| 1023 |
return ret; |
| 1024 |
msleep(SI1145_COMMAND_TIMEOUT_MS); |
| 1025 |
|
| 1026 |
/* Turn off autonomous mode */ |
| 1027 |
ret = si1145_set_meas_rate(data, 0); |
| 1028 |
if (ret < 0) |
| 1029 |
return ret; |
| 1030 |
|
| 1031 |
/* Initialize sampling freq to 10 Hz */ |
| 1032 |
ret = si1145_store_samp_freq(data, 10); |
| 1033 |
if (ret < 0) |
| 1034 |
return ret; |
| 1035 |
|
| 1036 |
/* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */ |
| 1037 |
switch (data->part_info->num_leds) { |
| 1038 |
case 3: |
| 1039 |
ret = i2c_smbus_write_byte_data(client, |
| 1040 |
SI1145_REG_PS_LED3, |
| 1041 |
SI1145_LED_CURRENT_45mA); |
| 1042 |
if (ret < 0) |
| 1043 |
return ret; |
| 1044 |
fallthrough; |
| 1045 |
case 2: |
| 1046 |
ret = i2c_smbus_write_byte_data(client, |
| 1047 |
SI1145_REG_PS_LED21, |
| 1048 |
(SI1145_LED_CURRENT_45mA << 4) | |
| 1049 |
SI1145_LED_CURRENT_45mA); |
| 1050 |
break; |
| 1051 |
case 1: |
| 1052 |
ret = i2c_smbus_write_byte_data(client, |
| 1053 |
SI1145_REG_PS_LED21, |
| 1054 |
SI1145_LED_CURRENT_45mA); |
| 1055 |
break; |
| 1056 |
default: |
| 1057 |
ret = 0; |
| 1058 |
break; |
| 1059 |
} |
| 1060 |
if (ret < 0) |
| 1061 |
return ret; |
| 1062 |
|
| 1063 |
/* Set normal proximity measurement mode */ |
| 1064 |
ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC, |
| 1065 |
SI1145_PS_ADC_MODE_NORMAL); |
| 1066 |
if (ret < 0) |
| 1067 |
return ret; |
| 1068 |
|
| 1069 |
ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01); |
| 1070 |
if (ret < 0) |
| 1071 |
return ret; |
| 1072 |
|
| 1073 |
/* ADC_COUNTER should be one complement of ADC_GAIN */ |
| 1074 |
ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4); |
| 1075 |
if (ret < 0) |
| 1076 |
return ret; |
| 1077 |
|
| 1078 |
/* Set ALS visible measurement mode */ |
| 1079 |
ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC, |
| 1080 |
SI1145_ADC_MISC_RANGE); |
| 1081 |
if (ret < 0) |
| 1082 |
return ret; |
| 1083 |
|
| 1084 |
ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03); |
| 1085 |
if (ret < 0) |
| 1086 |
return ret; |
| 1087 |
|
| 1088 |
ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER, |
| 1089 |
0x04 << 4); |
| 1090 |
if (ret < 0) |
| 1091 |
return ret; |
| 1092 |
|
| 1093 |
/* Set ALS IR measurement mode */ |
| 1094 |
ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC, |
| 1095 |
SI1145_ADC_MISC_RANGE); |
| 1096 |
if (ret < 0) |
| 1097 |
return ret; |
| 1098 |
|
| 1099 |
ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01); |
| 1100 |
if (ret < 0) |
| 1101 |
return ret; |
| 1102 |
|
| 1103 |
ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER, |
| 1104 |
0x06 << 4); |
| 1105 |
if (ret < 0) |
| 1106 |
return ret; |
| 1107 |
|
| 1108 |
/* |
| 1109 |
* Initialize UCOEF to default values in datasheet |
| 1110 |
* These registers are normally zero on reset |
| 1111 |
*/ |
| 1112 |
if (data->part_info == &si1145_part_info[SI1132] || |
| 1113 |
data->part_info == &si1145_part_info[SI1145] || |
| 1114 |
data->part_info == &si1145_part_info[SI1146] || |
| 1115 |
data->part_info == &si1145_part_info[SI1147]) { |
| 1116 |
ret = i2c_smbus_write_byte_data(data->client, |
| 1117 |
SI1145_REG_UCOEF1, |
| 1118 |
SI1145_UCOEF1_DEFAULT); |
| 1119 |
if (ret < 0) |
| 1120 |
return ret; |
| 1121 |
ret = i2c_smbus_write_byte_data(data->client, |
| 1122 |
SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT); |
| 1123 |
if (ret < 0) |
| 1124 |
return ret; |
| 1125 |
ret = i2c_smbus_write_byte_data(data->client, |
| 1126 |
SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT); |
| 1127 |
if (ret < 0) |
| 1128 |
return ret; |
| 1129 |
ret = i2c_smbus_write_byte_data(data->client, |
| 1130 |
SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT); |
| 1131 |
if (ret < 0) |
| 1132 |
return ret; |
| 1133 |
} |
| 1134 |
|
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/* |
| 1139 |
* Program the channels we want to measure with CMD_PSALS_AUTO. No need for |
| 1140 |
* _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct) |
| 1141 |
* mode reprograms the channels list anyway... |
| 1142 |
*/ |
| 1143 |
static int si1145_buffer_preenable(struct iio_dev *indio_dev) |
| 1144 |
{ |
| 1145 |
struct si1145_data *data = iio_priv(indio_dev); |
| 1146 |
int ret; |
| 1147 |
|
| 1148 |
mutex_lock(&data->lock); |
| 1149 |
ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask); |
| 1150 |
mutex_unlock(&data->lock); |
| 1151 |
|
| 1152 |
return ret; |
| 1153 |
} |
| 1154 |
|
| 1155 |
static bool si1145_validate_scan_mask(struct iio_dev *indio_dev, |
| 1156 |
const unsigned long *scan_mask) |
| 1157 |
{ |
| 1158 |
struct si1145_data *data = iio_priv(indio_dev); |
| 1159 |
unsigned int count = 0; |
| 1160 |
int i; |
| 1161 |
|
| 1162 |
/* Check that at most one AUX channel is enabled */ |
| 1163 |
for_each_set_bit(i, scan_mask, data->part_info->num_channels) { |
| 1164 |
if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA) |
| 1165 |
count++; |
| 1166 |
} |
| 1167 |
|
| 1168 |
return count <= 1; |
| 1169 |
} |
| 1170 |
|
| 1171 |
static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = { |
| 1172 |
.preenable = si1145_buffer_preenable, |
| 1173 |
.validate_scan_mask = si1145_validate_scan_mask, |
| 1174 |
}; |
| 1175 |
|
| 1176 |
/* |
| 1177 |
* si1145_trigger_set_state() - Set trigger state |
| 1178 |
* |
| 1179 |
* When not using triggers interrupts are disabled and measurement rate is |
| 1180 |
* set to zero in order to minimize power consumption. |
| 1181 |
*/ |
| 1182 |
static int si1145_trigger_set_state(struct iio_trigger *trig, bool state) |
| 1183 |
{ |
| 1184 |
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
| 1185 |
struct si1145_data *data = iio_priv(indio_dev); |
| 1186 |
int err = 0, ret; |
| 1187 |
|
| 1188 |
mutex_lock(&data->lock); |
| 1189 |
|
| 1190 |
if (state) { |
| 1191 |
data->autonomous = true; |
| 1192 |
err = i2c_smbus_write_byte_data(data->client, |
| 1193 |
SI1145_REG_INT_CFG, SI1145_INT_CFG_OE); |
| 1194 |
if (err < 0) |
| 1195 |
goto disable; |
| 1196 |
err = i2c_smbus_write_byte_data(data->client, |
| 1197 |
SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE); |
| 1198 |
if (err < 0) |
| 1199 |
goto disable; |
| 1200 |
err = si1145_set_meas_rate(data, data->meas_rate); |
| 1201 |
if (err < 0) |
| 1202 |
goto disable; |
| 1203 |
err = si1145_command(data, SI1145_CMD_PSALS_AUTO); |
| 1204 |
if (err < 0) |
| 1205 |
goto disable; |
| 1206 |
} else { |
| 1207 |
disable: |
| 1208 |
/* Disable as much as possible skipping errors */ |
| 1209 |
ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE); |
| 1210 |
if (ret < 0 && !err) |
| 1211 |
err = ret; |
| 1212 |
ret = si1145_set_meas_rate(data, 0); |
| 1213 |
if (ret < 0 && !err) |
| 1214 |
err = ret; |
| 1215 |
ret = i2c_smbus_write_byte_data(data->client, |
| 1216 |
SI1145_REG_IRQ_ENABLE, 0); |
| 1217 |
if (ret < 0 && !err) |
| 1218 |
err = ret; |
| 1219 |
ret = i2c_smbus_write_byte_data(data->client, |
| 1220 |
SI1145_REG_INT_CFG, 0); |
| 1221 |
if (ret < 0 && !err) |
| 1222 |
err = ret; |
| 1223 |
data->autonomous = false; |
| 1224 |
} |
| 1225 |
|
| 1226 |
mutex_unlock(&data->lock); |
| 1227 |
return err; |
| 1228 |
} |
| 1229 |
|
| 1230 |
static const struct iio_trigger_ops si1145_trigger_ops = { |
| 1231 |
.set_trigger_state = si1145_trigger_set_state, |
| 1232 |
}; |
| 1233 |
|
| 1234 |
static int si1145_probe_trigger(struct iio_dev *indio_dev) |
| 1235 |
{ |
| 1236 |
struct si1145_data *data = iio_priv(indio_dev); |
| 1237 |
struct i2c_client *client = data->client; |
| 1238 |
struct iio_trigger *trig; |
| 1239 |
int ret; |
| 1240 |
|
| 1241 |
trig = devm_iio_trigger_alloc(&client->dev, |
| 1242 |
"%s-dev%d", indio_dev->name, iio_device_id(indio_dev)); |
| 1243 |
if (!trig) |
| 1244 |
return -ENOMEM; |
| 1245 |
|
| 1246 |
trig->ops = &si1145_trigger_ops; |
| 1247 |
iio_trigger_set_drvdata(trig, indio_dev); |
| 1248 |
|
| 1249 |
ret = devm_request_irq(&client->dev, client->irq, |
| 1250 |
iio_trigger_generic_data_rdy_poll, |
| 1251 |
IRQF_TRIGGER_FALLING | IRQF_NO_THREAD, |
| 1252 |
"si1145_irq", |
| 1253 |
trig); |
| 1254 |
if (ret < 0) { |
| 1255 |
dev_err(&client->dev, "irq request failed\n"); |
| 1256 |
return ret; |
| 1257 |
} |
| 1258 |
|
| 1259 |
ret = devm_iio_trigger_register(&client->dev, trig); |
| 1260 |
if (ret) |
| 1261 |
return ret; |
| 1262 |
|
| 1263 |
data->trig = trig; |
| 1264 |
indio_dev->trig = iio_trigger_get(data->trig); |
| 1265 |
|
| 1266 |
return 0; |
| 1267 |
} |
| 1268 |
|
| 1269 |
static int si1145_probe(struct i2c_client *client) |
| 1270 |
{ |
| 1271 |
const struct i2c_device_id *id = i2c_client_get_device_id(client); |
| 1272 |
struct si1145_data *data; |
| 1273 |
struct iio_dev *indio_dev; |
| 1274 |
u8 part_id, rev_id, seq_id; |
| 1275 |
int ret; |
| 1276 |
|
| 1277 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 1278 |
if (!indio_dev) |
| 1279 |
return -ENOMEM; |
| 1280 |
|
| 1281 |
data = iio_priv(indio_dev); |
| 1282 |
i2c_set_clientdata(client, indio_dev); |
| 1283 |
data->client = client; |
| 1284 |
data->part_info = &si1145_part_info[id->driver_data]; |
| 1285 |
|
| 1286 |
part_id = ret = i2c_smbus_read_byte_data(data->client, |
| 1287 |
SI1145_REG_PART_ID); |
| 1288 |
if (ret < 0) |
| 1289 |
return ret; |
| 1290 |
rev_id = ret = i2c_smbus_read_byte_data(data->client, |
| 1291 |
SI1145_REG_REV_ID); |
| 1292 |
if (ret < 0) |
| 1293 |
return ret; |
| 1294 |
seq_id = ret = i2c_smbus_read_byte_data(data->client, |
| 1295 |
SI1145_REG_SEQ_ID); |
| 1296 |
if (ret < 0) |
| 1297 |
return ret; |
| 1298 |
dev_info(&client->dev, "device ID part 0x%02x rev 0x%02x seq 0x%02x\n", |
| 1299 |
part_id, rev_id, seq_id); |
| 1300 |
if (part_id != data->part_info->part) { |
| 1301 |
dev_err(&client->dev, "part ID mismatch got 0x%02x, expected 0x%02x\n", |
| 1302 |
part_id, data->part_info->part); |
| 1303 |
return -ENODEV; |
| 1304 |
} |
| 1305 |
|
| 1306 |
indio_dev->name = id->name; |
| 1307 |
indio_dev->channels = data->part_info->channels; |
| 1308 |
indio_dev->num_channels = data->part_info->num_channels; |
| 1309 |
indio_dev->info = data->part_info->iio_info; |
| 1310 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1311 |
|
| 1312 |
mutex_init(&data->lock); |
| 1313 |
mutex_init(&data->cmdlock); |
| 1314 |
|
| 1315 |
ret = si1145_initialize(data); |
| 1316 |
if (ret < 0) |
| 1317 |
return ret; |
| 1318 |
|
| 1319 |
ret = devm_iio_triggered_buffer_setup(&client->dev, |
| 1320 |
indio_dev, NULL, |
| 1321 |
si1145_trigger_handler, &si1145_buffer_setup_ops); |
| 1322 |
if (ret < 0) |
| 1323 |
return ret; |
| 1324 |
|
| 1325 |
if (client->irq) { |
| 1326 |
ret = si1145_probe_trigger(indio_dev); |
| 1327 |
if (ret < 0) |
| 1328 |
return ret; |
| 1329 |
} else { |
| 1330 |
dev_info(&client->dev, "no irq, using polling\n"); |
| 1331 |
} |
| 1332 |
|
| 1333 |
return devm_iio_device_register(&client->dev, indio_dev); |
| 1334 |
} |
| 1335 |
|
| 1336 |
static const struct i2c_device_id si1145_ids[] = { |
| 1337 |
{ "si1132", SI1132 }, |
| 1338 |
{ "si1141", SI1141 }, |
| 1339 |
{ "si1142", SI1142 }, |
| 1340 |
{ "si1143", SI1143 }, |
| 1341 |
{ "si1145", SI1145 }, |
| 1342 |
{ "si1146", SI1146 }, |
| 1343 |
{ "si1147", SI1147 }, |
| 1344 |
{ } |
| 1345 |
}; |
| 1346 |
MODULE_DEVICE_TABLE(i2c, si1145_ids); |
| 1347 |
|
| 1348 |
static struct i2c_driver si1145_driver = { |
| 1349 |
.driver = { |
| 1350 |
.name = "si1145", |
| 1351 |
}, |
| 1352 |
.probe = si1145_probe, |
| 1353 |
.id_table = si1145_ids, |
| 1354 |
}; |
| 1355 |
|
| 1356 |
module_i2c_driver(si1145_driver); |
| 1357 |
|
| 1358 |
MODULE_AUTHOR("Peter Meerwald-Stadler <[email protected]>"); |
| 1359 |
MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver"); |
| 1360 |
MODULE_LICENSE("GPL"); |