blob: a760d14e146a8f963e78cc46482aae9130723cec [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jon Brennerac4f6ee2011-04-15 18:38:43 +01002/*
3 * Device driver for monitoring ambient light intensity (lux)
Brian Masneyaadc4c92016-11-12 13:19:35 -05004 * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
Jon Brennerac4f6ee2011-04-15 18:38:43 +01005 *
6 * Copyright (c) 2011, TAOS Corporation.
Brian Masney371894f2017-04-25 04:06:33 -04007 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
Jon Brennerac4f6ee2011-04-15 18:38:43 +01008 */
9
10#include <linux/kernel.h>
11#include <linux/i2c.h>
12#include <linux/errno.h>
13#include <linux/delay.h>
14#include <linux/string.h>
15#include <linux/mutex.h>
16#include <linux/unistd.h>
17#include <linux/slab.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040018#include <linux/module.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010019#include <linux/iio/iio.h>
Brian Masneyb2fa81b2016-10-28 06:00:19 -040020#include <linux/iio/sysfs.h>
Brian Masney371894f2017-04-25 04:06:33 -040021#include <linux/pm_runtime.h>
Jon Brennerac4f6ee2011-04-15 18:38:43 +010022
Jon Brennerac4f6ee2011-04-15 18:38:43 +010023/* Device Registers and Masks */
Brian Masney686c5922016-11-12 13:19:23 -050024#define TSL2583_CNTRL 0x00
25#define TSL2583_ALS_TIME 0X01
26#define TSL2583_INTERRUPT 0x02
27#define TSL2583_GAIN 0x07
28#define TSL2583_REVID 0x11
29#define TSL2583_CHIPID 0x12
30#define TSL2583_ALS_CHAN0LO 0x14
31#define TSL2583_ALS_CHAN0HI 0x15
32#define TSL2583_ALS_CHAN1LO 0x16
33#define TSL2583_ALS_CHAN1HI 0x17
34#define TSL2583_TMR_LO 0x18
35#define TSL2583_TMR_HI 0x19
Jon Brennerac4f6ee2011-04-15 18:38:43 +010036
37/* tsl2583 cmd reg masks */
Brian Masney686c5922016-11-12 13:19:23 -050038#define TSL2583_CMD_REG 0x80
39#define TSL2583_CMD_SPL_FN 0x60
Brian Masney8391c882016-11-12 13:19:24 -050040#define TSL2583_CMD_ALS_INT_CLR 0x01
Jon Brennerac4f6ee2011-04-15 18:38:43 +010041
42/* tsl2583 cntrl reg masks */
Brian Masney8391c882016-11-12 13:19:24 -050043#define TSL2583_CNTL_ADC_ENBL 0x02
Brian Masney686c5922016-11-12 13:19:23 -050044#define TSL2583_CNTL_PWR_OFF 0x00
45#define TSL2583_CNTL_PWR_ON 0x01
Jon Brennerac4f6ee2011-04-15 18:38:43 +010046
47/* tsl2583 status reg masks */
Brian Masney8391c882016-11-12 13:19:24 -050048#define TSL2583_STA_ADC_VALID 0x01
49#define TSL2583_STA_ADC_INTR 0x10
Jon Brennerac4f6ee2011-04-15 18:38:43 +010050
51/* Lux calculation constants */
Brian Masney8391c882016-11-12 13:19:24 -050052#define TSL2583_LUX_CALC_OVER_FLOW 65535
Jon Brennerac4f6ee2011-04-15 18:38:43 +010053
Brian Masneyc908fb72016-11-10 04:25:37 -050054#define TSL2583_INTERRUPT_DISABLED 0x00
55
Brian Masney6bd0cb22016-11-03 08:56:13 -040056#define TSL2583_CHIP_ID 0x90
57#define TSL2583_CHIP_ID_MASK 0xf0
58
Brian Masney371894f2017-04-25 04:06:33 -040059#define TSL2583_POWER_OFF_DELAY_MS 2000
60
Jon Brennerac4f6ee2011-04-15 18:38:43 +010061/* Per-device data */
Brian Masney686c5922016-11-12 13:19:23 -050062struct tsl2583_als_info {
Jon Brennerac4f6ee2011-04-15 18:38:43 +010063 u16 als_ch0;
64 u16 als_ch1;
65 u16 lux;
66};
67
Brian Masney0b6b3612016-11-12 13:19:34 -050068struct tsl2583_lux {
69 unsigned int ratio;
70 unsigned int ch0;
71 unsigned int ch1;
72};
73
74static const struct tsl2583_lux tsl2583_default_lux[] = {
75 { 9830, 8520, 15729 },
76 { 12452, 10807, 23344 },
77 { 14746, 6383, 11705 },
78 { 17695, 4063, 6554 },
79 { 0, 0, 0 } /* Termination segment */
80};
81
82#define TSL2583_MAX_LUX_TABLE_ENTRIES 11
83
Brian Masney686c5922016-11-12 13:19:23 -050084struct tsl2583_settings {
Jon Brennerac4f6ee2011-04-15 18:38:43 +010085 int als_time;
86 int als_gain;
87 int als_gain_trim;
88 int als_cal_target;
Brian Masney0b6b3612016-11-12 13:19:34 -050089
90 /*
91 * This structure is intentionally large to accommodate updates via
92 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
93 * Assumption is that one and only one type of glass used.
94 */
95 struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
Jon Brennerac4f6ee2011-04-15 18:38:43 +010096};
97
98struct tsl2583_chip {
99 struct mutex als_mutex;
100 struct i2c_client *client;
Brian Masney686c5922016-11-12 13:19:23 -0500101 struct tsl2583_als_info als_cur_info;
102 struct tsl2583_settings als_settings;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100103 int als_time_scale;
104 int als_saturation;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100105};
106
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100107struct gainadj {
108 s16 ch0;
109 s16 ch1;
Brian Masney143c30f2016-10-28 06:00:18 -0400110 s16 mean;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100111};
112
113/* Index = (0 - 3) Used to validate the gain selection index */
114static const struct gainadj gainadj[] = {
Brian Masney143c30f2016-10-28 06:00:18 -0400115 { 1, 1, 1 },
116 { 8, 8, 8 },
117 { 16, 16, 16 },
118 { 107, 115, 111 }
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100119};
120
121/*
122 * Provides initial operational parameter defaults.
123 * These defaults may be changed through the device's sysfs files.
124 */
Brian Masney686c5922016-11-12 13:19:23 -0500125static void tsl2583_defaults(struct tsl2583_chip *chip)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100126{
Brian Masney4a0f3612016-11-03 08:56:14 -0400127 /*
128 * The integration time must be a multiple of 50ms and within the
129 * range [50, 600] ms.
130 */
Brian Masney686c5922016-11-12 13:19:23 -0500131 chip->als_settings.als_time = 100;
Brian Masney4a0f3612016-11-03 08:56:14 -0400132
133 /*
134 * This is an index into the gainadj table. Assume clear glass as the
135 * default.
136 */
Brian Masney686c5922016-11-12 13:19:23 -0500137 chip->als_settings.als_gain = 0;
Brian Masney4a0f3612016-11-03 08:56:14 -0400138
139 /* Default gain trim to account for aperture effects */
Brian Masney686c5922016-11-12 13:19:23 -0500140 chip->als_settings.als_gain_trim = 1000;
Brian Masney4a0f3612016-11-03 08:56:14 -0400141
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100142 /* Known external ALS reading used for calibration */
Brian Masney686c5922016-11-12 13:19:23 -0500143 chip->als_settings.als_cal_target = 130;
Brian Masney0b6b3612016-11-12 13:19:34 -0500144
145 /* Default lux table. */
146 memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
147 sizeof(tsl2583_default_lux));
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100148}
149
150/*
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100151 * Reads and calculates current lux value.
152 * The raw ch0 and ch1 values of the ambient light sensed in the last
153 * integration cycle are read from the device.
154 * Time scale factor array values are adjusted based on the integration time.
155 * The raw values are multiplied by a scale factor, and device gain is obtained
156 * using gain index. Limit checks are done next, then the ratio of a multiple
Brian Masney0b6b3612016-11-12 13:19:34 -0500157 * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100158 * declared above is then scanned to find the first ratio value that is just
159 * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
160 * the array are then used along with the time scale factor array values, to
161 * calculate the lux.
162 */
Brian Masney686c5922016-11-12 13:19:23 -0500163static int tsl2583_get_lux(struct iio_dev *indio_dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100164{
165 u16 ch0, ch1; /* separated ch0/ch1 data from device */
166 u32 lux; /* raw lux calculated from device data */
Bryan Freed014fcb12011-12-02 14:19:30 -0800167 u64 lux64;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100168 u32 ratio;
169 u8 buf[5];
Brian Masney686c5922016-11-12 13:19:23 -0500170 struct tsl2583_lux *p;
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100171 struct tsl2583_chip *chip = iio_priv(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100172 int i, ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100173
Brian Masney686c5922016-11-12 13:19:23 -0500174 ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100175 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500176 dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
177 __func__);
Brian Masneyb3d94152016-10-28 06:00:17 -0400178 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100179 }
Brian Masney6ba5dee2016-11-03 08:56:12 -0400180
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100181 /* is data new & valid */
Brian Masney686c5922016-11-12 13:19:23 -0500182 if (!(ret & TSL2583_STA_ADC_INTR)) {
Brian Masney24ad4302016-11-12 13:19:22 -0500183 dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
184 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100185 ret = chip->als_cur_info.lux; /* return LAST VALUE */
Brian Masneyb3d94152016-10-28 06:00:17 -0400186 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100187 }
188
189 for (i = 0; i < 4; i++) {
Brian Masney686c5922016-11-12 13:19:23 -0500190 int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
Aybuke Ozdemirf6077f42014-09-24 23:13:21 +0300191
Brian Masney6ba5dee2016-11-03 08:56:12 -0400192 ret = i2c_smbus_read_byte_data(chip->client, reg);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100193 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500194 dev_err(&chip->client->dev, "%s: failed to read register %x\n",
195 __func__, reg);
Brian Masneyb3d94152016-10-28 06:00:17 -0400196 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100197 }
Brian Masney6ba5dee2016-11-03 08:56:12 -0400198 buf[i] = ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100199 }
200
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800201 /*
Brian Masney2fb28482016-11-12 13:19:36 -0500202 * Clear the pending interrupt status bit on the chip to allow the next
203 * integration cycle to start. This has to be done even though this
204 * driver currently does not support interrupts.
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800205 */
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100206 ret = i2c_smbus_write_byte(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500207 (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
208 TSL2583_CMD_ALS_INT_CLR));
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100209 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500210 dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
211 __func__);
Brian Masneyb3d94152016-10-28 06:00:17 -0400212 goto done; /* have no data, so return failure */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100213 }
214
215 /* extract ALS/lux data */
216 ch0 = le16_to_cpup((const __le16 *)&buf[0]);
217 ch1 = le16_to_cpup((const __le16 *)&buf[2]);
218
219 chip->als_cur_info.als_ch0 = ch0;
220 chip->als_cur_info.als_ch1 = ch1;
221
222 if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
223 goto return_max;
224
Cristina Morarud59b7b62015-10-20 22:55:40 +0300225 if (!ch0) {
Brian Masney043c1da2016-11-12 13:19:29 -0500226 /*
227 * The sensor appears to be in total darkness so set the
228 * calculated lux to 0 and return early to avoid a division by
229 * zero below when calculating the ratio.
230 */
Eva Rachel Retuyaddab4a02016-02-18 15:35:40 +0800231 ret = 0;
232 chip->als_cur_info.lux = 0;
Brian Masneyb3d94152016-10-28 06:00:17 -0400233 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100234 }
Brian Masney184f9162016-11-12 13:19:26 -0500235
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100236 /* calculate ratio */
237 ratio = (ch1 << 15) / ch0;
Brian Masney184f9162016-11-12 13:19:26 -0500238
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100239 /* convert to unscaled lux using the pointer to the table */
Brian Masney0b6b3612016-11-12 13:19:34 -0500240 for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100241 p->ratio != 0 && p->ratio < ratio; p++)
242 ;
243
244 if (p->ratio == 0) {
245 lux = 0;
246 } else {
Brian Masneyed912552016-11-12 13:19:30 -0500247 u32 ch0lux, ch1lux;
248
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100249 ch0lux = ((ch0 * p->ch0) +
Brian Masney686c5922016-11-12 13:19:23 -0500250 (gainadj[chip->als_settings.als_gain].ch0 >> 1))
251 / gainadj[chip->als_settings.als_gain].ch0;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100252 ch1lux = ((ch1 * p->ch1) +
Brian Masney686c5922016-11-12 13:19:23 -0500253 (gainadj[chip->als_settings.als_gain].ch1 >> 1))
254 / gainadj[chip->als_settings.als_gain].ch1;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100255
Brian Masneyed912552016-11-12 13:19:30 -0500256 /* note: lux is 31 bit max at this point */
257 if (ch1lux > ch0lux) {
258 dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
259 __func__);
260 ret = 0;
261 chip->als_cur_info.lux = 0;
262 goto done;
263 }
264
265 lux = ch0lux - ch1lux;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100266 }
267
268 /* adjust for active time scale */
269 if (chip->als_time_scale == 0)
270 lux = 0;
271 else
272 lux = (lux + (chip->als_time_scale >> 1)) /
273 chip->als_time_scale;
274
Brian Masney2a1e3f02016-11-12 13:19:28 -0500275 /*
276 * Adjust for active gain scale.
Brian Masney0b6b3612016-11-12 13:19:34 -0500277 * The tsl2583_default_lux tables above have a factor of 8192 built in,
Bryan Freed014fcb12011-12-02 14:19:30 -0800278 * so we need to shift right.
279 * User-specified gain provides a multiplier.
280 * Apply user-specified gain before shifting right to retain precision.
281 * Use 64 bits to avoid overflow on multiplication.
282 * Then go back to 32 bits before division to avoid using div_u64().
283 */
284 lux64 = lux;
Brian Masney686c5922016-11-12 13:19:23 -0500285 lux64 = lux64 * chip->als_settings.als_gain_trim;
Bryan Freed014fcb12011-12-02 14:19:30 -0800286 lux64 >>= 13;
287 lux = lux64;
288 lux = (lux + 500) / 1000;
Brian Masney184f9162016-11-12 13:19:26 -0500289
Brian Masney686c5922016-11-12 13:19:23 -0500290 if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100291return_max:
Brian Masney686c5922016-11-12 13:19:23 -0500292 lux = TSL2583_LUX_CALC_OVER_FLOW;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100293 }
294
295 /* Update the structure with the latest VALID lux. */
296 chip->als_cur_info.lux = lux;
297 ret = lux;
298
Brian Masneyb3d94152016-10-28 06:00:17 -0400299done:
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100300 return ret;
301}
302
303/*
304 * Obtain single reading and calculate the als_gain_trim (later used
305 * to derive actual lux).
306 * Return updated gain_trim value.
307 */
Brian Masney686c5922016-11-12 13:19:23 -0500308static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100309{
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100310 struct tsl2583_chip *chip = iio_priv(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100311 unsigned int gain_trim_val;
312 int ret;
313 int lux_val;
314
Brian Masney6ba5dee2016-11-03 08:56:12 -0400315 ret = i2c_smbus_read_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500316 TSL2583_CMD_REG | TSL2583_CNTRL);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100317 if (ret < 0) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100318 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500319 "%s: failed to read from the CNTRL register\n",
Brian Masney9d5f36d2016-10-28 06:00:13 -0400320 __func__);
321 return ret;
322 }
323
Brian Masney686c5922016-11-12 13:19:23 -0500324 if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
325 != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100326 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500327 "%s: Device is not powered on and/or ADC is not enabled\n",
328 __func__);
Brian Masney20d823a2016-10-28 06:00:14 -0400329 return -EINVAL;
Brian Masney686c5922016-11-12 13:19:23 -0500330 } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100331 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500332 "%s: The two ADC channels have not completed an integration cycle\n",
333 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100334 return -ENODATA;
335 }
Brian Masney184f9162016-11-12 13:19:26 -0500336
Brian Masney686c5922016-11-12 13:19:23 -0500337 lux_val = tsl2583_get_lux(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100338 if (lux_val < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500339 dev_err(&chip->client->dev, "%s: failed to get lux\n",
340 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100341 return lux_val;
342 }
Brian Masney184f9162016-11-12 13:19:26 -0500343
Brian Masney686c5922016-11-12 13:19:23 -0500344 gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
345 * chip->als_settings.als_gain_trim) / lux_val);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100346 if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100347 dev_err(&chip->client->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500348 "%s: trim_val of %d is not within the range [250, 4000]\n",
349 __func__, gain_trim_val);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100350 return -ENODATA;
351 }
Brian Masney184f9162016-11-12 13:19:26 -0500352
Brian Masney686c5922016-11-12 13:19:23 -0500353 chip->als_settings.als_gain_trim = (int)gain_trim_val;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100354
Brian Masneya8898dc2016-11-12 13:19:31 -0500355 return 0;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100356}
357
Brian Masney21677692016-11-10 04:25:38 -0500358static int tsl2583_set_als_time(struct tsl2583_chip *chip)
359{
360 int als_count, als_time, ret;
361 u8 val;
362
363 /* determine als integration register */
Brian Masney686c5922016-11-12 13:19:23 -0500364 als_count = (chip->als_settings.als_time * 100 + 135) / 270;
Brian Masney21677692016-11-10 04:25:38 -0500365 if (!als_count)
366 als_count = 1; /* ensure at least one cycle */
367
368 /* convert back to time (encompasses overrides) */
369 als_time = (als_count * 27 + 5) / 10;
370
371 val = 256 - als_count;
372 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500373 TSL2583_CMD_REG | TSL2583_ALS_TIME,
Brian Masney21677692016-11-10 04:25:38 -0500374 val);
375 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500376 dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
Brian Masney21677692016-11-10 04:25:38 -0500377 __func__, val);
378 return ret;
379 }
380
381 /* set chip struct re scaling and saturation */
382 chip->als_saturation = als_count * 922; /* 90% of full scale */
383 chip->als_time_scale = (als_time + 25) / 50;
384
385 return ret;
386}
387
388static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
389{
390 int ret;
391
Brian Masney686c5922016-11-12 13:19:23 -0500392 /* Set the gain based on als_settings struct */
Brian Masney21677692016-11-10 04:25:38 -0500393 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500394 TSL2583_CMD_REG | TSL2583_GAIN,
395 chip->als_settings.als_gain);
Brian Masney21677692016-11-10 04:25:38 -0500396 if (ret < 0)
Brian Masney24ad4302016-11-12 13:19:22 -0500397 dev_err(&chip->client->dev,
398 "%s: failed to set the gain to %d\n", __func__,
Brian Masney686c5922016-11-12 13:19:23 -0500399 chip->als_settings.als_gain);
Brian Masney21677692016-11-10 04:25:38 -0500400
401 return ret;
402}
403
Brian Masneyc908fb72016-11-10 04:25:37 -0500404static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
405{
406 int ret;
407
408 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500409 TSL2583_CMD_REG | TSL2583_CNTRL, state);
Brian Masneyc908fb72016-11-10 04:25:37 -0500410 if (ret < 0)
Brian Masney24ad4302016-11-12 13:19:22 -0500411 dev_err(&chip->client->dev,
412 "%s: failed to set the power state to %d\n", __func__,
413 state);
Brian Masneyc908fb72016-11-10 04:25:37 -0500414
415 return ret;
416}
417
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100418/*
419 * Turn the device on.
420 * Configuration must be set before calling this function.
421 */
Brian Masneyc908fb72016-11-10 04:25:37 -0500422static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100423{
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100424 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney21677692016-11-10 04:25:38 -0500425 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100426
Brian Masneyc908fb72016-11-10 04:25:37 -0500427 /* Power on the device; ADC off. */
Brian Masney686c5922016-11-12 13:19:23 -0500428 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
Brian Masneyc908fb72016-11-10 04:25:37 -0500429 if (ret < 0)
430 return ret;
431
432 ret = i2c_smbus_write_byte_data(chip->client,
Brian Masney686c5922016-11-12 13:19:23 -0500433 TSL2583_CMD_REG | TSL2583_INTERRUPT,
Brian Masneyc908fb72016-11-10 04:25:37 -0500434 TSL2583_INTERRUPT_DISABLED);
435 if (ret < 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500436 dev_err(&chip->client->dev,
437 "%s: failed to disable interrupts\n", __func__);
Brian Masneyc908fb72016-11-10 04:25:37 -0500438 return ret;
439 }
440
Brian Masney21677692016-11-10 04:25:38 -0500441 ret = tsl2583_set_als_time(chip);
442 if (ret < 0)
Brian Masneyc908fb72016-11-10 04:25:37 -0500443 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100444
Brian Masney21677692016-11-10 04:25:38 -0500445 ret = tsl2583_set_als_gain(chip);
446 if (ret < 0)
Brian Masneyc908fb72016-11-10 04:25:37 -0500447 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100448
Michael Welling7fe704c2014-04-09 21:26:45 -0500449 usleep_range(3000, 3500);
Brian Masneyc908fb72016-11-10 04:25:37 -0500450
Brian Masney686c5922016-11-12 13:19:23 -0500451 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
452 TSL2583_CNTL_ADC_ENBL);
Brian Masneyc908fb72016-11-10 04:25:37 -0500453 if (ret < 0)
Monam Agarwal67fc0122014-03-09 10:58:40 +0530454 return ret;
Brian Masneyc908fb72016-11-10 04:25:37 -0500455
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100456 return ret;
457}
458
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100459/* Sysfs Interface Functions */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100460
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400461static ssize_t in_illuminance_input_target_show(struct device *dev,
462 struct device_attribute *attr,
463 char *buf)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100464{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200465 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100466 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney1b7e9e22016-10-28 06:00:21 -0400467 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100468
Brian Masney1b7e9e22016-10-28 06:00:21 -0400469 mutex_lock(&chip->als_mutex);
Brian Masney686c5922016-11-12 13:19:23 -0500470 ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
Brian Masney1b7e9e22016-10-28 06:00:21 -0400471 mutex_unlock(&chip->als_mutex);
472
473 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100474}
475
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400476static ssize_t in_illuminance_input_target_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t len)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100479{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200480 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100481 struct tsl2583_chip *chip = iio_priv(indio_dev);
Jingoo Hane5e26dd2013-08-20 03:31:00 +0100482 int value;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100483
Brian Masneyf375a492016-10-28 06:00:20 -0400484 if (kstrtoint(buf, 0, &value) || !value)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100485 return -EINVAL;
486
Brian Masney1b7e9e22016-10-28 06:00:21 -0400487 mutex_lock(&chip->als_mutex);
Brian Masney686c5922016-11-12 13:19:23 -0500488 chip->als_settings.als_cal_target = value;
Brian Masney1b7e9e22016-10-28 06:00:21 -0400489 mutex_unlock(&chip->als_mutex);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100490
491 return len;
492}
493
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400494static ssize_t in_illuminance_calibrate_store(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t len)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100497{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200498 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Brian Masney1b7e9e22016-10-28 06:00:21 -0400499 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masneyacf9ead2016-11-12 13:19:16 -0500500 int value, ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100501
Brian Masneyf375a492016-10-28 06:00:20 -0400502 if (kstrtoint(buf, 0, &value) || value != 1)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100503 return -EINVAL;
504
Brian Masney1b7e9e22016-10-28 06:00:21 -0400505 mutex_lock(&chip->als_mutex);
Brian Masneyacf9ead2016-11-12 13:19:16 -0500506
Brian Masney686c5922016-11-12 13:19:23 -0500507 ret = tsl2583_als_calibrate(indio_dev);
Brian Masneyacf9ead2016-11-12 13:19:16 -0500508 if (ret < 0)
509 goto done;
510
511 ret = len;
512done:
Brian Masney1b7e9e22016-10-28 06:00:21 -0400513 mutex_unlock(&chip->als_mutex);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100514
Brian Masneyacf9ead2016-11-12 13:19:16 -0500515 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100516}
517
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400518static ssize_t in_illuminance_lux_table_show(struct device *dev,
519 struct device_attribute *attr,
520 char *buf)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100521{
Brian Masney0b6b3612016-11-12 13:19:34 -0500522 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
523 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney6d94de6a2016-11-12 13:19:25 -0500524 unsigned int i;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100525 int offset = 0;
526
Brian Masney0b6b3612016-11-12 13:19:34 -0500527 for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
Asaf Vertz3a70eb82015-01-20 12:43:38 +0200528 offset += sprintf(buf + offset, "%u,%u,%u,",
Brian Masney0b6b3612016-11-12 13:19:34 -0500529 chip->als_settings.als_device_lux[i].ratio,
530 chip->als_settings.als_device_lux[i].ch0,
531 chip->als_settings.als_device_lux[i].ch1);
532 if (chip->als_settings.als_device_lux[i].ratio == 0) {
Eva Rachel Retuya7f3829a2016-02-18 15:35:38 +0800533 /*
534 * We just printed the first "0" entry.
535 * Now get rid of the extra "," and break.
536 */
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100537 offset--;
538 break;
539 }
540 }
541
542 offset += sprintf(buf + offset, "\n");
Brian Masney184f9162016-11-12 13:19:26 -0500543
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100544 return offset;
545}
546
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400547static ssize_t in_illuminance_lux_table_store(struct device *dev,
548 struct device_attribute *attr,
549 const char *buf, size_t len)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100550{
Lars-Peter Clausen96f691f2012-05-12 15:39:51 +0200551 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100552 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney0b6b3612016-11-12 13:19:34 -0500553 const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
Dan Carpenter0e8d2b02016-11-24 16:38:07 +0300554 int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
Brian Masney6d94de6a2016-11-12 13:19:25 -0500555 int ret = -EINVAL;
556 unsigned int n;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100557
Brian Masney1b7e9e22016-10-28 06:00:21 -0400558 mutex_lock(&chip->als_mutex);
559
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100560 get_options(buf, ARRAY_SIZE(value), value);
561
Brian Masney2a1e3f02016-11-12 13:19:28 -0500562 /*
563 * We now have an array of ints starting at value[1], and
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100564 * enumerated by value[0].
565 * We expect each group of three ints is one table entry,
566 * and the last table entry is all 0.
567 */
568 n = value[0];
Brian Masney0b6b3612016-11-12 13:19:34 -0500569 if ((n % 3) || n < 6 || n > max_ints) {
Brian Masney24ad4302016-11-12 13:19:22 -0500570 dev_err(dev,
Brian Masney0b6b3612016-11-12 13:19:34 -0500571 "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
572 __func__, max_ints);
Brian Masney84bc1702016-10-28 06:00:16 -0400573 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100574 }
Brian Masney1ad51362016-11-12 13:19:32 -0500575 if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
Brian Masney24ad4302016-11-12 13:19:22 -0500576 dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
577 __func__);
Brian Masney84bc1702016-10-28 06:00:16 -0400578 goto done;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100579 }
580
Brian Masney0b6b3612016-11-12 13:19:34 -0500581 memcpy(chip->als_settings.als_device_lux, &value[1],
582 value[0] * sizeof(value[1]));
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100583
Brian Masney84bc1702016-10-28 06:00:16 -0400584 ret = len;
585
586done:
Brian Masney1b7e9e22016-10-28 06:00:21 -0400587 mutex_unlock(&chip->als_mutex);
588
Brian Masney84bc1702016-10-28 06:00:16 -0400589 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100590}
591
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400592static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
593static IIO_CONST_ATTR(in_illuminance_integration_time_available,
Brian Masney4833dc42018-05-12 20:20:39 -0400594 "0.050 0.100 0.150 0.200 0.250 0.300 0.350 0.400 0.450 0.500 0.550 0.600 0.650");
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400595static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
596static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
597static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100598
599static struct attribute *sysfs_attrs_ctrl[] = {
Brian Masneyb2fa81b2016-10-28 06:00:19 -0400600 &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
601 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
602 &iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
603 &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
604 &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100605 NULL
606};
607
Bhumika Goyal58fce732016-09-26 10:31:39 +0530608static const struct attribute_group tsl2583_attribute_group = {
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100609 .attrs = sysfs_attrs_ctrl,
610};
611
Brian Masneyb3d94152016-10-28 06:00:17 -0400612static const struct iio_chan_spec tsl2583_channels[] = {
613 {
614 .type = IIO_LIGHT,
615 .modified = 1,
616 .channel2 = IIO_MOD_LIGHT_IR,
617 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
618 },
619 {
620 .type = IIO_LIGHT,
621 .modified = 1,
622 .channel2 = IIO_MOD_LIGHT_BOTH,
623 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
624 },
625 {
626 .type = IIO_LIGHT,
627 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
628 BIT(IIO_CHAN_INFO_CALIBBIAS) |
Brian Masney143c30f2016-10-28 06:00:18 -0400629 BIT(IIO_CHAN_INFO_CALIBSCALE) |
Brian Masneyb3d94152016-10-28 06:00:17 -0400630 BIT(IIO_CHAN_INFO_INT_TIME),
631 },
632};
633
Brian Masney371894f2017-04-25 04:06:33 -0400634static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
635{
636 int ret;
637
638 if (on) {
639 ret = pm_runtime_get_sync(&chip->client->dev);
640 if (ret < 0)
641 pm_runtime_put_noidle(&chip->client->dev);
642 } else {
643 pm_runtime_mark_last_busy(&chip->client->dev);
644 ret = pm_runtime_put_autosuspend(&chip->client->dev);
645 }
646
647 return ret;
648}
649
Brian Masneyb3d94152016-10-28 06:00:17 -0400650static int tsl2583_read_raw(struct iio_dev *indio_dev,
651 struct iio_chan_spec const *chan,
652 int *val, int *val2, long mask)
653{
654 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney371894f2017-04-25 04:06:33 -0400655 int ret, pm_ret;
656
657 ret = tsl2583_set_pm_runtime_busy(chip, true);
658 if (ret < 0)
659 return ret;
Brian Masneyb3d94152016-10-28 06:00:17 -0400660
661 mutex_lock(&chip->als_mutex);
662
Brian Masney371894f2017-04-25 04:06:33 -0400663 ret = -EINVAL;
Brian Masneyb3d94152016-10-28 06:00:17 -0400664 switch (mask) {
665 case IIO_CHAN_INFO_RAW:
666 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500667 ret = tsl2583_get_lux(indio_dev);
Brian Masneyb3d94152016-10-28 06:00:17 -0400668 if (ret < 0)
669 goto read_done;
670
671 /*
672 * From page 20 of the TSL2581, TSL2583 data
673 * sheet (TAOS134 − MARCH 2011):
674 *
675 * One of the photodiodes (channel 0) is
676 * sensitive to both visible and infrared light,
677 * while the second photodiode (channel 1) is
678 * sensitive primarily to infrared light.
679 */
680 if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
681 *val = chip->als_cur_info.als_ch0;
682 else
683 *val = chip->als_cur_info.als_ch1;
684
685 ret = IIO_VAL_INT;
686 }
687 break;
688 case IIO_CHAN_INFO_PROCESSED:
689 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500690 ret = tsl2583_get_lux(indio_dev);
Brian Masneyb3d94152016-10-28 06:00:17 -0400691 if (ret < 0)
692 goto read_done;
693
694 *val = ret;
695 ret = IIO_VAL_INT;
696 }
697 break;
698 case IIO_CHAN_INFO_CALIBBIAS:
699 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500700 *val = chip->als_settings.als_gain_trim;
Brian Masneyb3d94152016-10-28 06:00:17 -0400701 ret = IIO_VAL_INT;
702 }
703 break;
Brian Masney143c30f2016-10-28 06:00:18 -0400704 case IIO_CHAN_INFO_CALIBSCALE:
705 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500706 *val = gainadj[chip->als_settings.als_gain].mean;
Brian Masney143c30f2016-10-28 06:00:18 -0400707 ret = IIO_VAL_INT;
708 }
709 break;
Brian Masneyb3d94152016-10-28 06:00:17 -0400710 case IIO_CHAN_INFO_INT_TIME:
711 if (chan->type == IIO_LIGHT) {
712 *val = 0;
Brian Masney686c5922016-11-12 13:19:23 -0500713 *val2 = chip->als_settings.als_time;
Brian Masneyb3d94152016-10-28 06:00:17 -0400714 ret = IIO_VAL_INT_PLUS_MICRO;
715 }
716 break;
717 default:
718 break;
719 }
720
721read_done:
722 mutex_unlock(&chip->als_mutex);
723
Brian Masney371894f2017-04-25 04:06:33 -0400724 if (ret < 0)
725 return ret;
726
727 /*
728 * Preserve the ret variable if the call to
729 * tsl2583_set_pm_runtime_busy() is successful so the reading
730 * (if applicable) is returned to user space.
731 */
732 pm_ret = tsl2583_set_pm_runtime_busy(chip, false);
733 if (pm_ret < 0)
734 return pm_ret;
735
Brian Masneyb3d94152016-10-28 06:00:17 -0400736 return ret;
737}
738
739static int tsl2583_write_raw(struct iio_dev *indio_dev,
740 struct iio_chan_spec const *chan,
741 int val, int val2, long mask)
742{
743 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney371894f2017-04-25 04:06:33 -0400744 int ret;
745
746 ret = tsl2583_set_pm_runtime_busy(chip, true);
747 if (ret < 0)
748 return ret;
Brian Masneyb3d94152016-10-28 06:00:17 -0400749
750 mutex_lock(&chip->als_mutex);
751
Brian Masney371894f2017-04-25 04:06:33 -0400752 ret = -EINVAL;
Brian Masneyb3d94152016-10-28 06:00:17 -0400753 switch (mask) {
754 case IIO_CHAN_INFO_CALIBBIAS:
755 if (chan->type == IIO_LIGHT) {
Brian Masney686c5922016-11-12 13:19:23 -0500756 chip->als_settings.als_gain_trim = val;
Brian Masneyb3d94152016-10-28 06:00:17 -0400757 ret = 0;
758 }
759 break;
Brian Masney143c30f2016-10-28 06:00:18 -0400760 case IIO_CHAN_INFO_CALIBSCALE:
761 if (chan->type == IIO_LIGHT) {
Brian Masney6d94de6a2016-11-12 13:19:25 -0500762 unsigned int i;
Brian Masney143c30f2016-10-28 06:00:18 -0400763
764 for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
765 if (gainadj[i].mean == val) {
Brian Masney686c5922016-11-12 13:19:23 -0500766 chip->als_settings.als_gain = i;
Brian Masney21677692016-11-10 04:25:38 -0500767 ret = tsl2583_set_als_gain(chip);
Brian Masney143c30f2016-10-28 06:00:18 -0400768 break;
769 }
770 }
771 }
772 break;
Brian Masneyb3d94152016-10-28 06:00:17 -0400773 case IIO_CHAN_INFO_INT_TIME:
774 if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
775 val2 <= 650 && !(val2 % 50)) {
Brian Masney686c5922016-11-12 13:19:23 -0500776 chip->als_settings.als_time = val2;
Brian Masney21677692016-11-10 04:25:38 -0500777 ret = tsl2583_set_als_time(chip);
Brian Masneyb3d94152016-10-28 06:00:17 -0400778 }
779 break;
780 default:
781 break;
782 }
783
Brian Masneyb3d94152016-10-28 06:00:17 -0400784 mutex_unlock(&chip->als_mutex);
785
Brian Masney371894f2017-04-25 04:06:33 -0400786 if (ret < 0)
787 return ret;
788
789 ret = tsl2583_set_pm_runtime_busy(chip, false);
790 if (ret < 0)
791 return ret;
792
Brian Masneyb3d94152016-10-28 06:00:17 -0400793 return ret;
794}
795
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100796static const struct iio_info tsl2583_info = {
797 .attrs = &tsl2583_attribute_group,
Brian Masneyb3d94152016-10-28 06:00:17 -0400798 .read_raw = tsl2583_read_raw,
799 .write_raw = tsl2583_write_raw,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100800};
801
Brian Masney686c5922016-11-12 13:19:23 -0500802static int tsl2583_probe(struct i2c_client *clientp,
803 const struct i2c_device_id *idp)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100804{
Brian Masney6bd0cb22016-11-03 08:56:13 -0400805 int ret;
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100806 struct tsl2583_chip *chip;
807 struct iio_dev *indio_dev;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100808
809 if (!i2c_check_functionality(clientp->adapter,
Eva Rachel Retuyaca52c882016-02-18 15:35:36 +0800810 I2C_FUNC_SMBUS_BYTE_DATA)) {
Brian Masney24ad4302016-11-12 13:19:22 -0500811 dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
812 __func__);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100813 return -EOPNOTSUPP;
814 }
815
Sachin Kamat664716ae2013-09-05 10:29:00 +0100816 indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
817 if (!indio_dev)
818 return -ENOMEM;
Brian Masney184f9162016-11-12 13:19:26 -0500819
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100820 chip = iio_priv(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100821 chip->client = clientp;
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100822 i2c_set_clientdata(clientp, indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100823
824 mutex_init(&chip->als_mutex);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100825
Brian Masney6bd0cb22016-11-03 08:56:13 -0400826 ret = i2c_smbus_read_byte_data(clientp,
Brian Masney686c5922016-11-12 13:19:23 -0500827 TSL2583_CMD_REG | TSL2583_CHIPID);
Brian Masney6bd0cb22016-11-03 08:56:13 -0400828 if (ret < 0) {
829 dev_err(&clientp->dev,
Brian Masney24ad4302016-11-12 13:19:22 -0500830 "%s: failed to read the chip ID register\n", __func__);
Brian Masney6bd0cb22016-11-03 08:56:13 -0400831 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100832 }
833
Brian Masney6bd0cb22016-11-03 08:56:13 -0400834 if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
Brian Masney24ad4302016-11-12 13:19:22 -0500835 dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
836 __func__, ret);
Sachin Kamat664716ae2013-09-05 10:29:00 +0100837 return -EINVAL;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100838 }
839
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100840 indio_dev->info = &tsl2583_info;
Brian Masneyb3d94152016-10-28 06:00:17 -0400841 indio_dev->channels = tsl2583_channels;
842 indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100843 indio_dev->dev.parent = &clientp->dev;
844 indio_dev->modes = INDIO_DIRECT_MODE;
845 indio_dev->name = chip->client->name;
Brian Masney184f9162016-11-12 13:19:26 -0500846
Brian Masney371894f2017-04-25 04:06:33 -0400847 pm_runtime_enable(&clientp->dev);
848 pm_runtime_set_autosuspend_delay(&clientp->dev,
849 TSL2583_POWER_OFF_DELAY_MS);
850 pm_runtime_use_autosuspend(&clientp->dev);
851
Ioana Ciornei86727e32015-10-02 13:37:49 +0300852 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100853 if (ret) {
Brian Masney24ad4302016-11-12 13:19:22 -0500854 dev_err(&clientp->dev, "%s: iio registration failed\n",
855 __func__);
Sachin Kamat664716ae2013-09-05 10:29:00 +0100856 return ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100857 }
858
859 /* Load up the V2 defaults (these are hard coded defaults for now) */
Brian Masney686c5922016-11-12 13:19:23 -0500860 tsl2583_defaults(chip);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100861
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100862 dev_info(&clientp->dev, "Light sensor found.\n");
Brian Masney184f9162016-11-12 13:19:26 -0500863
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100864 return 0;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100865}
866
Brian Masney371894f2017-04-25 04:06:33 -0400867static int tsl2583_remove(struct i2c_client *client)
868{
869 struct iio_dev *indio_dev = i2c_get_clientdata(client);
870 struct tsl2583_chip *chip = iio_priv(indio_dev);
871
872 iio_device_unregister(indio_dev);
873
874 pm_runtime_disable(&client->dev);
875 pm_runtime_set_suspended(&client->dev);
876 pm_runtime_put_noidle(&client->dev);
877
878 return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
879}
880
Brian Masney686c5922016-11-12 13:19:23 -0500881static int __maybe_unused tsl2583_suspend(struct device *dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100882{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100883 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100884 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney0859fdd2016-11-12 13:19:39 -0500885 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100886
887 mutex_lock(&chip->als_mutex);
888
Brian Masney686c5922016-11-12 13:19:23 -0500889 ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100890
891 mutex_unlock(&chip->als_mutex);
Brian Masney184f9162016-11-12 13:19:26 -0500892
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100893 return ret;
894}
895
Brian Masney686c5922016-11-12 13:19:23 -0500896static int __maybe_unused tsl2583_resume(struct device *dev)
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100897{
Lars-Peter Clausen01788c52012-02-20 19:37:05 +0100898 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
Jonathan Camerone05e5f22011-08-12 17:08:52 +0100899 struct tsl2583_chip *chip = iio_priv(indio_dev);
Brian Masney0859fdd2016-11-12 13:19:39 -0500900 int ret;
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100901
902 mutex_lock(&chip->als_mutex);
903
Brian Masney2ff8a352016-11-12 13:19:18 -0500904 ret = tsl2583_chip_init_and_power_on(indio_dev);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100905
906 mutex_unlock(&chip->als_mutex);
Brian Masney184f9162016-11-12 13:19:26 -0500907
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100908 return ret;
909}
910
Brian Masney371894f2017-04-25 04:06:33 -0400911static const struct dev_pm_ops tsl2583_pm_ops = {
912 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
913 pm_runtime_force_resume)
914 SET_RUNTIME_PM_OPS(tsl2583_suspend, tsl2583_resume, NULL)
915};
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100916
Arvind Yadav3282fa32017-08-20 00:17:38 +0530917static const struct i2c_device_id tsl2583_idtable[] = {
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100918 { "tsl2580", 0 },
919 { "tsl2581", 1 },
920 { "tsl2583", 2 },
921 {}
922};
Brian Masney686c5922016-11-12 13:19:23 -0500923MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100924
Brian Masney686c5922016-11-12 13:19:23 -0500925static const struct of_device_id tsl2583_of_match[] = {
Brian Masneya3b6b4b2016-10-28 06:00:12 -0400926 { .compatible = "amstaos,tsl2580", },
927 { .compatible = "amstaos,tsl2581", },
928 { .compatible = "amstaos,tsl2583", },
929 { },
930};
Brian Masney686c5922016-11-12 13:19:23 -0500931MODULE_DEVICE_TABLE(of, tsl2583_of_match);
Brian Masneya3b6b4b2016-10-28 06:00:12 -0400932
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100933/* Driver definition */
Brian Masney686c5922016-11-12 13:19:23 -0500934static struct i2c_driver tsl2583_driver = {
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100935 .driver = {
936 .name = "tsl2583",
Brian Masney686c5922016-11-12 13:19:23 -0500937 .pm = &tsl2583_pm_ops,
938 .of_match_table = tsl2583_of_match,
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100939 },
Brian Masney686c5922016-11-12 13:19:23 -0500940 .id_table = tsl2583_idtable,
941 .probe = tsl2583_probe,
Brian Masney371894f2017-04-25 04:06:33 -0400942 .remove = tsl2583_remove,
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100943};
Brian Masney686c5922016-11-12 13:19:23 -0500944module_i2c_driver(tsl2583_driver);
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100945
Brian Masneyc45a2262016-11-12 13:19:40 -0500946MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
947MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
Jon Brennerac4f6ee2011-04-15 18:38:43 +0100948MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
949MODULE_LICENSE("GPL");