Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 1 | /* |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 2 | * A iio driver for the light sensor ISL 29018/29023/29035. |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 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. |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 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> |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 25 | #include <linux/regmap.h> |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 27 | #include <linux/iio/iio.h> |
| 28 | #include <linux/iio/sysfs.h> |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 29 | #include <linux/acpi.h> |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 30 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 31 | #define ISL29018_CONV_TIME_MS 100 |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 32 | |
| 33 | #define ISL29018_REG_ADD_COMMAND1 0x00 |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 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 |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 40 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 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) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 44 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 45 | #define ISL29018_CMD2_RANGE_SHIFT 0 |
| 46 | #define ISL29018_CMD2_RANGE_MASK (0x3 << ISL29018_CMD2_RANGE_SHIFT) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 47 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 48 | #define ISL29018_CMD2_SCHEME_SHIFT 7 |
| 49 | #define ISL29018_CMD2_SCHEME_MASK (0x1 << ISL29018_CMD2_SCHEME_SHIFT) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 50 | |
| 51 | #define ISL29018_REG_ADD_DATA_LSB 0x02 |
| 52 | #define ISL29018_REG_ADD_DATA_MSB 0x03 |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 53 | |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 54 | #define ISL29018_REG_TEST 0x08 |
| 55 | #define ISL29018_TEST_SHIFT 0 |
| 56 | #define ISL29018_TEST_MASK (0xFF << ISL29018_TEST_SHIFT) |
| 57 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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 | |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 98 | struct isl29018_chip { |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 99 | struct regmap *regmap; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 100 | struct mutex lock; |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 101 | int type; |
Roberta Dobrescu | 809a591 | 2015-04-16 22:20:58 +0300 | [diff] [blame] | 102 | unsigned int calibscale; |
| 103 | unsigned int ucalibscale; |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 104 | unsigned int int_time; |
| 105 | struct isl29018_scale scale; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 106 | int prox_scheme; |
Bryan Freed | 1e45cf3 | 2012-10-25 00:39:00 +0100 | [diff] [blame] | 107 | bool suspended; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 110 | static int isl29018_set_integration_time(struct isl29018_chip *chip, |
| 111 | unsigned int utime) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 112 | { |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 113 | int i, ret; |
| 114 | unsigned int int_time, new_int_time; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 115 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 123 | if (i >= ARRAY_SIZE(isl29018_int_utimes[chip->type])) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 124 | return -EINVAL; |
| 125 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 126 | ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2, |
| 127 | ISL29018_CMD2_RESOLUTION_MASK, |
| 128 | i << ISL29018_CMD2_RESOLUTION_SHIFT); |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | |
Peter Meerwald-Stadler | 96273f43 | 2016-07-05 13:55:39 +0200 | [diff] [blame] | 132 | /* Keep the same range when integration time changes */ |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 146 | static int isl29018_set_scale(struct isl29018_chip *chip, int scale, int uscale) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 147 | { |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 148 | int i, ret; |
| 149 | struct isl29018_scale new_scale; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 150 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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]; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 159 | if (i >= ARRAY_SIZE(isl29018_scales[chip->int_time])) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 162 | ret = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2, |
| 163 | ISL29018_CMD2_RANGE_MASK, |
| 164 | i << ISL29018_CMD2_RANGE_SHIFT); |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | |
| 168 | chip->scale = new_scale; |
| 169 | |
| 170 | return 0; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 173 | static int isl29018_read_sensor_input(struct isl29018_chip *chip, int mode) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 174 | { |
| 175 | int status; |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 176 | unsigned int lsb; |
| 177 | unsigned int msb; |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 178 | struct device *dev = regmap_get_device(chip->regmap); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 179 | |
| 180 | /* Set mode */ |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 181 | status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 182 | mode << ISL29018_CMD1_OPMODE_SHIFT); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 183 | if (status) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 184 | dev_err(dev, |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 185 | "Error in setting operating mode err %d\n", status); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 186 | return status; |
| 187 | } |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 188 | msleep(ISL29018_CONV_TIME_MS); |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 189 | status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_LSB, &lsb); |
| 190 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 191 | dev_err(dev, |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 192 | "Error in reading LSB DATA with err %d\n", status); |
| 193 | return status; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 196 | status = regmap_read(chip->regmap, ISL29018_REG_ADD_DATA_MSB, &msb); |
| 197 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 198 | dev_err(dev, |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 199 | "Error in reading MSB DATA with error %d\n", status); |
| 200 | return status; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 201 | } |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 202 | dev_vdbg(dev, "MSB 0x%x and LSB 0x%x\n", msb, lsb); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 203 | |
| 204 | return (msb << 8) | lsb; |
| 205 | } |
| 206 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 207 | static int isl29018_read_lux(struct isl29018_chip *chip, int *lux) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 208 | { |
| 209 | int lux_data; |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 210 | unsigned int data_x_range; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 211 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 212 | lux_data = isl29018_read_sensor_input(chip, |
| 213 | ISL29018_CMD1_OPMODE_ALS_ONCE); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 214 | if (lux_data < 0) |
| 215 | return lux_data; |
| 216 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 225 | static int isl29018_read_ir(struct isl29018_chip *chip, int *ir) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 226 | { |
| 227 | int ir_data; |
| 228 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 229 | ir_data = isl29018_read_sensor_input(chip, |
| 230 | ISL29018_CMD1_OPMODE_IR_ONCE); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 231 | if (ir_data < 0) |
| 232 | return ir_data; |
| 233 | |
| 234 | *ir = ir_data; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 239 | static int isl29018_read_proximity_ir(struct isl29018_chip *chip, int scheme, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 240 | int *near_ir) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 241 | { |
| 242 | int status; |
| 243 | int prox_data = -1; |
| 244 | int ir_data = -1; |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 245 | struct device *dev = regmap_get_device(chip->regmap); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 246 | |
| 247 | /* Do proximity sensing with required scheme */ |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 248 | status = regmap_update_bits(chip->regmap, ISL29018_REG_ADD_COMMAND2, |
| 249 | ISL29018_CMD2_SCHEME_MASK, |
| 250 | scheme << ISL29018_CMD2_SCHEME_SHIFT); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 251 | if (status) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 252 | dev_err(dev, "Error in setting operating mode\n"); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 253 | return status; |
| 254 | } |
| 255 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 256 | prox_data = isl29018_read_sensor_input(chip, |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 257 | ISL29018_CMD1_OPMODE_PROX_ONCE); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 258 | if (prox_data < 0) |
| 259 | return prox_data; |
| 260 | |
| 261 | if (scheme == 1) { |
| 262 | *near_ir = prox_data; |
| 263 | return 0; |
| 264 | } |
| 265 | |
Peter Meerwald-Stadler | 13e6d63 | 2016-07-05 13:55:36 +0200 | [diff] [blame] | 266 | ir_data = isl29018_read_sensor_input(chip, |
| 267 | ISL29018_CMD1_OPMODE_IR_ONCE); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 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 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 279 | static ssize_t isl29018_show_scale_available(struct device *dev, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 280 | struct device_attribute *attr, char *buf) |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 296 | static ssize_t isl29018_show_int_time_available(struct device *dev, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 297 | struct device_attribute *attr, char *buf) |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 312 | static ssize_t isl29018_show_prox_infrared_suppression(struct device *dev, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 313 | struct device_attribute *attr, |
| 314 | char *buf) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 315 | { |
Lars-Peter Clausen | 96f691f | 2012-05-12 15:39:51 +0200 | [diff] [blame] | 316 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 927afbe | 2011-06-27 13:07:57 +0100 | [diff] [blame] | 317 | struct isl29018_chip *chip = iio_priv(indio_dev); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 318 | |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 319 | /* |
Peter Meerwald-Stadler | 96273f43 | 2016-07-05 13:55:39 +0200 | [diff] [blame] | 320 | * Return the "proximity scheme" i.e. if the chip does on chip |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 321 | * infrared suppression (1 means perform on chip suppression) |
| 322 | */ |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 323 | return sprintf(buf, "%d\n", chip->prox_scheme); |
| 324 | } |
| 325 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 326 | static ssize_t isl29018_store_prox_infrared_suppression(struct device *dev, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 327 | struct device_attribute *attr, |
| 328 | const char *buf, size_t count) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 329 | { |
Lars-Peter Clausen | 96f691f | 2012-05-12 15:39:51 +0200 | [diff] [blame] | 330 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 927afbe | 2011-06-27 13:07:57 +0100 | [diff] [blame] | 331 | struct isl29018_chip *chip = iio_priv(indio_dev); |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 332 | int val; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 333 | |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 334 | if (kstrtoint(buf, 10, &val)) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 335 | return -EINVAL; |
Peter Meerwald-Stadler | ab5b949 | 2016-07-05 13:55:37 +0200 | [diff] [blame] | 336 | if (!(val == 0 || val == 1)) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 337 | return -EINVAL; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 338 | |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 339 | /* |
Peter Meerwald-Stadler | 96273f43 | 2016-07-05 13:55:39 +0200 | [diff] [blame] | 340 | * Get the "proximity scheme" i.e. if the chip does on chip |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 341 | * infrared suppression (1 means perform on chip suppression) |
| 342 | */ |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 343 | mutex_lock(&chip->lock); |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 344 | chip->prox_scheme = val; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 345 | mutex_unlock(&chip->lock); |
| 346 | |
| 347 | return count; |
| 348 | } |
| 349 | |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 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) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 355 | { |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 356 | struct isl29018_chip *chip = iio_priv(indio_dev); |
| 357 | int ret = -EINVAL; |
| 358 | |
| 359 | mutex_lock(&chip->lock); |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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: |
Jonathan Cameron | b91617e | 2015-05-02 12:05:05 +0100 | [diff] [blame] | 369 | if (chan->type == IIO_LIGHT) { |
Cristina Moraru | d59b7b6 | 2015-10-20 22:55:40 +0300 | [diff] [blame] | 370 | if (val) { |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 371 | mutex_unlock(&chip->lock); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | ret = isl29018_set_integration_time(chip, val2); |
Jonathan Cameron | b91617e | 2015-05-02 12:05:05 +0100 | [diff] [blame] | 375 | } |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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; |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 383 | } |
| 384 | mutex_unlock(&chip->lock); |
| 385 | |
Wei Yongjun | 6dc973d | 2012-11-08 09:05:00 +0000 | [diff] [blame] | 386 | return ret; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 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) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 394 | { |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 395 | int ret = -EINVAL; |
| 396 | struct isl29018_chip *chip = iio_priv(indio_dev); |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 397 | |
| 398 | mutex_lock(&chip->lock); |
Bryan Freed | 1e45cf3 | 2012-10-25 00:39:00 +0100 | [diff] [blame] | 399 | if (chip->suspended) { |
| 400 | mutex_unlock(&chip->lock); |
| 401 | return -EBUSY; |
| 402 | } |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 403 | switch (mask) { |
Jonathan Cameron | 90354d0 | 2012-04-15 17:41:22 +0100 | [diff] [blame] | 404 | case IIO_CHAN_INFO_RAW: |
| 405 | case IIO_CHAN_INFO_PROCESSED: |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 406 | switch (chan->type) { |
| 407 | case IIO_LIGHT: |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 408 | ret = isl29018_read_lux(chip, val); |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 409 | break; |
| 410 | case IIO_INTENSITY: |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 411 | ret = isl29018_read_ir(chip, val); |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 412 | break; |
| 413 | case IIO_PROXIMITY: |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 414 | ret = isl29018_read_proximity_ir(chip, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 415 | chip->prox_scheme, |
| 416 | val); |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 417 | break; |
| 418 | default: |
| 419 | break; |
| 420 | } |
| 421 | if (!ret) |
| 422 | ret = IIO_VAL_INT; |
| 423 | break; |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 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; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 438 | case IIO_CHAN_INFO_CALIBSCALE: |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 439 | if (chan->type == IIO_LIGHT) { |
Roberta Dobrescu | 809a591 | 2015-04-16 22:20:58 +0300 | [diff] [blame] | 440 | *val = chip->calibscale; |
| 441 | *val2 = chip->ucalibscale; |
Bryan Freed | 932323b | 2012-09-05 20:55:00 +0100 | [diff] [blame] | 442 | ret = IIO_VAL_INT_PLUS_MICRO; |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 443 | } |
| 444 | break; |
| 445 | default: |
| 446 | break; |
| 447 | } |
| 448 | mutex_unlock(&chip->lock); |
| 449 | return ret; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 452 | #define ISL29018_LIGHT_CHANNEL { \ |
| 453 | .type = IIO_LIGHT, \ |
| 454 | .indexed = 1, \ |
| 455 | .channel = 0, \ |
| 456 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | \ |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 457 | BIT(IIO_CHAN_INFO_CALIBSCALE) | \ |
| 458 | BIT(IIO_CHAN_INFO_SCALE) | \ |
| 459 | BIT(IIO_CHAN_INFO_INT_TIME), \ |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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 | |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 474 | static const struct iio_chan_spec isl29018_channels[] = { |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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, |
Bryan Freed | 01e57c5 | 2011-07-07 12:01:56 -0700 | [diff] [blame] | 483 | }; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 484 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 485 | static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, S_IRUGO, |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 486 | isl29018_show_int_time_available, NULL, 0); |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 487 | static IIO_DEVICE_ATTR(in_illuminance_scale_available, S_IRUGO, |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 488 | isl29018_show_scale_available, NULL, 0); |
Peter Meerwald | 2a1d45e | 2012-06-18 20:33:07 +0200 | [diff] [blame] | 489 | static IIO_DEVICE_ATTR(proximity_on_chip_ambient_infrared_suppression, |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 490 | S_IRUGO | S_IWUSR, |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 491 | isl29018_show_prox_infrared_suppression, |
| 492 | isl29018_store_prox_infrared_suppression, 0); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 493 | |
| 494 | #define ISL29018_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr) |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 495 | |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 496 | static struct attribute *isl29018_attributes[] = { |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 497 | ISL29018_DEV_ATTR(in_illuminance_scale_available), |
| 498 | ISL29018_DEV_ATTR(in_illuminance_integration_time_available), |
Peter Meerwald | 2a1d45e | 2012-06-18 20:33:07 +0200 | [diff] [blame] | 499 | ISL29018_DEV_ATTR(proximity_on_chip_ambient_infrared_suppression), |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 500 | NULL |
| 501 | }; |
| 502 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 503 | static struct attribute *isl29023_attributes[] = { |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 504 | ISL29018_DEV_ATTR(in_illuminance_scale_available), |
| 505 | ISL29018_DEV_ATTR(in_illuminance_integration_time_available), |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 506 | NULL |
| 507 | }; |
| 508 | |
Laurentiu Palcu | 5b4b5b9c | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 509 | static const struct attribute_group isl29018_group = { |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 510 | .attrs = isl29018_attributes, |
| 511 | }; |
| 512 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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; |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 521 | struct device *dev = regmap_get_device(chip->regmap); |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 522 | |
| 523 | status = regmap_read(chip->regmap, ISL29035_REG_DEVICE_ID, &id); |
| 524 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 525 | dev_err(dev, |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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 | |
Peter Meerwald-Stadler | 96273f43 | 2016-07-05 13:55:39 +0200 | [diff] [blame] | 536 | /* Clear brownout bit */ |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 547 | static int isl29018_chip_init(struct isl29018_chip *chip) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 548 | { |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 549 | int status; |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 550 | struct device *dev = regmap_get_device(chip->regmap); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 551 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 552 | if (chip->type == isl29035) { |
| 553 | status = isl29035_detect(chip); |
| 554 | if (status < 0) |
| 555 | return status; |
| 556 | } |
| 557 | |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 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 | * ... |
Peter Meerwald-Stadler | 96273f43 | 2016-07-05 13:55:39 +0200 | [diff] [blame] | 569 | * For ISL29011, ISL29018, ISL29021, ISL29023 |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 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 | */ |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 578 | status = regmap_write(chip->regmap, ISL29018_REG_TEST, 0x0); |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 579 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 580 | dev_err(dev, "Failed to clear isl29018 TEST reg.(%d)\n", |
Roberta Dobrescu | ad3e646 | 2014-09-25 20:09:09 +0300 | [diff] [blame] | 581 | status); |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 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 | */ |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 589 | status = regmap_write(chip->regmap, ISL29018_REG_ADD_COMMAND1, 0); |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 590 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 591 | dev_err(dev, "Failed to clear isl29018 CMD1 reg.(%d)\n", |
Roberta Dobrescu | ad3e646 | 2014-09-25 20:09:09 +0300 | [diff] [blame] | 592 | status); |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 593 | return status; |
| 594 | } |
| 595 | |
Vaishali Thakkar | 890d228 | 2014-09-23 09:22:30 +0530 | [diff] [blame] | 596 | usleep_range(1000, 2000); /* per data sheet, page 10 */ |
Grant Grundler | 176f9f2 | 2011-08-09 15:18:14 -0700 | [diff] [blame] | 597 | |
Peter Meerwald-Stadler | 96273f43 | 2016-07-05 13:55:39 +0200 | [diff] [blame] | 598 | /* Set defaults */ |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 599 | status = isl29018_set_scale(chip, chip->scale.scale, |
| 600 | chip->scale.uscale); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 601 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 602 | dev_err(dev, "Init of isl29018 fails\n"); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 603 | return status; |
| 604 | } |
| 605 | |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 606 | status = isl29018_set_integration_time(chip, |
| 607 | isl29018_int_utimes[chip->type][chip->int_time]); |
| 608 | if (status < 0) { |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 609 | dev_err(dev, "Init of isl29018 fails\n"); |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 610 | return status; |
| 611 | } |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Laurentiu Palcu | 5b4b5b9c | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 616 | static const struct iio_info isl29018_info = { |
| 617 | .attrs = &isl29018_group, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 618 | .driver_module = THIS_MODULE, |
Bhumika Goyal | 6d9b10c | 2016-02-12 20:40:27 +0530 | [diff] [blame] | 619 | .read_raw = isl29018_read_raw, |
| 620 | .write_raw = isl29018_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 621 | }; |
| 622 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 623 | static const struct iio_info isl29023_info = { |
| 624 | .attrs = &isl29023_group, |
| 625 | .driver_module = THIS_MODULE, |
Bhumika Goyal | 6d9b10c | 2016-02-12 20:40:27 +0530 | [diff] [blame] | 626 | .read_raw = isl29018_read_raw, |
| 627 | .write_raw = isl29018_write_raw, |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 628 | }; |
| 629 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 630 | static bool isl29018_is_volatile_reg(struct device *dev, unsigned int reg) |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 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: |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 637 | case ISL29035_REG_DEVICE_ID: |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 638 | return true; |
| 639 | default: |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 644 | static const struct regmap_config isl29018_regmap_config = { |
| 645 | .reg_bits = 8, |
| 646 | .val_bits = 8, |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 647 | .volatile_reg = isl29018_is_volatile_reg, |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 648 | .max_register = ISL29018_REG_TEST, |
| 649 | .num_reg_defaults_raw = ISL29018_REG_TEST + 1, |
| 650 | .cache_type = REGCACHE_RBTREE, |
| 651 | }; |
| 652 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 653 | static const struct regmap_config isl29035_regmap_config = { |
| 654 | .reg_bits = 8, |
| 655 | .val_bits = 8, |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 656 | .volatile_reg = isl29018_is_volatile_reg, |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 657 | .max_register = ISL29035_REG_DEVICE_ID, |
| 658 | .num_reg_defaults_raw = ISL29035_REG_DEVICE_ID + 1, |
| 659 | .cache_type = REGCACHE_RBTREE, |
| 660 | }; |
| 661 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 662 | struct isl29018_chip_info { |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 669 | static const struct isl29018_chip_info isl29018_chip_info_tbl[] = { |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 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 | |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 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 | |
Eva Rachel Retuya | 79783b3 | 2016-02-18 15:35:37 +0800 | [diff] [blame] | 699 | *data = (int)id->driver_data; |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 700 | |
| 701 | return dev_name(dev); |
| 702 | } |
| 703 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 704 | static int isl29018_probe(struct i2c_client *client, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 705 | const struct i2c_device_id *id) |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 706 | { |
| 707 | struct isl29018_chip *chip; |
Jonathan Cameron | 927afbe | 2011-06-27 13:07:57 +0100 | [diff] [blame] | 708 | struct iio_dev *indio_dev; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 709 | int err; |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 710 | const char *name = NULL; |
| 711 | int dev_id = 0; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 712 | |
Sachin Kamat | e434dcf | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 713 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
Peter Meerwald-Stadler | ab5b949 | 2016-07-05 13:55:37 +0200 | [diff] [blame] | 714 | if (!indio_dev) |
Sachin Kamat | e434dcf | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 715 | return -ENOMEM; |
Jonathan Cameron | 927afbe | 2011-06-27 13:07:57 +0100 | [diff] [blame] | 716 | chip = iio_priv(indio_dev); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 717 | |
Jonathan Cameron | 927afbe | 2011-06-27 13:07:57 +0100 | [diff] [blame] | 718 | i2c_set_clientdata(client, indio_dev); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 719 | |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 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 | |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 728 | mutex_init(&chip->lock); |
| 729 | |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 730 | chip->type = dev_id; |
Roberta Dobrescu | 809a591 | 2015-04-16 22:20:58 +0300 | [diff] [blame] | 731 | chip->calibscale = 1; |
| 732 | chip->ucalibscale = 0; |
Roberta Dobrescu | 6920ccf | 2015-04-16 22:20:59 +0300 | [diff] [blame] | 733 | chip->int_time = ISL29018_INT_TIME_16; |
| 734 | chip->scale = isl29018_scales[chip->int_time][0]; |
Bryan Freed | 1e45cf3 | 2012-10-25 00:39:00 +0100 | [diff] [blame] | 735 | chip->suspended = false; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 736 | |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 737 | chip->regmap = devm_regmap_init_i2c(client, |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 738 | isl29018_chip_info_tbl[dev_id].regmap_cfg); |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 739 | if (IS_ERR(chip->regmap)) { |
| 740 | err = PTR_ERR(chip->regmap); |
Alison Schofield | 6467086 | 2016-02-22 12:34:32 -0800 | [diff] [blame] | 741 | dev_err(&client->dev, "regmap initialization fails: %d\n", err); |
Sachin Kamat | e434dcf | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 742 | return err; |
Laxman Dewangan | 057340e | 2012-04-20 12:57:38 +0530 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | err = isl29018_chip_init(chip); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 746 | if (err) |
Sachin Kamat | e434dcf | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 747 | return err; |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 748 | |
Peter Meerwald-Stadler | 9219376 | 2016-07-05 13:55:40 +0200 | [diff] [blame] | 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; |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 752 | indio_dev->name = name; |
Jonathan Cameron | 927afbe | 2011-06-27 13:07:57 +0100 | [diff] [blame] | 753 | indio_dev->dev.parent = &client->dev; |
| 754 | indio_dev->modes = INDIO_DIRECT_MODE; |
Peter Meerwald-Stadler | ab5b949 | 2016-07-05 13:55:37 +0200 | [diff] [blame] | 755 | return devm_iio_device_register(&client->dev, indio_dev); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Bryan Freed | 1e45cf3 | 2012-10-25 00:39:00 +0100 | [diff] [blame] | 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 | |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 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 | |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 804 | static const struct i2c_device_id isl29018_id[] = { |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 805 | {"isl29018", isl29018}, |
| 806 | {"isl29023", isl29023}, |
| 807 | {"isl29035", isl29035}, |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 808 | {} |
| 809 | }; |
| 810 | |
| 811 | MODULE_DEVICE_TABLE(i2c, isl29018_id); |
| 812 | |
Olof Johansson | 4ee1952 | 2011-12-22 18:44:43 -0800 | [diff] [blame] | 813 | static const struct of_device_id isl29018_of_match[] = { |
Laxman Dewangan | 610202d | 2012-04-24 11:41:38 +0530 | [diff] [blame] | 814 | { .compatible = "isil,isl29018", }, |
Laurentiu Palcu | 609acef | 2014-08-29 15:26:00 +0100 | [diff] [blame] | 815 | { .compatible = "isil,isl29023", }, |
| 816 | { .compatible = "isil,isl29035", }, |
Olof Johansson | 4ee1952 | 2011-12-22 18:44:43 -0800 | [diff] [blame] | 817 | { }, |
| 818 | }; |
| 819 | MODULE_DEVICE_TABLE(of, isl29018_of_match); |
| 820 | |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 821 | static struct i2c_driver isl29018_driver = { |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 822 | .driver = { |
| 823 | .name = "isl29018", |
Laurentiu Palcu | dc4ecaf | 2014-01-09 10:20:00 +0000 | [diff] [blame] | 824 | .acpi_match_table = ACPI_PTR(isl29018_acpi_match), |
Bryan Freed | 1e45cf3 | 2012-10-25 00:39:00 +0100 | [diff] [blame] | 825 | .pm = ISL29018_PM_OPS, |
Olof Johansson | 4ee1952 | 2011-12-22 18:44:43 -0800 | [diff] [blame] | 826 | .of_match_table = isl29018_of_match, |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 827 | }, |
| 828 | .probe = isl29018_probe, |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 829 | .id_table = isl29018_id, |
| 830 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 831 | module_i2c_driver(isl29018_driver); |
Rhyland Klein | 9404287 | 2010-10-07 15:48:09 -0700 | [diff] [blame] | 832 | |
| 833 | MODULE_DESCRIPTION("ISL29018 Ambient Light Sensor driver"); |
| 834 | MODULE_LICENSE("GPL"); |