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