Version:  2.0.40 2.2.26 2.4.37 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 4.8 4.9

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

  1 /*
  2  * A iio driver for the light sensor ISL 29018/29023/29035.
  3  *
  4  * IIO driver for monitoring ambient light intensity in luxi, proximity
  5  * sensing and infrared sensing.
  6  *
  7  * Copyright (c) 2010, NVIDIA Corporation.
  8  *
  9  * This program is free software; you can redistribute it and/or modify
 10  * it under the terms of the GNU General Public License as published by
 11  * the Free Software Foundation; either version 2 of the License, or
 12  * (at your option) any later version.
 13  *
 14  * This program is distributed in the hope that it will be useful, but WITHOUT
 15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 17  * more details.
 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/regmap.h>
 26 #include <linux/slab.h>
 27 #include <linux/iio/iio.h>
 28 #include <linux/iio/sysfs.h>
 29 #include <linux/acpi.h>
 30 
 31 #define ISL29018_CONV_TIME_MS           100
 32 
 33 #define ISL29018_REG_ADD_COMMAND1       0x00
 34 #define ISL29018_CMD1_OPMODE_SHIFT      5
 35 #define ISL29018_CMD1_OPMODE_MASK       (7 << ISL29018_CMD1_OPMODE_SHIFT)
 36 #define ISL29018_CMD1_OPMODE_POWER_DOWN 0
 37 #define ISL29018_CMD1_OPMODE_ALS_ONCE   1
 38 #define ISL29018_CMD1_OPMODE_IR_ONCE    2
 39 #define ISL29018_CMD1_OPMODE_PROX_ONCE  3
 40 
 41 #define ISL29018_REG_ADD_COMMAND2       0x01
 42 #define ISL29018_CMD2_RESOLUTION_SHIFT  2
 43 #define ISL29018_CMD2_RESOLUTION_MASK   (0x3 << ISL29018_CMD2_RESOLUTION_SHIFT)
 44 
 45 #define ISL29018_CMD2_RANGE_SHIFT       0
 46 #define ISL29018_CMD2_RANGE_MASK        (0x3 << ISL29018_CMD2_RANGE_SHIFT)
 47 
 48 #define ISL29018_CMD2_SCHEME_SHIFT      7
 49 #define ISL29018_CMD2_SCHEME_MASK       (0x1 << ISL29018_CMD2_SCHEME_SHIFT)
 50 
 51 #define ISL29018_REG_ADD_DATA_LSB       0x02
 52 #define ISL29018_REG_ADD_DATA_MSB       0x03
 53 
 54 #define ISL29018_REG_TEST               0x08
 55 #define ISL29018_TEST_SHIFT             0
 56 #define ISL29018_TEST_MASK              (0xFF << ISL29018_TEST_SHIFT)
 57 
 58 #define ISL29035_REG_DEVICE_ID          0x0F
 59 #define ISL29035_DEVICE_ID_SHIFT        0x03
 60 #define ISL29035_DEVICE_ID_MASK         (0x7 << ISL29035_DEVICE_ID_SHIFT)
 61 #define ISL29035_DEVICE_ID              0x5
 62 #define ISL29035_BOUT_SHIFT             0x07
 63 #define ISL29035_BOUT_MASK              (0x01 << ISL29035_BOUT_SHIFT)
 64 
 65 #define ISL29018_INT_TIME_AVAIL         "0.090000 0.005630 0.000351 0.000021"
 66 #define ISL29023_INT_TIME_AVAIL         "0.090000 0.005600 0.000352 0.000022"
 67 #define ISL29035_INT_TIME_AVAIL         "0.105000 0.006500 0.000410 0.000025"
 68 
 69 static const char * const int_time_avail[] = {
 70         ISL29018_INT_TIME_AVAIL,
 71         ISL29023_INT_TIME_AVAIL,
 72         ISL29035_INT_TIME_AVAIL,
 73 };
 74 
 75 enum isl29018_int_time {
 76         ISL29018_INT_TIME_16,
 77         ISL29018_INT_TIME_12,
 78         ISL29018_INT_TIME_8,
 79         ISL29018_INT_TIME_4,
 80 };
 81 
 82 static const unsigned int isl29018_int_utimes[3][4] = {
 83         {90000, 5630, 351, 21},
 84         {90000, 5600, 352, 22},
 85         {105000, 6500, 410, 25},
 86 };
 87 
 88 static const struct isl29018_scale {
 89         unsigned int scale;
 90         unsigned int uscale;
 91 } isl29018_scales[4][4] = {
 92         { {0, 15258}, {0, 61035}, {0, 244140}, {0, 976562} },
 93         { {0, 244140}, {0, 976562}, {3, 906250}, {15, 625000} },
 94         { {3, 906250}, {15, 625000}, {62, 500000}, {250, 0} },
 95         { {62, 500000}, {250, 0}, {1000, 0}, {4000, 0} }
 96 };
 97 
 98 struct isl29018_chip {
 99         struct regmap           *regmap;
100         struct mutex            lock;
101         int                     type;
102         unsigned int            calibscale;
103         unsigned int            ucalibscale;
104         unsigned int            int_time;
105         struct isl29018_scale   scale;
106         int                     prox_scheme;
107         bool                    suspended;
108 };
109 
110 static int isl29018_set_integration_time(struct isl29018_chip *chip,
111                                          unsigned int utime)
112 {
113         int i, ret;
114         unsigned int int_time, new_int_time;
115 
116         for (i = 0; i < ARRAY_SIZE(isl29018_int_utimes[chip->type]); ++i) {
117                 if (utime == isl29018_int_utimes[chip->type][i]) {
118                         new_int_time = i;
119                         break;
120                 }
121         }
122 
123         if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type]))
124                 return -EINVAL;
125 
126         ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
127                                  ISL29018_CMD2_RESOLUTION_MASK,
128                                  i << ISL29018_CMD2_RESOLUTION_SHIFT);
129         if (ret < 0)
130                 return ret;
131 
132         /* Keep the same range when integration time changes */
133         int_time = chip->int_time;
134         for (i = 0; i < ARRAY_SIZE(isl29018_scales[int_time]); ++i) {
135                 if (chip->scale.scale == isl29018_scales[int_time][i].scale &&
136                     chip->scale.uscale == isl29018_scales[int_time][i].uscale) {
137                         chip->scale = isl29018_scales[new_int_time][i];
138                         break;
139                 }
140         }
141         chip->int_time = new_int_time;
142 
143         return 0;
144 }
145 
146 static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale)
147 {
148         int i, ret;
149         struct isl29018_scale new_scale;
150 
151         for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i) {
152                 if (scale == isl29018_scales[chip->int_time][i].scale &&
153                     uscale == isl29018_scales[chip->int_time][i].uscale) {
154                         new_scale = isl29018_scales[chip->int_time][i];
155                         break;
156                 }
157         }
158 
159         if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time]))
160                 return -EINVAL;
161 
162         ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
163                                  ISL29018_CMD2_RANGE_MASK,
164                                  i << ISL29018_CMD2_RANGE_SHIFT);
165         if (ret < 0)
166                 return ret;
167 
168         chip->scale = new_scale;
169 
170         return 0;
171 }
172 
173 static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode)
174 {
175         int status;
176         unsigned int lsb;
177         unsigned int msb;
178         struct device *dev = regmap_get_device(chip->regmap);
179 
180         /* Set mode */
181         status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1,
182                               mode << ISL29018_CMD1_OPMODE_SHIFT);
183         if (status) {
184                 dev_err(dev,
185                         "Error in setting operating mode err %d\n", status);
186                 return status;
187         }
188         msleep(ISL29018_CONV_TIME_MS);
189         status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb);
190         if (status < 0) {
191                 dev_err(dev,
192                         "Error in reading LSB DATA with err %d\n", status);
193                 return status;
194         }
195 
196         status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb);
197         if (status < 0) {
198                 dev_err(dev,
199                         "Error in reading MSB DATA with error %d\n", status);
200                 return status;
201         }
202         dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb);
203 
204         return (msb << 8) | lsb;
205 }
206 
207 static int isl29018_read_lux(struct isl29018_chip *chip, int *lux)
208 {
209         int lux_data;
210         unsigned int data_x_range;
211 
212         lux_data = isl29018_read_sensor_input(chip,
213                                               ISL29018_CMD1_OPMODE_ALS_ONCE);
214         if (lux_data < 0)
215                 return lux_data;
216 
217         data_x_range = lux_data * chip->scale.scale +
218                        lux_data * chip->scale.uscale / 1000000;
219         *lux = data_x_range * chip->calibscale +
220                data_x_range * chip->ucalibscale / 1000000;
221 
222         return 0;
223 }
224 
225 static int isl29018_read_ir(struct isl29018_chip *chip, int *ir)
226 {
227         int ir_data;
228 
229         ir_data = isl29018_read_sensor_input(chip,
230                                              ISL29018_CMD1_OPMODE_IR_ONCE);
231         if (ir_data < 0)
232                 return ir_data;
233 
234         *ir = ir_data;
235 
236         return 0;
237 }
238 
239 static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme,
240                                       int *near_ir)
241 {
242         int status;
243         int prox_data = -1;
244         int ir_data = -1;
245         struct device *dev = regmap_get_device(chip->regmap);
246 
247         /* Do proximity sensing with required scheme */
248         status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2,
249                                     ISL29018_CMD2_SCHEME_MASK,
250                                     scheme << ISL29018_CMD2_SCHEME_SHIFT);
251         if (status) {
252                 dev_err(dev, "Error in setting operating mode\n");
253                 return status;
254         }
255 
256         prox_data = isl29018_read_sensor_input(chip,
257                                                ISL29018_CMD1_OPMODE_PROX_ONCE);
258         if (prox_data < 0)
259                 return prox_data;
260 
261         if (scheme == 1) {
262                 *near_ir = prox_data;
263                 return 0;
264         }
265 
266         ir_data = isl29018_read_sensor_input(chip,
267                                              ISL29018_CMD1_OPMODE_IR_ONCE);
268         if (ir_data < 0)
269                 return ir_data;
270 
271         if (prox_data >= ir_data)
272                 *near_ir = prox_data - ir_data;
273         else
274                 *near_ir = 0;
275 
276         return 0;
277 }
278 
279 static ssize_t isl29018_show_scale_available(struct device *dev,
280                                     struct device_attribute *attr, char *buf)
281 {
282         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
283         struct isl29018_chip *chip = iio_priv(indio_dev);
284         int i, len = 0;
285 
286         for (i = 0; i < ARRAY_SIZE(isl29018_scales[chip->int_time]); ++i)
287                 len += sprintf(buf + len, "%d.%06d ",
288                                isl29018_scales[chip->int_time][i].scale,
289                                isl29018_scales[chip->int_time][i].uscale);
290 
291         buf[len - 1] = '\n';
292 
293         return len;
294 }
295 
296 static ssize_t isl29018_show_int_time_available(struct device *dev,
297                                        struct device_attribute *attr, char *buf)
298 {
299         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
300         struct isl29018_chip *chip = iio_priv(indio_dev);
301         int i, len = 0;
302 
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 
312 static ssize_t isl29018_show_prox_infrared_suppression(struct device *dev,
313                                               struct device_attribute *attr,
314                                               char *buf)
315 {
316         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
317         struct isl29018_chip *chip = iio_priv(indio_dev);
318 
319         /*
320          * Return the "proximity scheme" i.e. if the chip does on chip
321          * infrared suppression (1 means perform on chip suppression)
322          */
323         return sprintf(buf, "%d\n", chip->prox_scheme);
324 }
325 
326 static ssize_t isl29018_store_prox_infrared_suppression(struct device *dev,
327                                                struct device_attribute *attr,
328                                                const char *buf, size_t count)
329 {
330         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
331         struct isl29018_chip *chip = iio_priv(indio_dev);
332         int val;
333 
334         if (kstrtoint(buf, 10, &val))
335                 return -EINVAL;
336         if (!(val == 0 || val == 1))
337                 return -EINVAL;
338 
339         /*
340          * Get the "proximity scheme" i.e. if the chip does on chip
341          * infrared suppression (1 means perform on chip suppression)
342          */
343         mutex_lock(&chip->lock);
344         chip->prox_scheme = val;
345         mutex_unlock(&chip->lock);
346 
347         return count;
348 }
349 
350 static int isl29018_write_raw(struct iio_dev *indio_dev,
351                               struct iio_chan_spec const *chan,
352                               int val,
353                               int val2,
354                               long mask)
355 {
356         struct isl29018_chip *chip = iio_priv(indio_dev);
357         int ret = -EINVAL;
358 
359         mutex_lock(&chip->lock);
360         switch (mask) {
361         case IIO_CHAN_INFO_CALIBSCALE:
362                 if (chan->type == IIO_LIGHT) {
363                         chip->calibscale = val;
364                         chip->ucalibscale = val2;
365                         ret = 0;
366                 }
367                 break;
368         case IIO_CHAN_INFO_INT_TIME:
369                 if (chan->type == IIO_LIGHT) {
370                         if (val) {
371                                 mutex_unlock(&chip->lock);
372                                 return -EINVAL;
373                         }
374                         ret = isl29018_set_integration_time(chip, val2);
375                 }
376                 break;
377         case IIO_CHAN_INFO_SCALE:
378                 if (chan->type == IIO_LIGHT)
379                         ret = isl29018_set_scale(chip, val, val2);
380                 break;
381         default:
382                 break;
383         }
384         mutex_unlock(&chip->lock);
385 
386         return ret;
387 }
388 
389 static int isl29018_read_raw(struct iio_dev *indio_dev,
390                              struct iio_chan_spec const *chan,
391                              int *val,
392                              int *val2,
393                              long mask)
394 {
395         int ret = -EINVAL;
396         struct isl29018_chip *chip = iio_priv(indio_dev);
397 
398         mutex_lock(&chip->lock);
399         if (chip->suspended) {
400                 mutex_unlock(&chip->lock);
401                 return -EBUSY;
402         }
403         switch (mask) {
404         case IIO_CHAN_INFO_RAW:
405         case IIO_CHAN_INFO_PROCESSED:
406                 switch (chan->type) {
407                 case IIO_LIGHT:
408                         ret = isl29018_read_lux(chip, val);
409                         break;
410                 case IIO_INTENSITY:
411                         ret = isl29018_read_ir(chip, val);
412                         break;
413                 case IIO_PROXIMITY:
414                         ret = isl29018_read_proximity_ir(chip,
415                                                          chip->prox_scheme,
416                                                          val);
417                         break;
418                 default:
419                         break;
420                 }
421                 if (!ret)
422                         ret = IIO_VAL_INT;
423                 break;
424         case IIO_CHAN_INFO_INT_TIME:
425                 if (chan->type == IIO_LIGHT) {
426                         *val = 0;
427                         *val2 = isl29018_int_utimes[chip->type][chip->int_time];
428                         ret = IIO_VAL_INT_PLUS_MICRO;
429                 }
430                 break;
431         case IIO_CHAN_INFO_SCALE:
432                 if (chan->type == IIO_LIGHT) {
433                         *val = chip->scale.scale;
434                         *val2 = chip->scale.uscale;
435                         ret = IIO_VAL_INT_PLUS_MICRO;
436                 }
437                 break;
438         case IIO_CHAN_INFO_CALIBSCALE:
439                 if (chan->type == IIO_LIGHT) {
440                         *val = chip->calibscale;
441                         *val2 = chip->ucalibscale;
442                         ret = IIO_VAL_INT_PLUS_MICRO;
443                 }
444                 break;
445         default:
446                 break;
447         }
448         mutex_unlock(&chip->lock);
449         return ret;
450 }
451 
452 #define ISL29018_LIGHT_CHANNEL {                                        \
453         .type = IIO_LIGHT,                                              \
454         .indexed = 1,                                                   \
455         .channel = 0,                                                   \
456         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |            \
457         BIT(IIO_CHAN_INFO_CALIBSCALE) |                                 \
458         BIT(IIO_CHAN_INFO_SCALE) |                                      \
459         BIT(IIO_CHAN_INFO_INT_TIME),                                    \
460 }
461 
462 #define ISL29018_IR_CHANNEL {                                           \
463         .type = IIO_INTENSITY,                                          \
464         .modified = 1,                                                  \
465         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
466         .channel2 = IIO_MOD_LIGHT_IR,                                   \
467 }
468 
469 #define ISL29018_PROXIMITY_CHANNEL {                                    \
470         .type = IIO_PROXIMITY,                                          \
471         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),                   \
472 }
473 
474 static const struct iio_chan_spec isl29018_channels[] = {
475         ISL29018_LIGHT_CHANNEL,
476         ISL29018_IR_CHANNEL,
477         ISL29018_PROXIMITY_CHANNEL,
478 };
479 
480 static const struct iio_chan_spec isl29023_channels[] = {
481         ISL29018_LIGHT_CHANNEL,
482         ISL29018_IR_CHANNEL,
483 };
484 
485 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO,
486                        isl29018_show_int_time_available, NULL, 0);
487 static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO,
488                       isl29018_show_scale_available, NULL, 0);
489 static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression,
490                                         S_IRUGO | S_IWUSR,
491                                         isl29018_show_prox_infrared_suppression,
492                                         isl29018_store_prox_infrared_suppression, 0);
493 
494 #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
495 
496 static struct attribute *isl29018_attributes[] = {
497         ISL29018_DEV_ATTR(in_illuminance_scale_available),
498         ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
499         ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression),
500         NULL
501 };
502 
503 static struct attribute *isl29023_attributes[] = {
504         ISL29018_DEV_ATTR(in_illuminance_scale_available),
505         ISL29018_DEV_ATTR(in_illuminance_integration_time_available),
506         NULL
507 };
508 
509 static const struct attribute_group isl29018_group = {
510         .attrs = isl29018_attributes,
511 };
512 
513 static const struct attribute_group isl29023_group = {
514         .attrs = isl29023_attributes,
515 };
516 
517 static int isl29035_detect(struct isl29018_chip *chip)
518 {
519         int status;
520         unsigned int id;
521         struct device *dev = regmap_get_device(chip->regmap);
522 
523         status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id);
524         if (status < 0) {
525                 dev_err(dev,
526                         "Error reading ID register with error %d\n",
527                         status);
528                 return status;
529         }
530 
531         id = (id & ISL29035_DEVICE_ID_MASK) >> ISL29035_DEVICE_ID_SHIFT;
532 
533         if (id != ISL29035_DEVICE_ID)
534                 return -ENODEV;
535 
536         /* Clear brownout bit */
537         return regmap_update_bits(chip->regmap, ISL29035_REG_DEVICE_ID,
538                                   ISL29035_BOUT_MASK, 0);
539 }
540 
541 enum {
542         isl29018,
543         isl29023,
544         isl29035,
545 };
546 
547 static int isl29018_chip_init(struct isl29018_chip *chip)
548 {
549         int status;
550         struct device *dev = regmap_get_device(chip->regmap);
551 
552         if (chip->type == isl29035) {
553                 status = isl29035_detect(chip);
554                 if (status < 0)
555                         return status;
556         }
557 
558         /* Code added per Intersil Application Note 1534:
559          *     When VDD sinks to approximately 1.8V or below, some of
560          * the part's registers may change their state. When VDD
561          * recovers to 2.25V (or greater), the part may thus be in an
562          * unknown mode of operation. The user can return the part to
563          * a known mode of operation either by (a) setting VDD = 0V for
564          * 1 second or more and then powering back up with a slew rate
565          * of 0.5V/ms or greater, or (b) via I2C disable all ALS/PROX
566          * conversions, clear the test registers, and then rewrite all
567          * registers to the desired values.
568          * ...
569          * For ISL29011, ISL29018, ISL29021, ISL29023
570          * 1. Write 0x00 to register 0x08 (TEST)
571          * 2. Write 0x00 to register 0x00 (CMD1)
572          * 3. Rewrite all registers to the desired values
573          *
574          * ISL29018 Data Sheet (FN6619.1, Feb 11, 2010) essentially says
575          * the same thing EXCEPT the data sheet asks for a 1ms delay after
576          * writing the CMD1 register.
577          */
578         status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0);
579         if (status < 0) {
580                 dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n",
581                         status);
582                 return status;
583         }
584 
585         /* See Intersil AN1534 comments above.
586          * "Operating Mode" (COMMAND1) register is reprogrammed when
587          * data is read from the device.
588          */
589         status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0);
590         if (status < 0) {
591                 dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n",
592                         status);
593                 return status;
594         }
595 
596         usleep_range(1000, 2000);       /* per data sheet, page 10 */
597 
598         /* Set defaults */
599         status = isl29018_set_scale(chip, chip->scale.scale,
600                                     chip->scale.uscale);
601         if (status < 0) {
602                 dev_err(dev, "Init of isl29018 fails\n");
603                 return status;
604         }
605 
606         status = isl29018_set_integration_time(chip,
607                         isl29018_int_utimes[chip->type][chip->int_time]);
608         if (status < 0) {
609                 dev_err(dev, "Init of isl29018 fails\n");
610                 return status;
611         }
612 
613         return 0;
614 }
615 
616 static const struct iio_info isl29018_info = {
617         .attrs = &isl29018_group,
618         .driver_module = THIS_MODULE,
619         .read_raw = isl29018_read_raw,
620         .write_raw = isl29018_write_raw,
621 };
622 
623 static const struct iio_info isl29023_info = {
624         .attrs = &isl29023_group,
625         .driver_module = THIS_MODULE,
626         .read_raw = isl29018_read_raw,
627         .write_raw = isl29018_write_raw,
628 };
629 
630 static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg)
631 {
632         switch (reg) {
633         case ISL29018_REG_ADD_DATA_LSB:
634         case ISL29018_REG_ADD_DATA_MSB:
635         case ISL29018_REG_ADD_COMMAND1:
636         case ISL29018_REG_TEST:
637         case ISL29035_REG_DEVICE_ID:
638                 return true;
639         default:
640                 return false;
641         }
642 }
643 
644 static const struct regmap_config isl29018_regmap_config = {
645         .reg_bits = 8,
646         .val_bits = 8,
647         .volatile_reg = isl29018_is_volatile_reg,
648         .max_register = ISL29018_REG_TEST,
649         .num_reg_defaults_raw = ISL29018_REG_TEST + 1,
650         .cache_type = REGCACHE_RBTREE,
651 };
652 
653 static const struct regmap_config isl29035_regmap_config = {
654         .reg_bits = 8,
655         .val_bits = 8,
656         .volatile_reg = isl29018_is_volatile_reg,
657         .max_register = ISL29035_REG_DEVICE_ID,
658         .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1,
659         .cache_type = REGCACHE_RBTREE,
660 };
661 
662 struct isl29018_chip_info {
663         const struct iio_chan_spec *channels;
664         int num_channels;
665         const struct iio_info *indio_info;
666         const struct regmap_config *regmap_cfg;
667 };
668 
669 static const struct isl29018_chip_info isl29018_chip_info_tbl[] = {
670         [isl29018] = {
671                 .channels = isl29018_channels,
672                 .num_channels = ARRAY_SIZE(isl29018_channels),
673                 .indio_info = &isl29018_info,
674                 .regmap_cfg = &isl29018_regmap_config,
675         },
676         [isl29023] = {
677                 .channels = isl29023_channels,
678                 .num_channels = ARRAY_SIZE(isl29023_channels),
679                 .indio_info = &isl29023_info,
680                 .regmap_cfg = &isl29018_regmap_config,
681         },
682         [isl29035] = {
683                 .channels = isl29023_channels,
684                 .num_channels = ARRAY_SIZE(isl29023_channels),
685                 .indio_info = &isl29023_info,
686                 .regmap_cfg = &isl29035_regmap_config,
687         },
688 };
689 
690 static const char *isl29018_match_acpi_device(struct device *dev, int *data)
691 {
692         const struct acpi_device_id *id;
693 
694         id = acpi_match_device(dev->driver->acpi_match_table, dev);
695 
696         if (!id)
697                 return NULL;
698 
699         *data = (int)id->driver_data;
700 
701         return dev_name(dev);
702 }
703 
704 static int isl29018_probe(struct i2c_client *client,
705                           const struct i2c_device_id *id)
706 {
707         struct isl29018_chip *chip;
708         struct iio_dev *indio_dev;
709         int err;
710         const char *name = NULL;
711         int dev_id = 0;
712 
713         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
714         if (!indio_dev)
715                 return -ENOMEM;
716         chip = iio_priv(indio_dev);
717 
718         i2c_set_clientdata(client, indio_dev);
719 
720         if (id) {
721                 name = id->name;
722                 dev_id = id->driver_data;
723         }
724 
725         if (ACPI_HANDLE(&client->dev))
726                 name = isl29018_match_acpi_device(&client->dev, &dev_id);
727 
728         mutex_init(&chip->lock);
729 
730         chip->type = dev_id;
731         chip->calibscale = 1;
732         chip->ucalibscale = 0;
733         chip->int_time = ISL29018_INT_TIME_16;
734         chip->scale = isl29018_scales[chip->int_time][0];
735         chip->suspended = false;
736 
737         chip->regmap = devm_regmap_init_i2c(client,
738                                 isl29018_chip_info_tbl[dev_id].regmap_cfg);
739         if (IS_ERR(chip->regmap)) {
740                 err = PTR_ERR(chip->regmap);
741                 dev_err(&client->dev, "regmap initialization fails: %d\n", err);
742                 return err;
743         }
744 
745         err = isl29018_chip_init(chip);
746         if (err)
747                 return err;
748 
749         indio_dev->info = isl29018_chip_info_tbl[dev_id].indio_info;
750         indio_dev->channels = isl29018_chip_info_tbl[dev_id].channels;
751         indio_dev->num_channels = isl29018_chip_info_tbl[dev_id].num_channels;
752         indio_dev->name = name;
753         indio_dev->dev.parent = &client->dev;
754         indio_dev->modes = INDIO_DIRECT_MODE;
755         return devm_iio_device_register(&client->dev, indio_dev);
756 }
757 
758 #ifdef CONFIG_PM_SLEEP
759 static int isl29018_suspend(struct device *dev)
760 {
761         struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
762 
763         mutex_lock(&chip->lock);
764 
765         /* Since this driver uses only polling commands, we are by default in
766          * auto shutdown (ie, power-down) mode.
767          * So we do not have much to do here.
768          */
769         chip->suspended = true;
770 
771         mutex_unlock(&chip->lock);
772         return 0;
773 }
774 
775 static int isl29018_resume(struct device *dev)
776 {
777         struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
778         int err;
779 
780         mutex_lock(&chip->lock);
781 
782         err = isl29018_chip_init(chip);
783         if (!err)
784                 chip->suspended = false;
785 
786         mutex_unlock(&chip->lock);
787         return err;
788 }
789 
790 static SIMPLE_DEV_PM_OPS(isl29018_pm_ops, isl29018_suspend, isl29018_resume);
791 #define ISL29018_PM_OPS (&isl29018_pm_ops)
792 #else
793 #define ISL29018_PM_OPS NULL
794 #endif
795 
796 static const struct acpi_device_id isl29018_acpi_match[] = {
797         {"ISL29018", isl29018},
798         {"ISL29023", isl29023},
799         {"ISL29035", isl29035},
800         {},
801 };
802 MODULE_DEVICE_TABLE(acpi, isl29018_acpi_match);
803 
804 static const struct i2c_device_id isl29018_id[] = {
805         {"isl29018", isl29018},
806         {"isl29023", isl29023},
807         {"isl29035", isl29035},
808         {}
809 };
810 
811 MODULE_DEVICE_TABLE(i2c, isl29018_id);
812 
813 static const struct of_device_id isl29018_of_match[] = {
814         { .compatible = "isil,isl29018", },
815         { .compatible = "isil,isl29023", },
816         { .compatible = "isil,isl29035", },
817         { },
818 };
819 MODULE_DEVICE_TABLE(of, isl29018_of_match);
820 
821 static struct i2c_driver isl29018_driver = {
822         .driver  = {
823                         .name = "isl29018",
824                         .acpi_match_table = ACPI_PTR(isl29018_acpi_match),
825                         .pm = ISL29018_PM_OPS,
826                         .of_match_table = isl29018_of_match,
827                     },
828         .probe   = isl29018_probe,
829         .id_table = isl29018_id,
830 };
831 module_i2c_driver(isl29018_driver);
832 
833 MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver");
834 MODULE_LICENSE("GPL");
835 

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