1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/i2c.h>
26#include <linux/err.h>
27#include <linux/mutex.h>
28#include <linux/delay.h>
29#include <linux/regmap.h>
30#include <linux/slab.h>
31#include <linux/iio/iio.h>
32#include <linux/iio/sysfs.h>
33
34#define CONVERSION_TIME_MS 100
35
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
43
44#define ISL29018_REG_ADD_COMMANDII 0x01
45#define COMMANDII_RESOLUTION_SHIFT 2
46#define COMMANDII_RESOLUTION_MASK (0x3 << COMMANDII_RESOLUTION_SHIFT)
47
48#define COMMANDII_RANGE_SHIFT 0
49#define COMMANDII_RANGE_MASK (0x3 << COMMANDII_RANGE_SHIFT)
50
51#define COMMANDII_SCHEME_SHIFT 7
52#define COMMANDII_SCHEME_MASK (0x1 << COMMANDII_SCHEME_SHIFT)
53
54#define ISL29018_REG_ADD_DATA_LSB 0x02
55#define ISL29018_REG_ADD_DATA_MSB 0x03
56
57#define ISL29018_REG_TEST 0x08
58#define ISL29018_TEST_SHIFT 0
59#define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
60
61struct isl29018_chip {
62 struct device *dev;
63 struct regmap *regmap;
64 struct mutex lock;
65 unsigned int lux_scale;
66 unsigned int lux_uscale;
67 unsigned int range;
68 unsigned int adc_bit;
69 int prox_scheme;
70 bool suspended;
71};
72
73static int isl29018_set_range(struct isl29018_chip *chip, unsigned long range,
74 unsigned int *new_range)
75{
76 static const unsigned long supp_ranges[] = {1000, 4000, 16000, 64000};
77 int i;
78
79 for (i = 0; i < ARRAY_SIZE(supp_ranges); ++i) {
80 if (range <= supp_ranges[i]) {
81 *new_range = (unsigned int)supp_ranges[i];
82 break;
83 }
84 }
85
86 if (i >= ARRAY_SIZE(supp_ranges))
87 return -EINVAL;
88
89 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
90 COMMANDII_RANGE_MASK, i << COMMANDII_RANGE_SHIFT);
91}
92
93static int isl29018_set_resolution(struct isl29018_chip *chip,
94 unsigned long adcbit, unsigned int *conf_adc_bit)
95{
96 static const unsigned long supp_adcbit[] = {16, 12, 8, 4};
97 int i;
98
99 for (i = 0; i < ARRAY_SIZE(supp_adcbit); ++i) {
100 if (adcbit >= supp_adcbit[i]) {
101 *conf_adc_bit = (unsigned int)supp_adcbit[i];
102 break;
103 }
104 }
105
106 if (i >= ARRAY_SIZE(supp_adcbit))
107 return -EINVAL;
108
109 return regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
110 COMMANDII_RESOLUTION_MASK,
111 i << COMMANDII_RESOLUTION_SHIFT);
112}
113
114static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
115{
116 int status;
117 unsigned int lsb;
118 unsigned int msb;
119
120
121 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
122 mode << COMMMAND1_OPMODE_SHIFT);
123 if (status) {
124 dev_err(chip->dev,
125 "Error in setting operating mode err %d\n", status);
126 return status;
127 }
128 msleep(CONVERSION_TIME_MS);
129 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
130 if (status < 0) {
131 dev_err(chip->dev,
132 "Error in reading LSB DATA with err %d\n", status);
133 return status;
134 }
135
136 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
137 if (status < 0) {
138 dev_err(chip->dev,
139 "Error in reading MSB DATA with error %d\n", status);
140 return status;
141 }
142 dev_vdbg(chip->dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
143
144 return (msb << 8) | lsb;
145}
146
147static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
148{
149 int lux_data;
150 unsigned int data_x_range, lux_unshifted;
151
152 lux_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_ALS_ONCE);
153
154 if (lux_data < 0)
155 return lux_data;
156
157
158
159
160
161
162 data_x_range = lux_data * chip->range;
163 lux_unshifted = data_x_range * chip->lux_scale;
164 lux_unshifted += data_x_range / 1000 * chip->lux_uscale / 1000;
165 *lux = lux_unshifted >> chip->adc_bit;
166
167 return 0;
168}
169
170static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
171{
172 int ir_data;
173
174 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
175
176 if (ir_data < 0)
177 return ir_data;
178
179 *ir = ir_data;
180
181 return 0;
182}
183
184static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
185 int *near_ir)
186{
187 int status;
188 int prox_data = -1;
189 int ir_data = -1;
190
191
192 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMANDII,
193 COMMANDII_SCHEME_MASK,
194 scheme << COMMANDII_SCHEME_SHIFT);
195 if (status) {
196 dev_err(chip->dev, "Error in setting operating mode\n");
197 return status;
198 }
199
200 prox_data = isl29018_read_sensor_input(chip,
201 COMMMAND1_OPMODE_PROX_ONCE);
202 if (prox_data < 0)
203 return prox_data;
204
205 if (scheme == 1) {
206 *near_ir = prox_data;
207 return 0;
208 }
209
210 ir_data = isl29018_read_sensor_input(chip, COMMMAND1_OPMODE_IR_ONCE);
211
212 if (ir_data < 0)
213 return ir_data;
214
215 if (prox_data >= ir_data)
216 *near_ir = prox_data - ir_data;
217 else
218 *near_ir = 0;
219
220 return 0;
221}
222
223
224
225static ssize_t show_range(struct device *dev,
226 struct device_attribute *attr, char *buf)
227{
228 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
229 struct isl29018_chip *chip = iio_priv(indio_dev);
230
231 return sprintf(buf, "%u\n", chip->range);
232}
233
234static ssize_t store_range(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t count)
236{
237 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
238 struct isl29018_chip *chip = iio_priv(indio_dev);
239 int status;
240 unsigned long lval;
241 unsigned int new_range;
242
243 if (kstrtoul(buf, 10, &lval))
244 return -EINVAL;
245
246 if (!(lval == 1000UL || lval == 4000UL ||
247 lval == 16000UL || lval == 64000UL)) {
248 dev_err(dev, "The range is not supported\n");
249 return -EINVAL;
250 }
251
252 mutex_lock(&chip->lock);
253 status = isl29018_set_range(chip, lval, &new_range);
254 if (status < 0) {
255 mutex_unlock(&chip->lock);
256 dev_err(dev,
257 "Error in setting max range with err %d\n", status);
258 return status;
259 }
260 chip->range = new_range;
261 mutex_unlock(&chip->lock);
262
263 return count;
264}
265
266
267static ssize_t show_resolution(struct device *dev,
268 struct device_attribute *attr, char *buf)
269{
270 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
271 struct isl29018_chip *chip = iio_priv(indio_dev);
272
273 return sprintf(buf, "%u\n", chip->adc_bit);
274}
275
276static ssize_t store_resolution(struct device *dev,
277 struct device_attribute *attr, const char *buf, size_t count)
278{
279 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
280 struct isl29018_chip *chip = iio_priv(indio_dev);
281 int status;
282 unsigned int val;
283 unsigned int new_adc_bit;
284
285 if (kstrtouint(buf, 10, &val))
286 return -EINVAL;
287 if (!(val == 4 || val == 8 || val == 12 || val == 16)) {
288 dev_err(dev, "The resolution is not supported\n");
289 return -EINVAL;
290 }
291
292 mutex_lock(&chip->lock);
293 status = isl29018_set_resolution(chip, val, &new_adc_bit);
294 if (status < 0) {
295 mutex_unlock(&chip->lock);
296 dev_err(dev, "Error in setting resolution\n");
297 return status;
298 }
299 chip->adc_bit = new_adc_bit;
300 mutex_unlock(&chip->lock);
301
302 return count;
303}
304
305
306static ssize_t show_prox_infrared_suppression(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
310 struct isl29018_chip *chip = iio_priv(indio_dev);
311
312
313
314 return sprintf(buf, "%d\n", chip->prox_scheme);
315}
316
317static ssize_t store_prox_infrared_suppression(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
319{
320 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
321 struct isl29018_chip *chip = iio_priv(indio_dev);
322 int val;
323
324 if (kstrtoint(buf, 10, &val))
325 return -EINVAL;
326 if (!(val == 0 || val == 1)) {
327 dev_err(dev, "The mode is not supported\n");
328 return -EINVAL;
329 }
330
331
332
333 mutex_lock(&chip->lock);
334 chip->prox_scheme = val;
335 mutex_unlock(&chip->lock);
336
337 return count;
338}
339
340
341static int isl29018_write_raw(struct iio_dev *indio_dev,
342 struct iio_chan_spec const *chan,
343 int val,
344 int val2,
345 long mask)
346{
347 struct isl29018_chip *chip = iio_priv(indio_dev);
348 int ret = -EINVAL;
349
350 mutex_lock(&chip->lock);
351 if (mask == IIO_CHAN_INFO_CALIBSCALE && chan->type == IIO_LIGHT) {
352 chip->lux_scale = val;
353
354 chip->lux_uscale = val2;
355 ret = 0;
356 }
357 mutex_unlock(&chip->lock);
358
359 return ret;
360}
361
362static int isl29018_read_raw(struct iio_dev *indio_dev,
363 struct iio_chan_spec const *chan,
364 int *val,
365 int *val2,
366 long mask)
367{
368 int ret = -EINVAL;
369 struct isl29018_chip *chip = iio_priv(indio_dev);
370
371 mutex_lock(&chip->lock);
372 if (chip->suspended) {
373 mutex_unlock(&chip->lock);
374 return -EBUSY;
375 }
376 switch (mask) {
377 case IIO_CHAN_INFO_RAW:
378 case IIO_CHAN_INFO_PROCESSED:
379 switch (chan->type) {
380 case IIO_LIGHT:
381 ret = isl29018_read_lux(chip, val);
382 break;
383 case IIO_INTENSITY:
384 ret = isl29018_read_ir(chip, val);
385 break;
386 case IIO_PROXIMITY:
387 ret = isl29018_read_proximity_ir(chip,
388 chip->prox_scheme, val);
389 break;
390 default:
391 break;
392 }
393 if (!ret)
394 ret = IIO_VAL_INT;
395 break;
396 case IIO_CHAN_INFO_CALIBSCALE:
397 if (chan->type == IIO_LIGHT) {
398 *val = chip->lux_scale;
399 *val2 = chip->lux_uscale;
400 ret = IIO_VAL_INT_PLUS_MICRO;
401 }
402 break;
403 default:
404 break;
405 }
406 mutex_unlock(&chip->lock);
407 return ret;
408}
409
410static const struct iio_chan_spec isl29018_channels[] = {
411 {
412 .type = IIO_LIGHT,
413 .indexed = 1,
414 .channel = 0,
415 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
416 BIT(IIO_CHAN_INFO_CALIBSCALE),
417 }, {
418 .type = IIO_INTENSITY,
419 .modified = 1,
420 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
421 .channel2 = IIO_MOD_LIGHT_IR,
422 }, {
423
424 .type = IIO_PROXIMITY,
425 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
426 }
427};
428
429static IIO_DEVICE_ATTR(range, S_IRUGO | S_IWUSR, show_range, store_range, 0);
430static IIO_CONST_ATTR(range_available, "1000 4000 16000 64000");
431static IIO_CONST_ATTR(adc_resolution_available, "4 8 12 16");
432static IIO_DEVICE_ATTR(adc_resolution, S_IRUGO | S_IWUSR,
433 show_resolution, store_resolution, 0);
434static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
435 S_IRUGO | S_IWUSR,
436 show_prox_infrared_suppression,
437 store_prox_infrared_suppression, 0);
438
439#define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
440#define ISL29018_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
441static struct attribute *isl29018_attributes[] = {
442 ISL29018_DEV_ATTR(range),
443 ISL29018_CONST_ATTR(range_available),
444 ISL29018_DEV_ATTR(adc_resolution),
445 ISL29018_CONST_ATTR(adc_resolution_available),
446 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
447 NULL
448};
449
450static const struct attribute_group isl29108_group = {
451 .attrs = isl29018_attributes,
452};
453
454static int isl29018_chip_init(struct isl29018_chip *chip)
455{
456 int status;
457 int new_adc_bit;
458 unsigned int new_range;
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
481 if (status < 0) {
482 dev_err(chip->dev, "Failed to clear isl29018 TEST reg."
483 "(%d)\n", status);
484 return status;
485 }
486
487
488
489
490
491 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
492 if (status < 0) {
493 dev_err(chip->dev, "Failed to clear isl29018 CMD1 reg."
494 "(%d)\n", status);
495 return status;
496 }
497
498 msleep(1);
499
500
501 status = isl29018_set_range(chip, chip->range, &new_range);
502 if (status < 0) {
503 dev_err(chip->dev, "Init of isl29018 fails\n");
504 return status;
505 }
506
507 status = isl29018_set_resolution(chip, chip->adc_bit,
508 &new_adc_bit);
509
510 return 0;
511}
512
513static const struct iio_info isl29108_info = {
514 .attrs = &isl29108_group,
515 .driver_module = THIS_MODULE,
516 .read_raw = &isl29018_read_raw,
517 .write_raw = &isl29018_write_raw,
518};
519
520static bool is_volatile_reg(struct device *dev, unsigned int reg)
521{
522 switch (reg) {
523 case ISL29018_REG_ADD_DATA_LSB:
524 case ISL29018_REG_ADD_DATA_MSB:
525 case ISL29018_REG_ADD_COMMAND1:
526 case ISL29018_REG_TEST:
527 return true;
528 default:
529 return false;
530 }
531}
532
533
534
535
536
537static const struct regmap_config isl29018_regmap_config = {
538 .reg_bits = 8,
539 .val_bits = 8,
540 .volatile_reg = is_volatile_reg,
541 .max_register = ISL29018_REG_TEST,
542 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
543 .cache_type = REGCACHE_RBTREE,
544};
545
546static int isl29018_probe(struct i2c_client *client,
547 const struct i2c_device_id *id)
548{
549 struct isl29018_chip *chip;
550 struct iio_dev *indio_dev;
551 int err;
552
553 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
554 if (indio_dev == NULL) {
555 dev_err(&client->dev, "iio allocation fails\n");
556 return -ENOMEM;
557 }
558 chip = iio_priv(indio_dev);
559
560 i2c_set_clientdata(client, indio_dev);
561 chip->dev = &client->dev;
562
563 mutex_init(&chip->lock);
564
565 chip->lux_scale = 1;
566 chip->lux_uscale = 0;
567 chip->range = 1000;
568 chip->adc_bit = 16;
569 chip->suspended = false;
570
571 chip->regmap = devm_regmap_init_i2c(client, &isl29018_regmap_config);
572 if (IS_ERR(chip->regmap)) {
573 err = PTR_ERR(chip->regmap);
574 dev_err(chip->dev, "regmap initialization failed: %d\n", err);
575 return err;
576 }
577
578 err = isl29018_chip_init(chip);
579 if (err)
580 return err;
581
582 indio_dev->info = &isl29108_info;
583 indio_dev->channels = isl29018_channels;
584 indio_dev->num_channels = ARRAY_SIZE(isl29018_channels);
585 indio_dev->name = id->name;
586 indio_dev->dev.parent = &client->dev;
587 indio_dev->modes = INDIO_DIRECT_MODE;
588 err = iio_device_register(indio_dev);
589 if (err) {
590 dev_err(&client->dev, "iio registration fails\n");
591 return err;
592 }
593
594 return 0;
595}
596
597static int isl29018_remove(struct i2c_client *client)
598{
599 struct iio_dev *indio_dev = i2c_get_clientdata(client);
600
601 dev_dbg(&client->dev, "%s()\n", __func__);
602 iio_device_unregister(indio_dev);
603
604 return 0;
605}
606
607#ifdef CONFIG_PM_SLEEP
608static int isl29018_suspend(struct device *dev)
609{
610 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
611
612 mutex_lock(&chip->lock);
613
614
615
616
617
618 chip->suspended = true;
619
620 mutex_unlock(&chip->lock);
621 return 0;
622}
623
624static int isl29018_resume(struct device *dev)
625{
626 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
627 int err;
628
629 mutex_lock(&chip->lock);
630
631 err = isl29018_chip_init(chip);
632 if (!err)
633 chip->suspended = false;
634
635 mutex_unlock(&chip->lock);
636 return err;
637}
638
639static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
640#define ISL29018_PM_OPS (&isl29018_pm_ops)
641#else
642#define ISL29018_PM_OPS NULL
643#endif
644
645static const struct i2c_device_id isl29018_id[] = {
646 {"isl29018", 0},
647 {}
648};
649
650MODULE_DEVICE_TABLE(i2c, isl29018_id);
651
652static const struct of_device_id isl29018_of_match[] = {
653 { .compatible = "isil,isl29018", },
654 { },
655};
656MODULE_DEVICE_TABLE(of, isl29018_of_match);
657
658static struct i2c_driver isl29018_driver = {
659 .class = I2C_CLASS_HWMON,
660 .driver = {
661 .name = "isl29018",
662 .pm = ISL29018_PM_OPS,
663 .owner = THIS_MODULE,
664 .of_match_table = isl29018_of_match,
665 },
666 .probe = isl29018_probe,
667 .remove = isl29018_remove,
668 .id_table = isl29018_id,
669};
670module_i2c_driver(isl29018_driver);
671
672MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
673MODULE_LICENSE("GPL");
674