20 #include <linux/module.h>
21 #include <linux/i2c.h>
25 #include <linux/slab.h>
30 #define CONVERSION_TIME_MS 100
32 #define ISL29028_REG_CONFIGURE 0x01
34 #define CONFIGURE_ALS_IR_MODE_ALS 0
35 #define CONFIGURE_ALS_IR_MODE_IR BIT(0)
36 #define CONFIGURE_ALS_IR_MODE_MASK BIT(0)
38 #define CONFIGURE_ALS_RANGE_LOW_LUX 0
39 #define CONFIGURE_ALS_RANGE_HIGH_LUX BIT(1)
40 #define CONFIGURE_ALS_RANGE_MASK BIT(1)
42 #define CONFIGURE_ALS_DIS 0
43 #define CONFIGURE_ALS_EN BIT(2)
44 #define CONFIGURE_ALS_EN_MASK BIT(2)
46 #define CONFIGURE_PROX_DRIVE BIT(3)
48 #define CONFIGURE_PROX_SLP_SH 4
49 #define CONFIGURE_PROX_SLP_MASK (7 << CONFIGURE_PROX_SLP_SH)
51 #define CONFIGURE_PROX_EN BIT(7)
52 #define CONFIGURE_PROX_EN_MASK BIT(7)
54 #define ISL29028_REG_INTERRUPT 0x02
56 #define ISL29028_REG_PROX_DATA 0x08
57 #define ISL29028_REG_ALSIR_L 0x09
58 #define ISL29028_REG_ALSIR_U 0x0A
60 #define ISL29028_REG_TEST1_MODE 0x0E
61 #define ISL29028_REG_TEST2_MODE 0x0F
63 #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
84 unsigned int sampling)
86 static unsigned int prox_period[] = {800, 400, 200, 100, 75, 50, 12, 0};
91 if (period >= prox_period[sel])
115 static int isl29028_set_als_scale(
struct isl29028_chip *chip,
int lux_scale)
124 static int isl29028_set_als_ir_mode(
struct isl29028_chip *chip,
164 static int isl29028_read_als_ir(
struct isl29028_chip *chip,
int *als_ir)
173 "Error in reading register ALSIR_L err %d\n", ret);
180 "Error in reading register ALSIR_U err %d\n", ret);
184 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
188 static int isl29028_read_proxim(
struct isl29028_chip *chip,
int *prox)
195 dev_err(chip->
dev,
"Error in reading register %d, error %d\n",
203 static int isl29028_proxim_get(
struct isl29028_chip *chip,
int *prox_data)
208 ret = isl29028_enable_proximity(chip,
true);
213 return isl29028_read_proxim(chip, prox_data);
222 ret = isl29028_set_als_ir_mode(chip,
MODE_ALS);
225 "Error in enabling ALS mode err %d\n", ret);
231 ret = isl29028_read_als_ir(chip, &als_ir_data);
241 als_ir_data = (als_ir_data * 31) / 1000;
243 als_ir_data = (als_ir_data * 49) / 100;
245 *als_data = als_ir_data;
249 static int isl29028_ir_get(
struct isl29028_chip *chip,
int *ir_data)
254 ret = isl29028_set_als_ir_mode(chip,
MODE_IR);
257 "Error in enabling IR mode err %d\n", ret);
262 return isl29028_read_als_ir(chip, ir_data);
266 static int isl29028_write_raw(
struct iio_dev *indio_dev,
273 switch (chan->
type) {
277 "proximity: mask value 0x%08lx not supported\n",
281 if (val < 1 || val > 100) {
283 "Samp_freq %d is not in range[1:100]\n", val);
286 ret = isl29028_set_proxim_sampling(chip, val);
289 "Setting proximity samp_freq fail, err %d\n",
299 "light: mask value 0x%08lx not supported\n",
303 if ((val != 125) && (val != 2000)) {
305 "lux scale %d is invalid [125, 2000]\n", val);
308 ret = isl29028_set_als_scale(chip, val);
311 "Setting lux scale fail with error %d\n", ret);
318 dev_err(chip->
dev,
"Unsupported channel type\n");
325 static int isl29028_read_raw(
struct iio_dev *indio_dev,
326 struct iio_chan_spec const *chan,
int *val,
int *val2,
long mask)
335 switch (chan->
type) {
337 ret = isl29028_als_get(chip, val);
340 ret = isl29028_ir_get(chip, val);
343 ret = isl29028_proxim_get(chip, val);
368 dev_err(chip->
dev,
"mask value 0x%08lx not supported\n", mask);
376 "1, 3, 5, 10, 13, 20, 83, 100");
377 static IIO_CONST_ATTR(in_illuminance_scale_available,
"125, 2000");
379 #define ISL29028_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
380 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
381 static struct attribute *isl29028_attributes[] = {
388 .attrs = isl29028_attributes,
406 static const struct iio_info isl29028_info = {
407 .attrs = &isl29108_group,
409 .read_raw = &isl29028_read_raw,
410 .write_raw = &isl29028_write_raw,
424 dev_err(chip->
dev,
"%s(): write to reg %d failed, err = %d\n",
430 dev_err(chip->
dev,
"%s(): write to reg %d failed, err = %d\n",
437 dev_err(chip->
dev,
"%s(): write to reg %d failed, err = %d\n",
442 ret = isl29028_set_proxim_sampling(chip, chip->
prox_sampling);
444 dev_err(chip->
dev,
"%s(): setting the proximity, err = %d\n",
449 ret = isl29028_set_als_scale(chip, chip->
lux_scale);
451 dev_err(chip->
dev,
"%s(): setting als scale failed, err = %d\n",
456 static bool is_volatile_reg(
struct device *
dev,
unsigned int reg)
469 static const struct regmap_config isl29028_regmap_config = {
472 .volatile_reg = is_volatile_reg,
487 dev_err(&client->
dev,
"iio allocation fails\n");
491 chip = iio_priv(indio_dev);
493 i2c_set_clientdata(client, indio_dev);
498 if (IS_ERR(chip->
regmap)) {
499 ret = PTR_ERR(chip->
regmap);
500 dev_err(chip->
dev,
"regmap initialization failed: %d\n", ret);
504 ret = isl29028_chip_init(chip);
506 dev_err(chip->
dev,
"chip initialization failed: %d\n", ret);
510 indio_dev->
info = &isl29028_info;
511 indio_dev->
channels = isl29028_channels;
513 indio_dev->
name =
id->name;
514 indio_dev->
dev.parent = &client->
dev;
518 dev_err(chip->
dev,
"iio registration fails with error %d\n",
531 struct iio_dev *indio_dev = i2c_get_clientdata(client);
544 static const struct of_device_id isl29028_of_match[] = {
545 { .compatible =
"isil,isl29028", },
555 .of_match_table = isl29028_of_match,
557 .probe = isl29028_probe,
559 .id_table = isl29028_id,