24 #include <linux/module.h>
25 #include <linux/i2c.h>
30 #include <linux/slab.h>
34 #define CONVERSION_TIME_MS 100
36 #define ISL29018_REG_ADD_COMMAND1 0x00
37 #define COMMMAND1_OPMODE_SHIFT 5
38 #define COMMMAND1_OPMODE_MASK (7 << COMMMAND1_OPMODE_SHIFT)
39 #define COMMMAND1_OPMODE_POWER_DOWN 0
40 #define COMMMAND1_OPMODE_ALS_ONCE 1
41 #define COMMMAND1_OPMODE_IR_ONCE 2
42 #define COMMMAND1_OPMODE_PROX_ONCE 3
44 #define ISL29018_REG_ADD_COMMANDII 0x01
45 #define COMMANDII_RESOLUTION_SHIFT 2
46 #define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
48 #define COMMANDII_RANGE_SHIFT 0
49 #define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
51 #define COMMANDII_SCHEME_SHIFT 7
52 #define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
54 #define ISL29018_REG_ADD_DATA_LSB 0x02
55 #define ISL29018_REG_ADD_DATA_MSB 0x03
57 #define ISL29018_REG_TEST 0x08
58 #define ISL29018_TEST_SHIFT 0
59 #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
73 unsigned int *new_range)
75 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
79 if (range <= supp_ranges[i]) {
80 *new_range = (
unsigned int)supp_ranges[i];
92 static int isl29018_set_resolution(
struct isl29018_chip *chip,
93 unsigned long adcbit,
unsigned int *conf_adc_bit)
95 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
99 if (adcbit >= supp_adcbit[i]) {
100 *conf_adc_bit = (
unsigned int)supp_adcbit[i];
124 "Error in setting operating mode err %d\n", status);
131 "Error in reading LSB DATA with err %d\n", status);
138 "Error in reading MSB DATA with error %d\n", status);
141 dev_vdbg(chip->
dev,
"MSB 0x%x and LSB 0x%x\n", msb, lsb);
143 return (msb << 8) |
lsb;
146 static int isl29018_read_lux(
struct isl29018_chip *chip,
int *lux)
149 unsigned int data_x_range, lux_unshifted;
161 data_x_range = lux_data * chip->
range;
162 lux_unshifted = data_x_range * chip->
lux_scale;
163 lux_unshifted += data_x_range / 1000 * chip->
lux_uscale / 1000;
164 *lux = lux_unshifted >> chip->
adc_bit;
183 static int isl29018_read_proximity_ir(
struct isl29018_chip *chip,
int scheme,
195 dev_err(chip->
dev,
"Error in setting operating mode\n");
199 prox_data = isl29018_read_sensor_input(chip,
205 *near_ir = prox_data;
214 if (prox_data >= ir_data)
215 *near_ir = prox_data - ir_data;
227 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
236 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
240 unsigned int new_range;
245 if (!(lval == 1000
UL || lval == 4000
UL ||
246 lval == 16000
UL || lval == 64000
UL)) {
247 dev_err(dev,
"The range is not supported\n");
252 status = isl29018_set_range(chip, lval, &new_range);
256 "Error in setting max range with err %d\n", status);
259 chip->
range = new_range;
269 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
278 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
282 unsigned int new_adc_bit;
286 if (!(lval == 4 || lval == 8 || lval == 12 || lval == 16)) {
287 dev_err(dev,
"The resolution is not supported\n");
292 status = isl29018_set_resolution(chip, lval, &new_adc_bit);
295 dev_err(dev,
"Error in setting resolution\n");
305 static ssize_t show_prox_infrared_suppression(
struct device *dev,
308 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
316 static ssize_t store_prox_infrared_suppression(
struct device *dev,
319 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
325 if (!(lval == 0
UL || lval == 1
UL)) {
326 dev_err(dev,
"The mode is not supported\n");
340 static int isl29018_write_raw(
struct iio_dev *indio_dev,
361 static int isl29018_read_raw(
struct iio_dev *indio_dev,
374 switch (chan->
type) {
376 ret = isl29018_read_lux(chip, val);
379 ret = isl29018_read_ir(chip, val);
382 ret = isl29018_read_proximity_ir(chip,
428 show_resolution, store_resolution, 0);
431 show_prox_infrared_suppression,
432 store_prox_infrared_suppression, 0);
434 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
435 #define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
436 static struct attribute *isl29018_attributes[] = {
446 .attrs = isl29018_attributes,
453 unsigned int new_range;
477 dev_err(chip->
dev,
"Failed to clear isl29018 TEST reg."
488 dev_err(chip->
dev,
"Failed to clear isl29018 CMD1 reg."
496 status = isl29018_set_range(chip, chip->
range, &new_range);
498 dev_err(chip->
dev,
"Init of isl29018 fails\n");
502 status = isl29018_set_resolution(chip, chip->
adc_bit,
508 static const struct iio_info isl29108_info = {
509 .attrs = &isl29108_group,
511 .read_raw = &isl29018_read_raw,
512 .write_raw = &isl29018_write_raw,
515 static bool is_volatile_reg(
struct device *dev,
unsigned int reg)
532 static const struct regmap_config isl29018_regmap_config = {
535 .volatile_reg = is_volatile_reg,
549 if (indio_dev ==
NULL) {
550 dev_err(&client->
dev,
"iio allocation fails\n");
554 chip = iio_priv(indio_dev);
556 i2c_set_clientdata(client, indio_dev);
566 if (IS_ERR(chip->
regmap)) {
567 err = PTR_ERR(chip->
regmap);
568 dev_err(chip->
dev,
"regmap initialization failed: %d\n", err);
572 err = isl29018_chip_init(chip);
576 indio_dev->
info = &isl29108_info;
577 indio_dev->
channels = isl29018_channels;
579 indio_dev->
name =
id->name;
580 indio_dev->
dev.parent = &client->
dev;
584 dev_err(&client->
dev,
"iio registration fails\n");
597 struct iio_dev *indio_dev = i2c_get_clientdata(client);
613 static const struct of_device_id isl29018_of_match[] = {
614 { .compatible =
"isil,isl29018", },
624 .of_match_table = isl29018_of_match,
626 .probe = isl29018_probe,
628 .id_table = isl29018_id,