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