]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/iio/light/isl29018.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[thirdparty/kernel/stable.git] / drivers / iio / light / isl29018.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
94042874 2/*
609acefa 3 * A iio driver for the light sensor ISL 29018/29023/29035.
94042874
RK
4 *
5 * IIO driver for monitoring ambient light intensity in luxi, proximity
6 * sensing and infrared sensing.
7 *
8 * Copyright (c) 2010, NVIDIA Corporation.
94042874
RK
9 */
10
11#include <linux/module.h>
12#include <linux/i2c.h>
13#include <linux/err.h>
14#include <linux/mutex.h>
15#include <linux/delay.h>
057340e3 16#include <linux/regmap.h>
1a02d123 17#include <linux/regulator/consumer.h>
94042874 18#include <linux/slab.h>
06458e27
JC
19#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
dc4ecaf2 21#include <linux/acpi.h>
057340e3 22
13e6d634 23#define ISL29018_CONV_TIME_MS 100
94042874
RK
24
25#define ISL29018_REG_ADD_COMMAND1 0x00
13e6d634
PMS
26#define ISL29018_CMD1_OPMODE_SHIFT 5
27#define ISL29018_CMD1_OPMODE_MASK (7 << ISL29018_CMD1_OPMODE_SHIFT)
28#define ISL29018_CMD1_OPMODE_POWER_DOWN 0
29#define ISL29018_CMD1_OPMODE_ALS_ONCE 1
30#define ISL29018_CMD1_OPMODE_IR_ONCE 2
31#define ISL29018_CMD1_OPMODE_PROX_ONCE 3
94042874 32
13e6d634
PMS
33#define ISL29018_REG_ADD_COMMAND2 0x01
34#define ISL29018_CMD2_RESOLUTION_SHIFT 2
35#define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
94042874 36
13e6d634
PMS
37#define ISL29018_CMD2_RANGE_SHIFT 0
38#define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT)
94042874 39
13e6d634
PMS
40#define ISL29018_CMD2_SCHEME_SHIFT 7
41#define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
94042874
RK
42
43#define ISL29018_REG_ADD_DATA_LSB 0x02
44#define ISL29018_REG_ADD_DATA_MSB 0x03
94042874 45
176f9f29
GG
46#define ISL29018_REG_TEST 0x08
47#define ISL29018_TEST_SHIFT 0
48#define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
49
609acefa
LP
50#define ISL29035_REG_DEVICE_ID 0x0F
51#define ISL29035_DEVICE_ID_SHIFT 0x03
52#define ISL29035_DEVICE_ID_MASK (0x7 << ISL29035_DEVICE_ID_SHIFT)
53#define ISL29035_DEVICE_ID 0x5
54#define ISL29035_BOUT_SHIFT 0x07
55#define ISL29035_BOUT_MASK (0x01 << ISL29035_BOUT_SHIFT)
56
6920ccf6
RD
57enum isl29018_int_time {
58 ISL29018_INT_TIME_16,
59 ISL29018_INT_TIME_12,
60 ISL29018_INT_TIME_8,
61 ISL29018_INT_TIME_4,
62};
63
64static const unsigned int isl29018_int_utimes[3][4] = {
65 {90000, 5630, 351, 21},
66 {90000, 5600, 352, 22},
67 {105000, 6500, 410, 25},
68};
69
70static const struct isl29018_scale {
71 unsigned int scale;
72 unsigned int uscale;
73} isl29018_scales[4][4] = {
74 { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
75 { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
76 { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
77 { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
78};
79
94042874 80struct isl29018_chip {
057340e3 81 struct regmap *regmap;
94042874 82 struct mutex lock;
609acefa 83 int type;
809a591b
RD
84 unsigned int calibscale;
85 unsigned int ucalibscale;
6920ccf6
RD
86 unsigned int int_time;
87 struct isl29018_scale scale;
94042874 88 int prox_scheme;
1e45cf3c 89 bool suspended;
1a02d123 90 struct regulator *vcc_reg;
94042874
RK
91};
92
6920ccf6
RD
93static int isl29018_set_integration_time(struct isl29018_chip *chip,
94 unsigned int utime)
94042874 95{
ea908ab6
BM
96 unsigned int i;
97 int ret;
6920ccf6 98 unsigned int int_time, new_int_time;
6920ccf6
RD
99
100 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
101 if (utime == isl29018_int_utimes[chip->type][i]) {
102 new_int_time = i;
94042874
RK
103 break;
104 }
105 }
106
6920ccf6 107 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
94042874
RK
108 return -EINVAL;
109
13e6d634
PMS
110 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
111 ISL29018_CMD2_RESOLUTION_MASK,
112 i << ISL29018_CMD2_RESOLUTION_SHIFT);
6920ccf6
RD
113 if (ret < 0)
114 return ret;
115
96273f43 116 /* Keep the same range when integration time changes */
6920ccf6
RD
117 int_time = chip->int_time;
118 for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
119 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
120 chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
121 chip->scale = isl29018_scales[new_int_time][i];
122 break;
123 }
124 }
125 chip->int_time = new_int_time;
126
127 return 0;
94042874
RK
128}
129
6920ccf6 130static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
94042874 131{
ea908ab6
BM
132 unsigned int i;
133 int ret;
6920ccf6 134 struct isl29018_scale new_scale;
94042874 135
6920ccf6
RD
136 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
137 if (scale == isl29018_scales[chip->int_time][i].scale &&
138 uscale == isl29018_scales[chip->int_time][i].uscale) {
139 new_scale = isl29018_scales[chip->int_time][i];
94042874
RK
140 break;
141 }
142 }
143
6920ccf6 144 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
94042874
RK
145 return -EINVAL;
146
13e6d634
PMS
147 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
148 ISL29018_CMD2_RANGE_MASK,
149 i << ISL29018_CMD2_RANGE_SHIFT);
6920ccf6
RD
150 if (ret < 0)
151 return ret;
152
153 chip->scale = new_scale;
154
155 return 0;
94042874
RK
156}
157
057340e3 158static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
94042874
RK
159{
160 int status;
057340e3
LD
161 unsigned int lsb;
162 unsigned int msb;
64670869 163 struct device *dev = regmap_get_device(chip->regmap);
94042874
RK
164
165 /* Set mode */
057340e3 166 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
13e6d634 167 mode << ISL29018_CMD1_OPMODE_SHIFT);
94042874 168 if (status) {
64670869 169 dev_err(dev,
057340e3 170 "Error in setting operating mode err %d\n", status);
94042874
RK
171 return status;
172 }
13e6d634 173 msleep(ISL29018_CONV_TIME_MS);
057340e3
LD
174 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
175 if (status < 0) {
64670869 176 dev_err(dev,
057340e3
LD
177 "Error in reading LSB DATA with err %d\n", status);
178 return status;
94042874
RK
179 }
180
057340e3
LD
181 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
182 if (status < 0) {
64670869 183 dev_err(dev,
057340e3
LD
184 "Error in reading MSB DATA with error %d\n", status);
185 return status;
94042874 186 }
64670869 187 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
94042874
RK
188
189 return (msb << 8) | lsb;
190}
191
057340e3 192static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
94042874
RK
193{
194 int lux_data;
6920ccf6 195 unsigned int data_x_range;
94042874 196
13e6d634
PMS
197 lux_data = isl29018_read_sensor_input(chip,
198 ISL29018_CMD1_OPMODE_ALS_ONCE);
94042874
RK
199 if (lux_data < 0)
200 return lux_data;
201
6920ccf6
RD
202 data_x_range = lux_data * chip->scale.scale +
203 lux_data * chip->scale.uscale / 1000000;
204 *lux = data_x_range * chip->calibscale +
205 data_x_range * chip->ucalibscale / 1000000;
94042874
RK
206
207 return 0;
208}
209
057340e3 210static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
94042874
RK
211{
212 int ir_data;
213
13e6d634
PMS
214 ir_data = isl29018_read_sensor_input(chip,
215 ISL29018_CMD1_OPMODE_IR_ONCE);
94042874
RK
216 if (ir_data < 0)
217 return ir_data;
218
219 *ir = ir_data;
220
221 return 0;
222}
223
057340e3 224static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
ca52c88c 225 int *near_ir)
94042874
RK
226{
227 int status;
228 int prox_data = -1;
229 int ir_data = -1;
64670869 230 struct device *dev = regmap_get_device(chip->regmap);
94042874
RK
231
232 /* Do proximity sensing with required scheme */
13e6d634
PMS
233 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
234 ISL29018_CMD2_SCHEME_MASK,
235 scheme << ISL29018_CMD2_SCHEME_SHIFT);
94042874 236 if (status) {
64670869 237 dev_err(dev, "Error in setting operating mode\n");
94042874
RK
238 return status;
239 }
240
057340e3 241 prox_data = isl29018_read_sensor_input(chip,
13e6d634 242 ISL29018_CMD1_OPMODE_PROX_ONCE);
94042874
RK
243 if (prox_data < 0)
244 return prox_data;
245
246 if (scheme == 1) {
247 *near_ir = prox_data;
248 return 0;
249 }
250
13e6d634
PMS
251 ir_data = isl29018_read_sensor_input(chip,
252 ISL29018_CMD1_OPMODE_IR_ONCE);
94042874
RK
253 if (ir_data < 0)
254 return ir_data;
255
256 if (prox_data >= ir_data)
257 *near_ir = prox_data - ir_data;
258 else
259 *near_ir = 0;
260
261 return 0;
262}
263
02819966
BM
264static ssize_t in_illuminance_scale_available_show
265 (struct device *dev, struct device_attribute *attr,
266 char *buf)
6920ccf6
RD
267{
268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
269 struct isl29018_chip *chip = iio_priv(indio_dev);
ea908ab6
BM
270 unsigned int i;
271 int len = 0;
6920ccf6 272
5faf98cb 273 mutex_lock(&chip->lock);
6920ccf6
RD
274 for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
275 len += sprintf(buf + len, "%d.%06d ",
276 isl29018_scales[chip->int_time][i].scale,
277 isl29018_scales[chip->int_time][i].uscale);
5faf98cb 278 mutex_unlock(&chip->lock);
6920ccf6
RD
279
280 buf[len - 1] = '\n';
281
282 return len;
283}
284
02819966
BM
285static ssize_t in_illuminance_integration_time_available_show
286 (struct device *dev, struct device_attribute *attr,
287 char *buf)
6920ccf6
RD
288{
289 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
290 struct isl29018_chip *chip = iio_priv(indio_dev);
ea908ab6
BM
291 unsigned int i;
292 int len = 0;
6920ccf6
RD
293
294 for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i)
295 len += sprintf(buf + len, "0.%06d ",
296 isl29018_int_utimes[chip->type][i]);
297
298 buf[len - 1] = '\n';
299
300 return len;
301}
302
e748e280
BM
303/*
304 * From ISL29018 Data Sheet (FN6619.4, Oct 8, 2012) regarding the
305 * infrared suppression:
306 *
307 * Proximity Sensing Scheme: Bit 7. This bit programs the function
308 * of the proximity detection. Logic 0 of this bit, Scheme 0, makes
309 * full n (4, 8, 12, 16) bits (unsigned) proximity detection. The range
310 * of Scheme 0 proximity count is from 0 to 2^n. Logic 1 of this bit,
311 * Scheme 1, makes n-1 (3, 7, 11, 15) bits (2's complementary)
312 * proximity_less_ambient detection. The range of Scheme 1
313 * proximity count is from -2^(n-1) to 2^(n-1) . The sign bit is extended
314 * for resolutions less than 16. While Scheme 0 has wider dynamic
315 * range, Scheme 1 proximity detection is less affected by the
316 * ambient IR noise variation.
317 *
318 * 0 Sensing IR from LED and ambient
319 * 1 Sensing IR from LED with ambient IR rejection
320 */
02819966
BM
321static ssize_t proximity_on_chip_ambient_infrared_suppression_show
322 (struct device *dev, struct device_attribute *attr,
323 char *buf)
94042874 324{
96f691f9 325 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
927afbec 326 struct isl29018_chip *chip = iio_priv(indio_dev);
94042874 327
7f3829a3 328 /*
96273f43 329 * Return the "proximity scheme" i.e. if the chip does on chip
7f3829a3
ERR
330 * infrared suppression (1 means perform on chip suppression)
331 */
94042874
RK
332 return sprintf(buf, "%d\n", chip->prox_scheme);
333}
334
02819966
BM
335static ssize_t proximity_on_chip_ambient_infrared_suppression_store
336 (struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
94042874 338{
96f691f9 339 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
927afbec 340 struct isl29018_chip *chip = iio_priv(indio_dev);
e5e26dd5 341 int val;
94042874 342
e5e26dd5 343 if (kstrtoint(buf, 10, &val))
94042874 344 return -EINVAL;
ab5b9492 345 if (!(val == 0 || val == 1))
94042874 346 return -EINVAL;
94042874 347
7f3829a3 348 /*
96273f43 349 * Get the "proximity scheme" i.e. if the chip does on chip
7f3829a3
ERR
350 * infrared suppression (1 means perform on chip suppression)
351 */
94042874 352 mutex_lock(&chip->lock);
e5e26dd5 353 chip->prox_scheme = val;
94042874
RK
354 mutex_unlock(&chip->lock);
355
356 return count;
357}
358
01e57c57
BF
359static int isl29018_write_raw(struct iio_dev *indio_dev,
360 struct iio_chan_spec const *chan,
361 int val,
362 int val2,
363 long mask)
94042874 364{
01e57c57
BF
365 struct isl29018_chip *chip = iio_priv(indio_dev);
366 int ret = -EINVAL;
94042874 367
01e57c57 368 mutex_lock(&chip->lock);
00053566
BM
369 if (chip->suspended) {
370 ret = -EBUSY;
371 goto write_done;
372 }
6920ccf6
RD
373 switch (mask) {
374 case IIO_CHAN_INFO_CALIBSCALE:
375 if (chan->type == IIO_LIGHT) {
376 chip->calibscale = val;
377 chip->ucalibscale = val2;
378 ret = 0;
379 }
380 break;
381 case IIO_CHAN_INFO_INT_TIME:
528021fc 382 if (chan->type == IIO_LIGHT && !val)
6920ccf6
RD
383 ret = isl29018_set_integration_time(chip, val2);
384 break;
385 case IIO_CHAN_INFO_SCALE:
386 if (chan->type == IIO_LIGHT)
387 ret = isl29018_set_scale(chip, val, val2);
388 break;
389 default:
390 break;
01e57c57 391 }
01e57c57 392
00053566
BM
393write_done:
394 mutex_unlock(&chip->lock);
744ef62c 395
6dc973d4 396 return ret;
94042874
RK
397}
398
01e57c57
BF
399static int isl29018_read_raw(struct iio_dev *indio_dev,
400 struct iio_chan_spec const *chan,
401 int *val,
402 int *val2,
403 long mask)
94042874 404{
01e57c57
BF
405 int ret = -EINVAL;
406 struct isl29018_chip *chip = iio_priv(indio_dev);
01e57c57
BF
407
408 mutex_lock(&chip->lock);
1e45cf3c 409 if (chip->suspended) {
5611cd6f
BM
410 ret = -EBUSY;
411 goto read_done;
1e45cf3c 412 }
01e57c57 413 switch (mask) {
90354d00
JC
414 case IIO_CHAN_INFO_RAW:
415 case IIO_CHAN_INFO_PROCESSED:
01e57c57
BF
416 switch (chan->type) {
417 case IIO_LIGHT:
057340e3 418 ret = isl29018_read_lux(chip, val);
01e57c57
BF
419 break;
420 case IIO_INTENSITY:
057340e3 421 ret = isl29018_read_ir(chip, val);
01e57c57
BF
422 break;
423 case IIO_PROXIMITY:
057340e3 424 ret = isl29018_read_proximity_ir(chip,
ca52c88c
ERR
425 chip->prox_scheme,
426 val);
01e57c57
BF
427 break;
428 default:
429 break;
430 }
431 if (!ret)
432 ret = IIO_VAL_INT;
433 break;
6920ccf6
RD
434 case IIO_CHAN_INFO_INT_TIME:
435 if (chan->type == IIO_LIGHT) {
436 *val = 0;
437 *val2 = isl29018_int_utimes[chip->type][chip->int_time];
438 ret = IIO_VAL_INT_PLUS_MICRO;
439 }
440 break;
441 case IIO_CHAN_INFO_SCALE:
442 if (chan->type == IIO_LIGHT) {
443 *val = chip->scale.scale;
444 *val2 = chip->scale.uscale;
445 ret = IIO_VAL_INT_PLUS_MICRO;
446 }
447 break;
c8a9f805 448 case IIO_CHAN_INFO_CALIBSCALE:
01e57c57 449 if (chan->type == IIO_LIGHT) {
809a591b
RD
450 *val = chip->calibscale;
451 *val2 = chip->ucalibscale;
932323b7 452 ret = IIO_VAL_INT_PLUS_MICRO;
01e57c57
BF
453 }
454 break;
455 default:
456 break;
457 }
5611cd6f
BM
458
459read_done:
01e57c57 460 mutex_unlock(&chip->lock);
744ef62c 461
01e57c57 462 return ret;
94042874
RK
463}
464
609acefa
LP
465#define ISL29018_LIGHT_CHANNEL { \
466 .type = IIO_LIGHT, \
467 .indexed = 1, \
468 .channel = 0, \
469 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
6920ccf6
RD
470 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
471 BIT(IIO_CHAN_INFO_SCALE) | \
472 BIT(IIO_CHAN_INFO_INT_TIME), \
609acefa
LP
473}
474
475#define ISL29018_IR_CHANNEL { \
476 .type = IIO_INTENSITY, \
477 .modified = 1, \
478 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
479 .channel2 = IIO_MOD_LIGHT_IR, \
480}
481
482#define ISL29018_PROXIMITY_CHANNEL { \
483 .type = IIO_PROXIMITY, \
484 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
485}
486
01e57c57 487static const struct iio_chan_spec isl29018_channels[] = {
609acefa
LP
488 ISL29018_LIGHT_CHANNEL,
489 ISL29018_IR_CHANNEL,
490 ISL29018_PROXIMITY_CHANNEL,
491};
492
493static const struct iio_chan_spec isl29023_channels[] = {
494 ISL29018_LIGHT_CHANNEL,
495 ISL29018_IR_CHANNEL,
01e57c57
BF
496};
497
02819966
BM
498static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0);
499static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0);
500static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0);
94042874
RK
501
502#define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
6920ccf6 503
94042874 504static struct attribute *isl29018_attributes[] = {
6920ccf6
RD
505 ISL29018_DEV_ATTR(in_illuminance_scale_available),
506 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
2a1d45ec 507 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
94042874
RK
508 NULL
509};
510
609acefa 511static struct attribute *isl29023_attributes[] = {
6920ccf6
RD
512 ISL29018_DEV_ATTR(in_illuminance_scale_available),
513 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
609acefa
LP
514 NULL
515};
516
5b4b5b9c 517static const struct attribute_group isl29018_group = {
94042874
RK
518 .attrs = isl29018_attributes,
519};
520
609acefa
LP
521static const struct attribute_group isl29023_group = {
522 .attrs = isl29023_attributes,
523};
524
609acefa
LP
525enum {
526 isl29018,
527 isl29023,
528 isl29035,
529};
530
057340e3 531static int isl29018_chip_init(struct isl29018_chip *chip)
94042874 532{
94042874 533 int status;
64670869 534 struct device *dev = regmap_get_device(chip->regmap);
94042874 535
609acefa 536 if (chip->type == isl29035) {
8281101c
BM
537 unsigned int id;
538
539 status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
540 if (status < 0) {
541 dev_err(dev,
542 "Error reading ID register with error %d\n",
543 status);
544 return status;
545 }
546
547 id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
548
549 if (id != ISL29035_DEVICE_ID)
550 return -ENODEV;
551
552 /* Clear brownout bit */
553 status = regmap_update_bits(chip->regmap,
554 ISL29035_REG_DEVICE_ID,
555 ISL29035_BOUT_MASK, 0);
609acefa
LP
556 if (status < 0)
557 return status;
558 }
559
19a00a9d
BM
560 /*
561 * Code added per Intersil Application Note 1534:
176f9f29
GG
562 * When VDD sinks to approximately 1.8V or below, some of
563 * the part's registers may change their state. When VDD
564 * recovers to 2.25V (or greater), the part may thus be in an
565 * unknown mode of operation. The user can return the part to
566 * a known mode of operation either by (a) setting VDD = 0V for
567 * 1 second or more and then powering back up with a slew rate
568 * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
569 * conversions, clear the test registers, and then rewrite all
570 * registers to the desired values.
571 * ...
96273f43 572 * For ISL29011, ISL29018, ISL29021, ISL29023
176f9f29
GG
573 * 1. Write 0x00 to register 0x08 (TEST)
574 * 2. Write 0x00 to register 0x00 (CMD1)
575 * 3. Rewrite all registers to the desired values
576 *
577 * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
578 * the same thing EXCEPT the data sheet asks for a 1ms delay after
579 * writing the CMD1 register.
580 */
057340e3 581 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
176f9f29 582 if (status < 0) {
64670869 583 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
ad3e646c 584 status);
176f9f29
GG
585 return status;
586 }
587
19a00a9d
BM
588 /*
589 * See Intersil AN1534 comments above.
176f9f29
GG
590 * "Operating Mode" (COMMAND1) register is reprogrammed when
591 * data is read from the device.
592 */
057340e3 593 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
176f9f29 594 if (status < 0) {
64670869 595 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
ad3e646c 596 status);
176f9f29
GG
597 return status;
598 }
599
890d228f 600 usleep_range(1000, 2000); /* per data sheet, page 10 */
176f9f29 601
96273f43 602 /* Set defaults */
6920ccf6
RD
603 status = isl29018_set_scale(chip, chip->scale.scale,
604 chip->scale.uscale);
94042874 605 if (status < 0) {
64670869 606 dev_err(dev, "Init of isl29018 fails\n");
94042874
RK
607 return status;
608 }
609
6920ccf6
RD
610 status = isl29018_set_integration_time(chip,
611 isl29018_int_utimes[chip->type][chip->int_time]);
aba1a8d7 612 if (status < 0)
64670869 613 dev_err(dev, "Init of isl29018 fails\n");
94042874 614
aba1a8d7 615 return status;
94042874
RK
616}
617
5b4b5b9c
LP
618static const struct iio_info isl29018_info = {
619 .attrs = &isl29018_group,
6d9b10c8
BG
620 .read_raw = isl29018_read_raw,
621 .write_raw = isl29018_write_raw,
6fe8135f
JC
622};
623
609acefa
LP
624static const struct iio_info isl29023_info = {
625 .attrs = &isl29023_group,
6d9b10c8
BG
626 .read_raw = isl29018_read_raw,
627 .write_raw = isl29018_write_raw,
609acefa
LP
628};
629
9219376d 630static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
057340e3
LD
631{
632 switch (reg) {
633 case ISL29018_REG_ADD_DATA_LSB:
634 case ISL29018_REG_ADD_DATA_MSB:
635 case ISL29018_REG_ADD_COMMAND1:
636 case ISL29018_REG_TEST:
609acefa 637 case ISL29035_REG_DEVICE_ID:
057340e3
LD
638 return true;
639 default:
640 return false;
641 }
642}
643
057340e3
LD
644static const struct regmap_config isl29018_regmap_config = {
645 .reg_bits = 8,
646 .val_bits = 8,
9219376d 647 .volatile_reg = isl29018_is_volatile_reg,
057340e3
LD
648 .max_register = ISL29018_REG_TEST,
649 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
650 .cache_type = REGCACHE_RBTREE,
651};
652
609acefa
LP
653static const struct regmap_config isl29035_regmap_config = {
654 .reg_bits = 8,
655 .val_bits = 8,
9219376d 656 .volatile_reg = isl29018_is_volatile_reg,
609acefa
LP
657 .max_register = ISL29035_REG_DEVICE_ID,
658 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
659 .cache_type = REGCACHE_RBTREE,
660};
661
9219376d 662struct isl29018_chip_info {
609acefa
LP
663 const struct iio_chan_spec *channels;
664 int num_channels;
665 const struct iio_info *indio_info;
666 const struct regmap_config *regmap_cfg;
667};
668
9219376d 669static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
609acefa
LP
670 [isl29018] = {
671 .channels = isl29018_channels,
672 .num_channels = ARRAY_SIZE(isl29018_channels),
673 .indio_info = &isl29018_info,
674 .regmap_cfg = &isl29018_regmap_config,
675 },
676 [isl29023] = {
677 .channels = isl29023_channels,
678 .num_channels = ARRAY_SIZE(isl29023_channels),
679 .indio_info = &isl29023_info,
680 .regmap_cfg = &isl29018_regmap_config,
681 },
682 [isl29035] = {
683 .channels = isl29023_channels,
684 .num_channels = ARRAY_SIZE(isl29023_channels),
685 .indio_info = &isl29023_info,
686 .regmap_cfg = &isl29035_regmap_config,
687 },
688};
689
dc4ecaf2
LP
690static const char *isl29018_match_acpi_device(struct device *dev, int *data)
691{
692 const struct acpi_device_id *id;
693
694 id = acpi_match_device(dev->driver->acpi_match_table, dev);
695
696 if (!id)
697 return NULL;
698
79783b31 699 *data = (int)id->driver_data;
dc4ecaf2
LP
700
701 return dev_name(dev);
702}
703
1a02d123
AH
704static void isl29018_disable_regulator_action(void *_data)
705{
706 struct isl29018_chip *chip = _data;
707 int err;
708
709 err = regulator_disable(chip->vcc_reg);
710 if (err)
711 pr_err("failed to disable isl29018's VCC regulator!\n");
712}
713
4ae1c61f 714static int isl29018_probe(struct i2c_client *client,
ca52c88c 715 const struct i2c_device_id *id)
94042874
RK
716{
717 struct isl29018_chip *chip;
927afbec 718 struct iio_dev *indio_dev;
94042874 719 int err;
dc4ecaf2
LP
720 const char *name = NULL;
721 int dev_id = 0;
94042874 722
e434dcf3 723 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
ab5b9492 724 if (!indio_dev)
e434dcf3 725 return -ENOMEM;
744ef62c 726
927afbec 727 chip = iio_priv(indio_dev);
94042874 728
927afbec 729 i2c_set_clientdata(client, indio_dev);
94042874 730
dc4ecaf2
LP
731 if (id) {
732 name = id->name;
733 dev_id = id->driver_data;
734 }
735
736 if (ACPI_HANDLE(&client->dev))
737 name = isl29018_match_acpi_device(&client->dev, &dev_id);
738
94042874
RK
739 mutex_init(&chip->lock);
740
dc4ecaf2 741 chip->type = dev_id;
809a591b
RD
742 chip->calibscale = 1;
743 chip->ucalibscale = 0;
6920ccf6
RD
744 chip->int_time = ISL29018_INT_TIME_16;
745 chip->scale = isl29018_scales[chip->int_time][0];
1e45cf3c 746 chip->suspended = false;
94042874 747
1a02d123
AH
748 chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
749 if (IS_ERR(chip->vcc_reg)) {
750 err = PTR_ERR(chip->vcc_reg);
751 if (err != -EPROBE_DEFER)
752 dev_err(&client->dev, "failed to get VCC regulator!\n");
753 return err;
754 }
755
756 err = regulator_enable(chip->vcc_reg);
757 if (err) {
758 dev_err(&client->dev, "failed to enable VCC regulator!\n");
759 return err;
760 }
761
762 err = devm_add_action_or_reset(&client->dev, isl29018_disable_regulator_action,
763 chip);
764 if (err) {
765 dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
766 return err;
767 }
768
609acefa 769 chip->regmap = devm_regmap_init_i2c(client,
9219376d 770 isl29018_chip_info_tbl[dev_id].regmap_cfg);
057340e3
LD
771 if (IS_ERR(chip->regmap)) {
772 err = PTR_ERR(chip->regmap);
64670869 773 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
e434dcf3 774 return err;
057340e3
LD
775 }
776
777 err = isl29018_chip_init(chip);
94042874 778 if (err)
e434dcf3 779 return err;
94042874 780
9219376d
PMS
781 indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
782 indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
783 indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
dc4ecaf2 784 indio_dev->name = name;
927afbec
JC
785 indio_dev->dev.parent = &client->dev;
786 indio_dev->modes = INDIO_DIRECT_MODE;
744ef62c 787
ab5b9492 788 return devm_iio_device_register(&client->dev, indio_dev);
94042874
RK
789}
790
1e45cf3c
BF
791#ifdef CONFIG_PM_SLEEP
792static int isl29018_suspend(struct device *dev)
793{
794 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
1a02d123 795 int ret;
1e45cf3c
BF
796
797 mutex_lock(&chip->lock);
798
19a00a9d
BM
799 /*
800 * Since this driver uses only polling commands, we are by default in
1e45cf3c
BF
801 * auto shutdown (ie, power-down) mode.
802 * So we do not have much to do here.
803 */
804 chip->suspended = true;
1a02d123
AH
805 ret = regulator_disable(chip->vcc_reg);
806 if (ret)
807 dev_err(dev, "failed to disable VCC regulator\n");
1e45cf3c
BF
808
809 mutex_unlock(&chip->lock);
744ef62c 810
1a02d123 811 return ret;
1e45cf3c
BF
812}
813
814static int isl29018_resume(struct device *dev)
815{
816 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
817 int err;
818
819 mutex_lock(&chip->lock);
820
1a02d123
AH
821 err = regulator_enable(chip->vcc_reg);
822 if (err) {
823 dev_err(dev, "failed to enable VCC regulator\n");
824 mutex_unlock(&chip->lock);
825 return err;
826 }
827
1e45cf3c
BF
828 err = isl29018_chip_init(chip);
829 if (!err)
830 chip->suspended = false;
831
832 mutex_unlock(&chip->lock);
744ef62c 833
1e45cf3c
BF
834 return err;
835}
836
837static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
838#define ISL29018_PM_OPS (&isl29018_pm_ops)
839#else
840#define ISL29018_PM_OPS NULL
841#endif
842
14b39dce 843#ifdef CONFIG_ACPI
dc4ecaf2
LP
844static const struct acpi_device_id isl29018_acpi_match[] = {
845 {"ISL29018", isl29018},
846 {"ISL29023", isl29023},
847 {"ISL29035", isl29035},
848 {},
849};
850MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
14b39dce 851#endif
dc4ecaf2 852
94042874 853static const struct i2c_device_id isl29018_id[] = {
609acefa
LP
854 {"isl29018", isl29018},
855 {"isl29023", isl29023},
856 {"isl29035", isl29035},
94042874
RK
857 {}
858};
94042874
RK
859MODULE_DEVICE_TABLE(i2c, isl29018_id);
860
4ee19524 861static const struct of_device_id isl29018_of_match[] = {
610202dd 862 { .compatible = "isil,isl29018", },
609acefa
LP
863 { .compatible = "isil,isl29023", },
864 { .compatible = "isil,isl29035", },
4ee19524
OJ
865 { },
866};
867MODULE_DEVICE_TABLE(of, isl29018_of_match);
868
94042874 869static struct i2c_driver isl29018_driver = {
94042874
RK
870 .driver = {
871 .name = "isl29018",
dc4ecaf2 872 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
1e45cf3c 873 .pm = ISL29018_PM_OPS,
4ee19524 874 .of_match_table = isl29018_of_match,
94042874
RK
875 },
876 .probe = isl29018_probe,
94042874
RK
877 .id_table = isl29018_id,
878};
6e5af184 879module_i2c_driver(isl29018_driver);
94042874
RK
880
881MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
882MODULE_LICENSE("GPL");