Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 2 | /* |
| 3 | * IIO driver for the light sensor ISL29028. |
| 4 | * ISL29028 is Concurrent Ambient Light and Proximity Sensor |
| 5 | * |
| 6 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Brian Masney | bcbbd0f | 2017-02-08 20:54:29 -0500 | [diff] [blame] | 7 | * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org> |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 8 | * |
Sebastian Reichel | e530cc8 | 2017-04-28 17:55:58 +0200 | [diff] [blame] | 9 | * Datasheets: |
| 10 | * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29028.pdf |
| 11 | * - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29030.pdf |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/regmap.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/sysfs.h> |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 24 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 25 | #define ISL29028_CONV_TIME_MS 100 |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 26 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 27 | #define ISL29028_REG_CONFIGURE 0x01 |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 28 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 29 | #define ISL29028_CONF_ALS_IR_MODE_ALS 0 |
| 30 | #define ISL29028_CONF_ALS_IR_MODE_IR BIT(0) |
| 31 | #define ISL29028_CONF_ALS_IR_MODE_MASK BIT(0) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 32 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 33 | #define ISL29028_CONF_ALS_RANGE_LOW_LUX 0 |
Peter Meerwald-Stadler | 3a37f1c | 2016-07-05 13:55:41 +0200 | [diff] [blame] | 34 | #define ISL29028_CONF_ALS_RANGE_HIGH_LUX BIT(1) |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 35 | #define ISL29028_CONF_ALS_RANGE_MASK BIT(1) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 36 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 37 | #define ISL29028_CONF_ALS_DIS 0 |
| 38 | #define ISL29028_CONF_ALS_EN BIT(2) |
| 39 | #define ISL29028_CONF_ALS_EN_MASK BIT(2) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 40 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 41 | #define ISL29028_CONF_PROX_SLP_SH 4 |
| 42 | #define ISL29028_CONF_PROX_SLP_MASK (7 << ISL29028_CONF_PROX_SLP_SH) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 43 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 44 | #define ISL29028_CONF_PROX_EN BIT(7) |
| 45 | #define ISL29028_CONF_PROX_EN_MASK BIT(7) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 46 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 47 | #define ISL29028_REG_INTERRUPT 0x02 |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 48 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 49 | #define ISL29028_REG_PROX_DATA 0x08 |
| 50 | #define ISL29028_REG_ALSIR_L 0x09 |
| 51 | #define ISL29028_REG_ALSIR_U 0x0A |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 52 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 53 | #define ISL29028_REG_TEST1_MODE 0x0E |
| 54 | #define ISL29028_REG_TEST2_MODE 0x0F |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 55 | |
Brian Masney | 29e90cd | 2016-12-03 21:19:29 -0500 | [diff] [blame] | 56 | #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 57 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 58 | #define ISL29028_POWER_OFF_DELAY_MS 2000 |
| 59 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 60 | struct isl29028_prox_data { |
| 61 | int sampling_int; |
| 62 | int sampling_fract; |
| 63 | int sleep_time; |
| 64 | }; |
| 65 | |
| 66 | static const struct isl29028_prox_data isl29028_prox_data[] = { |
| 67 | { 1, 250000, 800 }, |
| 68 | { 2, 500000, 400 }, |
| 69 | { 5, 0, 200 }, |
| 70 | { 10, 0, 100 }, |
| 71 | { 13, 300000, 75 }, |
| 72 | { 20, 0, 50 }, |
| 73 | { 80, 0, 13 }, /* |
| 74 | * Note: Data sheet lists 12.5 ms sleep time. |
| 75 | * Round up a half millisecond for msleep(). |
| 76 | */ |
| 77 | { 100, 0, 0 } |
| 78 | }; |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 79 | |
Peter Meerwald-Stadler | bafaa6d | 2016-07-05 13:55:42 +0200 | [diff] [blame] | 80 | enum isl29028_als_ir_mode { |
| 81 | ISL29028_MODE_NONE = 0, |
| 82 | ISL29028_MODE_ALS, |
| 83 | ISL29028_MODE_IR, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct isl29028_chip { |
Brian Masney | 6db2fa0 | 2017-01-17 04:24:48 -0500 | [diff] [blame] | 87 | struct mutex lock; |
| 88 | struct regmap *regmap; |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 89 | int prox_sampling_int; |
| 90 | int prox_sampling_frac; |
Brian Masney | 6db2fa0 | 2017-01-17 04:24:48 -0500 | [diff] [blame] | 91 | bool enable_prox; |
Brian Masney | 6db2fa0 | 2017-01-17 04:24:48 -0500 | [diff] [blame] | 92 | int lux_scale; |
Peter Meerwald-Stadler | bafaa6d | 2016-07-05 13:55:42 +0200 | [diff] [blame] | 93 | enum isl29028_als_ir_mode als_ir_mode; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 94 | }; |
| 95 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 96 | static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract) |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 97 | { |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 98 | int i; |
| 99 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 100 | for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) { |
| 101 | if (isl29028_prox_data[i].sampling_int == sampling_int && |
| 102 | isl29028_prox_data[i].sampling_fract == sampling_fract) |
| 103 | return i; |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 106 | return -EINVAL; |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 109 | static int isl29028_set_proxim_sampling(struct isl29028_chip *chip, |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 110 | int sampling_int, int sampling_fract) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 111 | { |
Brian Masney | ae1e523 | 2017-01-17 04:24:53 -0500 | [diff] [blame] | 112 | struct device *dev = regmap_get_device(chip->regmap); |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 113 | int sleep_index, ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 114 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 115 | sleep_index = isl29028_find_prox_sleep_index(sampling_int, |
| 116 | sampling_fract); |
| 117 | if (sleep_index < 0) |
| 118 | return sleep_index; |
| 119 | |
Brian Masney | ae1e523 | 2017-01-17 04:24:53 -0500 | [diff] [blame] | 120 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
| 121 | ISL29028_CONF_PROX_SLP_MASK, |
Brian Masney | 45e2852 | 2017-02-08 20:54:26 -0500 | [diff] [blame] | 122 | sleep_index << ISL29028_CONF_PROX_SLP_SH); |
Brian Masney | ae1e523 | 2017-01-17 04:24:53 -0500 | [diff] [blame] | 123 | |
| 124 | if (ret < 0) { |
| 125 | dev_err(dev, "%s(): Error %d setting the proximity sampling\n", |
| 126 | __func__, ret); |
| 127 | return ret; |
| 128 | } |
| 129 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 130 | chip->prox_sampling_int = sampling_int; |
| 131 | chip->prox_sampling_frac = sampling_fract; |
Brian Masney | ae1e523 | 2017-01-17 04:24:53 -0500 | [diff] [blame] | 132 | |
| 133 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 134 | } |
| 135 | |
Brian Masney | 6766342 | 2017-01-17 04:24:59 -0500 | [diff] [blame] | 136 | static int isl29028_enable_proximity(struct isl29028_chip *chip) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 137 | { |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 138 | int prox_index, ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 139 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 140 | ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int, |
| 141 | chip->prox_sampling_frac); |
Brian Masney | 84a7669 | 2017-01-17 04:25:00 -0500 | [diff] [blame] | 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 145 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
Brian Masney | 6766342 | 2017-01-17 04:24:59 -0500 | [diff] [blame] | 146 | ISL29028_CONF_PROX_EN_MASK, |
| 147 | ISL29028_CONF_PROX_EN); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 148 | if (ret < 0) |
| 149 | return ret; |
| 150 | |
| 151 | /* Wait for conversion to be complete for first sample */ |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 152 | prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int, |
| 153 | chip->prox_sampling_frac); |
| 154 | if (prox_index < 0) |
| 155 | return prox_index; |
| 156 | |
| 157 | msleep(isl29028_prox_data[prox_index].sleep_time); |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 158 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale) |
| 163 | { |
Brian Masney | bcf5104 | 2017-01-17 04:24:54 -0500 | [diff] [blame] | 164 | struct device *dev = regmap_get_device(chip->regmap); |
Peter Meerwald-Stadler | 3a37f1c | 2016-07-05 13:55:41 +0200 | [diff] [blame] | 165 | int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX : |
| 166 | ISL29028_CONF_ALS_RANGE_LOW_LUX; |
Brian Masney | bcf5104 | 2017-01-17 04:24:54 -0500 | [diff] [blame] | 167 | int ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 168 | |
Brian Masney | bcf5104 | 2017-01-17 04:24:54 -0500 | [diff] [blame] | 169 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
| 170 | ISL29028_CONF_ALS_RANGE_MASK, val); |
| 171 | if (ret < 0) { |
| 172 | dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__, |
| 173 | ret); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | chip->lux_scale = lux_scale; |
| 178 | |
| 179 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int isl29028_set_als_ir_mode(struct isl29028_chip *chip, |
Peter Meerwald-Stadler | bafaa6d | 2016-07-05 13:55:42 +0200 | [diff] [blame] | 183 | enum isl29028_als_ir_mode mode) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 184 | { |
Brian Masney | 0fac96ed | 2017-01-17 04:25:01 -0500 | [diff] [blame] | 185 | int ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 186 | |
Brian Masney | f40531d | 2016-12-03 21:19:25 -0500 | [diff] [blame] | 187 | if (chip->als_ir_mode == mode) |
| 188 | return 0; |
| 189 | |
Brian Masney | 0fac96ed | 2017-01-17 04:25:01 -0500 | [diff] [blame] | 190 | ret = isl29028_set_als_scale(chip, chip->lux_scale); |
| 191 | if (ret < 0) |
| 192 | return ret; |
| 193 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 194 | switch (mode) { |
Peter Meerwald-Stadler | bafaa6d | 2016-07-05 13:55:42 +0200 | [diff] [blame] | 195 | case ISL29028_MODE_ALS: |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 196 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
Peter Meerwald-Stadler | 3a37f1c | 2016-07-05 13:55:41 +0200 | [diff] [blame] | 197 | ISL29028_CONF_ALS_IR_MODE_MASK, |
| 198 | ISL29028_CONF_ALS_IR_MODE_ALS); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 199 | if (ret < 0) |
| 200 | return ret; |
| 201 | |
| 202 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
Peter Meerwald-Stadler | 3a37f1c | 2016-07-05 13:55:41 +0200 | [diff] [blame] | 203 | ISL29028_CONF_ALS_RANGE_MASK, |
| 204 | ISL29028_CONF_ALS_RANGE_HIGH_LUX); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 205 | break; |
Peter Meerwald-Stadler | bafaa6d | 2016-07-05 13:55:42 +0200 | [diff] [blame] | 206 | case ISL29028_MODE_IR: |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 207 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
Peter Meerwald-Stadler | 3a37f1c | 2016-07-05 13:55:41 +0200 | [diff] [blame] | 208 | ISL29028_CONF_ALS_IR_MODE_MASK, |
| 209 | ISL29028_CONF_ALS_IR_MODE_IR); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 210 | break; |
Peter Meerwald-Stadler | bafaa6d | 2016-07-05 13:55:42 +0200 | [diff] [blame] | 211 | case ISL29028_MODE_NONE: |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 212 | return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
Brian Masney | c4f6092 | 2017-01-17 04:24:49 -0500 | [diff] [blame] | 213 | ISL29028_CONF_ALS_EN_MASK, |
| 214 | ISL29028_CONF_ALS_DIS); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | |
| 220 | /* Enable the ALS/IR */ |
| 221 | ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE, |
Peter Meerwald-Stadler | 3a37f1c | 2016-07-05 13:55:41 +0200 | [diff] [blame] | 222 | ISL29028_CONF_ALS_EN_MASK, |
| 223 | ISL29028_CONF_ALS_EN); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 224 | if (ret < 0) |
| 225 | return ret; |
| 226 | |
| 227 | /* Need to wait for conversion time if ALS/IR mode enabled */ |
Brian Masney | 55bf851 | 2017-02-08 20:54:25 -0500 | [diff] [blame] | 228 | msleep(ISL29028_CONV_TIME_MS); |
Brian Masney | f40531d | 2016-12-03 21:19:25 -0500 | [diff] [blame] | 229 | |
| 230 | chip->als_ir_mode = mode; |
| 231 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir) |
| 236 | { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 237 | struct device *dev = regmap_get_device(chip->regmap); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 238 | unsigned int lsb; |
| 239 | unsigned int msb; |
| 240 | int ret; |
| 241 | |
| 242 | ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb); |
| 243 | if (ret < 0) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 244 | dev_err(dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 245 | "%s(): Error %d reading register ALSIR_L\n", |
| 246 | __func__, ret); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb); |
| 251 | if (ret < 0) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 252 | dev_err(dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 253 | "%s(): Error %d reading register ALSIR_U\n", |
| 254 | __func__, ret); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF); |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 259 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox) |
| 264 | { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 265 | struct device *dev = regmap_get_device(chip->regmap); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 266 | unsigned int data; |
| 267 | int ret; |
| 268 | |
Brian Masney | b44b1f3 | 2017-01-17 04:24:50 -0500 | [diff] [blame] | 269 | if (!chip->enable_prox) { |
Brian Masney | 6766342 | 2017-01-17 04:24:59 -0500 | [diff] [blame] | 270 | ret = isl29028_enable_proximity(chip); |
Brian Masney | b44b1f3 | 2017-01-17 04:24:50 -0500 | [diff] [blame] | 271 | if (ret < 0) |
| 272 | return ret; |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 273 | |
Brian Masney | b44b1f3 | 2017-01-17 04:24:50 -0500 | [diff] [blame] | 274 | chip->enable_prox = true; |
| 275 | } |
| 276 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 277 | ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data); |
| 278 | if (ret < 0) { |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 279 | dev_err(dev, "%s(): Error %d reading register PROX_DATA\n", |
| 280 | __func__, ret); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 281 | return ret; |
| 282 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 283 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 284 | *prox = data; |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 285 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 289 | static int isl29028_als_get(struct isl29028_chip *chip, int *als_data) |
| 290 | { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 291 | struct device *dev = regmap_get_device(chip->regmap); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 292 | int ret; |
| 293 | int als_ir_data; |
| 294 | |
Brian Masney | f40531d | 2016-12-03 21:19:25 -0500 | [diff] [blame] | 295 | ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS); |
| 296 | if (ret < 0) { |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 297 | dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__, |
| 298 | ret); |
Brian Masney | f40531d | 2016-12-03 21:19:25 -0500 | [diff] [blame] | 299 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | ret = isl29028_read_als_ir(chip, &als_ir_data); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | /* |
| 307 | * convert als data count to lux. |
| 308 | * if lux_scale = 125, lux = count * 0.031 |
| 309 | * if lux_scale = 2000, lux = count * 0.49 |
| 310 | */ |
| 311 | if (chip->lux_scale == 125) |
| 312 | als_ir_data = (als_ir_data * 31) / 1000; |
| 313 | else |
| 314 | als_ir_data = (als_ir_data * 49) / 100; |
| 315 | |
| 316 | *als_data = als_ir_data; |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 317 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data) |
| 322 | { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 323 | struct device *dev = regmap_get_device(chip->regmap); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 324 | int ret; |
| 325 | |
Brian Masney | f40531d | 2016-12-03 21:19:25 -0500 | [diff] [blame] | 326 | ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR); |
| 327 | if (ret < 0) { |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 328 | dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__, |
| 329 | ret); |
Brian Masney | f40531d | 2016-12-03 21:19:25 -0500 | [diff] [blame] | 330 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 331 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 332 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 333 | return isl29028_read_als_ir(chip, ir_data); |
| 334 | } |
| 335 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 336 | static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on) |
| 337 | { |
| 338 | struct device *dev = regmap_get_device(chip->regmap); |
| 339 | int ret; |
| 340 | |
| 341 | if (on) { |
| 342 | ret = pm_runtime_get_sync(dev); |
| 343 | if (ret < 0) |
| 344 | pm_runtime_put_noidle(dev); |
| 345 | } else { |
| 346 | pm_runtime_mark_last_busy(dev); |
| 347 | ret = pm_runtime_put_autosuspend(dev); |
| 348 | } |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 353 | /* Channel IO */ |
| 354 | static int isl29028_write_raw(struct iio_dev *indio_dev, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 355 | struct iio_chan_spec const *chan, |
| 356 | int val, int val2, long mask) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 357 | { |
| 358 | struct isl29028_chip *chip = iio_priv(indio_dev); |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 359 | struct device *dev = regmap_get_device(chip->regmap); |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 360 | int ret; |
| 361 | |
| 362 | ret = isl29028_set_pm_runtime_busy(chip, true); |
| 363 | if (ret < 0) |
| 364 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 365 | |
| 366 | mutex_lock(&chip->lock); |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 367 | |
| 368 | ret = -EINVAL; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 369 | switch (chan->type) { |
| 370 | case IIO_PROXIMITY: |
Laxman Dewangan | 892cb6d | 2012-04-16 21:27:42 +0530 | [diff] [blame] | 371 | if (mask != IIO_CHAN_INFO_SAMP_FREQ) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 372 | dev_err(dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 373 | "%s(): proximity: Mask value 0x%08lx is not supported\n", |
| 374 | __func__, mask); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 375 | break; |
| 376 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 377 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 378 | if (val < 1 || val > 100) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 379 | dev_err(dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 380 | "%s(): proximity: Sampling frequency %d is not in the range [1:100]\n", |
| 381 | __func__, val); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 382 | break; |
| 383 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 384 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 385 | ret = isl29028_set_proxim_sampling(chip, val, val2); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 386 | break; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 387 | case IIO_LIGHT: |
Laxman Dewangan | 892cb6d | 2012-04-16 21:27:42 +0530 | [diff] [blame] | 388 | if (mask != IIO_CHAN_INFO_SCALE) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 389 | dev_err(dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 390 | "%s(): light: Mask value 0x%08lx is not supported\n", |
| 391 | __func__, mask); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 392 | break; |
| 393 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 394 | |
Brian Masney | 71a0a64 | 2017-01-17 04:24:58 -0500 | [diff] [blame] | 395 | if (val != 125 && val != 2000) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 396 | dev_err(dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 397 | "%s(): light: Lux scale %d is not in the set {125, 2000}\n", |
| 398 | __func__, val); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 399 | break; |
| 400 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 401 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 402 | ret = isl29028_set_als_scale(chip, val); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 403 | break; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 404 | default: |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 405 | dev_err(dev, "%s(): Unsupported channel type %x\n", |
| 406 | __func__, chan->type); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 407 | break; |
| 408 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 409 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 410 | mutex_unlock(&chip->lock); |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 411 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 412 | if (ret < 0) |
| 413 | return ret; |
| 414 | |
| 415 | ret = isl29028_set_pm_runtime_busy(chip, false); |
| 416 | if (ret < 0) |
| 417 | return ret; |
| 418 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static int isl29028_read_raw(struct iio_dev *indio_dev, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 423 | struct iio_chan_spec const *chan, |
| 424 | int *val, int *val2, long mask) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 425 | { |
| 426 | struct isl29028_chip *chip = iio_priv(indio_dev); |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 427 | struct device *dev = regmap_get_device(chip->regmap); |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 428 | int ret, pm_ret; |
| 429 | |
| 430 | ret = isl29028_set_pm_runtime_busy(chip, true); |
| 431 | if (ret < 0) |
| 432 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 433 | |
| 434 | mutex_lock(&chip->lock); |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 435 | |
| 436 | ret = -EINVAL; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 437 | switch (mask) { |
Jonathan Cameron | a8c0ed7 | 2012-04-15 17:41:29 +0100 | [diff] [blame] | 438 | case IIO_CHAN_INFO_RAW: |
| 439 | case IIO_CHAN_INFO_PROCESSED: |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 440 | switch (chan->type) { |
| 441 | case IIO_LIGHT: |
| 442 | ret = isl29028_als_get(chip, val); |
| 443 | break; |
| 444 | case IIO_INTENSITY: |
| 445 | ret = isl29028_ir_get(chip, val); |
| 446 | break; |
| 447 | case IIO_PROXIMITY: |
Brian Masney | b44b1f3 | 2017-01-17 04:24:50 -0500 | [diff] [blame] | 448 | ret = isl29028_read_proxim(chip, val); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 449 | break; |
| 450 | default: |
| 451 | break; |
| 452 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 453 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 454 | if (ret < 0) |
| 455 | break; |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 456 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 457 | ret = IIO_VAL_INT; |
| 458 | break; |
Laxman Dewangan | 892cb6d | 2012-04-16 21:27:42 +0530 | [diff] [blame] | 459 | case IIO_CHAN_INFO_SAMP_FREQ: |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 460 | if (chan->type != IIO_PROXIMITY) |
| 461 | break; |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 462 | |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 463 | *val = chip->prox_sampling_int; |
| 464 | *val2 = chip->prox_sampling_frac; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 465 | ret = IIO_VAL_INT; |
| 466 | break; |
Laxman Dewangan | 892cb6d | 2012-04-16 21:27:42 +0530 | [diff] [blame] | 467 | case IIO_CHAN_INFO_SCALE: |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 468 | if (chan->type != IIO_LIGHT) |
| 469 | break; |
| 470 | *val = chip->lux_scale; |
| 471 | ret = IIO_VAL_INT; |
| 472 | break; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 473 | default: |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 474 | dev_err(dev, "%s(): mask value 0x%08lx is not supported\n", |
| 475 | __func__, mask); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 476 | break; |
| 477 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 478 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 479 | mutex_unlock(&chip->lock); |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 480 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 481 | if (ret < 0) |
| 482 | return ret; |
| 483 | |
| 484 | /** |
| 485 | * Preserve the ret variable if the call to |
| 486 | * isl29028_set_pm_runtime_busy() is successful so the reading |
| 487 | * (if applicable) is returned to user space. |
| 488 | */ |
| 489 | pm_ret = isl29028_set_pm_runtime_busy(chip, false); |
| 490 | if (pm_ret < 0) |
| 491 | return pm_ret; |
| 492 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | static IIO_CONST_ATTR(in_proximity_sampling_frequency_available, |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 497 | "1.25 2.5 5 10 13.3 20 80 100"); |
Peter Meerwald-Stadler | b9b689c | 2016-07-05 13:55:43 +0200 | [diff] [blame] | 498 | static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000"); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 499 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 500 | #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr) |
| 501 | static struct attribute *isl29028_attributes[] = { |
| 502 | ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available), |
| 503 | ISL29028_CONST_ATTR(in_illuminance_scale_available), |
| 504 | NULL, |
| 505 | }; |
| 506 | |
| 507 | static const struct attribute_group isl29108_group = { |
| 508 | .attrs = isl29028_attributes, |
| 509 | }; |
| 510 | |
| 511 | static const struct iio_chan_spec isl29028_channels[] = { |
| 512 | { |
| 513 | .type = IIO_LIGHT, |
Jonathan Cameron | 93a9fdf | 2013-02-27 19:39:52 +0000 | [diff] [blame] | 514 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 515 | BIT(IIO_CHAN_INFO_SCALE), |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 516 | }, { |
| 517 | .type = IIO_INTENSITY, |
Jonathan Cameron | 93a9fdf | 2013-02-27 19:39:52 +0000 | [diff] [blame] | 518 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 519 | }, { |
| 520 | .type = IIO_PROXIMITY, |
Jonathan Cameron | 93a9fdf | 2013-02-27 19:39:52 +0000 | [diff] [blame] | 521 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 522 | BIT(IIO_CHAN_INFO_SAMP_FREQ), |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 523 | } |
| 524 | }; |
| 525 | |
| 526 | static const struct iio_info isl29028_info = { |
| 527 | .attrs = &isl29108_group, |
Bhumika Goyal | df27103 | 2016-02-12 20:40:28 +0530 | [diff] [blame] | 528 | .read_raw = isl29028_read_raw, |
| 529 | .write_raw = isl29028_write_raw, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 530 | }; |
| 531 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 532 | static int isl29028_clear_configure_reg(struct isl29028_chip *chip) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 533 | { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 534 | struct device *dev = regmap_get_device(chip->regmap); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 535 | int ret; |
| 536 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 537 | ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0); |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 538 | if (ret < 0) |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 539 | dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n", |
| 540 | __func__, ret); |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 541 | |
| 542 | chip->als_ir_mode = ISL29028_MODE_NONE; |
| 543 | chip->enable_prox = false; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 544 | |
Brian Masney | 0fac96ed | 2017-01-17 04:25:01 -0500 | [diff] [blame] | 545 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 546 | } |
| 547 | |
Peter Meerwald-Stadler | f6127704 | 2016-07-05 13:55:44 +0200 | [diff] [blame] | 548 | static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 549 | { |
| 550 | switch (reg) { |
| 551 | case ISL29028_REG_INTERRUPT: |
| 552 | case ISL29028_REG_PROX_DATA: |
| 553 | case ISL29028_REG_ALSIR_L: |
| 554 | case ISL29028_REG_ALSIR_U: |
| 555 | return true; |
| 556 | default: |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static const struct regmap_config isl29028_regmap_config = { |
| 562 | .reg_bits = 8, |
| 563 | .val_bits = 8, |
Peter Meerwald-Stadler | f6127704 | 2016-07-05 13:55:44 +0200 | [diff] [blame] | 564 | .volatile_reg = isl29028_is_volatile_reg, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 565 | .max_register = ISL29028_NUM_REGS - 1, |
| 566 | .num_reg_defaults_raw = ISL29028_NUM_REGS, |
| 567 | .cache_type = REGCACHE_RBTREE, |
| 568 | }; |
| 569 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 570 | static int isl29028_probe(struct i2c_client *client, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 571 | const struct i2c_device_id *id) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 572 | { |
| 573 | struct isl29028_chip *chip; |
| 574 | struct iio_dev *indio_dev; |
| 575 | int ret; |
| 576 | |
Sachin Kamat | 80cab1d | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 577 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
Brian Masney | 8c9394026 | 2017-01-17 04:24:57 -0500 | [diff] [blame] | 578 | if (!indio_dev) |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 579 | return -ENOMEM; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 580 | |
| 581 | chip = iio_priv(indio_dev); |
| 582 | |
| 583 | i2c_set_clientdata(client, indio_dev); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 584 | mutex_init(&chip->lock); |
| 585 | |
| 586 | chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config); |
| 587 | if (IS_ERR(chip->regmap)) { |
| 588 | ret = PTR_ERR(chip->regmap); |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 589 | dev_err(&client->dev, "%s: Error %d initializing regmap\n", |
| 590 | __func__, ret); |
Sachin Kamat | 80cab1d | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 591 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 592 | } |
| 593 | |
Brian Masney | 58f6436 | 2016-12-03 21:19:27 -0500 | [diff] [blame] | 594 | chip->enable_prox = false; |
Brian Masney | e4ff6c1 | 2017-04-24 21:34:33 -0400 | [diff] [blame] | 595 | chip->prox_sampling_int = 20; |
| 596 | chip->prox_sampling_frac = 0; |
Brian Masney | 58f6436 | 2016-12-03 21:19:27 -0500 | [diff] [blame] | 597 | chip->lux_scale = 2000; |
Brian Masney | 58f6436 | 2016-12-03 21:19:27 -0500 | [diff] [blame] | 598 | |
| 599 | ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0); |
| 600 | if (ret < 0) { |
| 601 | dev_err(&client->dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 602 | "%s(): Error %d writing to TEST1_MODE register\n", |
| 603 | __func__, ret); |
Brian Masney | 58f6436 | 2016-12-03 21:19:27 -0500 | [diff] [blame] | 604 | return ret; |
| 605 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 606 | |
Brian Masney | 58f6436 | 2016-12-03 21:19:27 -0500 | [diff] [blame] | 607 | ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0); |
| 608 | if (ret < 0) { |
| 609 | dev_err(&client->dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 610 | "%s(): Error %d writing to TEST2_MODE register\n", |
| 611 | __func__, ret); |
Brian Masney | 58f6436 | 2016-12-03 21:19:27 -0500 | [diff] [blame] | 612 | return ret; |
| 613 | } |
| 614 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 615 | ret = isl29028_clear_configure_reg(chip); |
Brian Masney | 65ac716 | 2017-01-17 04:24:56 -0500 | [diff] [blame] | 616 | if (ret < 0) |
Sachin Kamat | 80cab1d | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 617 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 618 | |
| 619 | indio_dev->info = &isl29028_info; |
| 620 | indio_dev->channels = isl29028_channels; |
| 621 | indio_dev->num_channels = ARRAY_SIZE(isl29028_channels); |
| 622 | indio_dev->name = id->name; |
| 623 | indio_dev->dev.parent = &client->dev; |
| 624 | indio_dev->modes = INDIO_DIRECT_MODE; |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 625 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 626 | pm_runtime_enable(&client->dev); |
| 627 | pm_runtime_set_autosuspend_delay(&client->dev, |
| 628 | ISL29028_POWER_OFF_DELAY_MS); |
| 629 | pm_runtime_use_autosuspend(&client->dev); |
| 630 | |
Ioana Ciornei | 86727e3 | 2015-10-02 13:37:49 +0300 | [diff] [blame] | 631 | ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev); |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 632 | if (ret < 0) { |
Alison Schofield | 722fc31 | 2016-03-15 11:49:12 -0700 | [diff] [blame] | 633 | dev_err(&client->dev, |
Brian Masney | 578e02a | 2017-01-17 04:24:55 -0500 | [diff] [blame] | 634 | "%s(): iio registration failed with error %d\n", |
| 635 | __func__, ret); |
Sachin Kamat | 80cab1d | 2013-07-22 12:03:00 +0100 | [diff] [blame] | 636 | return ret; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 637 | } |
Brian Masney | b1dda96 | 2017-01-17 04:24:51 -0500 | [diff] [blame] | 638 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 639 | return 0; |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 640 | } |
| 641 | |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 642 | static int isl29028_remove(struct i2c_client *client) |
| 643 | { |
| 644 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 645 | struct isl29028_chip *chip = iio_priv(indio_dev); |
| 646 | |
| 647 | iio_device_unregister(indio_dev); |
| 648 | |
| 649 | pm_runtime_disable(&client->dev); |
| 650 | pm_runtime_set_suspended(&client->dev); |
| 651 | pm_runtime_put_noidle(&client->dev); |
| 652 | |
| 653 | return isl29028_clear_configure_reg(chip); |
| 654 | } |
| 655 | |
| 656 | static int __maybe_unused isl29028_suspend(struct device *dev) |
| 657 | { |
| 658 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 659 | struct isl29028_chip *chip = iio_priv(indio_dev); |
| 660 | int ret; |
| 661 | |
| 662 | mutex_lock(&chip->lock); |
| 663 | |
| 664 | ret = isl29028_clear_configure_reg(chip); |
| 665 | |
| 666 | mutex_unlock(&chip->lock); |
| 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | static int __maybe_unused isl29028_resume(struct device *dev) |
| 672 | { |
| 673 | /** |
| 674 | * The specific component (ALS/IR or proximity) will enable itself as |
| 675 | * needed the next time that the user requests a reading. This is done |
| 676 | * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity(). |
| 677 | */ |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static const struct dev_pm_ops isl29028_pm_ops = { |
Brian Masney | 0b59c38 | 2017-02-08 20:54:28 -0500 | [diff] [blame] | 682 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 683 | pm_runtime_force_resume) |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 684 | SET_RUNTIME_PM_OPS(isl29028_suspend, isl29028_resume, NULL) |
| 685 | }; |
| 686 | |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 687 | static const struct i2c_device_id isl29028_id[] = { |
| 688 | {"isl29028", 0}, |
Sebastian Reichel | e530cc8 | 2017-04-28 17:55:58 +0200 | [diff] [blame] | 689 | {"isl29030", 0}, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 690 | {} |
| 691 | }; |
| 692 | MODULE_DEVICE_TABLE(i2c, isl29028_id); |
| 693 | |
| 694 | static const struct of_device_id isl29028_of_match[] = { |
Arnaud Ebalard | 37e5157 | 2015-02-16 15:58:39 -0800 | [diff] [blame] | 695 | { .compatible = "isl,isl29028", }, /* for backward compat., don't use */ |
| 696 | { .compatible = "isil,isl29028", }, |
Sebastian Reichel | e530cc8 | 2017-04-28 17:55:58 +0200 | [diff] [blame] | 697 | { .compatible = "isil,isl29030", }, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 698 | { }, |
| 699 | }; |
| 700 | MODULE_DEVICE_TABLE(of, isl29028_of_match); |
| 701 | |
| 702 | static struct i2c_driver isl29028_driver = { |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 703 | .driver = { |
| 704 | .name = "isl29028", |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 705 | .pm = &isl29028_pm_ops, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 706 | .of_match_table = isl29028_of_match, |
| 707 | }, |
| 708 | .probe = isl29028_probe, |
Brian Masney | 2db5054 | 2017-01-17 04:25:02 -0500 | [diff] [blame] | 709 | .remove = isl29028_remove, |
Laxman Dewangan | 6d59ba2 | 2012-04-13 16:03:32 +0530 | [diff] [blame] | 710 | .id_table = isl29028_id, |
| 711 | }; |
| 712 | |
| 713 | module_i2c_driver(isl29028_driver); |
| 714 | |
| 715 | MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver"); |
| 716 | MODULE_LICENSE("GPL v2"); |
| 717 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |