Version:  2.0.40 2.2.26 2.4.37 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7

Linux/drivers/staging/iio/light/isl29028.c

  1 /*
  2  * IIO driver for the light sensor ISL29028.
  3  * ISL29028 is Concurrent Ambient Light and Proximity Sensor
  4  *
  5  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
  6  *
  7  * This program is free software; you can redistribute it and/or modify it
  8  * under the terms and conditions of the GNU General Public License,
  9  * version 2, as published by the Free Software Foundation.
 10  *
 11  * This program is distributed in the hope it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 14  * more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 18  */
 19 
 20 #include <linux/module.h>
 21 #include <linux/i2c.h>
 22 #include <linux/err.h>
 23 #include <linux/mutex.h>
 24 #include <linux/delay.h>
 25 #include <linux/slab.h>
 26 #include <linux/regmap.h>
 27 #include <linux/iio/iio.h>
 28 #include <linux/iio/sysfs.h>
 29 
 30 #define CONVERSION_TIME_MS              100
 31 
 32 #define ISL29028_REG_CONFIGURE          0x01
 33 
 34 #define CONFIGURE_ALS_IR_MODE_ALS       0
 35 #define CONFIGURE_ALS_IR_MODE_IR        BIT(0)
 36 #define CONFIGURE_ALS_IR_MODE_MASK      BIT(0)
 37 
 38 #define CONFIGURE_ALS_RANGE_LOW_LUX     0
 39 #define CONFIGURE_ALS_RANGE_HIGH_LUX    BIT(1)
 40 #define CONFIGURE_ALS_RANGE_MASK        BIT(1)
 41 
 42 #define CONFIGURE_ALS_DIS               0
 43 #define CONFIGURE_ALS_EN                BIT(2)
 44 #define CONFIGURE_ALS_EN_MASK           BIT(2)
 45 
 46 #define CONFIGURE_PROX_DRIVE            BIT(3)
 47 
 48 #define CONFIGURE_PROX_SLP_SH           4
 49 #define CONFIGURE_PROX_SLP_MASK         (7 << CONFIGURE_PROX_SLP_SH)
 50 
 51 #define CONFIGURE_PROX_EN               BIT(7)
 52 #define CONFIGURE_PROX_EN_MASK          BIT(7)
 53 
 54 #define ISL29028_REG_INTERRUPT          0x02
 55 
 56 #define ISL29028_REG_PROX_DATA          0x08
 57 #define ISL29028_REG_ALSIR_L            0x09
 58 #define ISL29028_REG_ALSIR_U            0x0A
 59 
 60 #define ISL29028_REG_TEST1_MODE         0x0E
 61 #define ISL29028_REG_TEST2_MODE         0x0F
 62 
 63 #define ISL29028_NUM_REGS               (ISL29028_REG_TEST2_MODE + 1)
 64 
 65 enum als_ir_mode {
 66         MODE_NONE = 0,
 67         MODE_ALS,
 68         MODE_IR
 69 };
 70 
 71 struct isl29028_chip {
 72         struct mutex            lock;
 73         struct regmap           *regmap;
 74 
 75         unsigned int            prox_sampling;
 76         bool                    enable_prox;
 77 
 78         int                     lux_scale;
 79         int                     als_ir_mode;
 80 };
 81 
 82 static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
 83                                         unsigned int sampling)
 84 {
 85         static unsigned int prox_period[] = {800, 400, 200, 100, 75, 50, 12, 0};
 86         int sel;
 87         unsigned int period = DIV_ROUND_UP(1000, sampling);
 88 
 89         for (sel = 0; sel < ARRAY_SIZE(prox_period); ++sel) {
 90                 if (period >= prox_period[sel])
 91                         break;
 92         }
 93         return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
 94                         CONFIGURE_PROX_SLP_MASK, sel << CONFIGURE_PROX_SLP_SH);
 95 }
 96 
 97 static int isl29028_enable_proximity(struct isl29028_chip *chip, bool enable)
 98 {
 99         int ret;
100         int val = 0;
101 
102         if (enable)
103                 val = CONFIGURE_PROX_EN;
104         ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
105                                  CONFIGURE_PROX_EN_MASK, val);
106         if (ret < 0)
107                 return ret;
108 
109         /* Wait for conversion to be complete for first sample */
110         mdelay(DIV_ROUND_UP(1000, chip->prox_sampling));
111         return 0;
112 }
113 
114 static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
115 {
116         int val = (lux_scale == 2000) ? CONFIGURE_ALS_RANGE_HIGH_LUX :
117                                         CONFIGURE_ALS_RANGE_LOW_LUX;
118 
119         return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
120                 CONFIGURE_ALS_RANGE_MASK, val);
121 }
122 
123 static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
124                                     enum als_ir_mode mode)
125 {
126         int ret = 0;
127 
128         switch (mode) {
129         case MODE_ALS:
130                 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
131                                          CONFIGURE_ALS_IR_MODE_MASK,
132                                          CONFIGURE_ALS_IR_MODE_ALS);
133                 if (ret < 0)
134                         return ret;
135 
136                 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
137                                          CONFIGURE_ALS_RANGE_MASK,
138                                          CONFIGURE_ALS_RANGE_HIGH_LUX);
139                 break;
140 
141         case MODE_IR:
142                 ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
143                                          CONFIGURE_ALS_IR_MODE_MASK,
144                                          CONFIGURE_ALS_IR_MODE_IR);
145                 break;
146 
147         case MODE_NONE:
148                 return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
149                         CONFIGURE_ALS_EN_MASK, CONFIGURE_ALS_DIS);
150         }
151 
152         if (ret < 0)
153                 return ret;
154 
155         /* Enable the ALS/IR */
156         ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
157                                  CONFIGURE_ALS_EN_MASK, CONFIGURE_ALS_EN);
158         if (ret < 0)
159                 return ret;
160 
161         /* Need to wait for conversion time if ALS/IR mode enabled */
162         mdelay(CONVERSION_TIME_MS);
163         return 0;
164 }
165 
166 static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
167 {
168         struct device *dev = regmap_get_device(chip->regmap);
169         unsigned int lsb;
170         unsigned int msb;
171         int ret;
172 
173         ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
174         if (ret < 0) {
175                 dev_err(dev,
176                         "Error in reading register ALSIR_L err %d\n", ret);
177                 return ret;
178         }
179 
180         ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
181         if (ret < 0) {
182                 dev_err(dev,
183                         "Error in reading register ALSIR_U err %d\n", ret);
184                 return ret;
185         }
186 
187         *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
188         return 0;
189 }
190 
191 static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
192 {
193         struct device *dev = regmap_get_device(chip->regmap);
194         unsigned int data;
195         int ret;
196 
197         ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
198         if (ret < 0) {
199                 dev_err(dev, "Error in reading register %d, error %d\n",
200                         ISL29028_REG_PROX_DATA, ret);
201                 return ret;
202         }
203         *prox = data;
204         return 0;
205 }
206 
207 static int isl29028_proxim_get(struct isl29028_chip *chip, int *prox_data)
208 {
209         int ret;
210 
211         if (!chip->enable_prox) {
212                 ret = isl29028_enable_proximity(chip, true);
213                 if (ret < 0)
214                         return ret;
215                 chip->enable_prox = true;
216         }
217         return isl29028_read_proxim(chip, prox_data);
218 }
219 
220 static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
221 {
222         struct device *dev = regmap_get_device(chip->regmap);
223         int ret;
224         int als_ir_data;
225 
226         if (chip->als_ir_mode != MODE_ALS) {
227                 ret = isl29028_set_als_ir_mode(chip, MODE_ALS);
228                 if (ret < 0) {
229                         dev_err(dev,
230                                 "Error in enabling ALS mode err %d\n", ret);
231                         return ret;
232                 }
233                 chip->als_ir_mode = MODE_ALS;
234         }
235 
236         ret = isl29028_read_als_ir(chip, &als_ir_data);
237         if (ret < 0)
238                 return ret;
239 
240         /*
241          * convert als data count to lux.
242          * if lux_scale = 125,  lux = count * 0.031
243          * if lux_scale = 2000, lux = count * 0.49
244          */
245         if (chip->lux_scale == 125)
246                 als_ir_data = (als_ir_data * 31) / 1000;
247         else
248                 als_ir_data = (als_ir_data * 49) / 100;
249 
250         *als_data = als_ir_data;
251         return 0;
252 }
253 
254 static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
255 {
256         struct device *dev = regmap_get_device(chip->regmap);
257         int ret;
258 
259         if (chip->als_ir_mode != MODE_IR) {
260                 ret = isl29028_set_als_ir_mode(chip, MODE_IR);
261                 if (ret < 0) {
262                         dev_err(dev,
263                                 "Error in enabling IR mode err %d\n", ret);
264                         return ret;
265                 }
266                 chip->als_ir_mode = MODE_IR;
267         }
268         return isl29028_read_als_ir(chip, ir_data);
269 }
270 
271 /* Channel IO */
272 static int isl29028_write_raw(struct iio_dev *indio_dev,
273                               struct iio_chan_spec const *chan,
274                               int val, int val2, long mask)
275 {
276         struct isl29028_chip *chip = iio_priv(indio_dev);
277         struct device *dev = regmap_get_device(chip->regmap);
278         int ret = -EINVAL;
279 
280         mutex_lock(&chip->lock);
281         switch (chan->type) {
282         case IIO_PROXIMITY:
283                 if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
284                         dev_err(dev,
285                                 "proximity: mask value 0x%08lx not supported\n",
286                                 mask);
287                         break;
288                 }
289                 if (val < 1 || val > 100) {
290                         dev_err(dev,
291                                 "Samp_freq %d is not in range[1:100]\n", val);
292                         break;
293                 }
294                 ret = isl29028_set_proxim_sampling(chip, val);
295                 if (ret < 0) {
296                         dev_err(dev,
297                                 "Setting proximity samp_freq fail, err %d\n",
298                                 ret);
299                         break;
300                 }
301                 chip->prox_sampling = val;
302                 break;
303 
304         case IIO_LIGHT:
305                 if (mask != IIO_CHAN_INFO_SCALE) {
306                         dev_err(dev,
307                                 "light: mask value 0x%08lx not supported\n",
308                                 mask);
309                         break;
310                 }
311                 if ((val != 125) && (val != 2000)) {
312                         dev_err(dev,
313                                 "lux scale %d is invalid [125, 2000]\n", val);
314                         break;
315                 }
316                 ret = isl29028_set_als_scale(chip, val);
317                 if (ret < 0) {
318                         dev_err(dev,
319                                 "Setting lux scale fail with error %d\n", ret);
320                         break;
321                 }
322                 chip->lux_scale = val;
323                 break;
324 
325         default:
326                 dev_err(dev, "Unsupported channel type\n");
327                 break;
328         }
329         mutex_unlock(&chip->lock);
330         return ret;
331 }
332 
333 static int isl29028_read_raw(struct iio_dev *indio_dev,
334                              struct iio_chan_spec const *chan,
335                              int *val, int *val2, long mask)
336 {
337         struct isl29028_chip *chip = iio_priv(indio_dev);
338         struct device *dev = regmap_get_device(chip->regmap);
339         int ret = -EINVAL;
340 
341         mutex_lock(&chip->lock);
342         switch (mask) {
343         case IIO_CHAN_INFO_RAW:
344         case IIO_CHAN_INFO_PROCESSED:
345                 switch (chan->type) {
346                 case IIO_LIGHT:
347                         ret = isl29028_als_get(chip, val);
348                         break;
349                 case IIO_INTENSITY:
350                         ret = isl29028_ir_get(chip, val);
351                         break;
352                 case IIO_PROXIMITY:
353                         ret = isl29028_proxim_get(chip, val);
354                         break;
355                 default:
356                         break;
357                 }
358                 if (ret < 0)
359                         break;
360                 ret = IIO_VAL_INT;
361                 break;
362 
363         case IIO_CHAN_INFO_SAMP_FREQ:
364                 if (chan->type != IIO_PROXIMITY)
365                         break;
366                 *val = chip->prox_sampling;
367                 ret = IIO_VAL_INT;
368                 break;
369 
370         case IIO_CHAN_INFO_SCALE:
371                 if (chan->type != IIO_LIGHT)
372                         break;
373                 *val = chip->lux_scale;
374                 ret = IIO_VAL_INT;
375                 break;
376 
377         default:
378                 dev_err(dev, "mask value 0x%08lx not supported\n", mask);
379                 break;
380         }
381         mutex_unlock(&chip->lock);
382         return ret;
383 }
384 
385 static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
386                                 "1, 3, 5, 10, 13, 20, 83, 100");
387 static IIO_CONST_ATTR(in_illuminance_scale_available, "125, 2000");
388 
389 #define ISL29028_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
390 #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
391 static struct attribute *isl29028_attributes[] = {
392         ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
393         ISL29028_CONST_ATTR(in_illuminance_scale_available),
394         NULL,
395 };
396 
397 static const struct attribute_group isl29108_group = {
398         .attrs = isl29028_attributes,
399 };
400 
401 static const struct iio_chan_spec isl29028_channels[] = {
402         {
403                 .type = IIO_LIGHT,
404                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
405                 BIT(IIO_CHAN_INFO_SCALE),
406         }, {
407                 .type = IIO_INTENSITY,
408                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
409         }, {
410                 .type = IIO_PROXIMITY,
411                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
412                 BIT(IIO_CHAN_INFO_SAMP_FREQ),
413         }
414 };
415 
416 static const struct iio_info isl29028_info = {
417         .attrs = &isl29108_group,
418         .driver_module = THIS_MODULE,
419         .read_raw = isl29028_read_raw,
420         .write_raw = isl29028_write_raw,
421 };
422 
423 static int isl29028_chip_init(struct isl29028_chip *chip)
424 {
425         struct device *dev = regmap_get_device(chip->regmap);
426         int ret;
427 
428         chip->enable_prox  = false;
429         chip->prox_sampling = 20;
430         chip->lux_scale = 2000;
431         chip->als_ir_mode = MODE_NONE;
432 
433         ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
434         if (ret < 0) {
435                 dev_err(dev, "%s(): write to reg %d failed, err = %d\n",
436                         __func__, ISL29028_REG_TEST1_MODE, ret);
437                 return ret;
438         }
439         ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
440         if (ret < 0) {
441                 dev_err(dev, "%s(): write to reg %d failed, err = %d\n",
442                         __func__, ISL29028_REG_TEST2_MODE, ret);
443                 return ret;
444         }
445 
446         ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
447         if (ret < 0) {
448                 dev_err(dev, "%s(): write to reg %d failed, err = %d\n",
449                         __func__, ISL29028_REG_CONFIGURE, ret);
450                 return ret;
451         }
452 
453         ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling);
454         if (ret < 0) {
455                 dev_err(dev, "setting the proximity, err = %d\n", ret);
456                 return ret;
457         }
458 
459         ret = isl29028_set_als_scale(chip, chip->lux_scale);
460         if (ret < 0)
461                 dev_err(dev, "setting als scale failed, err = %d\n", ret);
462         return ret;
463 }
464 
465 static bool is_volatile_reg(struct device *dev, unsigned int reg)
466 {
467         switch (reg) {
468         case ISL29028_REG_INTERRUPT:
469         case ISL29028_REG_PROX_DATA:
470         case ISL29028_REG_ALSIR_L:
471         case ISL29028_REG_ALSIR_U:
472                 return true;
473         default:
474                 return false;
475         }
476 }
477 
478 static const struct regmap_config isl29028_regmap_config = {
479         .reg_bits = 8,
480         .val_bits = 8,
481         .volatile_reg = is_volatile_reg,
482         .max_register = ISL29028_NUM_REGS - 1,
483         .num_reg_defaults_raw = ISL29028_NUM_REGS,
484         .cache_type = REGCACHE_RBTREE,
485 };
486 
487 static int isl29028_probe(struct i2c_client *client,
488                           const struct i2c_device_id *id)
489 {
490         struct isl29028_chip *chip;
491         struct iio_dev *indio_dev;
492         int ret;
493 
494         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
495         if (!indio_dev) {
496                 dev_err(&client->dev, "iio allocation fails\n");
497                 return -ENOMEM;
498         }
499 
500         chip = iio_priv(indio_dev);
501 
502         i2c_set_clientdata(client, indio_dev);
503         mutex_init(&chip->lock);
504 
505         chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
506         if (IS_ERR(chip->regmap)) {
507                 ret = PTR_ERR(chip->regmap);
508                 dev_err(&client->dev, "regmap initialization failed: %d\n",
509                         ret);
510                 return ret;
511         }
512 
513         ret = isl29028_chip_init(chip);
514         if (ret < 0) {
515                 dev_err(&client->dev, "chip initialization failed: %d\n", ret);
516                 return ret;
517         }
518 
519         indio_dev->info = &isl29028_info;
520         indio_dev->channels = isl29028_channels;
521         indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
522         indio_dev->name = id->name;
523         indio_dev->dev.parent = &client->dev;
524         indio_dev->modes = INDIO_DIRECT_MODE;
525         ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
526         if (ret < 0) {
527                 dev_err(&client->dev,
528                         "iio registration fails with error %d\n",
529                         ret);
530                 return ret;
531         }
532         return 0;
533 }
534 
535 static const struct i2c_device_id isl29028_id[] = {
536         {"isl29028", 0},
537         {}
538 };
539 MODULE_DEVICE_TABLE(i2c, isl29028_id);
540 
541 static const struct of_device_id isl29028_of_match[] = {
542         { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
543         { .compatible = "isil,isl29028", },
544         { },
545 };
546 MODULE_DEVICE_TABLE(of, isl29028_of_match);
547 
548 static struct i2c_driver isl29028_driver = {
549         .class  = I2C_CLASS_HWMON,
550         .driver  = {
551                 .name = "isl29028",
552                 .of_match_table = isl29028_of_match,
553         },
554         .probe   = isl29028_probe,
555         .id_table = isl29028_id,
556 };
557 
558 module_i2c_driver(isl29028_driver);
559 
560 MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
561 MODULE_LICENSE("GPL v2");
562 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
563 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us