Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Device driver for monitoring ambient light intensity (lux) |
Brian Masney | aadc4c9 | 2016-11-12 13:19:35 -0500 | [diff] [blame] | 3 | * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583). |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2011, TAOS Corporation. |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 6 | * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org> |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/unistd.h> |
| 26 | #include <linux/slab.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 28 | #include <linux/iio/iio.h> |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 29 | #include <linux/iio/sysfs.h> |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 31 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 32 | /* Device Registers and Masks */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 33 | #define TSL2583_CNTRL 0x00 |
| 34 | #define TSL2583_ALS_TIME 0X01 |
| 35 | #define TSL2583_INTERRUPT 0x02 |
| 36 | #define TSL2583_GAIN 0x07 |
| 37 | #define TSL2583_REVID 0x11 |
| 38 | #define TSL2583_CHIPID 0x12 |
| 39 | #define TSL2583_ALS_CHAN0LO 0x14 |
| 40 | #define TSL2583_ALS_CHAN0HI 0x15 |
| 41 | #define TSL2583_ALS_CHAN1LO 0x16 |
| 42 | #define TSL2583_ALS_CHAN1HI 0x17 |
| 43 | #define TSL2583_TMR_LO 0x18 |
| 44 | #define TSL2583_TMR_HI 0x19 |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 45 | |
| 46 | /* tsl2583 cmd reg masks */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 47 | #define TSL2583_CMD_REG 0x80 |
| 48 | #define TSL2583_CMD_SPL_FN 0x60 |
Brian Masney | 8391c88 | 2016-11-12 13:19:24 -0500 | [diff] [blame] | 49 | #define TSL2583_CMD_ALS_INT_CLR 0x01 |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 50 | |
| 51 | /* tsl2583 cntrl reg masks */ |
Brian Masney | 8391c88 | 2016-11-12 13:19:24 -0500 | [diff] [blame] | 52 | #define TSL2583_CNTL_ADC_ENBL 0x02 |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 53 | #define TSL2583_CNTL_PWR_OFF 0x00 |
| 54 | #define TSL2583_CNTL_PWR_ON 0x01 |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 55 | |
| 56 | /* tsl2583 status reg masks */ |
Brian Masney | 8391c88 | 2016-11-12 13:19:24 -0500 | [diff] [blame] | 57 | #define TSL2583_STA_ADC_VALID 0x01 |
| 58 | #define TSL2583_STA_ADC_INTR 0x10 |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 59 | |
| 60 | /* Lux calculation constants */ |
Brian Masney | 8391c88 | 2016-11-12 13:19:24 -0500 | [diff] [blame] | 61 | #define TSL2583_LUX_CALC_OVER_FLOW 65535 |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 62 | |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 63 | #define TSL2583_INTERRUPT_DISABLED 0x00 |
| 64 | |
Brian Masney | 6bd0cb2 | 2016-11-03 08:56:13 -0400 | [diff] [blame] | 65 | #define TSL2583_CHIP_ID 0x90 |
| 66 | #define TSL2583_CHIP_ID_MASK 0xf0 |
| 67 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 68 | #define TSL2583_POWER_OFF_DELAY_MS 2000 |
| 69 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 70 | /* Per-device data */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 71 | struct tsl2583_als_info { |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 72 | u16 als_ch0; |
| 73 | u16 als_ch1; |
| 74 | u16 lux; |
| 75 | }; |
| 76 | |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 77 | struct tsl2583_lux { |
| 78 | unsigned int ratio; |
| 79 | unsigned int ch0; |
| 80 | unsigned int ch1; |
| 81 | }; |
| 82 | |
| 83 | static const struct tsl2583_lux tsl2583_default_lux[] = { |
| 84 | { 9830, 8520, 15729 }, |
| 85 | { 12452, 10807, 23344 }, |
| 86 | { 14746, 6383, 11705 }, |
| 87 | { 17695, 4063, 6554 }, |
| 88 | { 0, 0, 0 } /* Termination segment */ |
| 89 | }; |
| 90 | |
| 91 | #define TSL2583_MAX_LUX_TABLE_ENTRIES 11 |
| 92 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 93 | struct tsl2583_settings { |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 94 | int als_time; |
| 95 | int als_gain; |
| 96 | int als_gain_trim; |
| 97 | int als_cal_target; |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * This structure is intentionally large to accommodate updates via |
| 101 | * sysfs. Sized to 11 = max 10 segments + 1 termination segment. |
| 102 | * Assumption is that one and only one type of glass used. |
| 103 | */ |
| 104 | struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES]; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct tsl2583_chip { |
| 108 | struct mutex als_mutex; |
| 109 | struct i2c_client *client; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 110 | struct tsl2583_als_info als_cur_info; |
| 111 | struct tsl2583_settings als_settings; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 112 | int als_time_scale; |
| 113 | int als_saturation; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 116 | struct gainadj { |
| 117 | s16 ch0; |
| 118 | s16 ch1; |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 119 | s16 mean; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /* Index = (0 - 3) Used to validate the gain selection index */ |
| 123 | static const struct gainadj gainadj[] = { |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 124 | { 1, 1, 1 }, |
| 125 | { 8, 8, 8 }, |
| 126 | { 16, 16, 16 }, |
| 127 | { 107, 115, 111 } |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * Provides initial operational parameter defaults. |
| 132 | * These defaults may be changed through the device's sysfs files. |
| 133 | */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 134 | static void tsl2583_defaults(struct tsl2583_chip *chip) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 135 | { |
Brian Masney | 4a0f361 | 2016-11-03 08:56:14 -0400 | [diff] [blame] | 136 | /* |
| 137 | * The integration time must be a multiple of 50ms and within the |
| 138 | * range [50, 600] ms. |
| 139 | */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 140 | chip->als_settings.als_time = 100; |
Brian Masney | 4a0f361 | 2016-11-03 08:56:14 -0400 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * This is an index into the gainadj table. Assume clear glass as the |
| 144 | * default. |
| 145 | */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 146 | chip->als_settings.als_gain = 0; |
Brian Masney | 4a0f361 | 2016-11-03 08:56:14 -0400 | [diff] [blame] | 147 | |
| 148 | /* Default gain trim to account for aperture effects */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 149 | chip->als_settings.als_gain_trim = 1000; |
Brian Masney | 4a0f361 | 2016-11-03 08:56:14 -0400 | [diff] [blame] | 150 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 151 | /* Known external ALS reading used for calibration */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 152 | chip->als_settings.als_cal_target = 130; |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 153 | |
| 154 | /* Default lux table. */ |
| 155 | memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux, |
| 156 | sizeof(tsl2583_default_lux)); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 160 | * Reads and calculates current lux value. |
| 161 | * The raw ch0 and ch1 values of the ambient light sensed in the last |
| 162 | * integration cycle are read from the device. |
| 163 | * Time scale factor array values are adjusted based on the integration time. |
| 164 | * The raw values are multiplied by a scale factor, and device gain is obtained |
| 165 | * using gain index. Limit checks are done next, then the ratio of a multiple |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 166 | * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[] |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 167 | * declared above is then scanned to find the first ratio value that is just |
| 168 | * above the ratio we just calculated. The ch0 and ch1 multiplier constants in |
| 169 | * the array are then used along with the time scale factor array values, to |
| 170 | * calculate the lux. |
| 171 | */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 172 | static int tsl2583_get_lux(struct iio_dev *indio_dev) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 173 | { |
| 174 | u16 ch0, ch1; /* separated ch0/ch1 data from device */ |
| 175 | u32 lux; /* raw lux calculated from device data */ |
Bryan Freed | 014fcb1 | 2011-12-02 14:19:30 -0800 | [diff] [blame] | 176 | u64 lux64; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 177 | u32 ratio; |
| 178 | u8 buf[5]; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 179 | struct tsl2583_lux *p; |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 180 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 181 | int i, ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 182 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 183 | ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 184 | if (ret < 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 185 | dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n", |
| 186 | __func__); |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 187 | goto done; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 188 | } |
Brian Masney | 6ba5dee | 2016-11-03 08:56:12 -0400 | [diff] [blame] | 189 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 190 | /* is data new & valid */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 191 | if (!(ret & TSL2583_STA_ADC_INTR)) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 192 | dev_err(&chip->client->dev, "%s: data not valid; returning last value\n", |
| 193 | __func__); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 194 | ret = chip->als_cur_info.lux; /* return LAST VALUE */ |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 195 | goto done; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | for (i = 0; i < 4; i++) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 199 | int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i); |
Aybuke Ozdemir | f6077f4 | 2014-09-24 23:13:21 +0300 | [diff] [blame] | 200 | |
Brian Masney | 6ba5dee | 2016-11-03 08:56:12 -0400 | [diff] [blame] | 201 | ret = i2c_smbus_read_byte_data(chip->client, reg); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 202 | if (ret < 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 203 | dev_err(&chip->client->dev, "%s: failed to read register %x\n", |
| 204 | __func__, reg); |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 205 | goto done; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 206 | } |
Brian Masney | 6ba5dee | 2016-11-03 08:56:12 -0400 | [diff] [blame] | 207 | buf[i] = ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 210 | /* |
Brian Masney | 2fb2848 | 2016-11-12 13:19:36 -0500 | [diff] [blame] | 211 | * Clear the pending interrupt status bit on the chip to allow the next |
| 212 | * integration cycle to start. This has to be done even though this |
| 213 | * driver currently does not support interrupts. |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 214 | */ |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 215 | ret = i2c_smbus_write_byte(chip->client, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 216 | (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN | |
| 217 | TSL2583_CMD_ALS_INT_CLR)); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 218 | if (ret < 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 219 | dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n", |
| 220 | __func__); |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 221 | goto done; /* have no data, so return failure */ |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* extract ALS/lux data */ |
| 225 | ch0 = le16_to_cpup((const __le16 *)&buf[0]); |
| 226 | ch1 = le16_to_cpup((const __le16 *)&buf[2]); |
| 227 | |
| 228 | chip->als_cur_info.als_ch0 = ch0; |
| 229 | chip->als_cur_info.als_ch1 = ch1; |
| 230 | |
| 231 | if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation)) |
| 232 | goto return_max; |
| 233 | |
Cristina Moraru | d59b7b6 | 2015-10-20 22:55:40 +0300 | [diff] [blame] | 234 | if (!ch0) { |
Brian Masney | 043c1da | 2016-11-12 13:19:29 -0500 | [diff] [blame] | 235 | /* |
| 236 | * The sensor appears to be in total darkness so set the |
| 237 | * calculated lux to 0 and return early to avoid a division by |
| 238 | * zero below when calculating the ratio. |
| 239 | */ |
Eva Rachel Retuya | ddab4a0 | 2016-02-18 15:35:40 +0800 | [diff] [blame] | 240 | ret = 0; |
| 241 | chip->als_cur_info.lux = 0; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 242 | goto done; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 243 | } |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 244 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 245 | /* calculate ratio */ |
| 246 | ratio = (ch1 << 15) / ch0; |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 247 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 248 | /* convert to unscaled lux using the pointer to the table */ |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 249 | for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 250 | p->ratio != 0 && p->ratio < ratio; p++) |
| 251 | ; |
| 252 | |
| 253 | if (p->ratio == 0) { |
| 254 | lux = 0; |
| 255 | } else { |
Brian Masney | ed91255 | 2016-11-12 13:19:30 -0500 | [diff] [blame] | 256 | u32 ch0lux, ch1lux; |
| 257 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 258 | ch0lux = ((ch0 * p->ch0) + |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 259 | (gainadj[chip->als_settings.als_gain].ch0 >> 1)) |
| 260 | / gainadj[chip->als_settings.als_gain].ch0; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 261 | ch1lux = ((ch1 * p->ch1) + |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 262 | (gainadj[chip->als_settings.als_gain].ch1 >> 1)) |
| 263 | / gainadj[chip->als_settings.als_gain].ch1; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 264 | |
Brian Masney | ed91255 | 2016-11-12 13:19:30 -0500 | [diff] [blame] | 265 | /* note: lux is 31 bit max at this point */ |
| 266 | if (ch1lux > ch0lux) { |
| 267 | dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n", |
| 268 | __func__); |
| 269 | ret = 0; |
| 270 | chip->als_cur_info.lux = 0; |
| 271 | goto done; |
| 272 | } |
| 273 | |
| 274 | lux = ch0lux - ch1lux; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* adjust for active time scale */ |
| 278 | if (chip->als_time_scale == 0) |
| 279 | lux = 0; |
| 280 | else |
| 281 | lux = (lux + (chip->als_time_scale >> 1)) / |
| 282 | chip->als_time_scale; |
| 283 | |
Brian Masney | 2a1e3f0 | 2016-11-12 13:19:28 -0500 | [diff] [blame] | 284 | /* |
| 285 | * Adjust for active gain scale. |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 286 | * The tsl2583_default_lux tables above have a factor of 8192 built in, |
Bryan Freed | 014fcb1 | 2011-12-02 14:19:30 -0800 | [diff] [blame] | 287 | * so we need to shift right. |
| 288 | * User-specified gain provides a multiplier. |
| 289 | * Apply user-specified gain before shifting right to retain precision. |
| 290 | * Use 64 bits to avoid overflow on multiplication. |
| 291 | * Then go back to 32 bits before division to avoid using div_u64(). |
| 292 | */ |
| 293 | lux64 = lux; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 294 | lux64 = lux64 * chip->als_settings.als_gain_trim; |
Bryan Freed | 014fcb1 | 2011-12-02 14:19:30 -0800 | [diff] [blame] | 295 | lux64 >>= 13; |
| 296 | lux = lux64; |
| 297 | lux = (lux + 500) / 1000; |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 298 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 299 | if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */ |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 300 | return_max: |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 301 | lux = TSL2583_LUX_CALC_OVER_FLOW; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Update the structure with the latest VALID lux. */ |
| 305 | chip->als_cur_info.lux = lux; |
| 306 | ret = lux; |
| 307 | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 308 | done: |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Obtain single reading and calculate the als_gain_trim (later used |
| 314 | * to derive actual lux). |
| 315 | * Return updated gain_trim value. |
| 316 | */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 317 | static int tsl2583_als_calibrate(struct iio_dev *indio_dev) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 318 | { |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 319 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 320 | unsigned int gain_trim_val; |
| 321 | int ret; |
| 322 | int lux_val; |
| 323 | |
Brian Masney | 6ba5dee | 2016-11-03 08:56:12 -0400 | [diff] [blame] | 324 | ret = i2c_smbus_read_byte_data(chip->client, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 325 | TSL2583_CMD_REG | TSL2583_CNTRL); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 326 | if (ret < 0) { |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 327 | dev_err(&chip->client->dev, |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 328 | "%s: failed to read from the CNTRL register\n", |
Brian Masney | 9d5f36d | 2016-10-28 06:00:13 -0400 | [diff] [blame] | 329 | __func__); |
| 330 | return ret; |
| 331 | } |
| 332 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 333 | if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) |
| 334 | != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) { |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 335 | dev_err(&chip->client->dev, |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 336 | "%s: Device is not powered on and/or ADC is not enabled\n", |
| 337 | __func__); |
Brian Masney | 20d823a | 2016-10-28 06:00:14 -0400 | [diff] [blame] | 338 | return -EINVAL; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 339 | } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) { |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 340 | dev_err(&chip->client->dev, |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 341 | "%s: The two ADC channels have not completed an integration cycle\n", |
| 342 | __func__); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 343 | return -ENODATA; |
| 344 | } |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 345 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 346 | lux_val = tsl2583_get_lux(indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 347 | if (lux_val < 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 348 | dev_err(&chip->client->dev, "%s: failed to get lux\n", |
| 349 | __func__); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 350 | return lux_val; |
| 351 | } |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 352 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 353 | gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target) |
| 354 | * chip->als_settings.als_gain_trim) / lux_val); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 355 | if ((gain_trim_val < 250) || (gain_trim_val > 4000)) { |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 356 | dev_err(&chip->client->dev, |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 357 | "%s: trim_val of %d is not within the range [250, 4000]\n", |
| 358 | __func__, gain_trim_val); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 359 | return -ENODATA; |
| 360 | } |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 361 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 362 | chip->als_settings.als_gain_trim = (int)gain_trim_val; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 363 | |
Brian Masney | a8898dc | 2016-11-12 13:19:31 -0500 | [diff] [blame] | 364 | return 0; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 367 | static int tsl2583_set_als_time(struct tsl2583_chip *chip) |
| 368 | { |
| 369 | int als_count, als_time, ret; |
| 370 | u8 val; |
| 371 | |
| 372 | /* determine als integration register */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 373 | als_count = (chip->als_settings.als_time * 100 + 135) / 270; |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 374 | if (!als_count) |
| 375 | als_count = 1; /* ensure at least one cycle */ |
| 376 | |
| 377 | /* convert back to time (encompasses overrides) */ |
| 378 | als_time = (als_count * 27 + 5) / 10; |
| 379 | |
| 380 | val = 256 - als_count; |
| 381 | ret = i2c_smbus_write_byte_data(chip->client, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 382 | TSL2583_CMD_REG | TSL2583_ALS_TIME, |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 383 | val); |
| 384 | if (ret < 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 385 | dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n", |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 386 | __func__, val); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* set chip struct re scaling and saturation */ |
| 391 | chip->als_saturation = als_count * 922; /* 90% of full scale */ |
| 392 | chip->als_time_scale = (als_time + 25) / 50; |
| 393 | |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | static int tsl2583_set_als_gain(struct tsl2583_chip *chip) |
| 398 | { |
| 399 | int ret; |
| 400 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 401 | /* Set the gain based on als_settings struct */ |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 402 | ret = i2c_smbus_write_byte_data(chip->client, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 403 | TSL2583_CMD_REG | TSL2583_GAIN, |
| 404 | chip->als_settings.als_gain); |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 405 | if (ret < 0) |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 406 | dev_err(&chip->client->dev, |
| 407 | "%s: failed to set the gain to %d\n", __func__, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 408 | chip->als_settings.als_gain); |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 413 | static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state) |
| 414 | { |
| 415 | int ret; |
| 416 | |
| 417 | ret = i2c_smbus_write_byte_data(chip->client, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 418 | TSL2583_CMD_REG | TSL2583_CNTRL, state); |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 419 | if (ret < 0) |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 420 | dev_err(&chip->client->dev, |
| 421 | "%s: failed to set the power state to %d\n", __func__, |
| 422 | state); |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 427 | /* |
| 428 | * Turn the device on. |
| 429 | * Configuration must be set before calling this function. |
| 430 | */ |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 431 | static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 432 | { |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 433 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 434 | int ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 435 | |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 436 | /* Power on the device; ADC off. */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 437 | ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON); |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 438 | if (ret < 0) |
| 439 | return ret; |
| 440 | |
| 441 | ret = i2c_smbus_write_byte_data(chip->client, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 442 | TSL2583_CMD_REG | TSL2583_INTERRUPT, |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 443 | TSL2583_INTERRUPT_DISABLED); |
| 444 | if (ret < 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 445 | dev_err(&chip->client->dev, |
| 446 | "%s: failed to disable interrupts\n", __func__); |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 447 | return ret; |
| 448 | } |
| 449 | |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 450 | ret = tsl2583_set_als_time(chip); |
| 451 | if (ret < 0) |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 452 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 453 | |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 454 | ret = tsl2583_set_als_gain(chip); |
| 455 | if (ret < 0) |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 456 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 457 | |
Michael Welling | 7fe704c | 2014-04-09 21:26:45 -0500 | [diff] [blame] | 458 | usleep_range(3000, 3500); |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 459 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 460 | ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON | |
| 461 | TSL2583_CNTL_ADC_ENBL); |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 462 | if (ret < 0) |
Monam Agarwal | 67fc012 | 2014-03-09 10:58:40 +0530 | [diff] [blame] | 463 | return ret; |
Brian Masney | c908fb7 | 2016-11-10 04:25:37 -0500 | [diff] [blame] | 464 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 465 | return ret; |
| 466 | } |
| 467 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 468 | /* Sysfs Interface Functions */ |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 469 | |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 470 | static ssize_t in_illuminance_input_target_show(struct device *dev, |
| 471 | struct device_attribute *attr, |
| 472 | char *buf) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 473 | { |
Lars-Peter Clausen | 96f691f | 2012-05-12 15:39:51 +0200 | [diff] [blame] | 474 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 475 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 476 | int ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 477 | |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 478 | mutex_lock(&chip->als_mutex); |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 479 | ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target); |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 480 | mutex_unlock(&chip->als_mutex); |
| 481 | |
| 482 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 485 | static ssize_t in_illuminance_input_target_store(struct device *dev, |
| 486 | struct device_attribute *attr, |
| 487 | const char *buf, size_t len) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 488 | { |
Lars-Peter Clausen | 96f691f | 2012-05-12 15:39:51 +0200 | [diff] [blame] | 489 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 490 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Jingoo Han | e5e26dd | 2013-08-20 03:31:00 +0100 | [diff] [blame] | 491 | int value; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 492 | |
Brian Masney | f375a49 | 2016-10-28 06:00:20 -0400 | [diff] [blame] | 493 | if (kstrtoint(buf, 0, &value) || !value) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 494 | return -EINVAL; |
| 495 | |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 496 | mutex_lock(&chip->als_mutex); |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 497 | chip->als_settings.als_cal_target = value; |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 498 | mutex_unlock(&chip->als_mutex); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 499 | |
| 500 | return len; |
| 501 | } |
| 502 | |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 503 | static ssize_t in_illuminance_calibrate_store(struct device *dev, |
| 504 | struct device_attribute *attr, |
| 505 | const char *buf, size_t len) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 506 | { |
Lars-Peter Clausen | 96f691f | 2012-05-12 15:39:51 +0200 | [diff] [blame] | 507 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 508 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | acf9ead | 2016-11-12 13:19:16 -0500 | [diff] [blame] | 509 | int value, ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 510 | |
Brian Masney | f375a49 | 2016-10-28 06:00:20 -0400 | [diff] [blame] | 511 | if (kstrtoint(buf, 0, &value) || value != 1) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 514 | mutex_lock(&chip->als_mutex); |
Brian Masney | acf9ead | 2016-11-12 13:19:16 -0500 | [diff] [blame] | 515 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 516 | ret = tsl2583_als_calibrate(indio_dev); |
Brian Masney | acf9ead | 2016-11-12 13:19:16 -0500 | [diff] [blame] | 517 | if (ret < 0) |
| 518 | goto done; |
| 519 | |
| 520 | ret = len; |
| 521 | done: |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 522 | mutex_unlock(&chip->als_mutex); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 523 | |
Brian Masney | acf9ead | 2016-11-12 13:19:16 -0500 | [diff] [blame] | 524 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 527 | static ssize_t in_illuminance_lux_table_show(struct device *dev, |
| 528 | struct device_attribute *attr, |
| 529 | char *buf) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 530 | { |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 531 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 532 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 6d94de6 | 2016-11-12 13:19:25 -0500 | [diff] [blame] | 533 | unsigned int i; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 534 | int offset = 0; |
| 535 | |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 536 | for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) { |
Asaf Vertz | 3a70eb8 | 2015-01-20 12:43:38 +0200 | [diff] [blame] | 537 | offset += sprintf(buf + offset, "%u,%u,%u,", |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 538 | chip->als_settings.als_device_lux[i].ratio, |
| 539 | chip->als_settings.als_device_lux[i].ch0, |
| 540 | chip->als_settings.als_device_lux[i].ch1); |
| 541 | if (chip->als_settings.als_device_lux[i].ratio == 0) { |
Eva Rachel Retuya | 7f3829a | 2016-02-18 15:35:38 +0800 | [diff] [blame] | 542 | /* |
| 543 | * We just printed the first "0" entry. |
| 544 | * Now get rid of the extra "," and break. |
| 545 | */ |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 546 | offset--; |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | offset += sprintf(buf + offset, "\n"); |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 552 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 553 | return offset; |
| 554 | } |
| 555 | |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 556 | static ssize_t in_illuminance_lux_table_store(struct device *dev, |
| 557 | struct device_attribute *attr, |
| 558 | const char *buf, size_t len) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 559 | { |
Lars-Peter Clausen | 96f691f | 2012-05-12 15:39:51 +0200 | [diff] [blame] | 560 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 561 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 562 | const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3; |
Dan Carpenter | 0e8d2b0 | 2016-11-24 16:38:07 +0300 | [diff] [blame] | 563 | int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1]; |
Brian Masney | 6d94de6 | 2016-11-12 13:19:25 -0500 | [diff] [blame] | 564 | int ret = -EINVAL; |
| 565 | unsigned int n; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 566 | |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 567 | mutex_lock(&chip->als_mutex); |
| 568 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 569 | get_options(buf, ARRAY_SIZE(value), value); |
| 570 | |
Brian Masney | 2a1e3f0 | 2016-11-12 13:19:28 -0500 | [diff] [blame] | 571 | /* |
| 572 | * We now have an array of ints starting at value[1], and |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 573 | * enumerated by value[0]. |
| 574 | * We expect each group of three ints is one table entry, |
| 575 | * and the last table entry is all 0. |
| 576 | */ |
| 577 | n = value[0]; |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 578 | if ((n % 3) || n < 6 || n > max_ints) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 579 | dev_err(dev, |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 580 | "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n", |
| 581 | __func__, max_ints); |
Brian Masney | 84bc170 | 2016-10-28 06:00:16 -0400 | [diff] [blame] | 582 | goto done; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 583 | } |
Brian Masney | 1ad5136 | 2016-11-12 13:19:32 -0500 | [diff] [blame] | 584 | if ((value[n - 2] | value[n - 1] | value[n]) != 0) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 585 | dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n", |
| 586 | __func__); |
Brian Masney | 84bc170 | 2016-10-28 06:00:16 -0400 | [diff] [blame] | 587 | goto done; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Brian Masney | 0b6b361 | 2016-11-12 13:19:34 -0500 | [diff] [blame] | 590 | memcpy(chip->als_settings.als_device_lux, &value[1], |
| 591 | value[0] * sizeof(value[1])); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 592 | |
Brian Masney | 84bc170 | 2016-10-28 06:00:16 -0400 | [diff] [blame] | 593 | ret = len; |
| 594 | |
| 595 | done: |
Brian Masney | 1b7e9e2 | 2016-10-28 06:00:21 -0400 | [diff] [blame] | 596 | mutex_unlock(&chip->als_mutex); |
| 597 | |
Brian Masney | 84bc170 | 2016-10-28 06:00:16 -0400 | [diff] [blame] | 598 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 599 | } |
| 600 | |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 601 | static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111"); |
| 602 | static IIO_CONST_ATTR(in_illuminance_integration_time_available, |
| 603 | "0.000050 0.000100 0.000150 0.000200 0.000250 0.000300 0.000350 0.000400 0.000450 0.000500 0.000550 0.000600 0.000650"); |
| 604 | static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0); |
| 605 | static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0); |
| 606 | static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 607 | |
| 608 | static struct attribute *sysfs_attrs_ctrl[] = { |
Brian Masney | b2fa81b | 2016-10-28 06:00:19 -0400 | [diff] [blame] | 609 | &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr, |
| 610 | &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr, |
| 611 | &iio_dev_attr_in_illuminance_input_target.dev_attr.attr, |
| 612 | &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr, |
| 613 | &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr, |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 614 | NULL |
| 615 | }; |
| 616 | |
Bhumika Goyal | 58fce73 | 2016-09-26 10:31:39 +0530 | [diff] [blame] | 617 | static const struct attribute_group tsl2583_attribute_group = { |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 618 | .attrs = sysfs_attrs_ctrl, |
| 619 | }; |
| 620 | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 621 | static const struct iio_chan_spec tsl2583_channels[] = { |
| 622 | { |
| 623 | .type = IIO_LIGHT, |
| 624 | .modified = 1, |
| 625 | .channel2 = IIO_MOD_LIGHT_IR, |
| 626 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 627 | }, |
| 628 | { |
| 629 | .type = IIO_LIGHT, |
| 630 | .modified = 1, |
| 631 | .channel2 = IIO_MOD_LIGHT_BOTH, |
| 632 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 633 | }, |
| 634 | { |
| 635 | .type = IIO_LIGHT, |
| 636 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
| 637 | BIT(IIO_CHAN_INFO_CALIBBIAS) | |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 638 | BIT(IIO_CHAN_INFO_CALIBSCALE) | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 639 | BIT(IIO_CHAN_INFO_INT_TIME), |
| 640 | }, |
| 641 | }; |
| 642 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 643 | static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on) |
| 644 | { |
| 645 | int ret; |
| 646 | |
| 647 | if (on) { |
| 648 | ret = pm_runtime_get_sync(&chip->client->dev); |
| 649 | if (ret < 0) |
| 650 | pm_runtime_put_noidle(&chip->client->dev); |
| 651 | } else { |
| 652 | pm_runtime_mark_last_busy(&chip->client->dev); |
| 653 | ret = pm_runtime_put_autosuspend(&chip->client->dev); |
| 654 | } |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 659 | static int tsl2583_read_raw(struct iio_dev *indio_dev, |
| 660 | struct iio_chan_spec const *chan, |
| 661 | int *val, int *val2, long mask) |
| 662 | { |
| 663 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 664 | int ret, pm_ret; |
| 665 | |
| 666 | ret = tsl2583_set_pm_runtime_busy(chip, true); |
| 667 | if (ret < 0) |
| 668 | return ret; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 669 | |
| 670 | mutex_lock(&chip->als_mutex); |
| 671 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 672 | ret = -EINVAL; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 673 | switch (mask) { |
| 674 | case IIO_CHAN_INFO_RAW: |
| 675 | if (chan->type == IIO_LIGHT) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 676 | ret = tsl2583_get_lux(indio_dev); |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 677 | if (ret < 0) |
| 678 | goto read_done; |
| 679 | |
| 680 | /* |
| 681 | * From page 20 of the TSL2581, TSL2583 data |
| 682 | * sheet (TAOS134 − MARCH 2011): |
| 683 | * |
| 684 | * One of the photodiodes (channel 0) is |
| 685 | * sensitive to both visible and infrared light, |
| 686 | * while the second photodiode (channel 1) is |
| 687 | * sensitive primarily to infrared light. |
| 688 | */ |
| 689 | if (chan->channel2 == IIO_MOD_LIGHT_BOTH) |
| 690 | *val = chip->als_cur_info.als_ch0; |
| 691 | else |
| 692 | *val = chip->als_cur_info.als_ch1; |
| 693 | |
| 694 | ret = IIO_VAL_INT; |
| 695 | } |
| 696 | break; |
| 697 | case IIO_CHAN_INFO_PROCESSED: |
| 698 | if (chan->type == IIO_LIGHT) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 699 | ret = tsl2583_get_lux(indio_dev); |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 700 | if (ret < 0) |
| 701 | goto read_done; |
| 702 | |
| 703 | *val = ret; |
| 704 | ret = IIO_VAL_INT; |
| 705 | } |
| 706 | break; |
| 707 | case IIO_CHAN_INFO_CALIBBIAS: |
| 708 | if (chan->type == IIO_LIGHT) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 709 | *val = chip->als_settings.als_gain_trim; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 710 | ret = IIO_VAL_INT; |
| 711 | } |
| 712 | break; |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 713 | case IIO_CHAN_INFO_CALIBSCALE: |
| 714 | if (chan->type == IIO_LIGHT) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 715 | *val = gainadj[chip->als_settings.als_gain].mean; |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 716 | ret = IIO_VAL_INT; |
| 717 | } |
| 718 | break; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 719 | case IIO_CHAN_INFO_INT_TIME: |
| 720 | if (chan->type == IIO_LIGHT) { |
| 721 | *val = 0; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 722 | *val2 = chip->als_settings.als_time; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 723 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 724 | } |
| 725 | break; |
| 726 | default: |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | read_done: |
| 731 | mutex_unlock(&chip->als_mutex); |
| 732 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 733 | if (ret < 0) |
| 734 | return ret; |
| 735 | |
| 736 | /* |
| 737 | * Preserve the ret variable if the call to |
| 738 | * tsl2583_set_pm_runtime_busy() is successful so the reading |
| 739 | * (if applicable) is returned to user space. |
| 740 | */ |
| 741 | pm_ret = tsl2583_set_pm_runtime_busy(chip, false); |
| 742 | if (pm_ret < 0) |
| 743 | return pm_ret; |
| 744 | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | static int tsl2583_write_raw(struct iio_dev *indio_dev, |
| 749 | struct iio_chan_spec const *chan, |
| 750 | int val, int val2, long mask) |
| 751 | { |
| 752 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 753 | int ret; |
| 754 | |
| 755 | ret = tsl2583_set_pm_runtime_busy(chip, true); |
| 756 | if (ret < 0) |
| 757 | return ret; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 758 | |
| 759 | mutex_lock(&chip->als_mutex); |
| 760 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 761 | ret = -EINVAL; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 762 | switch (mask) { |
| 763 | case IIO_CHAN_INFO_CALIBBIAS: |
| 764 | if (chan->type == IIO_LIGHT) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 765 | chip->als_settings.als_gain_trim = val; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 766 | ret = 0; |
| 767 | } |
| 768 | break; |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 769 | case IIO_CHAN_INFO_CALIBSCALE: |
| 770 | if (chan->type == IIO_LIGHT) { |
Brian Masney | 6d94de6 | 2016-11-12 13:19:25 -0500 | [diff] [blame] | 771 | unsigned int i; |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 772 | |
| 773 | for (i = 0; i < ARRAY_SIZE(gainadj); i++) { |
| 774 | if (gainadj[i].mean == val) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 775 | chip->als_settings.als_gain = i; |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 776 | ret = tsl2583_set_als_gain(chip); |
Brian Masney | 143c30f | 2016-10-28 06:00:18 -0400 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | break; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 782 | case IIO_CHAN_INFO_INT_TIME: |
| 783 | if (chan->type == IIO_LIGHT && !val && val2 >= 50 && |
| 784 | val2 <= 650 && !(val2 % 50)) { |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 785 | chip->als_settings.als_time = val2; |
Brian Masney | 2167769 | 2016-11-10 04:25:38 -0500 | [diff] [blame] | 786 | ret = tsl2583_set_als_time(chip); |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 787 | } |
| 788 | break; |
| 789 | default: |
| 790 | break; |
| 791 | } |
| 792 | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 793 | mutex_unlock(&chip->als_mutex); |
| 794 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 795 | if (ret < 0) |
| 796 | return ret; |
| 797 | |
| 798 | ret = tsl2583_set_pm_runtime_busy(chip, false); |
| 799 | if (ret < 0) |
| 800 | return ret; |
| 801 | |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 802 | return ret; |
| 803 | } |
| 804 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 805 | static const struct iio_info tsl2583_info = { |
| 806 | .attrs = &tsl2583_attribute_group, |
| 807 | .driver_module = THIS_MODULE, |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 808 | .read_raw = tsl2583_read_raw, |
| 809 | .write_raw = tsl2583_write_raw, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 810 | }; |
| 811 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 812 | static int tsl2583_probe(struct i2c_client *clientp, |
| 813 | const struct i2c_device_id *idp) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 814 | { |
Brian Masney | 6bd0cb2 | 2016-11-03 08:56:13 -0400 | [diff] [blame] | 815 | int ret; |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 816 | struct tsl2583_chip *chip; |
| 817 | struct iio_dev *indio_dev; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 818 | |
| 819 | if (!i2c_check_functionality(clientp->adapter, |
Eva Rachel Retuya | ca52c88 | 2016-02-18 15:35:36 +0800 | [diff] [blame] | 820 | I2C_FUNC_SMBUS_BYTE_DATA)) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 821 | dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n", |
| 822 | __func__); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 823 | return -EOPNOTSUPP; |
| 824 | } |
| 825 | |
Sachin Kamat | 664716ae | 2013-09-05 10:29:00 +0100 | [diff] [blame] | 826 | indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip)); |
| 827 | if (!indio_dev) |
| 828 | return -ENOMEM; |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 829 | |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 830 | chip = iio_priv(indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 831 | chip->client = clientp; |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 832 | i2c_set_clientdata(clientp, indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 833 | |
| 834 | mutex_init(&chip->als_mutex); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 835 | |
Brian Masney | 6bd0cb2 | 2016-11-03 08:56:13 -0400 | [diff] [blame] | 836 | ret = i2c_smbus_read_byte_data(clientp, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 837 | TSL2583_CMD_REG | TSL2583_CHIPID); |
Brian Masney | 6bd0cb2 | 2016-11-03 08:56:13 -0400 | [diff] [blame] | 838 | if (ret < 0) { |
| 839 | dev_err(&clientp->dev, |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 840 | "%s: failed to read the chip ID register\n", __func__); |
Brian Masney | 6bd0cb2 | 2016-11-03 08:56:13 -0400 | [diff] [blame] | 841 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Brian Masney | 6bd0cb2 | 2016-11-03 08:56:13 -0400 | [diff] [blame] | 844 | if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 845 | dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n", |
| 846 | __func__, ret); |
Sachin Kamat | 664716ae | 2013-09-05 10:29:00 +0100 | [diff] [blame] | 847 | return -EINVAL; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 850 | indio_dev->info = &tsl2583_info; |
Brian Masney | b3d9415 | 2016-10-28 06:00:17 -0400 | [diff] [blame] | 851 | indio_dev->channels = tsl2583_channels; |
| 852 | indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels); |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 853 | indio_dev->dev.parent = &clientp->dev; |
| 854 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 855 | indio_dev->name = chip->client->name; |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 856 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 857 | pm_runtime_enable(&clientp->dev); |
| 858 | pm_runtime_set_autosuspend_delay(&clientp->dev, |
| 859 | TSL2583_POWER_OFF_DELAY_MS); |
| 860 | pm_runtime_use_autosuspend(&clientp->dev); |
| 861 | |
Ioana Ciornei | 86727e3 | 2015-10-02 13:37:49 +0300 | [diff] [blame] | 862 | ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 863 | if (ret) { |
Brian Masney | 24ad430 | 2016-11-12 13:19:22 -0500 | [diff] [blame] | 864 | dev_err(&clientp->dev, "%s: iio registration failed\n", |
| 865 | __func__); |
Sachin Kamat | 664716ae | 2013-09-05 10:29:00 +0100 | [diff] [blame] | 866 | return ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* Load up the V2 defaults (these are hard coded defaults for now) */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 870 | tsl2583_defaults(chip); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 871 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 872 | dev_info(&clientp->dev, "Light sensor found.\n"); |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 873 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 874 | return 0; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 877 | static int tsl2583_remove(struct i2c_client *client) |
| 878 | { |
| 879 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 880 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
| 881 | |
| 882 | iio_device_unregister(indio_dev); |
| 883 | |
| 884 | pm_runtime_disable(&client->dev); |
| 885 | pm_runtime_set_suspended(&client->dev); |
| 886 | pm_runtime_put_noidle(&client->dev); |
| 887 | |
| 888 | return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF); |
| 889 | } |
| 890 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 891 | static int __maybe_unused tsl2583_suspend(struct device *dev) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 892 | { |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 893 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 894 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 0859fdd | 2016-11-12 13:19:39 -0500 | [diff] [blame] | 895 | int ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 896 | |
| 897 | mutex_lock(&chip->als_mutex); |
| 898 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 899 | ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 900 | |
| 901 | mutex_unlock(&chip->als_mutex); |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 902 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 903 | return ret; |
| 904 | } |
| 905 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 906 | static int __maybe_unused tsl2583_resume(struct device *dev) |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 907 | { |
Lars-Peter Clausen | 01788c5 | 2012-02-20 19:37:05 +0100 | [diff] [blame] | 908 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
Jonathan Cameron | e05e5f2 | 2011-08-12 17:08:52 +0100 | [diff] [blame] | 909 | struct tsl2583_chip *chip = iio_priv(indio_dev); |
Brian Masney | 0859fdd | 2016-11-12 13:19:39 -0500 | [diff] [blame] | 910 | int ret; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 911 | |
| 912 | mutex_lock(&chip->als_mutex); |
| 913 | |
Brian Masney | 2ff8a35 | 2016-11-12 13:19:18 -0500 | [diff] [blame] | 914 | ret = tsl2583_chip_init_and_power_on(indio_dev); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 915 | |
| 916 | mutex_unlock(&chip->als_mutex); |
Brian Masney | 184f916 | 2016-11-12 13:19:26 -0500 | [diff] [blame] | 917 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 918 | return ret; |
| 919 | } |
| 920 | |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 921 | static const struct dev_pm_ops tsl2583_pm_ops = { |
| 922 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 923 | pm_runtime_force_resume) |
| 924 | SET_RUNTIME_PM_OPS(tsl2583_suspend, tsl2583_resume, NULL) |
| 925 | }; |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 926 | |
Arvind Yadav | 3282fa3 | 2017-08-20 00:17:38 +0530 | [diff] [blame] | 927 | static const struct i2c_device_id tsl2583_idtable[] = { |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 928 | { "tsl2580", 0 }, |
| 929 | { "tsl2581", 1 }, |
| 930 | { "tsl2583", 2 }, |
| 931 | {} |
| 932 | }; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 933 | MODULE_DEVICE_TABLE(i2c, tsl2583_idtable); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 934 | |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 935 | static const struct of_device_id tsl2583_of_match[] = { |
Brian Masney | a3b6b4b | 2016-10-28 06:00:12 -0400 | [diff] [blame] | 936 | { .compatible = "amstaos,tsl2580", }, |
| 937 | { .compatible = "amstaos,tsl2581", }, |
| 938 | { .compatible = "amstaos,tsl2583", }, |
| 939 | { }, |
| 940 | }; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 941 | MODULE_DEVICE_TABLE(of, tsl2583_of_match); |
Brian Masney | a3b6b4b | 2016-10-28 06:00:12 -0400 | [diff] [blame] | 942 | |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 943 | /* Driver definition */ |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 944 | static struct i2c_driver tsl2583_driver = { |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 945 | .driver = { |
| 946 | .name = "tsl2583", |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 947 | .pm = &tsl2583_pm_ops, |
| 948 | .of_match_table = tsl2583_of_match, |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 949 | }, |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 950 | .id_table = tsl2583_idtable, |
| 951 | .probe = tsl2583_probe, |
Brian Masney | 371894f | 2017-04-25 04:06:33 -0400 | [diff] [blame] | 952 | .remove = tsl2583_remove, |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 953 | }; |
Brian Masney | 686c592 | 2016-11-12 13:19:23 -0500 | [diff] [blame] | 954 | module_i2c_driver(tsl2583_driver); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 955 | |
Brian Masney | c45a226 | 2016-11-12 13:19:40 -0500 | [diff] [blame] | 956 | MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>"); |
| 957 | MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>"); |
Jon Brenner | ac4f6ee | 2011-04-15 18:38:43 +0100 | [diff] [blame] | 958 | MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver"); |
| 959 | MODULE_LICENSE("GPL"); |