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

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

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