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