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