Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 2 | /* |
Jonathan Cameron | 9c2251d | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 3 | * drivers/iio/light/tsl2563.c |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * |
| 7 | * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com> |
| 8 | * Contact: Amit Kucheria <amit.kucheria@verdurent.com> |
| 9 | * |
| 10 | * Converted to IIO driver |
| 11 | * Amit Kucheria <amit.kucheria@verdurent.com> |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/interrupt.h> |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 17 | #include <linux/irq.h> |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 18 | #include <linux/sched.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/delay.h> |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 21 | #include <linux/pm.h> |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 22 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 24 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 25 | #include <linux/iio/iio.h> |
| 26 | #include <linux/iio/sysfs.h> |
| 27 | #include <linux/iio/events.h> |
Jonathan Cameron | 9c2251d | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 28 | #include <linux/platform_data/tsl2563.h> |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 29 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 30 | /* Use this many bits for fraction part. */ |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 31 | #define ADC_FRAC_BITS 14 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 32 | |
| 33 | /* Given number of 1/10000's in ADC_FRAC_BITS precision. */ |
| 34 | #define FRAC10K(f) (((f) * (1L << (ADC_FRAC_BITS))) / (10000)) |
| 35 | |
| 36 | /* Bits used for fraction in calibration coefficients.*/ |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 37 | #define CALIB_FRAC_BITS 10 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 38 | /* 0.5 in CALIB_FRAC_BITS precision */ |
| 39 | #define CALIB_FRAC_HALF (1 << (CALIB_FRAC_BITS - 1)) |
| 40 | /* Make a fraction from a number n that was multiplied with b. */ |
| 41 | #define CALIB_FRAC(n, b) (((n) << CALIB_FRAC_BITS) / (b)) |
| 42 | /* Decimal 10^(digits in sysfs presentation) */ |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 43 | #define CALIB_BASE_SYSFS 1000 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 44 | |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 45 | #define TSL2563_CMD 0x80 |
| 46 | #define TSL2563_CLEARINT 0x40 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 47 | |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 48 | #define TSL2563_REG_CTRL 0x00 |
| 49 | #define TSL2563_REG_TIMING 0x01 |
| 50 | #define TSL2563_REG_LOWLOW 0x02 /* data0 low threshold, 2 bytes */ |
| 51 | #define TSL2563_REG_LOWHIGH 0x03 |
| 52 | #define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */ |
| 53 | #define TSL2563_REG_HIGHHIGH 0x05 |
| 54 | #define TSL2563_REG_INT 0x06 |
| 55 | #define TSL2563_REG_ID 0x0a |
| 56 | #define TSL2563_REG_DATA0LOW 0x0c /* broadband sensor value, 2 bytes */ |
| 57 | #define TSL2563_REG_DATA0HIGH 0x0d |
| 58 | #define TSL2563_REG_DATA1LOW 0x0e /* infrared sensor value, 2 bytes */ |
| 59 | #define TSL2563_REG_DATA1HIGH 0x0f |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 60 | |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 61 | #define TSL2563_CMD_POWER_ON 0x03 |
| 62 | #define TSL2563_CMD_POWER_OFF 0x00 |
| 63 | #define TSL2563_CTRL_POWER_MASK 0x03 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 64 | |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 65 | #define TSL2563_TIMING_13MS 0x00 |
| 66 | #define TSL2563_TIMING_100MS 0x01 |
| 67 | #define TSL2563_TIMING_400MS 0x02 |
| 68 | #define TSL2563_TIMING_MASK 0x03 |
| 69 | #define TSL2563_TIMING_GAIN16 0x10 |
| 70 | #define TSL2563_TIMING_GAIN1 0x00 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 71 | |
Jonathan Cameron | 5ade763 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 72 | #define TSL2563_INT_DISBLED 0x00 |
| 73 | #define TSL2563_INT_LEVEL 0x10 |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 74 | #define TSL2563_INT_PERSIST(n) ((n) & 0x0F) |
| 75 | |
| 76 | struct tsl2563_gainlevel_coeff { |
| 77 | u8 gaintime; |
| 78 | u16 min; |
| 79 | u16 max; |
| 80 | }; |
| 81 | |
Jonathan Cameron | 1ff7e1d | 2011-04-18 12:58:56 +0100 | [diff] [blame] | 82 | static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = { |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 83 | { |
| 84 | .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16, |
| 85 | .min = 0, |
| 86 | .max = 65534, |
| 87 | }, { |
| 88 | .gaintime = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1, |
| 89 | .min = 2048, |
| 90 | .max = 65534, |
| 91 | }, { |
| 92 | .gaintime = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1, |
| 93 | .min = 4095, |
| 94 | .max = 37177, |
| 95 | }, { |
| 96 | .gaintime = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1, |
| 97 | .min = 3000, |
| 98 | .max = 65535, |
| 99 | }, |
| 100 | }; |
| 101 | |
| 102 | struct tsl2563_chip { |
| 103 | struct mutex lock; |
| 104 | struct i2c_client *client; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 105 | struct delayed_work poweroff_work; |
| 106 | |
| 107 | /* Remember state for suspend and resume functions */ |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 108 | bool suspended; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 109 | |
Jonathan Cameron | 1ff7e1d | 2011-04-18 12:58:56 +0100 | [diff] [blame] | 110 | struct tsl2563_gainlevel_coeff const *gainlevel; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 111 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 112 | u16 low_thres; |
| 113 | u16 high_thres; |
| 114 | u8 intr; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 115 | bool int_enabled; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 116 | |
| 117 | /* Calibration coefficients */ |
| 118 | u32 calib0; |
| 119 | u32 calib1; |
| 120 | int cover_comp_gain; |
| 121 | |
| 122 | /* Cache current values, to be returned while suspended */ |
| 123 | u32 data0; |
| 124 | u32 data1; |
| 125 | }; |
| 126 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 127 | static int tsl2563_set_power(struct tsl2563_chip *chip, int on) |
| 128 | { |
| 129 | struct i2c_client *client = chip->client; |
| 130 | u8 cmd; |
| 131 | |
| 132 | cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 133 | return i2c_smbus_write_byte_data(client, |
| 134 | TSL2563_CMD | TSL2563_REG_CTRL, cmd); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Return value is 0 for off, 1 for on, or a negative error |
| 139 | * code if reading failed. |
| 140 | */ |
| 141 | static int tsl2563_get_power(struct tsl2563_chip *chip) |
| 142 | { |
| 143 | struct i2c_client *client = chip->client; |
| 144 | int ret; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 145 | |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 146 | ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL); |
| 147 | if (ret < 0) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 148 | return ret; |
| 149 | |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 150 | return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static int tsl2563_configure(struct tsl2563_chip *chip) |
| 154 | { |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 155 | int ret; |
| 156 | |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 157 | ret = i2c_smbus_write_byte_data(chip->client, |
| 158 | TSL2563_CMD | TSL2563_REG_TIMING, |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 159 | chip->gainlevel->gaintime); |
| 160 | if (ret) |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 161 | goto error_ret; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 162 | ret = i2c_smbus_write_byte_data(chip->client, |
| 163 | TSL2563_CMD | TSL2563_REG_HIGHLOW, |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 164 | chip->high_thres & 0xFF); |
| 165 | if (ret) |
| 166 | goto error_ret; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 167 | ret = i2c_smbus_write_byte_data(chip->client, |
| 168 | TSL2563_CMD | TSL2563_REG_HIGHHIGH, |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 169 | (chip->high_thres >> 8) & 0xFF); |
| 170 | if (ret) |
| 171 | goto error_ret; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 172 | ret = i2c_smbus_write_byte_data(chip->client, |
| 173 | TSL2563_CMD | TSL2563_REG_LOWLOW, |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 174 | chip->low_thres & 0xFF); |
| 175 | if (ret) |
| 176 | goto error_ret; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 177 | ret = i2c_smbus_write_byte_data(chip->client, |
| 178 | TSL2563_CMD | TSL2563_REG_LOWHIGH, |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 179 | (chip->low_thres >> 8) & 0xFF); |
Jonathan Cameron | a9e244f6 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Interrupt register is automatically written anyway if it is relevant |
| 182 | * so is not here. |
| 183 | */ |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 184 | error_ret: |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | static void tsl2563_poweroff_work(struct work_struct *work) |
| 189 | { |
| 190 | struct tsl2563_chip *chip = |
| 191 | container_of(work, struct tsl2563_chip, poweroff_work.work); |
| 192 | tsl2563_set_power(chip, 0); |
| 193 | } |
| 194 | |
| 195 | static int tsl2563_detect(struct tsl2563_chip *chip) |
| 196 | { |
| 197 | int ret; |
| 198 | |
| 199 | ret = tsl2563_set_power(chip, 1); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
| 203 | ret = tsl2563_get_power(chip); |
| 204 | if (ret < 0) |
| 205 | return ret; |
| 206 | |
| 207 | return ret ? 0 : -ENODEV; |
| 208 | } |
| 209 | |
| 210 | static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id) |
| 211 | { |
| 212 | struct i2c_client *client = chip->client; |
| 213 | int ret; |
| 214 | |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 215 | ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID); |
| 216 | if (ret < 0) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 217 | return ret; |
| 218 | |
Maxin B. John | 22dc09c | 2011-10-26 17:27:37 +0100 | [diff] [blame] | 219 | *id = ret; |
| 220 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * "Normalized" ADC value is one obtained with 400ms of integration time and |
| 226 | * 16x gain. This function returns the number of bits of shift needed to |
| 227 | * convert between normalized values and HW values obtained using given |
| 228 | * timing and gain settings. |
| 229 | */ |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 230 | static int tsl2563_adc_shiftbits(u8 timing) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 231 | { |
| 232 | int shift = 0; |
| 233 | |
| 234 | switch (timing & TSL2563_TIMING_MASK) { |
| 235 | case TSL2563_TIMING_13MS: |
| 236 | shift += 5; |
| 237 | break; |
| 238 | case TSL2563_TIMING_100MS: |
| 239 | shift += 2; |
| 240 | break; |
| 241 | case TSL2563_TIMING_400MS: |
| 242 | /* no-op */ |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | if (!(timing & TSL2563_TIMING_GAIN16)) |
| 247 | shift += 4; |
| 248 | |
| 249 | return shift; |
| 250 | } |
| 251 | |
| 252 | /* Convert a HW ADC value to normalized scale. */ |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 253 | static u32 tsl2563_normalize_adc(u16 adc, u8 timing) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 254 | { |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 255 | return adc << tsl2563_adc_shiftbits(timing); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static void tsl2563_wait_adc(struct tsl2563_chip *chip) |
| 259 | { |
| 260 | unsigned int delay; |
| 261 | |
| 262 | switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) { |
| 263 | case TSL2563_TIMING_13MS: |
| 264 | delay = 14; |
| 265 | break; |
| 266 | case TSL2563_TIMING_100MS: |
| 267 | delay = 101; |
| 268 | break; |
| 269 | default: |
| 270 | delay = 402; |
| 271 | } |
| 272 | /* |
| 273 | * TODO: Make sure that we wait at least required delay but why we |
| 274 | * have to extend it one tick more? |
| 275 | */ |
| 276 | schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2); |
| 277 | } |
| 278 | |
| 279 | static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc) |
| 280 | { |
| 281 | struct i2c_client *client = chip->client; |
| 282 | |
| 283 | if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) { |
| 284 | |
| 285 | (adc > chip->gainlevel->max) ? |
| 286 | chip->gainlevel++ : chip->gainlevel--; |
| 287 | |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 288 | i2c_smbus_write_byte_data(client, |
| 289 | TSL2563_CMD | TSL2563_REG_TIMING, |
| 290 | chip->gainlevel->gaintime); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 291 | |
| 292 | tsl2563_wait_adc(chip); |
| 293 | tsl2563_wait_adc(chip); |
| 294 | |
| 295 | return 1; |
| 296 | } else |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int tsl2563_get_adc(struct tsl2563_chip *chip) |
| 301 | { |
| 302 | struct i2c_client *client = chip->client; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 303 | u16 adc0, adc1; |
| 304 | int retry = 1; |
| 305 | int ret = 0; |
| 306 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 307 | if (chip->suspended) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 308 | goto out; |
| 309 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 310 | if (!chip->int_enabled) { |
| 311 | cancel_delayed_work(&chip->poweroff_work); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 312 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 313 | if (!tsl2563_get_power(chip)) { |
| 314 | ret = tsl2563_set_power(chip, 1); |
| 315 | if (ret) |
| 316 | goto out; |
| 317 | ret = tsl2563_configure(chip); |
| 318 | if (ret) |
| 319 | goto out; |
| 320 | tsl2563_wait_adc(chip); |
| 321 | } |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | while (retry) { |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 325 | ret = i2c_smbus_read_word_data(client, |
| 326 | TSL2563_CMD | TSL2563_REG_DATA0LOW); |
| 327 | if (ret < 0) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 328 | goto out; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 329 | adc0 = ret; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 330 | |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 331 | ret = i2c_smbus_read_word_data(client, |
| 332 | TSL2563_CMD | TSL2563_REG_DATA1LOW); |
| 333 | if (ret < 0) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 334 | goto out; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 335 | adc1 = ret; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 336 | |
| 337 | retry = tsl2563_adjust_gainlevel(chip, adc0); |
| 338 | } |
| 339 | |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 340 | chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime); |
| 341 | chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 342 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 343 | if (!chip->int_enabled) |
| 344 | schedule_delayed_work(&chip->poweroff_work, 5 * HZ); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 345 | |
| 346 | ret = 0; |
| 347 | out: |
| 348 | return ret; |
| 349 | } |
| 350 | |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 351 | static inline int tsl2563_calib_to_sysfs(u32 calib) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 352 | { |
| 353 | return (int) (((calib * CALIB_BASE_SYSFS) + |
| 354 | CALIB_FRAC_HALF) >> CALIB_FRAC_BITS); |
| 355 | } |
| 356 | |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 357 | static inline u32 tsl2563_calib_from_sysfs(int value) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 358 | { |
| 359 | return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Conversions between lux and ADC values. |
| 364 | * |
| 365 | * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are |
| 366 | * appropriate constants. Different constants are needed for different |
| 367 | * kinds of light, determined by the ratio adc1/adc0 (basically the ratio |
| 368 | * of the intensities in infrared and visible wavelengths). lux_table below |
| 369 | * lists the upper threshold of the adc1/adc0 ratio and the corresponding |
| 370 | * constants. |
| 371 | */ |
| 372 | |
| 373 | struct tsl2563_lux_coeff { |
| 374 | unsigned long ch_ratio; |
| 375 | unsigned long ch0_coeff; |
| 376 | unsigned long ch1_coeff; |
| 377 | }; |
| 378 | |
| 379 | static const struct tsl2563_lux_coeff lux_table[] = { |
| 380 | { |
| 381 | .ch_ratio = FRAC10K(1300), |
| 382 | .ch0_coeff = FRAC10K(315), |
| 383 | .ch1_coeff = FRAC10K(262), |
| 384 | }, { |
| 385 | .ch_ratio = FRAC10K(2600), |
| 386 | .ch0_coeff = FRAC10K(337), |
| 387 | .ch1_coeff = FRAC10K(430), |
| 388 | }, { |
| 389 | .ch_ratio = FRAC10K(3900), |
| 390 | .ch0_coeff = FRAC10K(363), |
| 391 | .ch1_coeff = FRAC10K(529), |
| 392 | }, { |
| 393 | .ch_ratio = FRAC10K(5200), |
| 394 | .ch0_coeff = FRAC10K(392), |
| 395 | .ch1_coeff = FRAC10K(605), |
| 396 | }, { |
| 397 | .ch_ratio = FRAC10K(6500), |
| 398 | .ch0_coeff = FRAC10K(229), |
| 399 | .ch1_coeff = FRAC10K(291), |
| 400 | }, { |
| 401 | .ch_ratio = FRAC10K(8000), |
| 402 | .ch0_coeff = FRAC10K(157), |
| 403 | .ch1_coeff = FRAC10K(180), |
| 404 | }, { |
| 405 | .ch_ratio = FRAC10K(13000), |
| 406 | .ch0_coeff = FRAC10K(34), |
| 407 | .ch1_coeff = FRAC10K(26), |
| 408 | }, { |
| 409 | .ch_ratio = ULONG_MAX, |
| 410 | .ch0_coeff = 0, |
| 411 | .ch1_coeff = 0, |
| 412 | }, |
| 413 | }; |
| 414 | |
Jonathan Cameron | a9e244f6 | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 415 | /* Convert normalized, scaled ADC values to lux. */ |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 416 | static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 417 | { |
| 418 | const struct tsl2563_lux_coeff *lp = lux_table; |
| 419 | unsigned long ratio, lux, ch0 = adc0, ch1 = adc1; |
| 420 | |
| 421 | ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX; |
| 422 | |
| 423 | while (lp->ch_ratio < ratio) |
| 424 | lp++; |
| 425 | |
| 426 | lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff; |
| 427 | |
| 428 | return (unsigned int) (lux >> ADC_FRAC_BITS); |
| 429 | } |
| 430 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 431 | /* Apply calibration coefficient to ADC count. */ |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 432 | static u32 tsl2563_calib_adc(u32 adc, u32 calib) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 433 | { |
| 434 | unsigned long scaled = adc; |
| 435 | |
| 436 | scaled *= calib; |
| 437 | scaled >>= CALIB_FRAC_BITS; |
| 438 | |
| 439 | return (u32) scaled; |
| 440 | } |
| 441 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 442 | static int tsl2563_write_raw(struct iio_dev *indio_dev, |
| 443 | struct iio_chan_spec const *chan, |
| 444 | int val, |
| 445 | int val2, |
| 446 | long mask) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 447 | { |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 448 | struct tsl2563_chip *chip = iio_priv(indio_dev); |
| 449 | |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 450 | if (mask != IIO_CHAN_INFO_CALIBSCALE) |
| 451 | return -EINVAL; |
| 452 | if (chan->channel2 == IIO_MOD_LIGHT_BOTH) |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 453 | chip->calib0 = tsl2563_calib_from_sysfs(val); |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 454 | else if (chan->channel2 == IIO_MOD_LIGHT_IR) |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 455 | chip->calib1 = tsl2563_calib_from_sysfs(val); |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 456 | else |
| 457 | return -EINVAL; |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int tsl2563_read_raw(struct iio_dev *indio_dev, |
| 463 | struct iio_chan_spec const *chan, |
| 464 | int *val, |
| 465 | int *val2, |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 466 | long mask) |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 467 | { |
| 468 | int ret = -EINVAL; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 469 | u32 calib0, calib1; |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 470 | struct tsl2563_chip *chip = iio_priv(indio_dev); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 471 | |
| 472 | mutex_lock(&chip->lock); |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 473 | switch (mask) { |
Jonathan Cameron | 90354d0 | 2012-04-15 17:41:22 +0100 | [diff] [blame] | 474 | case IIO_CHAN_INFO_RAW: |
| 475 | case IIO_CHAN_INFO_PROCESSED: |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 476 | switch (chan->type) { |
| 477 | case IIO_LIGHT: |
| 478 | ret = tsl2563_get_adc(chip); |
| 479 | if (ret) |
| 480 | goto error_ret; |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 481 | calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) * |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 482 | chip->cover_comp_gain; |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 483 | calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) * |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 484 | chip->cover_comp_gain; |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 485 | *val = tsl2563_adc_to_lux(calib0, calib1); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 486 | ret = IIO_VAL_INT; |
| 487 | break; |
| 488 | case IIO_INTENSITY: |
| 489 | ret = tsl2563_get_adc(chip); |
| 490 | if (ret) |
| 491 | goto error_ret; |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 492 | if (chan->channel2 == IIO_MOD_LIGHT_BOTH) |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 493 | *val = chip->data0; |
| 494 | else |
| 495 | *val = chip->data1; |
| 496 | ret = IIO_VAL_INT; |
| 497 | break; |
| 498 | default: |
| 499 | break; |
| 500 | } |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 501 | break; |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 502 | |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 503 | case IIO_CHAN_INFO_CALIBSCALE: |
Ivaylo Dimitrov | 3b5c163 | 2014-01-13 17:24:00 +0000 | [diff] [blame] | 504 | if (chan->channel2 == IIO_MOD_LIGHT_BOTH) |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 505 | *val = tsl2563_calib_to_sysfs(chip->calib0); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 506 | else |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 507 | *val = tsl2563_calib_to_sysfs(chip->calib1); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 508 | ret = IIO_VAL_INT; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 509 | break; |
| 510 | default: |
Dan Carpenter | 97d35f2 | 2011-10-06 09:15:03 +0300 | [diff] [blame] | 511 | ret = -EINVAL; |
| 512 | goto error_ret; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | error_ret: |
| 516 | mutex_unlock(&chip->lock); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 517 | return ret; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 520 | static const struct iio_event_spec tsl2563_events[] = { |
| 521 | { |
| 522 | .type = IIO_EV_TYPE_THRESH, |
| 523 | .dir = IIO_EV_DIR_RISING, |
| 524 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 525 | BIT(IIO_EV_INFO_ENABLE), |
| 526 | }, { |
| 527 | .type = IIO_EV_TYPE_THRESH, |
| 528 | .dir = IIO_EV_DIR_FALLING, |
| 529 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 530 | BIT(IIO_EV_INFO_ENABLE), |
| 531 | }, |
| 532 | }; |
| 533 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 534 | static const struct iio_chan_spec tsl2563_channels[] = { |
Jonathan Cameron | 7993906 | 2011-08-30 12:41:16 +0100 | [diff] [blame] | 535 | { |
| 536 | .type = IIO_LIGHT, |
| 537 | .indexed = 1, |
Jonathan Cameron | d292ef8 | 2013-02-27 19:33:40 +0000 | [diff] [blame] | 538 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
Jonathan Cameron | 7993906 | 2011-08-30 12:41:16 +0100 | [diff] [blame] | 539 | .channel = 0, |
| 540 | }, { |
| 541 | .type = IIO_INTENSITY, |
| 542 | .modified = 1, |
| 543 | .channel2 = IIO_MOD_LIGHT_BOTH, |
Jonathan Cameron | d292ef8 | 2013-02-27 19:33:40 +0000 | [diff] [blame] | 544 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 545 | BIT(IIO_CHAN_INFO_CALIBSCALE), |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 546 | .event_spec = tsl2563_events, |
| 547 | .num_event_specs = ARRAY_SIZE(tsl2563_events), |
Jonathan Cameron | 7993906 | 2011-08-30 12:41:16 +0100 | [diff] [blame] | 548 | }, { |
| 549 | .type = IIO_INTENSITY, |
| 550 | .modified = 1, |
Jonathan Cameron | a7e3bd6 | 2011-10-26 17:27:36 +0100 | [diff] [blame] | 551 | .channel2 = IIO_MOD_LIGHT_IR, |
Jonathan Cameron | d292ef8 | 2013-02-27 19:33:40 +0000 | [diff] [blame] | 552 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 553 | BIT(IIO_CHAN_INFO_CALIBSCALE), |
Jonathan Cameron | 7993906 | 2011-08-30 12:41:16 +0100 | [diff] [blame] | 554 | } |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 555 | }; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 556 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 557 | static int tsl2563_read_thresh(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 558 | const struct iio_chan_spec *chan, enum iio_event_type type, |
| 559 | enum iio_event_direction dir, enum iio_event_info info, int *val, |
| 560 | int *val2) |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 561 | { |
| 562 | struct tsl2563_chip *chip = iio_priv(indio_dev); |
| 563 | |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 564 | switch (dir) { |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 565 | case IIO_EV_DIR_RISING: |
| 566 | *val = chip->high_thres; |
| 567 | break; |
| 568 | case IIO_EV_DIR_FALLING: |
| 569 | *val = chip->low_thres; |
| 570 | break; |
| 571 | default: |
| 572 | return -EINVAL; |
| 573 | } |
| 574 | |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 575 | return IIO_VAL_INT; |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Dan Carpenter | 15fbc19 | 2011-10-06 09:15:36 +0300 | [diff] [blame] | 578 | static int tsl2563_write_thresh(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 579 | const struct iio_chan_spec *chan, enum iio_event_type type, |
| 580 | enum iio_event_direction dir, enum iio_event_info info, int val, |
| 581 | int val2) |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 582 | { |
| 583 | struct tsl2563_chip *chip = iio_priv(indio_dev); |
| 584 | int ret; |
| 585 | u8 address; |
| 586 | |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 587 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 588 | address = TSL2563_REG_HIGHLOW; |
| 589 | else |
| 590 | address = TSL2563_REG_LOWLOW; |
| 591 | mutex_lock(&chip->lock); |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 592 | ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address, |
| 593 | val & 0xFF); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 594 | if (ret) |
| 595 | goto error_ret; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 596 | ret = i2c_smbus_write_byte_data(chip->client, |
| 597 | TSL2563_CMD | (address + 1), |
| 598 | (val >> 8) & 0xFF); |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 599 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 600 | chip->high_thres = val; |
| 601 | else |
| 602 | chip->low_thres = val; |
| 603 | |
| 604 | error_ret: |
| 605 | mutex_unlock(&chip->lock); |
| 606 | |
| 607 | return ret; |
| 608 | } |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 609 | |
Jonathan Cameron | bdab100 | 2011-05-18 14:41:03 +0100 | [diff] [blame] | 610 | static irqreturn_t tsl2563_event_handler(int irq, void *private) |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 611 | { |
Jonathan Cameron | bdab100 | 2011-05-18 14:41:03 +0100 | [diff] [blame] | 612 | struct iio_dev *dev_info = private; |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 613 | struct tsl2563_chip *chip = iio_priv(dev_info); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 614 | |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 615 | iio_push_event(dev_info, |
Akinobu Mita | a3507e4 | 2017-06-21 01:46:37 +0900 | [diff] [blame] | 616 | IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, |
Jonathan Cameron | da1d8b6 | 2010-10-08 12:14:05 +0100 | [diff] [blame] | 617 | 0, |
| 618 | IIO_EV_TYPE_THRESH, |
| 619 | IIO_EV_DIR_EITHER), |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 620 | iio_get_time_ns(dev_info)); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 621 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 622 | /* clear the interrupt and push the event */ |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 623 | i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT); |
Jonathan Cameron | bdab100 | 2011-05-18 14:41:03 +0100 | [diff] [blame] | 624 | return IRQ_HANDLED; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 627 | static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 628 | const struct iio_chan_spec *chan, enum iio_event_type type, |
| 629 | enum iio_event_direction dir, int state) |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 630 | { |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 631 | struct tsl2563_chip *chip = iio_priv(indio_dev); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 632 | int ret = 0; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 633 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 634 | mutex_lock(&chip->lock); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 635 | if (state && !(chip->intr & 0x30)) { |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 636 | chip->intr &= ~0x30; |
| 637 | chip->intr |= 0x10; |
| 638 | /* ensure the chip is actually on */ |
| 639 | cancel_delayed_work(&chip->poweroff_work); |
| 640 | if (!tsl2563_get_power(chip)) { |
| 641 | ret = tsl2563_set_power(chip, 1); |
| 642 | if (ret) |
| 643 | goto out; |
| 644 | ret = tsl2563_configure(chip); |
| 645 | if (ret) |
| 646 | goto out; |
| 647 | } |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 648 | ret = i2c_smbus_write_byte_data(chip->client, |
| 649 | TSL2563_CMD | TSL2563_REG_INT, |
| 650 | chip->intr); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 651 | chip->int_enabled = true; |
| 652 | } |
| 653 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 654 | if (!state && (chip->intr & 0x30)) { |
Derek Basehore | 95273f8 | 2012-10-05 00:54:00 +0100 | [diff] [blame] | 655 | chip->intr &= ~0x30; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 656 | ret = i2c_smbus_write_byte_data(chip->client, |
| 657 | TSL2563_CMD | TSL2563_REG_INT, |
| 658 | chip->intr); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 659 | chip->int_enabled = false; |
| 660 | /* now the interrupt is not enabled, we can go to sleep */ |
| 661 | schedule_delayed_work(&chip->poweroff_work, 5 * HZ); |
| 662 | } |
| 663 | out: |
| 664 | mutex_unlock(&chip->lock); |
| 665 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 666 | return ret; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 667 | } |
| 668 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 669 | static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 6d59747 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 670 | const struct iio_chan_spec *chan, enum iio_event_type type, |
| 671 | enum iio_event_direction dir) |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 672 | { |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 673 | struct tsl2563_chip *chip = iio_priv(indio_dev); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 674 | int ret; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 675 | |
| 676 | mutex_lock(&chip->lock); |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 677 | ret = i2c_smbus_read_byte_data(chip->client, |
| 678 | TSL2563_CMD | TSL2563_REG_INT); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 679 | mutex_unlock(&chip->lock); |
| 680 | if (ret < 0) |
Jonathan Cameron | a722dcc | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 681 | return ret; |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 682 | |
Jonathan Cameron | a722dcc | 2013-01-12 10:35:00 +0000 | [diff] [blame] | 683 | return !!(ret & 0x30); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 684 | } |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 685 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 686 | static const struct iio_info tsl2563_info_no_irq = { |
Bryan Freed | 9e4216f | 2011-06-21 15:54:57 -0700 | [diff] [blame] | 687 | .read_raw = &tsl2563_read_raw, |
| 688 | .write_raw = &tsl2563_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 689 | }; |
| 690 | |
| 691 | static const struct iio_info tsl2563_info = { |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 692 | .read_raw = &tsl2563_read_raw, |
| 693 | .write_raw = &tsl2563_write_raw, |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 694 | .read_event_value = &tsl2563_read_thresh, |
| 695 | .write_event_value = &tsl2563_write_thresh, |
| 696 | .read_event_config = &tsl2563_read_interrupt_config, |
| 697 | .write_event_config = &tsl2563_write_interrupt_config, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 698 | }; |
| 699 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 700 | static int tsl2563_probe(struct i2c_client *client, |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 701 | const struct i2c_device_id *device_id) |
| 702 | { |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 703 | struct iio_dev *indio_dev; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 704 | struct tsl2563_chip *chip; |
| 705 | struct tsl2563_platform_data *pdata = client->dev.platform_data; |
Sebastian Reichel | 8175bff | 2013-10-25 10:10:00 +0100 | [diff] [blame] | 706 | struct device_node *np = client->dev.of_node; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 707 | int err = 0; |
Jonathan Cameron | deda386 | 2011-08-12 17:08:35 +0100 | [diff] [blame] | 708 | u8 id = 0; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 709 | |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 710 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 711 | if (!indio_dev) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 712 | return -ENOMEM; |
| 713 | |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 714 | chip = iio_priv(indio_dev); |
| 715 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 716 | i2c_set_clientdata(client, chip); |
| 717 | chip->client = client; |
| 718 | |
| 719 | err = tsl2563_detect(chip); |
| 720 | if (err) { |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 721 | dev_err(&client->dev, "detect error %d\n", -err); |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 722 | return err; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | err = tsl2563_read_id(chip, &id); |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 726 | if (err) { |
| 727 | dev_err(&client->dev, "read id error %d\n", -err); |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 728 | return err; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 729 | } |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 730 | |
| 731 | mutex_init(&chip->lock); |
| 732 | |
| 733 | /* Default values used until userspace says otherwise */ |
| 734 | chip->low_thres = 0x0; |
| 735 | chip->high_thres = 0xffff; |
| 736 | chip->gainlevel = tsl2563_gainlevel_table; |
| 737 | chip->intr = TSL2563_INT_PERSIST(4); |
Peter Meerwald | 6fa273c | 2015-04-15 22:39:37 +0200 | [diff] [blame] | 738 | chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS); |
| 739 | chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 740 | |
| 741 | if (pdata) |
| 742 | chip->cover_comp_gain = pdata->cover_comp_gain; |
Sebastian Reichel | 8175bff | 2013-10-25 10:10:00 +0100 | [diff] [blame] | 743 | else if (np) |
| 744 | of_property_read_u32(np, "amstaos,cover-comp-gain", |
| 745 | &chip->cover_comp_gain); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 746 | else |
| 747 | chip->cover_comp_gain = 1; |
| 748 | |
| 749 | dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f); |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 750 | indio_dev->name = client->name; |
| 751 | indio_dev->channels = tsl2563_channels; |
| 752 | indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels); |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 753 | indio_dev->dev.parent = &client->dev; |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 754 | indio_dev->modes = INDIO_DIRECT_MODE; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 755 | |
Jonathan Cameron | cbcdf4d | 2011-05-18 14:41:45 +0100 | [diff] [blame] | 756 | if (client->irq) |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 757 | indio_dev->info = &tsl2563_info; |
| 758 | else |
| 759 | indio_dev->info = &tsl2563_info_no_irq; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 760 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 761 | if (client->irq) { |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 762 | err = devm_request_threaded_irq(&client->dev, client->irq, |
Jonathan Cameron | bdab100 | 2011-05-18 14:41:03 +0100 | [diff] [blame] | 763 | NULL, |
| 764 | &tsl2563_event_handler, |
| 765 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 766 | "tsl2563_event", |
| 767 | indio_dev); |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 768 | if (err) { |
| 769 | dev_err(&client->dev, "irq request error %d\n", -err); |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 770 | return err; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 771 | } |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 772 | } |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 773 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 774 | err = tsl2563_configure(chip); |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 775 | if (err) { |
| 776 | dev_err(&client->dev, "configure error %d\n", -err); |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 777 | return err; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 778 | } |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 779 | |
| 780 | INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work); |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 781 | |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 782 | /* The interrupt cannot yet be enabled so this is fine without lock */ |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 783 | schedule_delayed_work(&chip->poweroff_work, 5 * HZ); |
| 784 | |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 785 | err = iio_device_register(indio_dev); |
| 786 | if (err) { |
| 787 | dev_err(&client->dev, "iio registration error %d\n", -err); |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 788 | goto fail; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 789 | } |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 790 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 791 | return 0; |
Grant Grundler | dbf717f | 2012-03-05 09:15:59 -0800 | [diff] [blame] | 792 | |
Sachin Kamat | bace48f | 2013-07-30 09:44:00 +0100 | [diff] [blame] | 793 | fail: |
Amitoj Kaur Chawla | 9e61d90 | 2016-03-17 19:25:11 +0530 | [diff] [blame] | 794 | cancel_delayed_work_sync(&chip->poweroff_work); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 795 | return err; |
| 796 | } |
| 797 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 798 | static int tsl2563_remove(struct i2c_client *client) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 799 | { |
| 800 | struct tsl2563_chip *chip = i2c_get_clientdata(client); |
Jonathan Cameron | 33789dc | 2011-04-18 12:58:55 +0100 | [diff] [blame] | 801 | struct iio_dev *indio_dev = iio_priv_to_dev(chip); |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 802 | |
| 803 | iio_device_unregister(indio_dev); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 804 | if (!chip->int_enabled) |
| 805 | cancel_delayed_work(&chip->poweroff_work); |
| 806 | /* Ensure that interrupts are disabled - then flush any bottom halves */ |
Derek Basehore | 95273f8 | 2012-10-05 00:54:00 +0100 | [diff] [blame] | 807 | chip->intr &= ~0x30; |
Bryan Freed | d9b42c0 | 2011-06-24 13:40:47 -0700 | [diff] [blame] | 808 | i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT, |
| 809 | chip->intr); |
Jonathan Cameron | 388be48 | 2010-06-26 12:54:21 +0100 | [diff] [blame] | 810 | flush_scheduled_work(); |
| 811 | tsl2563_set_power(chip, 0); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 812 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 816 | #ifdef CONFIG_PM_SLEEP |
| 817 | static int tsl2563_suspend(struct device *dev) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 818 | { |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 819 | struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev)); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 820 | int ret; |
| 821 | |
| 822 | mutex_lock(&chip->lock); |
| 823 | |
| 824 | ret = tsl2563_set_power(chip, 0); |
| 825 | if (ret) |
| 826 | goto out; |
| 827 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 828 | chip->suspended = true; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 829 | |
| 830 | out: |
| 831 | mutex_unlock(&chip->lock); |
| 832 | return ret; |
| 833 | } |
| 834 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 835 | static int tsl2563_resume(struct device *dev) |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 836 | { |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 837 | struct tsl2563_chip *chip = i2c_get_clientdata(to_i2c_client(dev)); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 838 | int ret; |
| 839 | |
| 840 | mutex_lock(&chip->lock); |
| 841 | |
| 842 | ret = tsl2563_set_power(chip, 1); |
| 843 | if (ret) |
| 844 | goto out; |
| 845 | |
| 846 | ret = tsl2563_configure(chip); |
| 847 | if (ret) |
| 848 | goto out; |
| 849 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 850 | chip->suspended = false; |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 851 | |
| 852 | out: |
| 853 | mutex_unlock(&chip->lock); |
| 854 | return ret; |
| 855 | } |
| 856 | |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 857 | static SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend, tsl2563_resume); |
| 858 | #define TSL2563_PM_OPS (&tsl2563_pm_ops) |
| 859 | #else |
| 860 | #define TSL2563_PM_OPS NULL |
| 861 | #endif |
| 862 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 863 | static const struct i2c_device_id tsl2563_id[] = { |
Jonathan Cameron | dbd5d23 | 2009-11-22 16:03:25 +0000 | [diff] [blame] | 864 | { "tsl2560", 0 }, |
| 865 | { "tsl2561", 1 }, |
| 866 | { "tsl2562", 2 }, |
| 867 | { "tsl2563", 3 }, |
| 868 | {} |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 869 | }; |
| 870 | MODULE_DEVICE_TABLE(i2c, tsl2563_id); |
| 871 | |
Javier Martinez Canillas | 9d3922b2 | 2017-03-15 01:44:54 -0300 | [diff] [blame] | 872 | static const struct of_device_id tsl2563_of_match[] = { |
| 873 | { .compatible = "amstaos,tsl2560" }, |
| 874 | { .compatible = "amstaos,tsl2561" }, |
| 875 | { .compatible = "amstaos,tsl2562" }, |
| 876 | { .compatible = "amstaos,tsl2563" }, |
| 877 | {} |
| 878 | }; |
| 879 | MODULE_DEVICE_TABLE(of, tsl2563_of_match); |
| 880 | |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 881 | static struct i2c_driver tsl2563_i2c_driver = { |
| 882 | .driver = { |
Jonathan Cameron | dbd5d23 | 2009-11-22 16:03:25 +0000 | [diff] [blame] | 883 | .name = "tsl2563", |
Javier Martinez Canillas | 9d3922b2 | 2017-03-15 01:44:54 -0300 | [diff] [blame] | 884 | .of_match_table = tsl2563_of_match, |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 885 | .pm = TSL2563_PM_OPS, |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 886 | }, |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 887 | .probe = tsl2563_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 888 | .remove = tsl2563_remove, |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 889 | .id_table = tsl2563_id, |
| 890 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 891 | module_i2c_driver(tsl2563_i2c_driver); |
Amit Kucheria | ee1f1fa | 2009-11-09 15:14:28 +0200 | [diff] [blame] | 892 | |
| 893 | MODULE_AUTHOR("Nokia Corporation"); |
| 894 | MODULE_DESCRIPTION("tsl2563 light sensor driver"); |
| 895 | MODULE_LICENSE("GPL"); |