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