blob: 4d220c835c7589b9bedbf0442704d9b712d9bf09 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Laxman Dewangan6d59ba22012-04-13 16:03:32 +05302/*
3 * IIO driver for the light sensor ISL29028.
4 * ISL29028 is Concurrent Ambient Light and Proximity Sensor
5 *
6 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Brian Masneybcbbd0f2017-02-08 20:54:29 -05007 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
Laxman Dewangan6d59ba22012-04-13 16:03:32 +05308 *
Sebastian Reichele530cc82017-04-28 17:55:58 +02009 * Datasheets:
10 * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29028.pdf
11 * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29030.pdf
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053012 */
13
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/err.h>
17#include <linux/mutex.h>
18#include <linux/delay.h>
19#include <linux/slab.h>
20#include <linux/regmap.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
Brian Masney2db50542017-01-17 04:25:02 -050023#include <linux/pm_runtime.h>
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053024
Brian Masney29e90cd2016-12-03 21:19:29 -050025#define ISL29028_CONV_TIME_MS 100
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053026
Brian Masney29e90cd2016-12-03 21:19:29 -050027#define ISL29028_REG_CONFIGURE 0x01
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053028
Brian Masney29e90cd2016-12-03 21:19:29 -050029#define ISL29028_CONF_ALS_IR_MODE_ALS 0
30#define ISL29028_CONF_ALS_IR_MODE_IR BIT(0)
31#define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053032
Brian Masney29e90cd2016-12-03 21:19:29 -050033#define ISL29028_CONF_ALS_RANGE_LOW_LUX 0
Peter Meerwald-Stadler3a37f1c2016-07-05 13:55:41 +020034#define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1)
Brian Masney29e90cd2016-12-03 21:19:29 -050035#define ISL29028_CONF_ALS_RANGE_MASK BIT(1)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053036
Brian Masney29e90cd2016-12-03 21:19:29 -050037#define ISL29028_CONF_ALS_DIS 0
38#define ISL29028_CONF_ALS_EN BIT(2)
39#define ISL29028_CONF_ALS_EN_MASK BIT(2)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053040
Brian Masney29e90cd2016-12-03 21:19:29 -050041#define ISL29028_CONF_PROX_SLP_SH 4
42#define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053043
Brian Masney29e90cd2016-12-03 21:19:29 -050044#define ISL29028_CONF_PROX_EN BIT(7)
45#define ISL29028_CONF_PROX_EN_MASK BIT(7)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053046
Brian Masney29e90cd2016-12-03 21:19:29 -050047#define ISL29028_REG_INTERRUPT 0x02
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053048
Brian Masney29e90cd2016-12-03 21:19:29 -050049#define ISL29028_REG_PROX_DATA 0x08
50#define ISL29028_REG_ALSIR_L 0x09
51#define ISL29028_REG_ALSIR_U 0x0A
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053052
Brian Masney29e90cd2016-12-03 21:19:29 -050053#define ISL29028_REG_TEST1_MODE 0x0E
54#define ISL29028_REG_TEST2_MODE 0x0F
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053055
Brian Masney29e90cd2016-12-03 21:19:29 -050056#define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053057
Brian Masney2db50542017-01-17 04:25:02 -050058#define ISL29028_POWER_OFF_DELAY_MS 2000
59
Brian Masneye4ff6c12017-04-24 21:34:33 -040060struct isl29028_prox_data {
61 int sampling_int;
62 int sampling_fract;
63 int sleep_time;
64};
65
66static const struct isl29028_prox_data isl29028_prox_data[] = {
67 { 1, 250000, 800 },
68 { 2, 500000, 400 },
69 { 5, 0, 200 },
70 { 10, 0, 100 },
71 { 13, 300000, 75 },
72 { 20, 0, 50 },
73 { 80, 0, 13 }, /*
74 * Note: Data sheet lists 12.5 ms sleep time.
75 * Round up a half millisecond for msleep().
76 */
77 { 100, 0, 0 }
78};
Brian Masney45e28522017-02-08 20:54:26 -050079
Peter Meerwald-Stadlerbafaa6d2016-07-05 13:55:42 +020080enum isl29028_als_ir_mode {
81 ISL29028_MODE_NONE = 0,
82 ISL29028_MODE_ALS,
83 ISL29028_MODE_IR,
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053084};
85
86struct isl29028_chip {
Brian Masney6db2fa02017-01-17 04:24:48 -050087 struct mutex lock;
88 struct regmap *regmap;
Brian Masneye4ff6c12017-04-24 21:34:33 -040089 int prox_sampling_int;
90 int prox_sampling_frac;
Brian Masney6db2fa02017-01-17 04:24:48 -050091 bool enable_prox;
Brian Masney6db2fa02017-01-17 04:24:48 -050092 int lux_scale;
Peter Meerwald-Stadlerbafaa6d2016-07-05 13:55:42 +020093 enum isl29028_als_ir_mode als_ir_mode;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +053094};
95
Brian Masneye4ff6c12017-04-24 21:34:33 -040096static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract)
Brian Masney45e28522017-02-08 20:54:26 -050097{
Brian Masney45e28522017-02-08 20:54:26 -050098 int i;
99
Brian Masneye4ff6c12017-04-24 21:34:33 -0400100 for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) {
101 if (isl29028_prox_data[i].sampling_int == sampling_int &&
102 isl29028_prox_data[i].sampling_fract == sampling_fract)
103 return i;
Brian Masney45e28522017-02-08 20:54:26 -0500104 }
105
Brian Masneye4ff6c12017-04-24 21:34:33 -0400106 return -EINVAL;
Brian Masney45e28522017-02-08 20:54:26 -0500107}
108
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530109static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
Brian Masneye4ff6c12017-04-24 21:34:33 -0400110 int sampling_int, int sampling_fract)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530111{
Brian Masneyae1e5232017-01-17 04:24:53 -0500112 struct device *dev = regmap_get_device(chip->regmap);
Brian Masney45e28522017-02-08 20:54:26 -0500113 int sleep_index, ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530114
Brian Masneye4ff6c12017-04-24 21:34:33 -0400115 sleep_index = isl29028_find_prox_sleep_index(sampling_int,
116 sampling_fract);
117 if (sleep_index < 0)
118 return sleep_index;
119
Brian Masneyae1e5232017-01-17 04:24:53 -0500120 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
121 ISL29028_CONF_PROX_SLP_MASK,
Brian Masney45e28522017-02-08 20:54:26 -0500122 sleep_index << ISL29028_CONF_PROX_SLP_SH);
Brian Masneyae1e5232017-01-17 04:24:53 -0500123
124 if (ret < 0) {
125 dev_err(dev, "%s(): Error %d setting the proximity sampling\n",
126 __func__, ret);
127 return ret;
128 }
129
Brian Masneye4ff6c12017-04-24 21:34:33 -0400130 chip->prox_sampling_int = sampling_int;
131 chip->prox_sampling_frac = sampling_fract;
Brian Masneyae1e5232017-01-17 04:24:53 -0500132
133 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530134}
135
Brian Masney67663422017-01-17 04:24:59 -0500136static int isl29028_enable_proximity(struct isl29028_chip *chip)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530137{
Brian Masneye4ff6c12017-04-24 21:34:33 -0400138 int prox_index, ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530139
Brian Masneye4ff6c12017-04-24 21:34:33 -0400140 ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int,
141 chip->prox_sampling_frac);
Brian Masney84a76692017-01-17 04:25:00 -0500142 if (ret < 0)
143 return ret;
144
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530145 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
Brian Masney67663422017-01-17 04:24:59 -0500146 ISL29028_CONF_PROX_EN_MASK,
147 ISL29028_CONF_PROX_EN);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530148 if (ret < 0)
149 return ret;
150
151 /* Wait for conversion to be complete for first sample */
Brian Masneye4ff6c12017-04-24 21:34:33 -0400152 prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int,
153 chip->prox_sampling_frac);
154 if (prox_index < 0)
155 return prox_index;
156
157 msleep(isl29028_prox_data[prox_index].sleep_time);
Brian Masneyb1dda962017-01-17 04:24:51 -0500158
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530159 return 0;
160}
161
162static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
163{
Brian Masneybcf51042017-01-17 04:24:54 -0500164 struct device *dev = regmap_get_device(chip->regmap);
Peter Meerwald-Stadler3a37f1c2016-07-05 13:55:41 +0200165 int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
166 ISL29028_CONF_ALS_RANGE_LOW_LUX;
Brian Masneybcf51042017-01-17 04:24:54 -0500167 int ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530168
Brian Masneybcf51042017-01-17 04:24:54 -0500169 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
170 ISL29028_CONF_ALS_RANGE_MASK, val);
171 if (ret < 0) {
172 dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__,
173 ret);
174 return ret;
175 }
176
177 chip->lux_scale = lux_scale;
178
179 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530180}
181
182static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
Peter Meerwald-Stadlerbafaa6d2016-07-05 13:55:42 +0200183 enum isl29028_als_ir_mode mode)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530184{
Brian Masney0fac96ed2017-01-17 04:25:01 -0500185 int ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530186
Brian Masneyf40531d2016-12-03 21:19:25 -0500187 if (chip->als_ir_mode == mode)
188 return 0;
189
Brian Masney0fac96ed2017-01-17 04:25:01 -0500190 ret = isl29028_set_als_scale(chip, chip->lux_scale);
191 if (ret < 0)
192 return ret;
193
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530194 switch (mode) {
Peter Meerwald-Stadlerbafaa6d2016-07-05 13:55:42 +0200195 case ISL29028_MODE_ALS:
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530196 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
Peter Meerwald-Stadler3a37f1c2016-07-05 13:55:41 +0200197 ISL29028_CONF_ALS_IR_MODE_MASK,
198 ISL29028_CONF_ALS_IR_MODE_ALS);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530199 if (ret < 0)
200 return ret;
201
202 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
Peter Meerwald-Stadler3a37f1c2016-07-05 13:55:41 +0200203 ISL29028_CONF_ALS_RANGE_MASK,
204 ISL29028_CONF_ALS_RANGE_HIGH_LUX);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530205 break;
Peter Meerwald-Stadlerbafaa6d2016-07-05 13:55:42 +0200206 case ISL29028_MODE_IR:
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530207 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
Peter Meerwald-Stadler3a37f1c2016-07-05 13:55:41 +0200208 ISL29028_CONF_ALS_IR_MODE_MASK,
209 ISL29028_CONF_ALS_IR_MODE_IR);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530210 break;
Peter Meerwald-Stadlerbafaa6d2016-07-05 13:55:42 +0200211 case ISL29028_MODE_NONE:
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530212 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
Brian Masneyc4f60922017-01-17 04:24:49 -0500213 ISL29028_CONF_ALS_EN_MASK,
214 ISL29028_CONF_ALS_DIS);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530215 }
216
217 if (ret < 0)
218 return ret;
219
220 /* Enable the ALS/IR */
221 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
Peter Meerwald-Stadler3a37f1c2016-07-05 13:55:41 +0200222 ISL29028_CONF_ALS_EN_MASK,
223 ISL29028_CONF_ALS_EN);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530224 if (ret < 0)
225 return ret;
226
227 /* Need to wait for conversion time if ALS/IR mode enabled */
Brian Masney55bf8512017-02-08 20:54:25 -0500228 msleep(ISL29028_CONV_TIME_MS);
Brian Masneyf40531d2016-12-03 21:19:25 -0500229
230 chip->als_ir_mode = mode;
231
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530232 return 0;
233}
234
235static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
236{
Alison Schofield722fc312016-03-15 11:49:12 -0700237 struct device *dev = regmap_get_device(chip->regmap);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530238 unsigned int lsb;
239 unsigned int msb;
240 int ret;
241
242 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
243 if (ret < 0) {
Alison Schofield722fc312016-03-15 11:49:12 -0700244 dev_err(dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500245 "%s(): Error %d reading register ALSIR_L\n",
246 __func__, ret);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530247 return ret;
248 }
249
250 ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
251 if (ret < 0) {
Alison Schofield722fc312016-03-15 11:49:12 -0700252 dev_err(dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500253 "%s(): Error %d reading register ALSIR_U\n",
254 __func__, ret);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530255 return ret;
256 }
257
258 *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
Brian Masneyb1dda962017-01-17 04:24:51 -0500259
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530260 return 0;
261}
262
263static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
264{
Alison Schofield722fc312016-03-15 11:49:12 -0700265 struct device *dev = regmap_get_device(chip->regmap);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530266 unsigned int data;
267 int ret;
268
Brian Masneyb44b1f32017-01-17 04:24:50 -0500269 if (!chip->enable_prox) {
Brian Masney67663422017-01-17 04:24:59 -0500270 ret = isl29028_enable_proximity(chip);
Brian Masneyb44b1f32017-01-17 04:24:50 -0500271 if (ret < 0)
272 return ret;
Brian Masneyb1dda962017-01-17 04:24:51 -0500273
Brian Masneyb44b1f32017-01-17 04:24:50 -0500274 chip->enable_prox = true;
275 }
276
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530277 ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
278 if (ret < 0) {
Brian Masney578e02a2017-01-17 04:24:55 -0500279 dev_err(dev, "%s(): Error %d reading register PROX_DATA\n",
280 __func__, ret);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530281 return ret;
282 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500283
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530284 *prox = data;
Brian Masneyb1dda962017-01-17 04:24:51 -0500285
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530286 return 0;
287}
288
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530289static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
290{
Alison Schofield722fc312016-03-15 11:49:12 -0700291 struct device *dev = regmap_get_device(chip->regmap);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530292 int ret;
293 int als_ir_data;
294
Brian Masneyf40531d2016-12-03 21:19:25 -0500295 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
296 if (ret < 0) {
Brian Masney578e02a2017-01-17 04:24:55 -0500297 dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__,
298 ret);
Brian Masneyf40531d2016-12-03 21:19:25 -0500299 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530300 }
301
302 ret = isl29028_read_als_ir(chip, &als_ir_data);
303 if (ret < 0)
304 return ret;
305
306 /*
307 * convert als data count to lux.
308 * if lux_scale = 125, lux = count * 0.031
309 * if lux_scale = 2000, lux = count * 0.49
310 */
311 if (chip->lux_scale == 125)
312 als_ir_data = (als_ir_data * 31) / 1000;
313 else
314 als_ir_data = (als_ir_data * 49) / 100;
315
316 *als_data = als_ir_data;
Brian Masneyb1dda962017-01-17 04:24:51 -0500317
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530318 return 0;
319}
320
321static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
322{
Alison Schofield722fc312016-03-15 11:49:12 -0700323 struct device *dev = regmap_get_device(chip->regmap);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530324 int ret;
325
Brian Masneyf40531d2016-12-03 21:19:25 -0500326 ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
327 if (ret < 0) {
Brian Masney578e02a2017-01-17 04:24:55 -0500328 dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__,
329 ret);
Brian Masneyf40531d2016-12-03 21:19:25 -0500330 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530331 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500332
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530333 return isl29028_read_als_ir(chip, ir_data);
334}
335
Brian Masney2db50542017-01-17 04:25:02 -0500336static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
337{
338 struct device *dev = regmap_get_device(chip->regmap);
339 int ret;
340
341 if (on) {
342 ret = pm_runtime_get_sync(dev);
343 if (ret < 0)
344 pm_runtime_put_noidle(dev);
345 } else {
346 pm_runtime_mark_last_busy(dev);
347 ret = pm_runtime_put_autosuspend(dev);
348 }
349
350 return ret;
351}
352
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530353/* Channel IO */
354static int isl29028_write_raw(struct iio_dev *indio_dev,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800355 struct iio_chan_spec const *chan,
356 int val, int val2, long mask)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530357{
358 struct isl29028_chip *chip = iio_priv(indio_dev);
Alison Schofield722fc312016-03-15 11:49:12 -0700359 struct device *dev = regmap_get_device(chip->regmap);
Brian Masney2db50542017-01-17 04:25:02 -0500360 int ret;
361
362 ret = isl29028_set_pm_runtime_busy(chip, true);
363 if (ret < 0)
364 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530365
366 mutex_lock(&chip->lock);
Brian Masney2db50542017-01-17 04:25:02 -0500367
368 ret = -EINVAL;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530369 switch (chan->type) {
370 case IIO_PROXIMITY:
Laxman Dewangan892cb6d2012-04-16 21:27:42 +0530371 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
Alison Schofield722fc312016-03-15 11:49:12 -0700372 dev_err(dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500373 "%s(): proximity: Mask value 0x%08lx is not supported\n",
374 __func__, mask);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530375 break;
376 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500377
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530378 if (val < 1 || val > 100) {
Alison Schofield722fc312016-03-15 11:49:12 -0700379 dev_err(dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500380 "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
381 __func__, val);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530382 break;
383 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500384
Brian Masneye4ff6c12017-04-24 21:34:33 -0400385 ret = isl29028_set_proxim_sampling(chip, val, val2);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530386 break;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530387 case IIO_LIGHT:
Laxman Dewangan892cb6d2012-04-16 21:27:42 +0530388 if (mask != IIO_CHAN_INFO_SCALE) {
Alison Schofield722fc312016-03-15 11:49:12 -0700389 dev_err(dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500390 "%s(): light: Mask value 0x%08lx is not supported\n",
391 __func__, mask);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530392 break;
393 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500394
Brian Masney71a0a642017-01-17 04:24:58 -0500395 if (val != 125 && val != 2000) {
Alison Schofield722fc312016-03-15 11:49:12 -0700396 dev_err(dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500397 "%s(): light: Lux scale %d is not in the set {125, 2000}\n",
398 __func__, val);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530399 break;
400 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500401
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530402 ret = isl29028_set_als_scale(chip, val);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530403 break;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530404 default:
Brian Masney578e02a2017-01-17 04:24:55 -0500405 dev_err(dev, "%s(): Unsupported channel type %x\n",
406 __func__, chan->type);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530407 break;
408 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500409
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530410 mutex_unlock(&chip->lock);
Brian Masneyb1dda962017-01-17 04:24:51 -0500411
Brian Masney2db50542017-01-17 04:25:02 -0500412 if (ret < 0)
413 return ret;
414
415 ret = isl29028_set_pm_runtime_busy(chip, false);
416 if (ret < 0)
417 return ret;
418
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530419 return ret;
420}
421
422static int isl29028_read_raw(struct iio_dev *indio_dev,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800423 struct iio_chan_spec const *chan,
424 int *val, int *val2, long mask)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530425{
426 struct isl29028_chip *chip = iio_priv(indio_dev);
Alison Schofield722fc312016-03-15 11:49:12 -0700427 struct device *dev = regmap_get_device(chip->regmap);
Brian Masney2db50542017-01-17 04:25:02 -0500428 int ret, pm_ret;
429
430 ret = isl29028_set_pm_runtime_busy(chip, true);
431 if (ret < 0)
432 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530433
434 mutex_lock(&chip->lock);
Brian Masney2db50542017-01-17 04:25:02 -0500435
436 ret = -EINVAL;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530437 switch (mask) {
Jonathan Camerona8c0ed72012-04-15 17:41:29 +0100438 case IIO_CHAN_INFO_RAW:
439 case IIO_CHAN_INFO_PROCESSED:
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530440 switch (chan->type) {
441 case IIO_LIGHT:
442 ret = isl29028_als_get(chip, val);
443 break;
444 case IIO_INTENSITY:
445 ret = isl29028_ir_get(chip, val);
446 break;
447 case IIO_PROXIMITY:
Brian Masneyb44b1f32017-01-17 04:24:50 -0500448 ret = isl29028_read_proxim(chip, val);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530449 break;
450 default:
451 break;
452 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500453
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530454 if (ret < 0)
455 break;
Brian Masneyb1dda962017-01-17 04:24:51 -0500456
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530457 ret = IIO_VAL_INT;
458 break;
Laxman Dewangan892cb6d2012-04-16 21:27:42 +0530459 case IIO_CHAN_INFO_SAMP_FREQ:
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530460 if (chan->type != IIO_PROXIMITY)
461 break;
Brian Masneyb1dda962017-01-17 04:24:51 -0500462
Brian Masneye4ff6c12017-04-24 21:34:33 -0400463 *val = chip->prox_sampling_int;
464 *val2 = chip->prox_sampling_frac;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530465 ret = IIO_VAL_INT;
466 break;
Laxman Dewangan892cb6d2012-04-16 21:27:42 +0530467 case IIO_CHAN_INFO_SCALE:
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530468 if (chan->type != IIO_LIGHT)
469 break;
470 *val = chip->lux_scale;
471 ret = IIO_VAL_INT;
472 break;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530473 default:
Brian Masney578e02a2017-01-17 04:24:55 -0500474 dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
475 __func__, mask);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530476 break;
477 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500478
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530479 mutex_unlock(&chip->lock);
Brian Masneyb1dda962017-01-17 04:24:51 -0500480
Brian Masney2db50542017-01-17 04:25:02 -0500481 if (ret < 0)
482 return ret;
483
484 /**
485 * Preserve the ret variable if the call to
486 * isl29028_set_pm_runtime_busy() is successful so the reading
487 * (if applicable) is returned to user space.
488 */
489 pm_ret = isl29028_set_pm_runtime_busy(chip, false);
490 if (pm_ret < 0)
491 return pm_ret;
492
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530493 return ret;
494}
495
496static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
Brian Masneye4ff6c12017-04-24 21:34:33 -0400497 "1.25 2.5 5 10 13.3 20 80 100");
Peter Meerwald-Stadlerb9b689c2016-07-05 13:55:43 +0200498static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530499
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530500#define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
501static struct attribute *isl29028_attributes[] = {
502 ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
503 ISL29028_CONST_ATTR(in_illuminance_scale_available),
504 NULL,
505};
506
507static const struct attribute_group isl29108_group = {
508 .attrs = isl29028_attributes,
509};
510
511static const struct iio_chan_spec isl29028_channels[] = {
512 {
513 .type = IIO_LIGHT,
Jonathan Cameron93a9fdf2013-02-27 19:39:52 +0000514 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
515 BIT(IIO_CHAN_INFO_SCALE),
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530516 }, {
517 .type = IIO_INTENSITY,
Jonathan Cameron93a9fdf2013-02-27 19:39:52 +0000518 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530519 }, {
520 .type = IIO_PROXIMITY,
Jonathan Cameron93a9fdf2013-02-27 19:39:52 +0000521 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
522 BIT(IIO_CHAN_INFO_SAMP_FREQ),
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530523 }
524};
525
526static const struct iio_info isl29028_info = {
527 .attrs = &isl29108_group,
Bhumika Goyaldf271032016-02-12 20:40:28 +0530528 .read_raw = isl29028_read_raw,
529 .write_raw = isl29028_write_raw,
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530530};
531
Brian Masney2db50542017-01-17 04:25:02 -0500532static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530533{
Alison Schofield722fc312016-03-15 11:49:12 -0700534 struct device *dev = regmap_get_device(chip->regmap);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530535 int ret;
536
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530537 ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
Brian Masney2db50542017-01-17 04:25:02 -0500538 if (ret < 0)
Brian Masney578e02a2017-01-17 04:24:55 -0500539 dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
540 __func__, ret);
Brian Masney2db50542017-01-17 04:25:02 -0500541
542 chip->als_ir_mode = ISL29028_MODE_NONE;
543 chip->enable_prox = false;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530544
Brian Masney0fac96ed2017-01-17 04:25:01 -0500545 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530546}
547
Peter Meerwald-Stadlerf61277042016-07-05 13:55:44 +0200548static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530549{
550 switch (reg) {
551 case ISL29028_REG_INTERRUPT:
552 case ISL29028_REG_PROX_DATA:
553 case ISL29028_REG_ALSIR_L:
554 case ISL29028_REG_ALSIR_U:
555 return true;
556 default:
557 return false;
558 }
559}
560
561static const struct regmap_config isl29028_regmap_config = {
562 .reg_bits = 8,
563 .val_bits = 8,
Peter Meerwald-Stadlerf61277042016-07-05 13:55:44 +0200564 .volatile_reg = isl29028_is_volatile_reg,
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530565 .max_register = ISL29028_NUM_REGS - 1,
566 .num_reg_defaults_raw = ISL29028_NUM_REGS,
567 .cache_type = REGCACHE_RBTREE,
568};
569
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500570static int isl29028_probe(struct i2c_client *client,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800571 const struct i2c_device_id *id)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530572{
573 struct isl29028_chip *chip;
574 struct iio_dev *indio_dev;
575 int ret;
576
Sachin Kamat80cab1d2013-07-22 12:03:00 +0100577 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
Brian Masney8c93940262017-01-17 04:24:57 -0500578 if (!indio_dev)
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530579 return -ENOMEM;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530580
581 chip = iio_priv(indio_dev);
582
583 i2c_set_clientdata(client, indio_dev);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530584 mutex_init(&chip->lock);
585
586 chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
587 if (IS_ERR(chip->regmap)) {
588 ret = PTR_ERR(chip->regmap);
Brian Masney578e02a2017-01-17 04:24:55 -0500589 dev_err(&client->dev, "%s: Error %d initializing regmap\n",
590 __func__, ret);
Sachin Kamat80cab1d2013-07-22 12:03:00 +0100591 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530592 }
593
Brian Masney58f64362016-12-03 21:19:27 -0500594 chip->enable_prox = false;
Brian Masneye4ff6c12017-04-24 21:34:33 -0400595 chip->prox_sampling_int = 20;
596 chip->prox_sampling_frac = 0;
Brian Masney58f64362016-12-03 21:19:27 -0500597 chip->lux_scale = 2000;
Brian Masney58f64362016-12-03 21:19:27 -0500598
599 ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
600 if (ret < 0) {
601 dev_err(&client->dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500602 "%s(): Error %d writing to TEST1_MODE register\n",
603 __func__, ret);
Brian Masney58f64362016-12-03 21:19:27 -0500604 return ret;
605 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500606
Brian Masney58f64362016-12-03 21:19:27 -0500607 ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
608 if (ret < 0) {
609 dev_err(&client->dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500610 "%s(): Error %d writing to TEST2_MODE register\n",
611 __func__, ret);
Brian Masney58f64362016-12-03 21:19:27 -0500612 return ret;
613 }
614
Brian Masney2db50542017-01-17 04:25:02 -0500615 ret = isl29028_clear_configure_reg(chip);
Brian Masney65ac7162017-01-17 04:24:56 -0500616 if (ret < 0)
Sachin Kamat80cab1d2013-07-22 12:03:00 +0100617 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530618
619 indio_dev->info = &isl29028_info;
620 indio_dev->channels = isl29028_channels;
621 indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
622 indio_dev->name = id->name;
623 indio_dev->dev.parent = &client->dev;
624 indio_dev->modes = INDIO_DIRECT_MODE;
Brian Masneyb1dda962017-01-17 04:24:51 -0500625
Brian Masney2db50542017-01-17 04:25:02 -0500626 pm_runtime_enable(&client->dev);
627 pm_runtime_set_autosuspend_delay(&client->dev,
628 ISL29028_POWER_OFF_DELAY_MS);
629 pm_runtime_use_autosuspend(&client->dev);
630
Ioana Ciornei86727e32015-10-02 13:37:49 +0300631 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530632 if (ret < 0) {
Alison Schofield722fc312016-03-15 11:49:12 -0700633 dev_err(&client->dev,
Brian Masney578e02a2017-01-17 04:24:55 -0500634 "%s(): iio registration failed with error %d\n",
635 __func__, ret);
Sachin Kamat80cab1d2013-07-22 12:03:00 +0100636 return ret;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530637 }
Brian Masneyb1dda962017-01-17 04:24:51 -0500638
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530639 return 0;
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530640}
641
Brian Masney2db50542017-01-17 04:25:02 -0500642static int isl29028_remove(struct i2c_client *client)
643{
644 struct iio_dev *indio_dev = i2c_get_clientdata(client);
645 struct isl29028_chip *chip = iio_priv(indio_dev);
646
647 iio_device_unregister(indio_dev);
648
649 pm_runtime_disable(&client->dev);
650 pm_runtime_set_suspended(&client->dev);
651 pm_runtime_put_noidle(&client->dev);
652
653 return isl29028_clear_configure_reg(chip);
654}
655
656static int __maybe_unused isl29028_suspend(struct device *dev)
657{
658 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
659 struct isl29028_chip *chip = iio_priv(indio_dev);
660 int ret;
661
662 mutex_lock(&chip->lock);
663
664 ret = isl29028_clear_configure_reg(chip);
665
666 mutex_unlock(&chip->lock);
667
668 return ret;
669}
670
671static int __maybe_unused isl29028_resume(struct device *dev)
672{
673 /**
674 * The specific component (ALS/IR or proximity) will enable itself as
675 * needed the next time that the user requests a reading. This is done
676 * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity().
677 */
678 return 0;
679}
680
681static const struct dev_pm_ops isl29028_pm_ops = {
Brian Masney0b59c382017-02-08 20:54:28 -0500682 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
683 pm_runtime_force_resume)
Brian Masney2db50542017-01-17 04:25:02 -0500684 SET_RUNTIME_PM_OPS(isl29028_suspend, isl29028_resume, NULL)
685};
686
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530687static const struct i2c_device_id isl29028_id[] = {
688 {"isl29028", 0},
Sebastian Reichele530cc82017-04-28 17:55:58 +0200689 {"isl29030", 0},
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530690 {}
691};
692MODULE_DEVICE_TABLE(i2c, isl29028_id);
693
694static const struct of_device_id isl29028_of_match[] = {
Arnaud Ebalard37e51572015-02-16 15:58:39 -0800695 { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
696 { .compatible = "isil,isl29028", },
Sebastian Reichele530cc82017-04-28 17:55:58 +0200697 { .compatible = "isil,isl29030", },
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530698 { },
699};
700MODULE_DEVICE_TABLE(of, isl29028_of_match);
701
702static struct i2c_driver isl29028_driver = {
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530703 .driver = {
704 .name = "isl29028",
Brian Masney2db50542017-01-17 04:25:02 -0500705 .pm = &isl29028_pm_ops,
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530706 .of_match_table = isl29028_of_match,
707 },
708 .probe = isl29028_probe,
Brian Masney2db50542017-01-17 04:25:02 -0500709 .remove = isl29028_remove,
Laxman Dewangan6d59ba22012-04-13 16:03:32 +0530710 .id_table = isl29028_id,
711};
712
713module_i2c_driver(isl29028_driver);
714
715MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
716MODULE_LICENSE("GPL v2");
717MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");