blob: 805a74f08ad118daaa401301739cebcd398d07ca [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rhyland Klein94042872010-10-07 15:48:09 -07002/*
Laurentiu Palcu609acef2014-08-29 15:26:00 +01003 * A iio driver for the light sensor ISL 29018/29023/29035.
Rhyland Klein94042872010-10-07 15:48:09 -07004 *
5 * IIO driver for monitoring ambient light intensity in luxi, proximity
6 * sensing and infrared sensing.
7 *
8 * Copyright (c) 2010, NVIDIA Corporation.
Rhyland Klein94042872010-10-07 15:48:09 -07009 */
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>
Laxman Dewangan057340e2012-04-20 12:57:38 +053016#include <linux/regmap.h>
Anson Huang1a02d122019-01-08 09:09:39 +000017#include <linux/regulator/consumer.h>
Rhyland Klein94042872010-10-07 15:48:09 -070018#include <linux/slab.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
20#include <linux/iio/sysfs.h>
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +000021#include <linux/acpi.h>
Laxman Dewangan057340e2012-04-20 12:57:38 +053022
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020023#define ISL29018_CONV_TIME_MS 100
Rhyland Klein94042872010-10-07 15:48:09 -070024
25#define ISL29018_REG_ADD_COMMAND1 0x00
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020026#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
Rhyland Klein94042872010-10-07 15:48:09 -070032
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020033#define ISL29018_REG_ADD_COMMAND2 0x01
34#define ISL29018_CMD2_RESOLUTION_SHIFT 2
35#define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
Rhyland Klein94042872010-10-07 15:48:09 -070036
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020037#define ISL29018_CMD2_RANGE_SHIFT 0
38#define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT)
Rhyland Klein94042872010-10-07 15:48:09 -070039
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020040#define ISL29018_CMD2_SCHEME_SHIFT 7
41#define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
Rhyland Klein94042872010-10-07 15:48:09 -070042
43#define ISL29018_REG_ADD_DATA_LSB 0x02
44#define ISL29018_REG_ADD_DATA_MSB 0x03
Rhyland Klein94042872010-10-07 15:48:09 -070045
Grant Grundler176f9f22011-08-09 15:18:14 -070046#define ISL29018_REG_TEST 0x08
47#define ISL29018_TEST_SHIFT 0
48#define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
49
Laurentiu Palcu609acef2014-08-29 15:26:00 +010050#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
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +030057enum 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
Rhyland Klein94042872010-10-07 15:48:09 -070080struct isl29018_chip {
Laxman Dewangan057340e2012-04-20 12:57:38 +053081 struct regmap *regmap;
Rhyland Klein94042872010-10-07 15:48:09 -070082 struct mutex lock;
Laurentiu Palcu609acef2014-08-29 15:26:00 +010083 int type;
Roberta Dobrescu809a5912015-04-16 22:20:58 +030084 unsigned int calibscale;
85 unsigned int ucalibscale;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +030086 unsigned int int_time;
87 struct isl29018_scale scale;
Rhyland Klein94042872010-10-07 15:48:09 -070088 int prox_scheme;
Bryan Freed1e45cf32012-10-25 00:39:00 +010089 bool suspended;
Anson Huang1a02d122019-01-08 09:09:39 +000090 struct regulator *vcc_reg;
Rhyland Klein94042872010-10-07 15:48:09 -070091};
92
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +030093static int isl29018_set_integration_time(struct isl29018_chip *chip,
94 unsigned int utime)
Rhyland Klein94042872010-10-07 15:48:09 -070095{
Brian Masneyea908ab2016-09-25 07:41:07 -040096 unsigned int i;
97 int ret;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +030098 unsigned int int_time, new_int_time;
Rhyland Klein94042872010-10-07 15:48:09 -070099
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300100 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;
Rhyland Klein94042872010-10-07 15:48:09 -0700103 break;
104 }
105 }
106
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300107 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
Rhyland Klein94042872010-10-07 15:48:09 -0700108 return -EINVAL;
109
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200110 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
111 ISL29018_CMD2_RESOLUTION_MASK,
112 i << ISL29018_CMD2_RESOLUTION_SHIFT);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300113 if (ret < 0)
114 return ret;
115
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200116 /* Keep the same range when integration time changes */
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300117 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;
Rhyland Klein94042872010-10-07 15:48:09 -0700128}
129
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300130static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
Rhyland Klein94042872010-10-07 15:48:09 -0700131{
Brian Masneyea908ab2016-09-25 07:41:07 -0400132 unsigned int i;
133 int ret;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300134 struct isl29018_scale new_scale;
Rhyland Klein94042872010-10-07 15:48:09 -0700135
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300136 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];
Rhyland Klein94042872010-10-07 15:48:09 -0700140 break;
141 }
142 }
143
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300144 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
Rhyland Klein94042872010-10-07 15:48:09 -0700145 return -EINVAL;
146
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200147 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
148 ISL29018_CMD2_RANGE_MASK,
149 i << ISL29018_CMD2_RANGE_SHIFT);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300150 if (ret < 0)
151 return ret;
152
153 chip->scale = new_scale;
154
155 return 0;
Rhyland Klein94042872010-10-07 15:48:09 -0700156}
157
Laxman Dewangan057340e2012-04-20 12:57:38 +0530158static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
Rhyland Klein94042872010-10-07 15:48:09 -0700159{
160 int status;
Laxman Dewangan057340e2012-04-20 12:57:38 +0530161 unsigned int lsb;
162 unsigned int msb;
Alison Schofield64670862016-02-22 12:34:32 -0800163 struct device *dev = regmap_get_device(chip->regmap);
Rhyland Klein94042872010-10-07 15:48:09 -0700164
165 /* Set mode */
Laxman Dewangan057340e2012-04-20 12:57:38 +0530166 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200167 mode << ISL29018_CMD1_OPMODE_SHIFT);
Rhyland Klein94042872010-10-07 15:48:09 -0700168 if (status) {
Alison Schofield64670862016-02-22 12:34:32 -0800169 dev_err(dev,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530170 "Error in setting operating mode err %d\n", status);
Rhyland Klein94042872010-10-07 15:48:09 -0700171 return status;
172 }
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200173 msleep(ISL29018_CONV_TIME_MS);
Laxman Dewangan057340e2012-04-20 12:57:38 +0530174 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
175 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800176 dev_err(dev,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530177 "Error in reading LSB DATA with err %d\n", status);
178 return status;
Rhyland Klein94042872010-10-07 15:48:09 -0700179 }
180
Laxman Dewangan057340e2012-04-20 12:57:38 +0530181 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
182 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800183 dev_err(dev,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530184 "Error in reading MSB DATA with error %d\n", status);
185 return status;
Rhyland Klein94042872010-10-07 15:48:09 -0700186 }
Alison Schofield64670862016-02-22 12:34:32 -0800187 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
Rhyland Klein94042872010-10-07 15:48:09 -0700188
189 return (msb << 8) | lsb;
190}
191
Laxman Dewangan057340e2012-04-20 12:57:38 +0530192static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
Rhyland Klein94042872010-10-07 15:48:09 -0700193{
194 int lux_data;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300195 unsigned int data_x_range;
Rhyland Klein94042872010-10-07 15:48:09 -0700196
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200197 lux_data = isl29018_read_sensor_input(chip,
198 ISL29018_CMD1_OPMODE_ALS_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700199 if (lux_data < 0)
200 return lux_data;
201
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300202 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;
Rhyland Klein94042872010-10-07 15:48:09 -0700206
207 return 0;
208}
209
Laxman Dewangan057340e2012-04-20 12:57:38 +0530210static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
Rhyland Klein94042872010-10-07 15:48:09 -0700211{
212 int ir_data;
213
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200214 ir_data = isl29018_read_sensor_input(chip,
215 ISL29018_CMD1_OPMODE_IR_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700216 if (ir_data < 0)
217 return ir_data;
218
219 *ir = ir_data;
220
221 return 0;
222}
223
Laxman Dewangan057340e2012-04-20 12:57:38 +0530224static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800225 int *near_ir)
Rhyland Klein94042872010-10-07 15:48:09 -0700226{
227 int status;
228 int prox_data = -1;
229 int ir_data = -1;
Alison Schofield64670862016-02-22 12:34:32 -0800230 struct device *dev = regmap_get_device(chip->regmap);
Rhyland Klein94042872010-10-07 15:48:09 -0700231
232 /* Do proximity sensing with required scheme */
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200233 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
234 ISL29018_CMD2_SCHEME_MASK,
235 scheme << ISL29018_CMD2_SCHEME_SHIFT);
Rhyland Klein94042872010-10-07 15:48:09 -0700236 if (status) {
Alison Schofield64670862016-02-22 12:34:32 -0800237 dev_err(dev, "Error in setting operating mode\n");
Rhyland Klein94042872010-10-07 15:48:09 -0700238 return status;
239 }
240
Laxman Dewangan057340e2012-04-20 12:57:38 +0530241 prox_data = isl29018_read_sensor_input(chip,
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200242 ISL29018_CMD1_OPMODE_PROX_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700243 if (prox_data < 0)
244 return prox_data;
245
246 if (scheme == 1) {
247 *near_ir = prox_data;
248 return 0;
249 }
250
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200251 ir_data = isl29018_read_sensor_input(chip,
252 ISL29018_CMD1_OPMODE_IR_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700253 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
Brian Masney02819962016-09-26 20:20:17 -0400264static ssize_t in_illuminance_scale_available_show
265 (struct device *dev, struct device_attribute *attr,
266 char *buf)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300267{
268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
269 struct isl29018_chip *chip = iio_priv(indio_dev);
Brian Masneyea908ab2016-09-25 07:41:07 -0400270 unsigned int i;
271 int len = 0;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300272
Brian Masney5faf98c2016-09-26 20:20:18 -0400273 mutex_lock(&chip->lock);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300274 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);
Brian Masney5faf98c2016-09-26 20:20:18 -0400278 mutex_unlock(&chip->lock);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300279
280 buf[len - 1] = '\n';
281
282 return len;
283}
284
Brian Masney02819962016-09-26 20:20:17 -0400285static ssize_t in_illuminance_integration_time_available_show
286 (struct device *dev, struct device_attribute *attr,
287 char *buf)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300288{
289 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
290 struct isl29018_chip *chip = iio_priv(indio_dev);
Brian Masneyea908ab2016-09-25 07:41:07 -0400291 unsigned int i;
292 int len = 0;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300293
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
Brian Masneye748e282016-10-06 20:48:34 -0400303/*
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 */
Brian Masney02819962016-09-26 20:20:17 -0400321static ssize_t proximity_on_chip_ambient_infrared_suppression_show
322 (struct device *dev, struct device_attribute *attr,
323 char *buf)
Rhyland Klein94042872010-10-07 15:48:09 -0700324{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200325 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100326 struct isl29018_chip *chip = iio_priv(indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700327
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800328 /*
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200329 * Return the "proximity scheme" i.e. if the chip does on chip
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800330 * infrared suppression (1 means perform on chip suppression)
331 */
Rhyland Klein94042872010-10-07 15:48:09 -0700332 return sprintf(buf, "%d\n", chip->prox_scheme);
333}
334
Brian Masney02819962016-09-26 20:20:17 -0400335static ssize_t proximity_on_chip_ambient_infrared_suppression_store
336 (struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
Rhyland Klein94042872010-10-07 15:48:09 -0700338{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200339 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100340 struct isl29018_chip *chip = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100341 int val;
Rhyland Klein94042872010-10-07 15:48:09 -0700342
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100343 if (kstrtoint(buf, 10, &val))
Rhyland Klein94042872010-10-07 15:48:09 -0700344 return -EINVAL;
Peter Meerwald-Stadlerab5b9492016-07-05 13:55:37 +0200345 if (!(val == 0 || val == 1))
Rhyland Klein94042872010-10-07 15:48:09 -0700346 return -EINVAL;
Rhyland Klein94042872010-10-07 15:48:09 -0700347
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800348 /*
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200349 * Get the "proximity scheme" i.e. if the chip does on chip
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800350 * infrared suppression (1 means perform on chip suppression)
351 */
Rhyland Klein94042872010-10-07 15:48:09 -0700352 mutex_lock(&chip->lock);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100353 chip->prox_scheme = val;
Rhyland Klein94042872010-10-07 15:48:09 -0700354 mutex_unlock(&chip->lock);
355
356 return count;
357}
358
Bryan Freed01e57c52011-07-07 12:01:56 -0700359static 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)
Rhyland Klein94042872010-10-07 15:48:09 -0700364{
Bryan Freed01e57c52011-07-07 12:01:56 -0700365 struct isl29018_chip *chip = iio_priv(indio_dev);
366 int ret = -EINVAL;
367
368 mutex_lock(&chip->lock);
Brian Masney00053562016-09-26 20:20:20 -0400369 if (chip->suspended) {
370 ret = -EBUSY;
371 goto write_done;
372 }
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300373 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:
Brian Masney528021f2016-09-25 07:41:06 -0400382 if (chan->type == IIO_LIGHT && !val)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300383 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;
Bryan Freed01e57c52011-07-07 12:01:56 -0700391 }
Bryan Freed01e57c52011-07-07 12:01:56 -0700392
Brian Masney00053562016-09-26 20:20:20 -0400393write_done:
394 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400395
Wei Yongjun6dc973d2012-11-08 09:05:00 +0000396 return ret;
Rhyland Klein94042872010-10-07 15:48:09 -0700397}
398
Bryan Freed01e57c52011-07-07 12:01:56 -0700399static 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)
Rhyland Klein94042872010-10-07 15:48:09 -0700404{
Bryan Freed01e57c52011-07-07 12:01:56 -0700405 int ret = -EINVAL;
406 struct isl29018_chip *chip = iio_priv(indio_dev);
Bryan Freed01e57c52011-07-07 12:01:56 -0700407
408 mutex_lock(&chip->lock);
Bryan Freed1e45cf32012-10-25 00:39:00 +0100409 if (chip->suspended) {
Brian Masney5611cd62016-09-26 20:20:19 -0400410 ret = -EBUSY;
411 goto read_done;
Bryan Freed1e45cf32012-10-25 00:39:00 +0100412 }
Bryan Freed01e57c52011-07-07 12:01:56 -0700413 switch (mask) {
Jonathan Cameron90354d02012-04-15 17:41:22 +0100414 case IIO_CHAN_INFO_RAW:
415 case IIO_CHAN_INFO_PROCESSED:
Bryan Freed01e57c52011-07-07 12:01:56 -0700416 switch (chan->type) {
417 case IIO_LIGHT:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530418 ret = isl29018_read_lux(chip, val);
Bryan Freed01e57c52011-07-07 12:01:56 -0700419 break;
420 case IIO_INTENSITY:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530421 ret = isl29018_read_ir(chip, val);
Bryan Freed01e57c52011-07-07 12:01:56 -0700422 break;
423 case IIO_PROXIMITY:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530424 ret = isl29018_read_proximity_ir(chip,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800425 chip->prox_scheme,
426 val);
Bryan Freed01e57c52011-07-07 12:01:56 -0700427 break;
428 default:
429 break;
430 }
431 if (!ret)
432 ret = IIO_VAL_INT;
433 break;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300434 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;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100448 case IIO_CHAN_INFO_CALIBSCALE:
Bryan Freed01e57c52011-07-07 12:01:56 -0700449 if (chan->type == IIO_LIGHT) {
Roberta Dobrescu809a5912015-04-16 22:20:58 +0300450 *val = chip->calibscale;
451 *val2 = chip->ucalibscale;
Bryan Freed932323b2012-09-05 20:55:00 +0100452 ret = IIO_VAL_INT_PLUS_MICRO;
Bryan Freed01e57c52011-07-07 12:01:56 -0700453 }
454 break;
455 default:
456 break;
457 }
Brian Masney5611cd62016-09-26 20:20:19 -0400458
459read_done:
Bryan Freed01e57c52011-07-07 12:01:56 -0700460 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400461
Bryan Freed01e57c52011-07-07 12:01:56 -0700462 return ret;
Rhyland Klein94042872010-10-07 15:48:09 -0700463}
464
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100465#define ISL29018_LIGHT_CHANNEL { \
466 .type = IIO_LIGHT, \
467 .indexed = 1, \
468 .channel = 0, \
469 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300470 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
471 BIT(IIO_CHAN_INFO_SCALE) | \
472 BIT(IIO_CHAN_INFO_INT_TIME), \
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100473}
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
Bryan Freed01e57c52011-07-07 12:01:56 -0700487static const struct iio_chan_spec isl29018_channels[] = {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100488 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,
Bryan Freed01e57c52011-07-07 12:01:56 -0700496};
Rhyland Klein94042872010-10-07 15:48:09 -0700497
Brian Masney02819962016-09-26 20:20:17 -0400498static 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);
Rhyland Klein94042872010-10-07 15:48:09 -0700501
502#define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300503
Rhyland Klein94042872010-10-07 15:48:09 -0700504static struct attribute *isl29018_attributes[] = {
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300505 ISL29018_DEV_ATTR(in_illuminance_scale_available),
506 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
Peter Meerwald2a1d45e2012-06-18 20:33:07 +0200507 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
Rhyland Klein94042872010-10-07 15:48:09 -0700508 NULL
509};
510
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100511static struct attribute *isl29023_attributes[] = {
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300512 ISL29018_DEV_ATTR(in_illuminance_scale_available),
513 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100514 NULL
515};
516
Laurentiu Palcu5b4b5b92014-08-29 15:26:00 +0100517static const struct attribute_group isl29018_group = {
Rhyland Klein94042872010-10-07 15:48:09 -0700518 .attrs = isl29018_attributes,
519};
520
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100521static const struct attribute_group isl29023_group = {
522 .attrs = isl29023_attributes,
523};
524
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100525enum {
526 isl29018,
527 isl29023,
528 isl29035,
529};
530
Laxman Dewangan057340e2012-04-20 12:57:38 +0530531static int isl29018_chip_init(struct isl29018_chip *chip)
Rhyland Klein94042872010-10-07 15:48:09 -0700532{
Rhyland Klein94042872010-10-07 15:48:09 -0700533 int status;
Alison Schofield64670862016-02-22 12:34:32 -0800534 struct device *dev = regmap_get_device(chip->regmap);
Rhyland Klein94042872010-10-07 15:48:09 -0700535
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100536 if (chip->type == isl29035) {
Brian Masney82811012016-10-10 03:19:56 -0400537 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);
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100556 if (status < 0)
557 return status;
558 }
559
Brian Masney19a00a92016-10-10 03:19:57 -0400560 /*
561 * Code added per Intersil Application Note 1534:
Grant Grundler176f9f22011-08-09 15:18:14 -0700562 * 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 * ...
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200572 * For ISL29011, ISL29018, ISL29021, ISL29023
Grant Grundler176f9f22011-08-09 15:18:14 -0700573 * 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 */
Laxman Dewangan057340e2012-04-20 12:57:38 +0530581 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
Grant Grundler176f9f22011-08-09 15:18:14 -0700582 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800583 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
Roberta Dobrescuad3e6462014-09-25 20:09:09 +0300584 status);
Grant Grundler176f9f22011-08-09 15:18:14 -0700585 return status;
586 }
587
Brian Masney19a00a92016-10-10 03:19:57 -0400588 /*
589 * See Intersil AN1534 comments above.
Grant Grundler176f9f22011-08-09 15:18:14 -0700590 * "Operating Mode" (COMMAND1) register is reprogrammed when
591 * data is read from the device.
592 */
Laxman Dewangan057340e2012-04-20 12:57:38 +0530593 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
Grant Grundler176f9f22011-08-09 15:18:14 -0700594 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800595 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
Roberta Dobrescuad3e6462014-09-25 20:09:09 +0300596 status);
Grant Grundler176f9f22011-08-09 15:18:14 -0700597 return status;
598 }
599
Vaishali Thakkar890d2282014-09-23 09:22:30 +0530600 usleep_range(1000, 2000); /* per data sheet, page 10 */
Grant Grundler176f9f22011-08-09 15:18:14 -0700601
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200602 /* Set defaults */
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300603 status = isl29018_set_scale(chip, chip->scale.scale,
604 chip->scale.uscale);
Rhyland Klein94042872010-10-07 15:48:09 -0700605 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800606 dev_err(dev, "Init of isl29018 fails\n");
Rhyland Klein94042872010-10-07 15:48:09 -0700607 return status;
608 }
609
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300610 status = isl29018_set_integration_time(chip,
611 isl29018_int_utimes[chip->type][chip->int_time]);
Brian Masneyaba1a8d2016-10-10 03:19:58 -0400612 if (status < 0)
Alison Schofield64670862016-02-22 12:34:32 -0800613 dev_err(dev, "Init of isl29018 fails\n");
Rhyland Klein94042872010-10-07 15:48:09 -0700614
Brian Masneyaba1a8d2016-10-10 03:19:58 -0400615 return status;
Rhyland Klein94042872010-10-07 15:48:09 -0700616}
617
Laurentiu Palcu5b4b5b92014-08-29 15:26:00 +0100618static const struct iio_info isl29018_info = {
619 .attrs = &isl29018_group,
Bhumika Goyal6d9b10c2016-02-12 20:40:27 +0530620 .read_raw = isl29018_read_raw,
621 .write_raw = isl29018_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100622};
623
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100624static const struct iio_info isl29023_info = {
625 .attrs = &isl29023_group,
Bhumika Goyal6d9b10c2016-02-12 20:40:27 +0530626 .read_raw = isl29018_read_raw,
627 .write_raw = isl29018_write_raw,
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100628};
629
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200630static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
Laxman Dewangan057340e2012-04-20 12:57:38 +0530631{
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:
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100637 case ISL29035_REG_DEVICE_ID:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530638 return true;
639 default:
640 return false;
641 }
642}
643
Laxman Dewangan057340e2012-04-20 12:57:38 +0530644static const struct regmap_config isl29018_regmap_config = {
645 .reg_bits = 8,
646 .val_bits = 8,
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200647 .volatile_reg = isl29018_is_volatile_reg,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530648 .max_register = ISL29018_REG_TEST,
649 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
650 .cache_type = REGCACHE_RBTREE,
651};
652
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100653static const struct regmap_config isl29035_regmap_config = {
654 .reg_bits = 8,
655 .val_bits = 8,
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200656 .volatile_reg = isl29018_is_volatile_reg,
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100657 .max_register = ISL29035_REG_DEVICE_ID,
658 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
659 .cache_type = REGCACHE_RBTREE,
660};
661
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200662struct isl29018_chip_info {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100663 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
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200669static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100670 [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
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000690static 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
Eva Rachel Retuya79783b32016-02-18 15:35:37 +0800699 *data = (int)id->driver_data;
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000700
701 return dev_name(dev);
702}
703
Anson Huang1a02d122019-01-08 09:09:39 +0000704static 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
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500714static int isl29018_probe(struct i2c_client *client,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800715 const struct i2c_device_id *id)
Rhyland Klein94042872010-10-07 15:48:09 -0700716{
717 struct isl29018_chip *chip;
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100718 struct iio_dev *indio_dev;
Rhyland Klein94042872010-10-07 15:48:09 -0700719 int err;
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000720 const char *name = NULL;
721 int dev_id = 0;
Rhyland Klein94042872010-10-07 15:48:09 -0700722
Sachin Kamate434dcf2013-07-22 12:03:00 +0100723 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
Peter Meerwald-Stadlerab5b9492016-07-05 13:55:37 +0200724 if (!indio_dev)
Sachin Kamate434dcf2013-07-22 12:03:00 +0100725 return -ENOMEM;
Brian Masney744ef622016-10-10 03:19:55 -0400726
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100727 chip = iio_priv(indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700728
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100729 i2c_set_clientdata(client, indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700730
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000731 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
Rhyland Klein94042872010-10-07 15:48:09 -0700739 mutex_init(&chip->lock);
740
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000741 chip->type = dev_id;
Roberta Dobrescu809a5912015-04-16 22:20:58 +0300742 chip->calibscale = 1;
743 chip->ucalibscale = 0;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300744 chip->int_time = ISL29018_INT_TIME_16;
745 chip->scale = isl29018_scales[chip->int_time][0];
Bryan Freed1e45cf32012-10-25 00:39:00 +0100746 chip->suspended = false;
Rhyland Klein94042872010-10-07 15:48:09 -0700747
Anson Huang1a02d122019-01-08 09:09:39 +0000748 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
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100769 chip->regmap = devm_regmap_init_i2c(client,
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200770 isl29018_chip_info_tbl[dev_id].regmap_cfg);
Laxman Dewangan057340e2012-04-20 12:57:38 +0530771 if (IS_ERR(chip->regmap)) {
772 err = PTR_ERR(chip->regmap);
Alison Schofield64670862016-02-22 12:34:32 -0800773 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
Sachin Kamate434dcf2013-07-22 12:03:00 +0100774 return err;
Laxman Dewangan057340e2012-04-20 12:57:38 +0530775 }
776
777 err = isl29018_chip_init(chip);
Rhyland Klein94042872010-10-07 15:48:09 -0700778 if (err)
Sachin Kamate434dcf2013-07-22 12:03:00 +0100779 return err;
Rhyland Klein94042872010-10-07 15:48:09 -0700780
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200781 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;
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000784 indio_dev->name = name;
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100785 indio_dev->dev.parent = &client->dev;
786 indio_dev->modes = INDIO_DIRECT_MODE;
Brian Masney744ef622016-10-10 03:19:55 -0400787
Peter Meerwald-Stadlerab5b9492016-07-05 13:55:37 +0200788 return devm_iio_device_register(&client->dev, indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700789}
790
Bryan Freed1e45cf32012-10-25 00:39:00 +0100791#ifdef CONFIG_PM_SLEEP
792static int isl29018_suspend(struct device *dev)
793{
794 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
Anson Huang1a02d122019-01-08 09:09:39 +0000795 int ret;
Bryan Freed1e45cf32012-10-25 00:39:00 +0100796
797 mutex_lock(&chip->lock);
798
Brian Masney19a00a92016-10-10 03:19:57 -0400799 /*
800 * Since this driver uses only polling commands, we are by default in
Bryan Freed1e45cf32012-10-25 00:39:00 +0100801 * auto shutdown (ie, power-down) mode.
802 * So we do not have much to do here.
803 */
804 chip->suspended = true;
Anson Huang1a02d122019-01-08 09:09:39 +0000805 ret = regulator_disable(chip->vcc_reg);
806 if (ret)
807 dev_err(dev, "failed to disable VCC regulator\n");
Bryan Freed1e45cf32012-10-25 00:39:00 +0100808
809 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400810
Anson Huang1a02d122019-01-08 09:09:39 +0000811 return ret;
Bryan Freed1e45cf32012-10-25 00:39:00 +0100812}
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
Anson Huang1a02d122019-01-08 09:09:39 +0000821 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
Bryan Freed1e45cf32012-10-25 00:39:00 +0100828 err = isl29018_chip_init(chip);
829 if (!err)
830 chip->suspended = false;
831
832 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400833
Bryan Freed1e45cf32012-10-25 00:39:00 +0100834 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
Matthias Kaehlcke14b39dc2017-05-19 14:28:53 -0700843#ifdef CONFIG_ACPI
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000844static 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);
Matthias Kaehlcke14b39dc2017-05-19 14:28:53 -0700851#endif
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000852
Rhyland Klein94042872010-10-07 15:48:09 -0700853static const struct i2c_device_id isl29018_id[] = {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100854 {"isl29018", isl29018},
855 {"isl29023", isl29023},
856 {"isl29035", isl29035},
Rhyland Klein94042872010-10-07 15:48:09 -0700857 {}
858};
Rhyland Klein94042872010-10-07 15:48:09 -0700859MODULE_DEVICE_TABLE(i2c, isl29018_id);
860
Olof Johansson4ee19522011-12-22 18:44:43 -0800861static const struct of_device_id isl29018_of_match[] = {
Laxman Dewangan610202d2012-04-24 11:41:38 +0530862 { .compatible = "isil,isl29018", },
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100863 { .compatible = "isil,isl29023", },
864 { .compatible = "isil,isl29035", },
Olof Johansson4ee19522011-12-22 18:44:43 -0800865 { },
866};
867MODULE_DEVICE_TABLE(of, isl29018_of_match);
868
Rhyland Klein94042872010-10-07 15:48:09 -0700869static struct i2c_driver isl29018_driver = {
Rhyland Klein94042872010-10-07 15:48:09 -0700870 .driver = {
871 .name = "isl29018",
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000872 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
Bryan Freed1e45cf32012-10-25 00:39:00 +0100873 .pm = ISL29018_PM_OPS,
Olof Johansson4ee19522011-12-22 18:44:43 -0800874 .of_match_table = isl29018_of_match,
Rhyland Klein94042872010-10-07 15:48:09 -0700875 },
876 .probe = isl29018_probe,
Rhyland Klein94042872010-10-07 15:48:09 -0700877 .id_table = isl29018_id,
878};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100879module_i2c_driver(isl29018_driver);
Rhyland Klein94042872010-10-07 15:48:09 -0700880
881MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
882MODULE_LICENSE("GPL");