blob: 846df4dce48cbeef0acbcdcbb89088866884db2e [file] [log] [blame]
Rhyland Klein94042872010-10-07 15:48:09 -07001/*
Laurentiu Palcu609acef2014-08-29 15:26:00 +01002 * A iio driver for the light sensor ISL 29018/29023/29035.
Rhyland Klein94042872010-10-07 15:48:09 -07003 *
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.
Rhyland Klein94042872010-10-07 15:48:09 -070018 */
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>
Laxman Dewangan057340e2012-04-20 12:57:38 +053025#include <linux/regmap.h>
Anson Huang1a02d122019-01-08 09:09:39 +000026#include <linux/regulator/consumer.h>
Rhyland Klein94042872010-10-07 15:48:09 -070027#include <linux/slab.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010028#include <linux/iio/iio.h>
29#include <linux/iio/sysfs.h>
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +000030#include <linux/acpi.h>
Laxman Dewangan057340e2012-04-20 12:57:38 +053031
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020032#define ISL29018_CONV_TIME_MS 100
Rhyland Klein94042872010-10-07 15:48:09 -070033
34#define ISL29018_REG_ADD_COMMAND1 0x00
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020035#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
Rhyland Klein94042872010-10-07 15:48:09 -070041
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020042#define ISL29018_REG_ADD_COMMAND2 0x01
43#define ISL29018_CMD2_RESOLUTION_SHIFT 2
44#define ISL29018_CMD2_RESOLUTION_MASK (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
Rhyland Klein94042872010-10-07 15:48:09 -070045
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020046#define ISL29018_CMD2_RANGE_SHIFT 0
47#define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT)
Rhyland Klein94042872010-10-07 15:48:09 -070048
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +020049#define ISL29018_CMD2_SCHEME_SHIFT 7
50#define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
Rhyland Klein94042872010-10-07 15:48:09 -070051
52#define ISL29018_REG_ADD_DATA_LSB 0x02
53#define ISL29018_REG_ADD_DATA_MSB 0x03
Rhyland Klein94042872010-10-07 15:48:09 -070054
Grant Grundler176f9f22011-08-09 15:18:14 -070055#define ISL29018_REG_TEST 0x08
56#define ISL29018_TEST_SHIFT 0
57#define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT)
58
Laurentiu Palcu609acef2014-08-29 15:26:00 +010059#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
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +030066enum 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
73static 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
79static 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
Rhyland Klein94042872010-10-07 15:48:09 -070089struct isl29018_chip {
Laxman Dewangan057340e2012-04-20 12:57:38 +053090 struct regmap *regmap;
Rhyland Klein94042872010-10-07 15:48:09 -070091 struct mutex lock;
Laurentiu Palcu609acef2014-08-29 15:26:00 +010092 int type;
Roberta Dobrescu809a5912015-04-16 22:20:58 +030093 unsigned int calibscale;
94 unsigned int ucalibscale;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +030095 unsigned int int_time;
96 struct isl29018_scale scale;
Rhyland Klein94042872010-10-07 15:48:09 -070097 int prox_scheme;
Bryan Freed1e45cf32012-10-25 00:39:00 +010098 bool suspended;
Anson Huang1a02d122019-01-08 09:09:39 +000099 struct regulator *vcc_reg;
Rhyland Klein94042872010-10-07 15:48:09 -0700100};
101
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300102static int isl29018_set_integration_time(struct isl29018_chip *chip,
103 unsigned int utime)
Rhyland Klein94042872010-10-07 15:48:09 -0700104{
Brian Masneyea908ab2016-09-25 07:41:07 -0400105 unsigned int i;
106 int ret;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300107 unsigned int int_time, new_int_time;
Rhyland Klein94042872010-10-07 15:48:09 -0700108
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300109 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;
Rhyland Klein94042872010-10-07 15:48:09 -0700112 break;
113 }
114 }
115
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300116 if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
Rhyland Klein94042872010-10-07 15:48:09 -0700117 return -EINVAL;
118
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200119 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
120 ISL29018_CMD2_RESOLUTION_MASK,
121 i << ISL29018_CMD2_RESOLUTION_SHIFT);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300122 if (ret < 0)
123 return ret;
124
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200125 /* Keep the same range when integration time changes */
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300126 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;
Rhyland Klein94042872010-10-07 15:48:09 -0700137}
138
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300139static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
Rhyland Klein94042872010-10-07 15:48:09 -0700140{
Brian Masneyea908ab2016-09-25 07:41:07 -0400141 unsigned int i;
142 int ret;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300143 struct isl29018_scale new_scale;
Rhyland Klein94042872010-10-07 15:48:09 -0700144
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300145 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];
Rhyland Klein94042872010-10-07 15:48:09 -0700149 break;
150 }
151 }
152
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300153 if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
Rhyland Klein94042872010-10-07 15:48:09 -0700154 return -EINVAL;
155
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200156 ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
157 ISL29018_CMD2_RANGE_MASK,
158 i << ISL29018_CMD2_RANGE_SHIFT);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300159 if (ret < 0)
160 return ret;
161
162 chip->scale = new_scale;
163
164 return 0;
Rhyland Klein94042872010-10-07 15:48:09 -0700165}
166
Laxman Dewangan057340e2012-04-20 12:57:38 +0530167static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
Rhyland Klein94042872010-10-07 15:48:09 -0700168{
169 int status;
Laxman Dewangan057340e2012-04-20 12:57:38 +0530170 unsigned int lsb;
171 unsigned int msb;
Alison Schofield64670862016-02-22 12:34:32 -0800172 struct device *dev = regmap_get_device(chip->regmap);
Rhyland Klein94042872010-10-07 15:48:09 -0700173
174 /* Set mode */
Laxman Dewangan057340e2012-04-20 12:57:38 +0530175 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200176 mode << ISL29018_CMD1_OPMODE_SHIFT);
Rhyland Klein94042872010-10-07 15:48:09 -0700177 if (status) {
Alison Schofield64670862016-02-22 12:34:32 -0800178 dev_err(dev,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530179 "Error in setting operating mode err %d\n", status);
Rhyland Klein94042872010-10-07 15:48:09 -0700180 return status;
181 }
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200182 msleep(ISL29018_CONV_TIME_MS);
Laxman Dewangan057340e2012-04-20 12:57:38 +0530183 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
184 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800185 dev_err(dev,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530186 "Error in reading LSB DATA with err %d\n", status);
187 return status;
Rhyland Klein94042872010-10-07 15:48:09 -0700188 }
189
Laxman Dewangan057340e2012-04-20 12:57:38 +0530190 status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
191 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800192 dev_err(dev,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530193 "Error in reading MSB DATA with error %d\n", status);
194 return status;
Rhyland Klein94042872010-10-07 15:48:09 -0700195 }
Alison Schofield64670862016-02-22 12:34:32 -0800196 dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
Rhyland Klein94042872010-10-07 15:48:09 -0700197
198 return (msb << 8) | lsb;
199}
200
Laxman Dewangan057340e2012-04-20 12:57:38 +0530201static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
Rhyland Klein94042872010-10-07 15:48:09 -0700202{
203 int lux_data;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300204 unsigned int data_x_range;
Rhyland Klein94042872010-10-07 15:48:09 -0700205
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200206 lux_data = isl29018_read_sensor_input(chip,
207 ISL29018_CMD1_OPMODE_ALS_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700208 if (lux_data < 0)
209 return lux_data;
210
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300211 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;
Rhyland Klein94042872010-10-07 15:48:09 -0700215
216 return 0;
217}
218
Laxman Dewangan057340e2012-04-20 12:57:38 +0530219static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
Rhyland Klein94042872010-10-07 15:48:09 -0700220{
221 int ir_data;
222
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200223 ir_data = isl29018_read_sensor_input(chip,
224 ISL29018_CMD1_OPMODE_IR_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700225 if (ir_data < 0)
226 return ir_data;
227
228 *ir = ir_data;
229
230 return 0;
231}
232
Laxman Dewangan057340e2012-04-20 12:57:38 +0530233static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800234 int *near_ir)
Rhyland Klein94042872010-10-07 15:48:09 -0700235{
236 int status;
237 int prox_data = -1;
238 int ir_data = -1;
Alison Schofield64670862016-02-22 12:34:32 -0800239 struct device *dev = regmap_get_device(chip->regmap);
Rhyland Klein94042872010-10-07 15:48:09 -0700240
241 /* Do proximity sensing with required scheme */
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200242 status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
243 ISL29018_CMD2_SCHEME_MASK,
244 scheme << ISL29018_CMD2_SCHEME_SHIFT);
Rhyland Klein94042872010-10-07 15:48:09 -0700245 if (status) {
Alison Schofield64670862016-02-22 12:34:32 -0800246 dev_err(dev, "Error in setting operating mode\n");
Rhyland Klein94042872010-10-07 15:48:09 -0700247 return status;
248 }
249
Laxman Dewangan057340e2012-04-20 12:57:38 +0530250 prox_data = isl29018_read_sensor_input(chip,
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200251 ISL29018_CMD1_OPMODE_PROX_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700252 if (prox_data < 0)
253 return prox_data;
254
255 if (scheme == 1) {
256 *near_ir = prox_data;
257 return 0;
258 }
259
Peter Meerwald-Stadler13e6d632016-07-05 13:55:36 +0200260 ir_data = isl29018_read_sensor_input(chip,
261 ISL29018_CMD1_OPMODE_IR_ONCE);
Rhyland Klein94042872010-10-07 15:48:09 -0700262 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
Brian Masney02819962016-09-26 20:20:17 -0400273static ssize_t in_illuminance_scale_available_show
274 (struct device *dev, struct device_attribute *attr,
275 char *buf)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300276{
277 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
278 struct isl29018_chip *chip = iio_priv(indio_dev);
Brian Masneyea908ab2016-09-25 07:41:07 -0400279 unsigned int i;
280 int len = 0;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300281
Brian Masney5faf98c2016-09-26 20:20:18 -0400282 mutex_lock(&chip->lock);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300283 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);
Brian Masney5faf98c2016-09-26 20:20:18 -0400287 mutex_unlock(&chip->lock);
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300288
289 buf[len - 1] = '\n';
290
291 return len;
292}
293
Brian Masney02819962016-09-26 20:20:17 -0400294static ssize_t in_illuminance_integration_time_available_show
295 (struct device *dev, struct device_attribute *attr,
296 char *buf)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300297{
298 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
299 struct isl29018_chip *chip = iio_priv(indio_dev);
Brian Masneyea908ab2016-09-25 07:41:07 -0400300 unsigned int i;
301 int len = 0;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300302
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
Brian Masneye748e282016-10-06 20:48:34 -0400312/*
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 */
Brian Masney02819962016-09-26 20:20:17 -0400330static ssize_t proximity_on_chip_ambient_infrared_suppression_show
331 (struct device *dev, struct device_attribute *attr,
332 char *buf)
Rhyland Klein94042872010-10-07 15:48:09 -0700333{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200334 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100335 struct isl29018_chip *chip = iio_priv(indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700336
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800337 /*
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200338 * Return the "proximity scheme" i.e. if the chip does on chip
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800339 * infrared suppression (1 means perform on chip suppression)
340 */
Rhyland Klein94042872010-10-07 15:48:09 -0700341 return sprintf(buf, "%d\n", chip->prox_scheme);
342}
343
Brian Masney02819962016-09-26 20:20:17 -0400344static ssize_t proximity_on_chip_ambient_infrared_suppression_store
345 (struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count)
Rhyland Klein94042872010-10-07 15:48:09 -0700347{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200348 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100349 struct isl29018_chip *chip = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100350 int val;
Rhyland Klein94042872010-10-07 15:48:09 -0700351
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100352 if (kstrtoint(buf, 10, &val))
Rhyland Klein94042872010-10-07 15:48:09 -0700353 return -EINVAL;
Peter Meerwald-Stadlerab5b9492016-07-05 13:55:37 +0200354 if (!(val == 0 || val == 1))
Rhyland Klein94042872010-10-07 15:48:09 -0700355 return -EINVAL;
Rhyland Klein94042872010-10-07 15:48:09 -0700356
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800357 /*
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200358 * Get the "proximity scheme" i.e. if the chip does on chip
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800359 * infrared suppression (1 means perform on chip suppression)
360 */
Rhyland Klein94042872010-10-07 15:48:09 -0700361 mutex_lock(&chip->lock);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100362 chip->prox_scheme = val;
Rhyland Klein94042872010-10-07 15:48:09 -0700363 mutex_unlock(&chip->lock);
364
365 return count;
366}
367
Bryan Freed01e57c52011-07-07 12:01:56 -0700368static 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)
Rhyland Klein94042872010-10-07 15:48:09 -0700373{
Bryan Freed01e57c52011-07-07 12:01:56 -0700374 struct isl29018_chip *chip = iio_priv(indio_dev);
375 int ret = -EINVAL;
376
377 mutex_lock(&chip->lock);
Brian Masney00053562016-09-26 20:20:20 -0400378 if (chip->suspended) {
379 ret = -EBUSY;
380 goto write_done;
381 }
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300382 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:
Brian Masney528021f2016-09-25 07:41:06 -0400391 if (chan->type == IIO_LIGHT && !val)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300392 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;
Bryan Freed01e57c52011-07-07 12:01:56 -0700400 }
Bryan Freed01e57c52011-07-07 12:01:56 -0700401
Brian Masney00053562016-09-26 20:20:20 -0400402write_done:
403 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400404
Wei Yongjun6dc973d2012-11-08 09:05:00 +0000405 return ret;
Rhyland Klein94042872010-10-07 15:48:09 -0700406}
407
Bryan Freed01e57c52011-07-07 12:01:56 -0700408static 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)
Rhyland Klein94042872010-10-07 15:48:09 -0700413{
Bryan Freed01e57c52011-07-07 12:01:56 -0700414 int ret = -EINVAL;
415 struct isl29018_chip *chip = iio_priv(indio_dev);
Bryan Freed01e57c52011-07-07 12:01:56 -0700416
417 mutex_lock(&chip->lock);
Bryan Freed1e45cf32012-10-25 00:39:00 +0100418 if (chip->suspended) {
Brian Masney5611cd62016-09-26 20:20:19 -0400419 ret = -EBUSY;
420 goto read_done;
Bryan Freed1e45cf32012-10-25 00:39:00 +0100421 }
Bryan Freed01e57c52011-07-07 12:01:56 -0700422 switch (mask) {
Jonathan Cameron90354d02012-04-15 17:41:22 +0100423 case IIO_CHAN_INFO_RAW:
424 case IIO_CHAN_INFO_PROCESSED:
Bryan Freed01e57c52011-07-07 12:01:56 -0700425 switch (chan->type) {
426 case IIO_LIGHT:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530427 ret = isl29018_read_lux(chip, val);
Bryan Freed01e57c52011-07-07 12:01:56 -0700428 break;
429 case IIO_INTENSITY:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530430 ret = isl29018_read_ir(chip, val);
Bryan Freed01e57c52011-07-07 12:01:56 -0700431 break;
432 case IIO_PROXIMITY:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530433 ret = isl29018_read_proximity_ir(chip,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800434 chip->prox_scheme,
435 val);
Bryan Freed01e57c52011-07-07 12:01:56 -0700436 break;
437 default:
438 break;
439 }
440 if (!ret)
441 ret = IIO_VAL_INT;
442 break;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300443 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;
Jonathan Cameronc8a9f802011-10-26 17:41:36 +0100457 case IIO_CHAN_INFO_CALIBSCALE:
Bryan Freed01e57c52011-07-07 12:01:56 -0700458 if (chan->type == IIO_LIGHT) {
Roberta Dobrescu809a5912015-04-16 22:20:58 +0300459 *val = chip->calibscale;
460 *val2 = chip->ucalibscale;
Bryan Freed932323b2012-09-05 20:55:00 +0100461 ret = IIO_VAL_INT_PLUS_MICRO;
Bryan Freed01e57c52011-07-07 12:01:56 -0700462 }
463 break;
464 default:
465 break;
466 }
Brian Masney5611cd62016-09-26 20:20:19 -0400467
468read_done:
Bryan Freed01e57c52011-07-07 12:01:56 -0700469 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400470
Bryan Freed01e57c52011-07-07 12:01:56 -0700471 return ret;
Rhyland Klein94042872010-10-07 15:48:09 -0700472}
473
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100474#define ISL29018_LIGHT_CHANNEL { \
475 .type = IIO_LIGHT, \
476 .indexed = 1, \
477 .channel = 0, \
478 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300479 BIT(IIO_CHAN_INFO_CALIBSCALE) | \
480 BIT(IIO_CHAN_INFO_SCALE) | \
481 BIT(IIO_CHAN_INFO_INT_TIME), \
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100482}
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
Bryan Freed01e57c52011-07-07 12:01:56 -0700496static const struct iio_chan_spec isl29018_channels[] = {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100497 ISL29018_LIGHT_CHANNEL,
498 ISL29018_IR_CHANNEL,
499 ISL29018_PROXIMITY_CHANNEL,
500};
501
502static const struct iio_chan_spec isl29023_channels[] = {
503 ISL29018_LIGHT_CHANNEL,
504 ISL29018_IR_CHANNEL,
Bryan Freed01e57c52011-07-07 12:01:56 -0700505};
Rhyland Klein94042872010-10-07 15:48:09 -0700506
Brian Masney02819962016-09-26 20:20:17 -0400507static IIO_DEVICE_ATTR_RO(in_illuminance_integration_time_available, 0);
508static IIO_DEVICE_ATTR_RO(in_illuminance_scale_available, 0);
509static IIO_DEVICE_ATTR_RW(proximity_on_chip_ambient_infrared_suppression, 0);
Rhyland Klein94042872010-10-07 15:48:09 -0700510
511#define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300512
Rhyland Klein94042872010-10-07 15:48:09 -0700513static struct attribute *isl29018_attributes[] = {
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300514 ISL29018_DEV_ATTR(in_illuminance_scale_available),
515 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
Peter Meerwald2a1d45e2012-06-18 20:33:07 +0200516 ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
Rhyland Klein94042872010-10-07 15:48:09 -0700517 NULL
518};
519
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100520static struct attribute *isl29023_attributes[] = {
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300521 ISL29018_DEV_ATTR(in_illuminance_scale_available),
522 ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100523 NULL
524};
525
Laurentiu Palcu5b4b5b92014-08-29 15:26:00 +0100526static const struct attribute_group isl29018_group = {
Rhyland Klein94042872010-10-07 15:48:09 -0700527 .attrs = isl29018_attributes,
528};
529
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100530static const struct attribute_group isl29023_group = {
531 .attrs = isl29023_attributes,
532};
533
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100534enum {
535 isl29018,
536 isl29023,
537 isl29035,
538};
539
Laxman Dewangan057340e2012-04-20 12:57:38 +0530540static int isl29018_chip_init(struct isl29018_chip *chip)
Rhyland Klein94042872010-10-07 15:48:09 -0700541{
Rhyland Klein94042872010-10-07 15:48:09 -0700542 int status;
Alison Schofield64670862016-02-22 12:34:32 -0800543 struct device *dev = regmap_get_device(chip->regmap);
Rhyland Klein94042872010-10-07 15:48:09 -0700544
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100545 if (chip->type == isl29035) {
Brian Masney82811012016-10-10 03:19:56 -0400546 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);
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100565 if (status < 0)
566 return status;
567 }
568
Brian Masney19a00a92016-10-10 03:19:57 -0400569 /*
570 * Code added per Intersil Application Note 1534:
Grant Grundler176f9f22011-08-09 15:18:14 -0700571 * 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 * ...
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200581 * For ISL29011, ISL29018, ISL29021, ISL29023
Grant Grundler176f9f22011-08-09 15:18:14 -0700582 * 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 */
Laxman Dewangan057340e2012-04-20 12:57:38 +0530590 status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
Grant Grundler176f9f22011-08-09 15:18:14 -0700591 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800592 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
Roberta Dobrescuad3e6462014-09-25 20:09:09 +0300593 status);
Grant Grundler176f9f22011-08-09 15:18:14 -0700594 return status;
595 }
596
Brian Masney19a00a92016-10-10 03:19:57 -0400597 /*
598 * See Intersil AN1534 comments above.
Grant Grundler176f9f22011-08-09 15:18:14 -0700599 * "Operating Mode" (COMMAND1) register is reprogrammed when
600 * data is read from the device.
601 */
Laxman Dewangan057340e2012-04-20 12:57:38 +0530602 status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
Grant Grundler176f9f22011-08-09 15:18:14 -0700603 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800604 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
Roberta Dobrescuad3e6462014-09-25 20:09:09 +0300605 status);
Grant Grundler176f9f22011-08-09 15:18:14 -0700606 return status;
607 }
608
Vaishali Thakkar890d2282014-09-23 09:22:30 +0530609 usleep_range(1000, 2000); /* per data sheet, page 10 */
Grant Grundler176f9f22011-08-09 15:18:14 -0700610
Peter Meerwald-Stadler96273f432016-07-05 13:55:39 +0200611 /* Set defaults */
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300612 status = isl29018_set_scale(chip, chip->scale.scale,
613 chip->scale.uscale);
Rhyland Klein94042872010-10-07 15:48:09 -0700614 if (status < 0) {
Alison Schofield64670862016-02-22 12:34:32 -0800615 dev_err(dev, "Init of isl29018 fails\n");
Rhyland Klein94042872010-10-07 15:48:09 -0700616 return status;
617 }
618
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300619 status = isl29018_set_integration_time(chip,
620 isl29018_int_utimes[chip->type][chip->int_time]);
Brian Masneyaba1a8d2016-10-10 03:19:58 -0400621 if (status < 0)
Alison Schofield64670862016-02-22 12:34:32 -0800622 dev_err(dev, "Init of isl29018 fails\n");
Rhyland Klein94042872010-10-07 15:48:09 -0700623
Brian Masneyaba1a8d2016-10-10 03:19:58 -0400624 return status;
Rhyland Klein94042872010-10-07 15:48:09 -0700625}
626
Laurentiu Palcu5b4b5b92014-08-29 15:26:00 +0100627static const struct iio_info isl29018_info = {
628 .attrs = &isl29018_group,
Bhumika Goyal6d9b10c2016-02-12 20:40:27 +0530629 .read_raw = isl29018_read_raw,
630 .write_raw = isl29018_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100631};
632
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100633static const struct iio_info isl29023_info = {
634 .attrs = &isl29023_group,
Bhumika Goyal6d9b10c2016-02-12 20:40:27 +0530635 .read_raw = isl29018_read_raw,
636 .write_raw = isl29018_write_raw,
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100637};
638
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200639static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
Laxman Dewangan057340e2012-04-20 12:57:38 +0530640{
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:
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100646 case ISL29035_REG_DEVICE_ID:
Laxman Dewangan057340e2012-04-20 12:57:38 +0530647 return true;
648 default:
649 return false;
650 }
651}
652
Laxman Dewangan057340e2012-04-20 12:57:38 +0530653static const struct regmap_config isl29018_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,
Laxman Dewangan057340e2012-04-20 12:57:38 +0530657 .max_register = ISL29018_REG_TEST,
658 .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
659 .cache_type = REGCACHE_RBTREE,
660};
661
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100662static const struct regmap_config isl29035_regmap_config = {
663 .reg_bits = 8,
664 .val_bits = 8,
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200665 .volatile_reg = isl29018_is_volatile_reg,
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100666 .max_register = ISL29035_REG_DEVICE_ID,
667 .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
668 .cache_type = REGCACHE_RBTREE,
669};
670
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200671struct isl29018_chip_info {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100672 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
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200678static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100679 [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
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000699static 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
Eva Rachel Retuya79783b32016-02-18 15:35:37 +0800708 *data = (int)id->driver_data;
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000709
710 return dev_name(dev);
711}
712
Anson Huang1a02d122019-01-08 09:09:39 +0000713static 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
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500723static int isl29018_probe(struct i2c_client *client,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800724 const struct i2c_device_id *id)
Rhyland Klein94042872010-10-07 15:48:09 -0700725{
726 struct isl29018_chip *chip;
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100727 struct iio_dev *indio_dev;
Rhyland Klein94042872010-10-07 15:48:09 -0700728 int err;
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000729 const char *name = NULL;
730 int dev_id = 0;
Rhyland Klein94042872010-10-07 15:48:09 -0700731
Sachin Kamate434dcf2013-07-22 12:03:00 +0100732 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
Peter Meerwald-Stadlerab5b9492016-07-05 13:55:37 +0200733 if (!indio_dev)
Sachin Kamate434dcf2013-07-22 12:03:00 +0100734 return -ENOMEM;
Brian Masney744ef622016-10-10 03:19:55 -0400735
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100736 chip = iio_priv(indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700737
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100738 i2c_set_clientdata(client, indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700739
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000740 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
Rhyland Klein94042872010-10-07 15:48:09 -0700748 mutex_init(&chip->lock);
749
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000750 chip->type = dev_id;
Roberta Dobrescu809a5912015-04-16 22:20:58 +0300751 chip->calibscale = 1;
752 chip->ucalibscale = 0;
Roberta Dobrescu6920ccf2015-04-16 22:20:59 +0300753 chip->int_time = ISL29018_INT_TIME_16;
754 chip->scale = isl29018_scales[chip->int_time][0];
Bryan Freed1e45cf32012-10-25 00:39:00 +0100755 chip->suspended = false;
Rhyland Klein94042872010-10-07 15:48:09 -0700756
Anson Huang1a02d122019-01-08 09:09:39 +0000757 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
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100778 chip->regmap = devm_regmap_init_i2c(client,
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200779 isl29018_chip_info_tbl[dev_id].regmap_cfg);
Laxman Dewangan057340e2012-04-20 12:57:38 +0530780 if (IS_ERR(chip->regmap)) {
781 err = PTR_ERR(chip->regmap);
Alison Schofield64670862016-02-22 12:34:32 -0800782 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
Sachin Kamate434dcf2013-07-22 12:03:00 +0100783 return err;
Laxman Dewangan057340e2012-04-20 12:57:38 +0530784 }
785
786 err = isl29018_chip_init(chip);
Rhyland Klein94042872010-10-07 15:48:09 -0700787 if (err)
Sachin Kamate434dcf2013-07-22 12:03:00 +0100788 return err;
Rhyland Klein94042872010-10-07 15:48:09 -0700789
Peter Meerwald-Stadler92193762016-07-05 13:55:40 +0200790 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;
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000793 indio_dev->name = name;
Jonathan Cameron927afbe2011-06-27 13:07:57 +0100794 indio_dev->dev.parent = &client->dev;
795 indio_dev->modes = INDIO_DIRECT_MODE;
Brian Masney744ef622016-10-10 03:19:55 -0400796
Peter Meerwald-Stadlerab5b9492016-07-05 13:55:37 +0200797 return devm_iio_device_register(&client->dev, indio_dev);
Rhyland Klein94042872010-10-07 15:48:09 -0700798}
799
Bryan Freed1e45cf32012-10-25 00:39:00 +0100800#ifdef CONFIG_PM_SLEEP
801static int isl29018_suspend(struct device *dev)
802{
803 struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
Anson Huang1a02d122019-01-08 09:09:39 +0000804 int ret;
Bryan Freed1e45cf32012-10-25 00:39:00 +0100805
806 mutex_lock(&chip->lock);
807
Brian Masney19a00a92016-10-10 03:19:57 -0400808 /*
809 * Since this driver uses only polling commands, we are by default in
Bryan Freed1e45cf32012-10-25 00:39:00 +0100810 * auto shutdown (ie, power-down) mode.
811 * So we do not have much to do here.
812 */
813 chip->suspended = true;
Anson Huang1a02d122019-01-08 09:09:39 +0000814 ret = regulator_disable(chip->vcc_reg);
815 if (ret)
816 dev_err(dev, "failed to disable VCC regulator\n");
Bryan Freed1e45cf32012-10-25 00:39:00 +0100817
818 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400819
Anson Huang1a02d122019-01-08 09:09:39 +0000820 return ret;
Bryan Freed1e45cf32012-10-25 00:39:00 +0100821}
822
823static 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
Anson Huang1a02d122019-01-08 09:09:39 +0000830 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
Bryan Freed1e45cf32012-10-25 00:39:00 +0100837 err = isl29018_chip_init(chip);
838 if (!err)
839 chip->suspended = false;
840
841 mutex_unlock(&chip->lock);
Brian Masney744ef622016-10-10 03:19:55 -0400842
Bryan Freed1e45cf32012-10-25 00:39:00 +0100843 return err;
844}
845
846static 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
Matthias Kaehlcke14b39dc2017-05-19 14:28:53 -0700852#ifdef CONFIG_ACPI
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000853static const struct acpi_device_id isl29018_acpi_match[] = {
854 {"ISL29018", isl29018},
855 {"ISL29023", isl29023},
856 {"ISL29035", isl29035},
857 {},
858};
859MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
Matthias Kaehlcke14b39dc2017-05-19 14:28:53 -0700860#endif
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000861
Rhyland Klein94042872010-10-07 15:48:09 -0700862static const struct i2c_device_id isl29018_id[] = {
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100863 {"isl29018", isl29018},
864 {"isl29023", isl29023},
865 {"isl29035", isl29035},
Rhyland Klein94042872010-10-07 15:48:09 -0700866 {}
867};
Rhyland Klein94042872010-10-07 15:48:09 -0700868MODULE_DEVICE_TABLE(i2c, isl29018_id);
869
Olof Johansson4ee19522011-12-22 18:44:43 -0800870static const struct of_device_id isl29018_of_match[] = {
Laxman Dewangan610202d2012-04-24 11:41:38 +0530871 { .compatible = "isil,isl29018", },
Laurentiu Palcu609acef2014-08-29 15:26:00 +0100872 { .compatible = "isil,isl29023", },
873 { .compatible = "isil,isl29035", },
Olof Johansson4ee19522011-12-22 18:44:43 -0800874 { },
875};
876MODULE_DEVICE_TABLE(of, isl29018_of_match);
877
Rhyland Klein94042872010-10-07 15:48:09 -0700878static struct i2c_driver isl29018_driver = {
Rhyland Klein94042872010-10-07 15:48:09 -0700879 .driver = {
880 .name = "isl29018",
Laurentiu Palcudc4ecaf2014-01-09 10:20:00 +0000881 .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
Bryan Freed1e45cf32012-10-25 00:39:00 +0100882 .pm = ISL29018_PM_OPS,
Olof Johansson4ee19522011-12-22 18:44:43 -0800883 .of_match_table = isl29018_of_match,
Rhyland Klein94042872010-10-07 15:48:09 -0700884 },
885 .probe = isl29018_probe,
Rhyland Klein94042872010-10-07 15:48:09 -0700886 .id_table = isl29018_id,
887};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100888module_i2c_driver(isl29018_driver);
Rhyland Klein94042872010-10-07 15:48:09 -0700889
890MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
891MODULE_LICENSE("GPL");